diff --git a/src/languageservice/services/yamlCompletion.ts b/src/languageservice/services/yamlCompletion.ts index b2efbd68..0e73a54d 100644 --- a/src/languageservice/services/yamlCompletion.ts +++ b/src/languageservice/services/yamlCompletion.ts @@ -144,7 +144,7 @@ export class YamlCompletion { this.arrayPrefixIndentation = ''; let overwriteRange: Range = null; - const isOnlyHyphen = lineContent.match(/^\s*(-)\s*$/); + const isOnlyHyphen = lineContent.match(/^\s*(-)\s*($|#)/); if (areOnlySpacesAfterPosition) { overwriteRange = Range.create(position, Position.create(position.line, lineContent.length)); const isOnlyWhitespace = lineContent.trim().length === 0; diff --git a/test/autoCompletionFix.test.ts b/test/autoCompletionFix.test.ts index a88b9787..2486246a 100644 --- a/test/autoCompletionFix.test.ts +++ b/test/autoCompletionFix.test.ts @@ -883,8 +883,8 @@ objB: }, }; languageService.addSchema(SCHEMA_ID, schema); - const content = 'array:\n - na '; - const completion = await parseSetup(content, 1, 5); + const content = 'array:\n - na| | '; + const completion = await parseCaret(content); expect(completion.items.length).equal(1); expect(completion.items[0].insertText).eql('obj1:\n '); @@ -1208,16 +1208,25 @@ objB: }; it('should get extra space compensation for the 1st prop in array object item', async () => { languageService.addSchema(SCHEMA_ID, schema); - const content = 'array1:\n - \n propB: test'; - const result = await parseSetup(content, 1, 4); // after `- ` + const content = 'array1:\n - |\n| propB: test'; + const result = await parseCaret(content); expect(result.items.length).to.be.equal(1); expect(result.items[0].insertText).to.be.equal('objA:\n '); }); it('should get extra space compensation for the 1st prop in array object item - extra spaces', async () => { languageService.addSchema(SCHEMA_ID, schema); - const content = 'array1:\n - \n propB: test'; - const result = await parseSetup(content, 1, 4); // after `- ` + const content = 'array1:\n - | | \n propB: test'; + const result = await parseCaret(content); + + expect(result.items.length).to.be.equal(1); + expect(result.items[0].insertText).to.be.equal('objA:\n '); + }); + // previous PR doesn't fix this + it.skip('should get extra space compensation for the 1st prop in array object item - extra lines', async () => { + languageService.addSchema(SCHEMA_ID, schema); + const content = 'array1:\n - \n |\n| propB: test'; + const result = await parseCaret(content); expect(result.items.length).to.be.equal(1); expect(result.items[0].insertText).to.be.equal('objA:\n '); diff --git a/test/yaml-documents.test.ts b/test/yaml-documents.test.ts index c2278b96..d4c08618 100644 --- a/test/yaml-documents.test.ts +++ b/test/yaml-documents.test.ts @@ -211,16 +211,77 @@ objB: expect(((result as YAMLMap).items[0].key as Scalar).value).eqls('bar'); }); - it('Find closes node: array', () => { - const doc = setupTextDocument('foo:\n - bar: aaa\n '); - const yamlDoc = documents.getYamlDocument(doc); - const textBuffer = new TextBuffer(doc); + describe('Array', () => { + it('Find closes node: array', () => { + const doc = setupTextDocument('foo:\n - bar: aaa\n '); + const yamlDoc = documents.getYamlDocument(doc); + const textBuffer = new TextBuffer(doc); + + const result = yamlDoc.documents[0].findClosestNode(20, textBuffer); + + expect(result).is.not.undefined; + expect(isSeq(result)).is.true; + expect((((result as YAMLSeq).items[0] as YAMLMap).items[0].key as Scalar).value).eqls('bar'); + }); + it.skip('Find first array item node', () => { + const doc = setupTextDocument(`foo: + - + item1: aaa +`); + const yamlDoc = documents.getYamlDocument(doc); + const textBuffer = new TextBuffer(doc); + + const result = yamlDoc.documents[0].findClosestNode(9, textBuffer); + + expect(result).is.not.undefined; + expect(isMap(result)).is.true; + expect(((result as YAMLMap).items[0].key as Scalar).value).eqls('item1'); + }); + it.skip('Find first array item node - extra indent', () => { + const doc = setupTextDocument(`foo: + - + + item1: aaa +`); + const yamlDoc = documents.getYamlDocument(doc); + const textBuffer = new TextBuffer(doc); + + const result = yamlDoc.documents[0].findClosestNode(9, textBuffer); + + expect(result).is.not.undefined; + expect(isMap(result)).is.true; + expect(((result as YAMLMap).items[0].key as Scalar).value).eqls('item1'); + }); + + it.skip('Find second array item node', () => { + const doc = setupTextDocument(`foo: + - item1: aaa + - + item2: bbb`); + const yamlDoc = documents.getYamlDocument(doc); + const textBuffer = new TextBuffer(doc); + + const result = yamlDoc.documents[0].findClosestNode(24, textBuffer); + + expect(result).is.not.undefined; + expect(isMap(result)).is.true; + expect(((result as YAMLMap).items[0].key as Scalar).value).eqls('item2'); + }); + it.skip('Find second array item node: - extra indent', () => { + const doc = setupTextDocument(`foo: + - item1: aaa + - + + item2: bbb`); + const yamlDoc = documents.getYamlDocument(doc); + const textBuffer = new TextBuffer(doc); - const result = yamlDoc.documents[0].findClosestNode(20, textBuffer); + const result = yamlDoc.documents[0].findClosestNode(28, textBuffer); - expect(result).is.not.undefined; - expect(isSeq(result)).is.true; - expect((((result as YAMLSeq).items[0] as YAMLMap).items[0].key as Scalar).value).eqls('bar'); + expect(result).is.not.undefined; + expect(isMap(result)).is.true; + expect(((result as YAMLMap).items[0].key as Scalar).value).eqls('item2'); + }); }); it('Find closes node: root map', () => {