Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: proper indenting of snippet within an array #745

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/languageservice/services/yamlCompletion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -634,6 +634,7 @@ export class YamlCompletion {
const lineContent = textBuffer.getLineContent(overwriteRange.start.line);
const hasOnlyWhitespace = lineContent.trim().length === 0;
const hasColon = lineContent.indexOf(':') !== -1;
const isInArray = lineContent.trimLeft().indexOf('-') === 0;
const nodeParent = doc.getParent(node);
const matchOriginal = matchingSchemas.find((it) => it.node.internalNode === originalNode && it.schema.properties);
for (const schema of matchingSchemas) {
Expand All @@ -644,7 +645,7 @@ export class YamlCompletion {
this.collectDefaultSnippets(schema.schema, separatorAfter, collector, {
newLineFirst: false,
indentFirstObject: false,
shouldIndentWithTab: false,
shouldIndentWithTab: isInArray,
});

const schemaProperties = schema.schema.properties;
Expand Down
13 changes: 12 additions & 1 deletion test/defaultSnippets.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ describe('Default Snippet Tests', () => {
const completion = parseSetup(content);
completion
.then(function (result) {
assert.equal(result.items.length, 14); // This is just checking the total number of snippets in the defaultSnippets.json
assert.equal(result.items.length, 15); // 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 Down Expand Up @@ -303,6 +303,17 @@ describe('Default Snippet Tests', () => {
.then(done, done);
});

it('Test snippet in array indented completion', (done) => {
const content = 'arrayWithSnippet:\n - ';
const completion = parseSetup(content, content.length);
completion
.then(function (result) {
assert.equal(result.items.length, 4);
assert.equal(result.items[0].insertText, 'item1: $1\n item2: $2');
})
.then(done, done);
});

it('Test array of objects extra new line', (done) => {
const content = 'arrayObjectSnippet:\n ';
const completion = parseSetup(content, content.length);
Expand Down
12 changes: 12 additions & 0 deletions test/fixtures/defaultSnippets.json
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,18 @@
"suggestionKind": 9
}
]
},
"arrayWithSnippet": {
"type": "array",
"items": {
"defaultSnippets": [
{
"label": "My array item",
"body": { "item1": "$1", "item2": "$2" }
}
],
"type": "string"
}
}
}
}