Skip to content

Commit

Permalink
#364 add space before value from default snippets (#373)
Browse files Browse the repository at this point in the history
Signed-off-by: Yevhen Vydolob <[email protected]>
  • Loading branch information
evidolob committed Dec 14, 2020
1 parent 3571b76 commit cef3f43
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
6 changes: 5 additions & 1 deletion src/languageservice/services/yamlCompletion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
13 changes: 11 additions & 2 deletions test/defaultSnippets.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
Expand Down Expand Up @@ -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(
Expand All @@ -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,
Expand Down Expand Up @@ -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');
});
});
});
9 changes: 9 additions & 0 deletions test/fixtures/defaultSnippets.json
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,15 @@
}
}
]
},
"name": {
"type": "string",
"defaultSnippets": [
{
"label": "some",
"body": "some"
}
]
}
}
}

0 comments on commit cef3f43

Please sign in to comment.