From cef3f435acaf8a472a04254e07179753925b1e84 Mon Sep 17 00:00:00 2001 From: Yevhen Vydolob Date: Mon, 14 Dec 2020 14:54:16 +0200 Subject: [PATCH] #364 add space before value from default snippets (#373) Signed-off-by: Yevhen Vydolob --- src/languageservice/services/yamlCompletion.ts | 6 +++++- test/defaultSnippets.test.ts | 13 +++++++++++-- test/fixtures/defaultSnippets.json | 9 +++++++++ 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/src/languageservice/services/yamlCompletion.ts b/src/languageservice/services/yamlCompletion.ts index 041fe3aa..e54850e1 100644 --- a/src/languageservice/services/yamlCompletion.ts +++ b/src/languageservice/services/yamlCompletion.ts @@ -792,7 +792,7 @@ export class YAMLCompletion extends JSONCompletion { const propertyText = this.getInsertTextForValue(key, '', 'string'); const resultText = propertyText + ':'; - let value; + let value: string; let nValueProposals = 0; if (propertySchema) { let type = Array.isArray(propertySchema.type) ? propertySchema.type[0] : propertySchema.type; @@ -817,6 +817,10 @@ export class YAMLCompletion extends JSONCompletion { }, 1 ); + // add space before default snippet value + if (!value.startsWith(' ') && !value.startsWith('\n')) { + value = ' ' + value; + } } } nValueProposals += propertySchema.defaultSnippets.length; diff --git a/test/defaultSnippets.test.ts b/test/defaultSnippets.test.ts index e0e90b9d..5b302802 100644 --- a/test/defaultSnippets.test.ts +++ b/test/defaultSnippets.test.ts @@ -7,6 +7,7 @@ import assert = require('assert'); import path = require('path'); import { ServiceSetup } from './utils/serviceSetup'; import { CompletionList } from 'vscode-languageserver'; +import { expect } from 'chai'; const uri = toFsPath(path.join(__dirname, './fixtures/defaultSnippets.json')); const fileMatch = ['*.yml', '*.yaml']; @@ -154,7 +155,7 @@ suite('Default Snippet Tests', () => { const completion = parseSetup(content, 3); completion .then(function (result) { - assert.equal(result.items.length, 8); // This is just checking the total number of snippets in the defaultSnippets.json + assert.equal(result.items.length, 9); // This is just checking the total number of snippets in the defaultSnippets.json assert.equal(result.items[4].label, 'longSnippet'); // eslint-disable-next-line assert.equal( @@ -170,7 +171,7 @@ suite('Default Snippet Tests', () => { const completion = parseSetup(content, 11); completion .then(function (result) { - assert.equal(result.items.length, 8); // This is just checking the total number of snippets in the defaultSnippets.json + assert.equal(result.items.length, 9); // This is just checking the total number of snippets in the defaultSnippets.json assert.equal(result.items[5].label, 'arrayArraySnippet'); assert.equal( result.items[5].insertText, @@ -253,5 +254,13 @@ suite('Default Snippet Tests', () => { assert.equal(result.items[0].textEdit.range.end.line, 0); assert.equal(result.items[0].textEdit.range.end.character, 9); }); + + it('should add space before value on root node', async () => { + const content = 'name\n'; + const result = await parseSetup(content, 4); + const item = result.items.find((i) => i.label === 'name'); + expect(item).is.not.undefined; + expect(item.textEdit.newText).to.be.equal('name: some'); + }); }); }); diff --git a/test/fixtures/defaultSnippets.json b/test/fixtures/defaultSnippets.json index 6edb9d23..e1a4fe50 100644 --- a/test/fixtures/defaultSnippets.json +++ b/test/fixtures/defaultSnippets.json @@ -122,6 +122,15 @@ } } ] + }, + "name": { + "type": "string", + "defaultSnippets": [ + { + "label": "some", + "body": "some" + } + ] } } }