Skip to content

Commit f61734c

Browse files
committed
fix: array item with existing property
1 parent ed03cbf commit f61734c

2 files changed

Lines changed: 83 additions & 15 deletions

File tree

src/languageservice/services/yamlCompletion.ts

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -144,11 +144,11 @@ export class YamlCompletion {
144144

145145
this.arrayPrefixIndentation = '';
146146
let overwriteRange: Range = null;
147+
const isOnlyHyphen = lineContent.match(/^\s*(-)\s*$/);
147148
if (areOnlySpacesAfterPosition) {
148149
overwriteRange = Range.create(position, Position.create(position.line, lineContent.length));
149150
const isOnlyWhitespace = lineContent.trim().length === 0;
150-
const isOnlyDash = lineContent.match(/^\s*(-)\s*$/);
151-
if (node && isScalar(node) && !isOnlyWhitespace && !isOnlyDash) {
151+
if (node && isScalar(node) && !isOnlyWhitespace && !isOnlyHyphen) {
152152
const lineToPosition = lineContent.substring(0, position.character);
153153
const matches =
154154
// get indentation of unfinished property (between indent and cursor)
@@ -357,6 +357,12 @@ export class YamlCompletion {
357357
if (node) {
358358
if (lineContent.length === 0) {
359359
node = currentDoc.internalDocument.contents as Node;
360+
} else if (isSeq(node) && isOnlyHyphen) {
361+
const index = this.findItemAtOffset(node, document, offset);
362+
const item = node.items[index];
363+
if (isNode(item)) {
364+
node = item;
365+
}
360366
} else {
361367
const parent = currentDoc.getParent(node);
362368
if (parent) {
@@ -717,19 +723,19 @@ export class YamlCompletion {
717723
const propertySchema = schemaProperties[key];
718724

719725
if (typeof propertySchema === 'object' && !propertySchema.deprecationMessage && !propertySchema['doNotSuggest']) {
720-
let identCompensation = '';
726+
let indentCompensation = '';
721727
if (nodeParent && isSeq(nodeParent) && node.items.length <= 1 && !hasOnlyWhitespace) {
722-
// because there is a slash '-' to prevent the properties generated to have the correct
723-
// indent
724-
const sourceText = textBuffer.getText();
725-
const indexOfSlash = sourceText.lastIndexOf('-', node.range[0] - 1);
726-
if (indexOfSlash >= 0) {
727-
// add one space to compensate the '-'
728-
const overwriteChars = overwriteRange.end.character - overwriteRange.start.character;
729-
identCompensation = ' ' + sourceText.slice(indexOfSlash + 1, node.range[1] - overwriteChars);
728+
// because there is a slash '-' to prevent the properties generated to have the correct indent
729+
const fromLastHyphenToPosition = lineContent.slice(
730+
lineContent.lastIndexOf('-'),
731+
overwriteRange.start.character
732+
);
733+
const hyphenFollowedByEmpty = fromLastHyphenToPosition.match(/-([ \t]*)/);
734+
if (hyphenFollowedByEmpty) {
735+
indentCompensation = ' ' + hyphenFollowedByEmpty[1];
730736
}
731737
}
732-
identCompensation += this.arrayPrefixIndentation;
738+
indentCompensation += this.arrayPrefixIndentation;
733739

734740
// if check that current node has last pair with "null" value and key witch match key from schema,
735741
// and if schema has array definition it add completion item for array item creation
@@ -760,7 +766,7 @@ export class YamlCompletion {
760766
key,
761767
propertySchema,
762768
separatorAfter,
763-
identCompensation + this.indentation
769+
indentCompensation + this.indentation
764770
);
765771
}
766772
const isNodeNull =
@@ -787,13 +793,13 @@ export class YamlCompletion {
787793
key,
788794
propertySchema,
789795
separatorAfter,
790-
identCompensation + this.indentation
796+
indentCompensation + this.indentation
791797
),
792798
insertTextFormat: InsertTextFormat.Snippet,
793799
documentation: this.fromMarkup(propertySchema.markdownDescription) || propertySchema.description || '',
794800
parent: {
795801
schema: schema.schema,
796-
indent: identCompensation,
802+
indent: indentCompensation,
797803
},
798804
});
799805
}

test/autoCompletionFix.test.ts

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -865,6 +865,30 @@ objB:
865865
})
866866
);
867867
});
868+
it('indent compensation for partial key with trailing spaces', async () => {
869+
const schema: JSONSchema = {
870+
type: 'object',
871+
properties: {
872+
array: {
873+
type: 'array',
874+
items: {
875+
type: 'object',
876+
properties: {
877+
obj1: {
878+
type: 'object',
879+
},
880+
},
881+
},
882+
},
883+
},
884+
};
885+
languageService.addSchema(SCHEMA_ID, schema);
886+
const content = 'array:\n - na ';
887+
const completion = await parseSetup(content, 1, 5);
888+
889+
expect(completion.items.length).equal(1);
890+
expect(completion.items[0].insertText).eql('obj1:\n ');
891+
});
868892

869893
describe('partial value with trailing spaces', () => {
870894
it('partial value with trailing spaces', async () => {
@@ -1161,6 +1185,44 @@ objB:
11611185
expect(result.items.length).to.be.equal(1);
11621186
expect(result.items[0].insertText).to.be.equal('objA:\n itemA: ');
11631187
});
1188+
1189+
describe('array item with existing property', () => {
1190+
const schema: JSONSchema = {
1191+
type: 'object',
1192+
properties: {
1193+
array1: {
1194+
type: 'array',
1195+
items: {
1196+
type: 'object',
1197+
properties: {
1198+
objA: {
1199+
type: 'object',
1200+
},
1201+
propB: {
1202+
const: 'test',
1203+
},
1204+
},
1205+
},
1206+
},
1207+
},
1208+
};
1209+
it('should get extra space compensation for the 1st prop in array object item', async () => {
1210+
languageService.addSchema(SCHEMA_ID, schema);
1211+
const content = 'array1:\n - \n propB: test';
1212+
const result = await parseSetup(content, 1, 4); // after `- `
1213+
1214+
expect(result.items.length).to.be.equal(1);
1215+
expect(result.items[0].insertText).to.be.equal('objA:\n ');
1216+
});
1217+
it('should get extra space compensation for the 1st prop in array object item - extra spaces', async () => {
1218+
languageService.addSchema(SCHEMA_ID, schema);
1219+
const content = 'array1:\n - \n propB: test';
1220+
const result = await parseSetup(content, 1, 4); // after `- `
1221+
1222+
expect(result.items.length).to.be.equal(1);
1223+
expect(result.items[0].insertText).to.be.equal('objA:\n ');
1224+
});
1225+
});
11641226
}); //'extra space after cursor'
11651227

11661228
it('should suggest from additionalProperties', async () => {

0 commit comments

Comments
 (0)