chore: more sane line wraps in prettier

This commit is contained in:
Daniel Bauer
2024-12-14 11:36:25 +01:00
parent 1d2a860df2
commit a1245356ad
6 changed files with 60 additions and 263 deletions

View File

@@ -1 +1,3 @@
{} {
"printWidth": 120
}

View File

@@ -9,9 +9,7 @@ const BASE_CONFIG = {
}; };
const getRollupPlugins = (tsconfig, ...plugins) => const getRollupPlugins = (tsconfig, ...plugins) =>
[typescript2(tsconfig), nodeResolve({ browser: true }), commonjs()].concat( [typescript2(tsconfig), nodeResolve({ browser: true }), commonjs()].concat(plugins);
plugins,
);
const DEV_PLUGIN_CONFIG = { const DEV_PLUGIN_CONFIG = {
...BASE_CONFIG, ...BASE_CONFIG,

View File

@@ -37,11 +37,7 @@ export class DataviewSuggester extends EditorSuggest<String> {
this.dataviewApi = getAPI(plugin.app); this.dataviewApi = getAPI(plugin.app);
} }
onTrigger( onTrigger(cursor: EditorPosition, editor: Editor, file: TFile): EditorSuggestTriggerInfo | null {
cursor: EditorPosition,
editor: Editor,
file: TFile,
): EditorSuggestTriggerInfo | null {
const line = editor.getLine(cursor.line); const line = editor.getLine(cursor.line);
let trigger = getTriggerText(line, cursor.ch); let trigger = getTriggerText(line, cursor.ch);
@@ -55,39 +51,24 @@ export class DataviewSuggester extends EditorSuggest<String> {
return null; return null;
} }
getSuggestions( getSuggestions(context: EditorSuggestContext): string[] | Promise<string[]> {
context: EditorSuggestContext,
): string[] | Promise<string[]> {
let idxs = this.searcher.filter(this.suggestionsList, context.query); let idxs = this.searcher.filter(this.suggestionsList, context.query);
if (idxs != null && idxs.length > 0) { if (idxs != null && idxs.length > 0) {
let info = this.searcher.info( let info = this.searcher.info(idxs, this.suggestionsList, context.query);
idxs, let order = this.searcher.sort(info, this.suggestionsList, context.query);
this.suggestionsList,
context.query,
);
let order = this.searcher.sort(
info,
this.suggestionsList,
context.query,
);
// return top N suggestions with marks // return top N suggestions with marks
return order return order
.slice(0, this.maxSuggestions) .slice(0, this.maxSuggestions)
.map((idx) => [idx, this.suggestionsList[info.idx[idx]]]) .map((idx) => [idx, this.suggestionsList[info.idx[idx]]])
.map((suggestion: [number, string]) => .map((suggestion: [number, string]) => uFuzzy.highlight(suggestion[1], info.ranges[suggestion[0]]));
uFuzzy.highlight(suggestion[1], info.ranges[suggestion[0]]),
);
} }
return []; return [];
} }
renderSuggestion(value: string, el: HTMLElement): void { renderSuggestion(value: string, el: HTMLElement): void {
// replace marks with bold // replace marks with bold
const formattedHtml = value.replace( const formattedHtml = value.replace(/<mark>(.*?)<\/mark>/g, '<span style="font-weight: bold;">$1</span>');
/<mark>(.*?)<\/mark>/g,
'<span style="font-weight: bold;">$1</span>',
);
el.innerHTML = formattedHtml; el.innerHTML = formattedHtml;
} }
@@ -110,15 +91,9 @@ export class DataviewSuggester extends EditorSuggest<String> {
} }
// possible types: update, rename, delete. rename has oldPath // possible types: update, rename, delete. rename has oldPath
public onDataviewMetadataChange( public onDataviewMetadataChange(type: string, file: TFile, oldPath?: string) {
type: string,
file: TFile,
oldPath?: string,
) {
if (!this.initialized) { if (!this.initialized) {
console.log( console.log("Dataview Autocompletion index not ready yet. Skipping index update");
"Dataview Autocompletion index not ready yet. Skipping index update",
);
return; return;
} }
this.updateIndex(type, file, oldPath); this.updateIndex(type, file, oldPath);
@@ -132,11 +107,7 @@ export class DataviewSuggester extends EditorSuggest<String> {
let stringValue: string; let stringValue: string;
// If the value is a string, number or boolean, we can simply convert it to a string // If the value is a string, number or boolean, we can simply convert it to a string
if ( if (typeof value === "string" || typeof value === "number" || typeof value === "boolean") {
typeof value === "string" ||
typeof value === "number" ||
typeof value === "boolean"
) {
stringValue = value.toString(); stringValue = value.toString();
} else if ( } else if (
this.dataviewApi.value.typeOf(value) === "link" && this.dataviewApi.value.typeOf(value) === "link" &&
@@ -237,24 +208,14 @@ export class DataviewSuggester extends EditorSuggest<String> {
// delete value // delete value
this.suggestionsRefCount[compositeValue] -= 1; this.suggestionsRefCount[compositeValue] -= 1;
if (this.suggestionsRefCount[compositeValue] == 0) { if (this.suggestionsRefCount[compositeValue] == 0) {
this.suggestionsList.splice( this.suggestionsList.splice(this.suggestionsList.indexOf(compositeValue), 1);
this.suggestionsList.indexOf(compositeValue),
1,
);
} }
} }
} }
for (const newCompositeValue of updateCompositeValues) { for (const newCompositeValue of updateCompositeValues) {
if ( if (this.suggestionsRefs[file.path].indexOf(newCompositeValue) === -1) {
this.suggestionsRefs[file.path].indexOf(
newCompositeValue,
) === -1
) {
// add value (also check presence in other files via refcount first) // add value (also check presence in other files via refcount first)
if ( if (this.suggestionsList.indexOf(newCompositeValue) === -1) {
this.suggestionsList.indexOf(newCompositeValue) ===
-1
) {
this.suggestionsList.push(newCompositeValue); this.suggestionsList.push(newCompositeValue);
this.suggestionsRefCount[newCompositeValue] += 1; this.suggestionsRefCount[newCompositeValue] += 1;
} }
@@ -271,10 +232,7 @@ export class DataviewSuggester extends EditorSuggest<String> {
for (const value of this.suggestionsRefs[file.path]) { for (const value of this.suggestionsRefs[file.path]) {
this.suggestionsRefCount[value] -= 1; this.suggestionsRefCount[value] -= 1;
if (this.suggestionsRefCount[value] == 0) { if (this.suggestionsRefCount[value] == 0) {
this.suggestionsList.splice( this.suggestionsList.splice(this.suggestionsList.indexOf(value), 1);
this.suggestionsList.indexOf(value),
1,
);
} }
} }
delete this.suggestionsRefs[file.path]; delete this.suggestionsRefs[file.path];

View File

@@ -32,11 +32,7 @@ export default class DataviewAutocompletePlugin extends Plugin {
// @ts-ignore // @ts-ignore
"dataview:metadata-change", "dataview:metadata-change",
(type: string, file: TFile, oldPath?: string) => { (type: string, file: TFile, oldPath?: string) => {
this.suggester.onDataviewMetadataChange( this.suggester.onDataviewMetadataChange(type, file, oldPath);
type,
file,
oldPath,
);
}, },
), ),
); );
@@ -45,11 +41,7 @@ export default class DataviewAutocompletePlugin extends Plugin {
onunload() {} onunload() {}
async loadSettings() { async loadSettings() {
this.settings = Object.assign( this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
{},
DEFAULT_SETTINGS,
await this.loadData(),
);
} }
async saveSettings() { async saveSettings() {

View File

@@ -22,121 +22,53 @@ describe("trigger", () => {
expect(getTriggerText("(capture)", 1)).toEqual(["capture", 1, 8]); expect(getTriggerText("(capture)", 1)).toEqual(["capture", 1, 8]);
}); });
test("text behind", () => { test("text behind", () => {
expect(getTriggerText("(capture) testing", 1)).toEqual([ expect(getTriggerText("(capture) testing", 1)).toEqual(["capture", 1, 8]);
"capture",
1,
8,
]);
}); });
test("text before", () => { test("text before", () => {
expect(getTriggerText("testing (capture)", 12)).toEqual([ expect(getTriggerText("testing (capture)", 12)).toEqual(["capture", 9, 16]);
"capture",
9,
16,
]);
}); });
test("text behind no whitespace", () => { test("text behind no whitespace", () => {
expect(getTriggerText("(capture)testing", 1)).toEqual([ expect(getTriggerText("(capture)testing", 1)).toEqual(["capture", 1, 8]);
"capture",
1,
8,
]);
}); });
test("text before no whitespace", () => { test("text before no whitespace", () => {
expect(getTriggerText("test(capture)", 5)).toEqual([ expect(getTriggerText("test(capture)", 5)).toEqual(["capture", 5, 12]);
"capture",
5,
12,
]);
}); });
test("text before and behind", () => { test("text before and behind", () => {
expect(getTriggerText("test (capture) testing", 6)).toEqual([ expect(getTriggerText("test (capture) testing", 6)).toEqual(["capture", 6, 13]);
"capture",
6,
13,
]);
}); });
test("with colon", () => { test("with colon", () => {
expect(getTriggerText("(capture:)", 1)).toEqual(["capture:", 1, 9]); expect(getTriggerText("(capture:)", 1)).toEqual(["capture:", 1, 9]);
}); });
test("with double colon", () => { test("with double colon", () => {
expect(getTriggerText("(capture::)", 1)).toEqual([ expect(getTriggerText("(capture::)", 1)).toEqual(["capture::", 1, 10]);
"capture::",
1,
10,
]);
}); });
test("metadata field", () => { test("metadata field", () => {
expect(getTriggerText("(capture::Bob)", 1)).toEqual([ expect(getTriggerText("(capture::Bob)", 1)).toEqual(["capture::Bob", 1, 13]);
"capture::Bob",
1,
13,
]);
}); });
test("metadata field with whitespace", () => { test("metadata field with whitespace", () => {
expect(getTriggerText("(capture:: Bob)", 1)).toEqual([ expect(getTriggerText("(capture:: Bob)", 1)).toEqual(["capture:: Bob", 1, 14]);
"capture:: Bob",
1,
14,
]);
}); });
test("metadata field with more white space", () => { test("metadata field with more white space", () => {
expect(getTriggerText("(capture:: Bob)", 1)).toEqual([ expect(getTriggerText("(capture:: Bob)", 1)).toEqual(["capture:: Bob", 1, 16]);
"capture:: Bob",
1,
16,
]);
}); });
test("double field", () => { test("double field", () => {
expect(getTriggerText("(capture1)(capture2)", 1)).toEqual([ expect(getTriggerText("(capture1)(capture2)", 1)).toEqual(["capture1", 1, 9]);
"capture1", expect(getTriggerText("(capture1)(capture2)", 11)).toEqual(["capture2", 11, 19]);
1,
9,
]);
expect(getTriggerText("(capture1)(capture2)", 11)).toEqual([
"capture2",
11,
19,
]);
}); });
test("wiki link", () => { test("wiki link", () => {
expect(getTriggerText("(test [[test]])", 1)).toEqual([ expect(getTriggerText("(test [[test]])", 1)).toEqual(["test [[test]]", 1, 14]);
"test [[test]]", expect(getTriggerText("(test [[test]])", 14)).toEqual(["test [[test]]", 1, 14]);
1,
14,
]);
expect(getTriggerText("(test [[test]])", 14)).toEqual([
"test [[test]]",
1,
14,
]);
}); });
test("aliased wiki link", () => { test("aliased wiki link", () => {
expect(getTriggerText("(test [[test|display]])", 1)).toEqual([ expect(getTriggerText("(test [[test|display]])", 1)).toEqual(["test [[test|display]]", 1, 22]);
"test [[test|display]]", expect(getTriggerText("(test [[test|display]])", 22)).toEqual(["test [[test|display]]", 1, 22]);
1,
22,
]);
expect(getTriggerText("(test [[test|display]])", 22)).toEqual([
"test [[test|display]]",
1,
22,
]);
}); });
test("markdown link", () => { test("markdown link", () => {
expect(getTriggerText("([test](https://example.com))", 1)).toEqual([ expect(getTriggerText("([test](https://example.com))", 1)).toEqual(["[test](https://example.com)", 1, 28]);
"[test](https://example.com)",
1,
28,
]);
}); });
test("markdown link local", () => { test("markdown link local", () => {
expect(getTriggerText("([test](test))", 1)).toEqual([ expect(getTriggerText("([test](test))", 1)).toEqual(["[test](test)", 1, 13]);
"[test](test)",
1,
13,
]);
}); });
test("nested parantheses", () => { test("nested parantheses", () => {
expect(getTriggerText("((test))", 1)).toEqual(["(test)", 1, 7]); expect(getTriggerText("((test))", 1)).toEqual(["(test)", 1, 7]);
@@ -148,120 +80,52 @@ describe("trigger", () => {
expect(getTriggerText("[capture]", 1)).toEqual(["capture", 1, 8]); expect(getTriggerText("[capture]", 1)).toEqual(["capture", 1, 8]);
}); });
test("text behind", () => { test("text behind", () => {
expect(getTriggerText("[capture] testing", 1)).toEqual([ expect(getTriggerText("[capture] testing", 1)).toEqual(["capture", 1, 8]);
"capture",
1,
8,
]);
}); });
test("text before", () => { test("text before", () => {
expect(getTriggerText("testing [capture]", 12)).toEqual([ expect(getTriggerText("testing [capture]", 12)).toEqual(["capture", 9, 16]);
"capture",
9,
16,
]);
}); });
test("text behind no whitespace", () => { test("text behind no whitespace", () => {
expect(getTriggerText("[capture]testing", 1)).toEqual([ expect(getTriggerText("[capture]testing", 1)).toEqual(["capture", 1, 8]);
"capture",
1,
8,
]);
}); });
test("text before no whitespace", () => { test("text before no whitespace", () => {
expect(getTriggerText("test[capture]", 5)).toEqual([ expect(getTriggerText("test[capture]", 5)).toEqual(["capture", 5, 12]);
"capture",
5,
12,
]);
}); });
test("text before and behind", () => { test("text before and behind", () => {
expect(getTriggerText("test [capture] testing", 6)).toEqual([ expect(getTriggerText("test [capture] testing", 6)).toEqual(["capture", 6, 13]);
"capture",
6,
13,
]);
}); });
test("with colon", () => { test("with colon", () => {
expect(getTriggerText("[capture:]", 1)).toEqual(["capture:", 1, 9]); expect(getTriggerText("[capture:]", 1)).toEqual(["capture:", 1, 9]);
}); });
test("with double colon", () => { test("with double colon", () => {
expect(getTriggerText("[capture::]", 1)).toEqual([ expect(getTriggerText("[capture::]", 1)).toEqual(["capture::", 1, 10]);
"capture::",
1,
10,
]);
}); });
test("metadata field", () => { test("metadata field", () => {
expect(getTriggerText("[capture::Bob]", 1)).toEqual([ expect(getTriggerText("[capture::Bob]", 1)).toEqual(["capture::Bob", 1, 13]);
"capture::Bob",
1,
13,
]);
}); });
test("metadata field with whitespace", () => { test("metadata field with whitespace", () => {
expect(getTriggerText("[capture:: Bob]", 1)).toEqual([ expect(getTriggerText("[capture:: Bob]", 1)).toEqual(["capture:: Bob", 1, 14]);
"capture:: Bob",
1,
14,
]);
}); });
test("metadata field with more white space", () => { test("metadata field with more white space", () => {
expect(getTriggerText("[capture:: Bob]", 1)).toEqual([ expect(getTriggerText("[capture:: Bob]", 1)).toEqual(["capture:: Bob", 1, 16]);
"capture:: Bob",
1,
16,
]);
}); });
test("double field", () => { test("double field", () => {
expect(getTriggerText("[capture1][capture2]", 1)).toEqual([ expect(getTriggerText("[capture1][capture2]", 1)).toEqual(["capture1", 1, 9]);
"capture1", expect(getTriggerText("[capture1][capture2]", 11)).toEqual(["capture2", 11, 19]);
1,
9,
]);
expect(getTriggerText("[capture1][capture2]", 11)).toEqual([
"capture2",
11,
19,
]);
}); });
test("wiki link", () => { test("wiki link", () => {
expect(getTriggerText("[test [[test]]]", 1)).toEqual([ expect(getTriggerText("[test [[test]]]", 1)).toEqual(["test [[test]]", 1, 14]);
"test [[test]]", expect(getTriggerText("[test [[test]]]", 14)).toEqual(["test [[test]]", 1, 14]);
1,
14,
]);
expect(getTriggerText("[test [[test]]]", 14)).toEqual([
"test [[test]]",
1,
14,
]);
}); });
test("aliased wiki link", () => { test("aliased wiki link", () => {
expect(getTriggerText("[test [[test|display]]]", 1)).toEqual([ expect(getTriggerText("[test [[test|display]]]", 1)).toEqual(["test [[test|display]]", 1, 22]);
"test [[test|display]]", expect(getTriggerText("[test [[test|display]]]", 22)).toEqual(["test [[test|display]]", 1, 22]);
1,
22,
]);
expect(getTriggerText("[test [[test|display]]]", 22)).toEqual([
"test [[test|display]]",
1,
22,
]);
}); });
test("markdown link", () => { test("markdown link", () => {
expect(getTriggerText("[[test](https://example.com)]", 1)).toEqual([ expect(getTriggerText("[[test](https://example.com)]", 1)).toEqual(["[test](https://example.com)", 1, 28]);
"[test](https://example.com)",
1,
28,
]);
}); });
test("markdown link local", () => { test("markdown link local", () => {
expect(getTriggerText("[[test](test)]", 1)).toEqual([ expect(getTriggerText("[[test](test)]", 1)).toEqual(["[test](test)", 1, 13]);
"[test](test)",
1,
13,
]);
}); });
test("nested parantheses", () => { test("nested parantheses", () => {
expect(getTriggerText("[(test)]", 1)).toEqual(["(test)", 1, 7]); expect(getTriggerText("[(test)]", 1)).toEqual(["(test)", 1, 7]);
@@ -270,32 +134,22 @@ describe("trigger", () => {
describe("cursor position", () => { describe("cursor position", () => {
test("cursor in first field", () => { test("cursor in first field", () => {
expect( expect(getTriggerText("test (test) string (test2) testing", 9)).toEqual(["test", 6, 10]);
getTriggerText("test (test) string (test2) testing", 9),
).toEqual(["test", 6, 10]);
}); });
test("cursor in second field", () => { test("cursor in second field", () => {
expect( expect(getTriggerText("test (test) string (test2) testing", 21)).toEqual(["test2", 20, 25]);
getTriggerText("test (test) string (test2) testing", 21),
).toEqual(["test2", 20, 25]);
}); });
test("ignore cursor out of field", () => { test("ignore cursor out of field", () => {
expect( expect(getTriggerText("test (test) string (test2) testing", 2)).toEqual(null);
getTriggerText("test (test) string (test2) testing", 2),
).toEqual(null);
}); });
}); });
describe("ignore plain markdown links", () => { describe("ignore plain markdown links", () => {
test("cursor in link text", () => { test("cursor in link text", () => {
expect(getTriggerText("[test](https://example.com)", 3)).toEqual( expect(getTriggerText("[test](https://example.com)", 3)).toEqual(null);
null,
);
}); });
test("cursor in link url", () => { test("cursor in link url", () => {
expect(getTriggerText("[test](https://example.com)", 11)).toEqual( expect(getTriggerText("[test](https://example.com)", 11)).toEqual(null);
null,
);
}); });
test("cursor in empty link text", () => { test("cursor in empty link text", () => {
expect(getTriggerText("[](https://example.com)", 1)).toEqual(null); expect(getTriggerText("[](https://example.com)", 1)).toEqual(null);

View File

@@ -35,10 +35,7 @@ const filledRegex = new RegExp(
* and start and end are the cursor positions of the start and end of the field. * and start and end are the cursor positions of the start and end of the field.
* If the user is not inside a field, it returns null. * If the user is not inside a field, it returns null.
*/ */
export function getTriggerText( export function getTriggerText(line: string, cursorPos: number): [string, number, number] | null {
line: string,
cursorPos: number,
): [string, number, number] | null {
let match = getTriggerTextFromRegex(line, cursorPos, filledRegex); let match = getTriggerTextFromRegex(line, cursorPos, filledRegex);
if (match !== null) { if (match !== null) {
return match; return match;
@@ -53,11 +50,7 @@ export function getTriggerText(
* start is the position of the start of the match, and end is the position of the end of the match. * start is the position of the start of the match, and end is the position of the end of the match.
* Otherwise, returns null. * Otherwise, returns null.
*/ */
function getTriggerTextFromRegex( function getTriggerTextFromRegex(line: string, cursorPos: number, regex: RegExp): [string, number, number] | null {
line: string,
cursorPos: number,
regex: RegExp,
): [string, number, number] | null {
let matches = Array.from(line.matchAll(regex)); let matches = Array.from(line.matchAll(regex));
for (const match of matches) { for (const match of matches) {
console.warn(match); console.warn(match);