From a1245356add9958c32450c057c74da3c7382640d Mon Sep 17 00:00:00 2001 From: Daniel Bauer Date: Sat, 14 Dec 2024 11:36:25 +0100 Subject: [PATCH] chore: more sane line wraps in prettier --- .prettierrc | 4 +- rollup.config.js | 4 +- src/DataviewSuggester.ts | 68 +++--------- src/main.ts | 12 +-- src/trigger.test.ts | 224 +++++++-------------------------------- src/trigger.ts | 11 +- 6 files changed, 60 insertions(+), 263 deletions(-) diff --git a/.prettierrc b/.prettierrc index 0967ef4..963354f 100644 --- a/.prettierrc +++ b/.prettierrc @@ -1 +1,3 @@ -{} +{ + "printWidth": 120 +} diff --git a/rollup.config.js b/rollup.config.js index 67a6d28..6bda63b 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -9,9 +9,7 @@ const BASE_CONFIG = { }; const getRollupPlugins = (tsconfig, ...plugins) => - [typescript2(tsconfig), nodeResolve({ browser: true }), commonjs()].concat( - plugins, - ); + [typescript2(tsconfig), nodeResolve({ browser: true }), commonjs()].concat(plugins); const DEV_PLUGIN_CONFIG = { ...BASE_CONFIG, diff --git a/src/DataviewSuggester.ts b/src/DataviewSuggester.ts index 436cbc3..96d4e14 100644 --- a/src/DataviewSuggester.ts +++ b/src/DataviewSuggester.ts @@ -37,11 +37,7 @@ export class DataviewSuggester extends EditorSuggest { this.dataviewApi = getAPI(plugin.app); } - onTrigger( - cursor: EditorPosition, - editor: Editor, - file: TFile, - ): EditorSuggestTriggerInfo | null { + onTrigger(cursor: EditorPosition, editor: Editor, file: TFile): EditorSuggestTriggerInfo | null { const line = editor.getLine(cursor.line); let trigger = getTriggerText(line, cursor.ch); @@ -55,39 +51,24 @@ export class DataviewSuggester extends EditorSuggest { return null; } - getSuggestions( - context: EditorSuggestContext, - ): string[] | Promise { + getSuggestions(context: EditorSuggestContext): string[] | Promise { let idxs = this.searcher.filter(this.suggestionsList, context.query); if (idxs != null && idxs.length > 0) { - let info = this.searcher.info( - idxs, - this.suggestionsList, - context.query, - ); - let order = this.searcher.sort( - info, - this.suggestionsList, - context.query, - ); + let info = this.searcher.info(idxs, this.suggestionsList, context.query); + let order = this.searcher.sort(info, this.suggestionsList, context.query); // return top N suggestions with marks return order .slice(0, this.maxSuggestions) .map((idx) => [idx, this.suggestionsList[info.idx[idx]]]) - .map((suggestion: [number, string]) => - uFuzzy.highlight(suggestion[1], info.ranges[suggestion[0]]), - ); + .map((suggestion: [number, string]) => uFuzzy.highlight(suggestion[1], info.ranges[suggestion[0]])); } return []; } renderSuggestion(value: string, el: HTMLElement): void { // replace marks with bold - const formattedHtml = value.replace( - /(.*?)<\/mark>/g, - '$1', - ); + const formattedHtml = value.replace(/(.*?)<\/mark>/g, '$1'); el.innerHTML = formattedHtml; } @@ -110,15 +91,9 @@ export class DataviewSuggester extends EditorSuggest { } // possible types: update, rename, delete. rename has oldPath - public onDataviewMetadataChange( - type: string, - file: TFile, - oldPath?: string, - ) { + public onDataviewMetadataChange(type: string, file: TFile, oldPath?: string) { if (!this.initialized) { - console.log( - "Dataview Autocompletion index not ready yet. Skipping index update", - ); + console.log("Dataview Autocompletion index not ready yet. Skipping index update"); return; } this.updateIndex(type, file, oldPath); @@ -132,11 +107,7 @@ export class DataviewSuggester extends EditorSuggest { let stringValue: string; // If the value is a string, number or boolean, we can simply convert it to a string - if ( - typeof value === "string" || - typeof value === "number" || - typeof value === "boolean" - ) { + if (typeof value === "string" || typeof value === "number" || typeof value === "boolean") { stringValue = value.toString(); } else if ( this.dataviewApi.value.typeOf(value) === "link" && @@ -237,24 +208,14 @@ export class DataviewSuggester extends EditorSuggest { // delete value this.suggestionsRefCount[compositeValue] -= 1; if (this.suggestionsRefCount[compositeValue] == 0) { - this.suggestionsList.splice( - this.suggestionsList.indexOf(compositeValue), - 1, - ); + this.suggestionsList.splice(this.suggestionsList.indexOf(compositeValue), 1); } } } for (const newCompositeValue of updateCompositeValues) { - if ( - this.suggestionsRefs[file.path].indexOf( - newCompositeValue, - ) === -1 - ) { + if (this.suggestionsRefs[file.path].indexOf(newCompositeValue) === -1) { // add value (also check presence in other files via refcount first) - if ( - this.suggestionsList.indexOf(newCompositeValue) === - -1 - ) { + if (this.suggestionsList.indexOf(newCompositeValue) === -1) { this.suggestionsList.push(newCompositeValue); this.suggestionsRefCount[newCompositeValue] += 1; } @@ -271,10 +232,7 @@ export class DataviewSuggester extends EditorSuggest { for (const value of this.suggestionsRefs[file.path]) { this.suggestionsRefCount[value] -= 1; if (this.suggestionsRefCount[value] == 0) { - this.suggestionsList.splice( - this.suggestionsList.indexOf(value), - 1, - ); + this.suggestionsList.splice(this.suggestionsList.indexOf(value), 1); } } delete this.suggestionsRefs[file.path]; diff --git a/src/main.ts b/src/main.ts index 3aefc71..6e1549c 100644 --- a/src/main.ts +++ b/src/main.ts @@ -32,11 +32,7 @@ export default class DataviewAutocompletePlugin extends Plugin { // @ts-ignore "dataview:metadata-change", (type: string, file: TFile, oldPath?: string) => { - this.suggester.onDataviewMetadataChange( - type, - file, - oldPath, - ); + this.suggester.onDataviewMetadataChange(type, file, oldPath); }, ), ); @@ -45,11 +41,7 @@ export default class DataviewAutocompletePlugin extends Plugin { onunload() {} async loadSettings() { - this.settings = Object.assign( - {}, - DEFAULT_SETTINGS, - await this.loadData(), - ); + this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData()); } async saveSettings() { diff --git a/src/trigger.test.ts b/src/trigger.test.ts index 7b2ac28..65adeee 100644 --- a/src/trigger.test.ts +++ b/src/trigger.test.ts @@ -22,121 +22,53 @@ describe("trigger", () => { expect(getTriggerText("(capture)", 1)).toEqual(["capture", 1, 8]); }); test("text behind", () => { - expect(getTriggerText("(capture) testing", 1)).toEqual([ - "capture", - 1, - 8, - ]); + expect(getTriggerText("(capture) testing", 1)).toEqual(["capture", 1, 8]); }); test("text before", () => { - expect(getTriggerText("testing (capture)", 12)).toEqual([ - "capture", - 9, - 16, - ]); + expect(getTriggerText("testing (capture)", 12)).toEqual(["capture", 9, 16]); }); test("text behind no whitespace", () => { - expect(getTriggerText("(capture)testing", 1)).toEqual([ - "capture", - 1, - 8, - ]); + expect(getTriggerText("(capture)testing", 1)).toEqual(["capture", 1, 8]); }); test("text before no whitespace", () => { - expect(getTriggerText("test(capture)", 5)).toEqual([ - "capture", - 5, - 12, - ]); + expect(getTriggerText("test(capture)", 5)).toEqual(["capture", 5, 12]); }); test("text before and behind", () => { - expect(getTriggerText("test (capture) testing", 6)).toEqual([ - "capture", - 6, - 13, - ]); + expect(getTriggerText("test (capture) testing", 6)).toEqual(["capture", 6, 13]); }); test("with colon", () => { expect(getTriggerText("(capture:)", 1)).toEqual(["capture:", 1, 9]); }); test("with double colon", () => { - expect(getTriggerText("(capture::)", 1)).toEqual([ - "capture::", - 1, - 10, - ]); + expect(getTriggerText("(capture::)", 1)).toEqual(["capture::", 1, 10]); }); test("metadata field", () => { - expect(getTriggerText("(capture::Bob)", 1)).toEqual([ - "capture::Bob", - 1, - 13, - ]); + expect(getTriggerText("(capture::Bob)", 1)).toEqual(["capture::Bob", 1, 13]); }); test("metadata field with whitespace", () => { - expect(getTriggerText("(capture:: Bob)", 1)).toEqual([ - "capture:: Bob", - 1, - 14, - ]); + expect(getTriggerText("(capture:: Bob)", 1)).toEqual(["capture:: Bob", 1, 14]); }); test("metadata field with more white space", () => { - expect(getTriggerText("(capture:: Bob)", 1)).toEqual([ - "capture:: Bob", - 1, - 16, - ]); + expect(getTriggerText("(capture:: Bob)", 1)).toEqual(["capture:: Bob", 1, 16]); }); test("double field", () => { - expect(getTriggerText("(capture1)(capture2)", 1)).toEqual([ - "capture1", - 1, - 9, - ]); - expect(getTriggerText("(capture1)(capture2)", 11)).toEqual([ - "capture2", - 11, - 19, - ]); + expect(getTriggerText("(capture1)(capture2)", 1)).toEqual(["capture1", 1, 9]); + expect(getTriggerText("(capture1)(capture2)", 11)).toEqual(["capture2", 11, 19]); }); test("wiki link", () => { - expect(getTriggerText("(test [[test]])", 1)).toEqual([ - "test [[test]]", - 1, - 14, - ]); - expect(getTriggerText("(test [[test]])", 14)).toEqual([ - "test [[test]]", - 1, - 14, - ]); + expect(getTriggerText("(test [[test]])", 1)).toEqual(["test [[test]]", 1, 14]); + expect(getTriggerText("(test [[test]])", 14)).toEqual(["test [[test]]", 1, 14]); }); test("aliased wiki link", () => { - expect(getTriggerText("(test [[test|display]])", 1)).toEqual([ - "test [[test|display]]", - 1, - 22, - ]); - expect(getTriggerText("(test [[test|display]])", 22)).toEqual([ - "test [[test|display]]", - 1, - 22, - ]); + expect(getTriggerText("(test [[test|display]])", 1)).toEqual(["test [[test|display]]", 1, 22]); + expect(getTriggerText("(test [[test|display]])", 22)).toEqual(["test [[test|display]]", 1, 22]); }); test("markdown link", () => { - expect(getTriggerText("([test](https://example.com))", 1)).toEqual([ - "[test](https://example.com)", - 1, - 28, - ]); + expect(getTriggerText("([test](https://example.com))", 1)).toEqual(["[test](https://example.com)", 1, 28]); }); test("markdown link local", () => { - expect(getTriggerText("([test](test))", 1)).toEqual([ - "[test](test)", - 1, - 13, - ]); + expect(getTriggerText("([test](test))", 1)).toEqual(["[test](test)", 1, 13]); }); test("nested parantheses", () => { expect(getTriggerText("((test))", 1)).toEqual(["(test)", 1, 7]); @@ -148,120 +80,52 @@ describe("trigger", () => { expect(getTriggerText("[capture]", 1)).toEqual(["capture", 1, 8]); }); test("text behind", () => { - expect(getTriggerText("[capture] testing", 1)).toEqual([ - "capture", - 1, - 8, - ]); + expect(getTriggerText("[capture] testing", 1)).toEqual(["capture", 1, 8]); }); test("text before", () => { - expect(getTriggerText("testing [capture]", 12)).toEqual([ - "capture", - 9, - 16, - ]); + expect(getTriggerText("testing [capture]", 12)).toEqual(["capture", 9, 16]); }); test("text behind no whitespace", () => { - expect(getTriggerText("[capture]testing", 1)).toEqual([ - "capture", - 1, - 8, - ]); + expect(getTriggerText("[capture]testing", 1)).toEqual(["capture", 1, 8]); }); test("text before no whitespace", () => { - expect(getTriggerText("test[capture]", 5)).toEqual([ - "capture", - 5, - 12, - ]); + expect(getTriggerText("test[capture]", 5)).toEqual(["capture", 5, 12]); }); test("text before and behind", () => { - expect(getTriggerText("test [capture] testing", 6)).toEqual([ - "capture", - 6, - 13, - ]); + expect(getTriggerText("test [capture] testing", 6)).toEqual(["capture", 6, 13]); }); test("with colon", () => { expect(getTriggerText("[capture:]", 1)).toEqual(["capture:", 1, 9]); }); test("with double colon", () => { - expect(getTriggerText("[capture::]", 1)).toEqual([ - "capture::", - 1, - 10, - ]); + expect(getTriggerText("[capture::]", 1)).toEqual(["capture::", 1, 10]); }); test("metadata field", () => { - expect(getTriggerText("[capture::Bob]", 1)).toEqual([ - "capture::Bob", - 1, - 13, - ]); + expect(getTriggerText("[capture::Bob]", 1)).toEqual(["capture::Bob", 1, 13]); }); test("metadata field with whitespace", () => { - expect(getTriggerText("[capture:: Bob]", 1)).toEqual([ - "capture:: Bob", - 1, - 14, - ]); + expect(getTriggerText("[capture:: Bob]", 1)).toEqual(["capture:: Bob", 1, 14]); }); test("metadata field with more white space", () => { - expect(getTriggerText("[capture:: Bob]", 1)).toEqual([ - "capture:: Bob", - 1, - 16, - ]); + expect(getTriggerText("[capture:: Bob]", 1)).toEqual(["capture:: Bob", 1, 16]); }); test("double field", () => { - expect(getTriggerText("[capture1][capture2]", 1)).toEqual([ - "capture1", - 1, - 9, - ]); - expect(getTriggerText("[capture1][capture2]", 11)).toEqual([ - "capture2", - 11, - 19, - ]); + expect(getTriggerText("[capture1][capture2]", 1)).toEqual(["capture1", 1, 9]); + expect(getTriggerText("[capture1][capture2]", 11)).toEqual(["capture2", 11, 19]); }); test("wiki link", () => { - expect(getTriggerText("[test [[test]]]", 1)).toEqual([ - "test [[test]]", - 1, - 14, - ]); - expect(getTriggerText("[test [[test]]]", 14)).toEqual([ - "test [[test]]", - 1, - 14, - ]); + expect(getTriggerText("[test [[test]]]", 1)).toEqual(["test [[test]]", 1, 14]); + expect(getTriggerText("[test [[test]]]", 14)).toEqual(["test [[test]]", 1, 14]); }); test("aliased wiki link", () => { - expect(getTriggerText("[test [[test|display]]]", 1)).toEqual([ - "test [[test|display]]", - 1, - 22, - ]); - expect(getTriggerText("[test [[test|display]]]", 22)).toEqual([ - "test [[test|display]]", - 1, - 22, - ]); + expect(getTriggerText("[test [[test|display]]]", 1)).toEqual(["test [[test|display]]", 1, 22]); + expect(getTriggerText("[test [[test|display]]]", 22)).toEqual(["test [[test|display]]", 1, 22]); }); test("markdown link", () => { - expect(getTriggerText("[[test](https://example.com)]", 1)).toEqual([ - "[test](https://example.com)", - 1, - 28, - ]); + expect(getTriggerText("[[test](https://example.com)]", 1)).toEqual(["[test](https://example.com)", 1, 28]); }); test("markdown link local", () => { - expect(getTriggerText("[[test](test)]", 1)).toEqual([ - "[test](test)", - 1, - 13, - ]); + expect(getTriggerText("[[test](test)]", 1)).toEqual(["[test](test)", 1, 13]); }); test("nested parantheses", () => { expect(getTriggerText("[(test)]", 1)).toEqual(["(test)", 1, 7]); @@ -270,32 +134,22 @@ describe("trigger", () => { describe("cursor position", () => { test("cursor in first field", () => { - expect( - getTriggerText("test (test) string (test2) testing", 9), - ).toEqual(["test", 6, 10]); + expect(getTriggerText("test (test) string (test2) testing", 9)).toEqual(["test", 6, 10]); }); test("cursor in second field", () => { - expect( - getTriggerText("test (test) string (test2) testing", 21), - ).toEqual(["test2", 20, 25]); + expect(getTriggerText("test (test) string (test2) testing", 21)).toEqual(["test2", 20, 25]); }); test("ignore cursor out of field", () => { - expect( - getTriggerText("test (test) string (test2) testing", 2), - ).toEqual(null); + expect(getTriggerText("test (test) string (test2) testing", 2)).toEqual(null); }); }); describe("ignore plain markdown links", () => { test("cursor in link text", () => { - expect(getTriggerText("[test](https://example.com)", 3)).toEqual( - null, - ); + expect(getTriggerText("[test](https://example.com)", 3)).toEqual(null); }); test("cursor in link url", () => { - expect(getTriggerText("[test](https://example.com)", 11)).toEqual( - null, - ); + expect(getTriggerText("[test](https://example.com)", 11)).toEqual(null); }); test("cursor in empty link text", () => { expect(getTriggerText("[](https://example.com)", 1)).toEqual(null); diff --git a/src/trigger.ts b/src/trigger.ts index a43991b..44e2683 100644 --- a/src/trigger.ts +++ b/src/trigger.ts @@ -35,10 +35,7 @@ const filledRegex = new RegExp( * 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. */ -export function getTriggerText( - line: string, - cursorPos: number, -): [string, number, number] | null { +export function getTriggerText(line: string, cursorPos: number): [string, number, number] | null { let match = getTriggerTextFromRegex(line, cursorPos, filledRegex); if (match !== null) { 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. * Otherwise, returns null. */ -function getTriggerTextFromRegex( - line: string, - cursorPos: number, - regex: RegExp, -): [string, number, number] | null { +function getTriggerTextFromRegex(line: string, cursorPos: number, regex: RegExp): [string, number, number] | null { let matches = Array.from(line.matchAll(regex)); for (const match of matches) { console.warn(match);