mirror of
https://github.com/dnlbauer/obsidian-dataview-autocompletion.git
synced 2026-09-10 22:25:29 +00:00
chore: more sane line wraps in prettier
This commit is contained in:
@@ -1 +1,3 @@
|
||||
{}
|
||||
{
|
||||
"printWidth": 120
|
||||
}
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -37,11 +37,7 @@ export class DataviewSuggester extends EditorSuggest<String> {
|
||||
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<String> {
|
||||
return null;
|
||||
}
|
||||
|
||||
getSuggestions(
|
||||
context: EditorSuggestContext,
|
||||
): string[] | Promise<string[]> {
|
||||
getSuggestions(context: EditorSuggestContext): string[] | Promise<string[]> {
|
||||
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>(.*?)<\/mark>/g,
|
||||
'<span style="font-weight: bold;">$1</span>',
|
||||
);
|
||||
const formattedHtml = value.replace(/<mark>(.*?)<\/mark>/g, '<span style="font-weight: bold;">$1</span>');
|
||||
el.innerHTML = formattedHtml;
|
||||
}
|
||||
|
||||
@@ -110,15 +91,9 @@ export class DataviewSuggester extends EditorSuggest<String> {
|
||||
}
|
||||
|
||||
// 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<String> {
|
||||
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<String> {
|
||||
// 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<String> {
|
||||
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];
|
||||
|
||||
12
src/main.ts
12
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() {
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user