Skip to content

Commit 840a2d9

Browse files
Merge pull request #696 from p-spacek/fix/completion-in-the-middle-of-the-empty-text
fix: completion in the middle of the empty text
2 parents 9cfb0c7 + 7e3e98d commit 840a2d9

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

src/languageservice/services/yamlCompletion.ts

+14
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,20 @@ export class YamlCompletion {
326326

327327
let originalNode = node;
328328
if (node) {
329+
// when the value is null but the cursor is between prop name and null value (cursor is not at the end of the line)
330+
if (isMap(node) && node.items.length && isPair(node.items[0])) {
331+
const pairNode = node.items[0];
332+
if (
333+
isScalar(pairNode.value) &&
334+
isScalar(pairNode.key) &&
335+
pairNode.value.value === null && // value is null
336+
pairNode.key.range[2] < offset && // cursor is after colon
337+
pairNode.value.range[0] > offset // cursor is before null
338+
) {
339+
node = pairNode.value;
340+
overwriteRange.end.character += pairNode.value.range[2] - offset; // extend range to the end of the null element
341+
}
342+
}
329343
// when the array item value is null but the cursor is between '-' and null value (cursor is not at the end of the line)
330344
if (isSeq(node) && node.items.length && isScalar(node.items[0]) && lineContent.includes('-')) {
331345
const nullNode = node.items[0];

test/autoCompletionFix.test.ts

+17
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,23 @@ objB:
471471
expect(completion.items[1].insertText).to.be.equal('obj1:\n prop2: ${1:value}');
472472
});
473473

474+
it('should suggest when cursor is not on the end of the line', async () => {
475+
const schema: JSONSchema = {
476+
properties: {
477+
prop: {
478+
const: 'const',
479+
},
480+
},
481+
};
482+
languageService.addSchema(SCHEMA_ID, schema);
483+
const content = 'prop: ';
484+
const completion = await parseSetup(content, 0, 6);
485+
486+
expect(completion.items.length).equal(1);
487+
expect(completion.items[0].label).to.be.equal('const');
488+
expect(completion.items[0].textEdit).to.be.deep.equal({ newText: 'const', range: Range.create(0, 6, 0, content.length) });
489+
});
490+
474491
it('should suggest object array when extra space is after cursor', async () => {
475492
const schema: JSONSchema = {
476493
properties: {

0 commit comments

Comments
 (0)