Skip to content

Commit

Permalink
(incomplete) retrieve value prefix for proper value completion
Browse files Browse the repository at this point in the history
  • Loading branch information
imolorhe committed May 29, 2024
1 parent b191d5e commit 605771b
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/__tests__/json-completion.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ describe.each([
},
],
},
// TODO: should provide true/false completions
// TODO: should provide true/false completions. Issue is the detected node is the Property node, which contains the property name and value. The prefix for the autocompletion therefore contains the property name, so it never matches the results
// {
// name: "include value completions for boolean",
// mode: MODES.JSON,
Expand Down
34 changes: 23 additions & 11 deletions src/json-completion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export class JSONCompletion {
let node: SyntaxNode | null = getNodeAtPosition(ctx.state, ctx.pos);

// position node word prefix (without quotes) for matching
const prefix = ctx.state.sliceDoc(node.from, ctx.pos).replace(/^("|')/, "");
let prefix = ctx.state.sliceDoc(node.from, ctx.pos).replace(/^(["'])/, "");

debug.log("xxx", "node", node, "prefix", prefix, "ctx", ctx);

Expand Down Expand Up @@ -213,7 +213,14 @@ export class JSONCompletion {
const types: { [type: string]: boolean } = {};

// value proposals with schema
this.getValueCompletions(this.schema, ctx, types, collector);
const res = this.getValueCompletions(this.schema, ctx, types, collector);
debug.log("xxx", "getValueCompletions res", res);
if (res) {
// TODO: While this works, we also need to handle the completion from and to positions to use it
// // use the value node to calculate the prefix
// prefix = res.valuePrefix;
// debug.log("xxx", "using valueNode prefix", prefix);
}
}

// handle filtering
Expand Down Expand Up @@ -460,15 +467,7 @@ export class JSONCompletion {
}
private getInsertTextForPropertyName(key: string, rawWord: string) {
switch (this.mode) {
case MODES.JSON5: {
if (rawWord.startsWith('"')) {
return `"${key}"`;
}
if (rawWord.startsWith("'")) {
return `'${key}'`;
}
return key;
}
case MODES.JSON5:
case MODES.YAML: {
if (rawWord.startsWith('"')) {
return `"${key}"`;
Expand Down Expand Up @@ -660,6 +659,19 @@ export class JSONCompletion {
}
}
}

// TODO: We need to pass the from and to for the value node as well
// TODO: What should be the from and to when the value node is null?
// TODO: (NOTE: if we pass a prefix but no from and to, it will autocomplete the value but replace
// TODO: the entire property nodewhich isn't what we want). Instead we need to change the from and to
// TODO: based on the corresponding (relevant) value node
const valuePrefix = valueNode
? getWord(ctx.state.doc, valueNode, true, false)
: "";

return {
valuePrefix,
};
}

private addSchemaValueCompletions(
Expand Down
11 changes: 9 additions & 2 deletions src/utils/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,17 @@ export const surroundingDoubleQuotesToSingle = (str: string) => {
export const getWord = (
doc: Text,
node: SyntaxNode | null,
stripQuotes = true
stripQuotes = true,
onlyEvenQuotes = true
) => {
const word = node ? doc.sliceString(node.from, node.to) : "";
return stripQuotes ? stripSurroundingQuotes(word) : word;
if (!stripQuotes) {
return word;
}
if (onlyEvenQuotes) {
return stripSurroundingQuotes(word);
}
return word.replace(/(^["'])|(["']$)/g, "");
};

export const isInvalidValueNode = (node: SyntaxNode, mode: JSONMode) => {
Expand Down

0 comments on commit 605771b

Please sign in to comment.