Skip to content

Commit

Permalink
Merge pull request #669 from msivasubramaniaan/fix-auto-completion-on…
Browse files Browse the repository at this point in the history
…-list-indentation

fixed auto completion provides valid suggetion even though list indentation not proper
  • Loading branch information
msivasubramaniaan authored Mar 5, 2022
2 parents 387a6ef + b2e3391 commit b0fee20
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 9 deletions.
41 changes: 34 additions & 7 deletions src/languageservice/parser/yaml-documents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,15 @@ export class SingleYAMLDocument extends JSONDocument {
return this.internalDocument.warnings.map(YAMLErrorToYamlDocDiagnostics);
}

getNodeFromPosition(positionOffset: number, textBuffer: TextBuffer): [YamlNode | undefined, boolean] {
getNodeFromPosition(
positionOffset: number,
textBuffer: TextBuffer,
configuredIndentation?: number
): [YamlNode | undefined, boolean] {
const position = textBuffer.getPosition(positionOffset);
const lineContent = textBuffer.getLineContent(position.line);
if (lineContent.trim().length === 0) {
return [this.findClosestNode(positionOffset, textBuffer), true];
return [this.findClosestNode(positionOffset, textBuffer, configuredIndentation), true];
}

let closestNode: Node;
Expand All @@ -122,7 +126,7 @@ export class SingleYAMLDocument extends JSONDocument {
return [closestNode, false];
}

findClosestNode(offset: number, textBuffer: TextBuffer): YamlNode {
findClosestNode(offset: number, textBuffer: TextBuffer, configuredIndentation?: number): YamlNode {
let offsetDiff = this.internalDocument.range[2];
let maxOffset = this.internalDocument.range[0];
let closestNode: YamlNode;
Expand Down Expand Up @@ -151,34 +155,57 @@ export class SingleYAMLDocument extends JSONDocument {
}

if (indentation === position.character) {
closestNode = this.getProperParentByIndentation(indentation, closestNode, textBuffer);
closestNode = this.getProperParentByIndentation(indentation, closestNode, textBuffer, '', configuredIndentation);
}

return closestNode;
}

private getProperParentByIndentation(indentation: number, node: YamlNode, textBuffer: TextBuffer): YamlNode {
private getProperParentByIndentation(
indentation: number,
node: YamlNode,
textBuffer: TextBuffer,
currentLine: string,
configuredIndentation: number,
rootParent?: YamlNode
): YamlNode {
if (!node) {
return this.internalDocument.contents as Node;
}
configuredIndentation = !configuredIndentation ? 2 : configuredIndentation;
if (isNode(node) && node.range) {
const position = textBuffer.getPosition(node.range[0]);
const lineContent = textBuffer.getLineContent(position.line);
currentLine = currentLine === '' ? lineContent.trim() : currentLine;
if (currentLine.startsWith('-') && indentation === configuredIndentation && currentLine === lineContent.trim()) {
position.character += indentation;
}
if (position.character > indentation && position.character > 0) {
const parent = this.getParent(node);
if (parent) {
return this.getProperParentByIndentation(indentation, parent, textBuffer);
return this.getProperParentByIndentation(
indentation,
parent,
textBuffer,
currentLine,
configuredIndentation,
rootParent
);
}
} else if (position.character < indentation) {
const parent = this.getParent(node);
if (isPair(parent) && isNode(parent.value)) {
return parent.value;
} else if (isPair(rootParent) && isNode(rootParent.value)) {
return rootParent.value;
}
} else {
return node;
}
} else if (isPair(node)) {
rootParent = node;
const parent = this.getParent(node);
return this.getProperParentByIndentation(indentation, parent, textBuffer);
return this.getProperParentByIndentation(indentation, parent, textBuffer, currentLine, configuredIndentation, rootParent);
}
return node;
}
Expand Down
2 changes: 1 addition & 1 deletion src/languageservice/services/yamlCompletion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ export class YamlCompletion {
// as we modify AST for completion, we need to use copy of original document
currentDoc = currentDoc.clone();

let [node, foundByClosest] = currentDoc.getNodeFromPosition(offset, textBuffer);
let [node, foundByClosest] = currentDoc.getNodeFromPosition(offset, textBuffer, this.indentation.length);

const currentWord = this.getCurrentWord(document, offset);

Expand Down
34 changes: 34 additions & 0 deletions test/autoCompletion.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2066,6 +2066,40 @@ describe('Auto Completion Tests', () => {
expect(completion.items[0].insertText).eq('fooBar:\n name: $1\n aaa:\n - $2');
});

it('auto completion based on the list indentation', async () => {
languageService.addSchema(SCHEMA_ID, {
type: 'array',
items: {
type: 'object',
properties: {
prop1: {
type: 'string',
},
prop2: {
type: 'string',
},
Object: {
type: 'array',
items: {
type: 'object',
properties: {
env_prop1: {
type: 'string',
},
},
},
},
},
},
});

const content = '- prop1: value\n object:\n - env_prop1: value\n ';
const completion = await parseSetup(content, 49);
expect(completion.items).lengthOf(2);
expect(completion.items[0].label).eq('prop2');
expect(completion.items[0].insertText).eq('prop2: ');
});

it('should complete string which contains number in default value', async () => {
languageService.addSchema(SCHEMA_ID, {
type: 'object',
Expand Down
2 changes: 1 addition & 1 deletion test/yaml-documents.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ objB:

expect(result).is.not.undefined;
expect(isMap(result)).is.true;
expect(((result as YAMLMap).items[0].key as Scalar).value).eqls('foo');
expect(((result as YAMLMap).items[0].key as Scalar).value).eqls('bar');
});
});
});
Expand Down

0 comments on commit b0fee20

Please sign in to comment.