Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: completion in the middle of the empty text #696

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/languageservice/services/yamlCompletion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,20 @@ export class YamlCompletion {

let originalNode = node;
if (node) {
// when the value is null but the cursor is between prop name and null value (cursor is not at the end of the line)
if (isMap(node) && node.items.length && isPair(node.items[0])) {
const pairNode = node.items[0];
if (
isScalar(pairNode.value) &&
isScalar(pairNode.key) &&
pairNode.value.value === null && // value is null
pairNode.key.range[2] < offset && // cursor is after colon
pairNode.value.range[0] > offset // cursor is before null
) {
node = pairNode.value;
overwriteRange.end.character += pairNode.value.range[2] - offset; // extend range to the end of the null element
}
}
// when the array item value is null but the cursor is between '-' and null value (cursor is not at the end of the line)
if (isSeq(node) && node.items.length && isScalar(node.items[0]) && lineContent.includes('-')) {
const nullNode = node.items[0];
Expand Down
17 changes: 17 additions & 0 deletions test/autoCompletionFix.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,23 @@ objB:
expect(completion.items[1].insertText).to.be.equal('obj1:\n prop2: ${1:value}');
});

it('should suggest when cursor is not on the end of the line', async () => {
const schema: JSONSchema = {
properties: {
prop: {
const: 'const',
},
},
};
languageService.addSchema(SCHEMA_ID, schema);
const content = 'prop: ';
const completion = await parseSetup(content, 0, 6);

expect(completion.items.length).equal(1);
expect(completion.items[0].label).to.be.equal('const');
expect(completion.items[0].textEdit).to.be.deep.equal({ newText: 'const', range: Range.create(0, 6, 0, content.length) });
});

it('should suggest object array when extra space is after cursor', async () => {
const schema: JSONSchema = {
properties: {
Expand Down