Skip to content

Commit 8e62ec0

Browse files
evidolobdatho7561
authored andcommitted
Add on type conversion of tab char to spaces
Signed-off-by: Yevhen Vydolob <yvydolob@redhat.com> Signed-off-by: David Thompson <davthomp@redhat.com>
1 parent 410cff9 commit 8e62ec0

File tree

3 files changed

+65
-46
lines changed

3 files changed

+65
-46
lines changed

src/languageservice/services/yamlOnTypeFormatting.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,14 @@ export function doDocumentOnTypeFormatting(
5050
return [TextEdit.insert(position, ' ')];
5151
}
5252
}
53+
54+
if (params.ch === '\t' && params.options.insertSpaces) {
55+
return [
56+
TextEdit.replace(
57+
Range.create(position.line, position.character - 1, position.line, position.character),
58+
' '.repeat(params.options.tabSize)
59+
),
60+
];
61+
}
62+
return;
5363
}

src/yamlServerInit.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ export class YAMLServerInit {
128128
documentFormattingProvider: !this.yamlSettings.clientDynamicRegisterSupport,
129129
documentOnTypeFormattingProvider: {
130130
firstTriggerCharacter: '\n',
131+
moreTriggerCharacter: ['\t'],
131132
},
132133
documentRangeFormattingProvider: false,
133134
definitionProvider: true,

test/yamlOnTypeFormatting.test.ts

Lines changed: 54 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -17,58 +17,66 @@ function createParams(position: Position): DocumentOnTypeFormattingParams {
1717
};
1818
}
1919
describe('YAML On Type Formatter', () => {
20-
it('should react on "\n" only', () => {
21-
const doc = setupTextDocument('foo:');
22-
const params = createParams(Position.create(1, 0));
23-
params.ch = '\t';
24-
const result = doDocumentOnTypeFormatting(doc, params);
25-
expect(result).is.undefined;
26-
});
20+
describe('On Enter Formatter', () => {
21+
it('should add indentation for mapping', () => {
22+
const doc = setupTextDocument('foo:\n');
23+
const params = createParams(Position.create(1, 0));
24+
const result = doDocumentOnTypeFormatting(doc, params);
25+
expect(result).to.deep.include(TextEdit.insert(Position.create(1, 0), ' '));
26+
});
2727

28-
it('should add indentation for mapping', () => {
29-
const doc = setupTextDocument('foo:\n');
30-
const params = createParams(Position.create(1, 0));
31-
const result = doDocumentOnTypeFormatting(doc, params);
32-
expect(result).to.deep.include(TextEdit.insert(Position.create(1, 0), ' '));
33-
});
28+
it('should add indentation for scalar array items', () => {
29+
const doc = setupTextDocument('foo:\n - some\n ');
30+
const pos = Position.create(2, 2);
31+
const params = createParams(pos);
32+
const result = doDocumentOnTypeFormatting(doc, params);
33+
expect(result[0]).to.eqls(TextEdit.insert(pos, '- '));
34+
});
3435

35-
it('should add indentation for scalar array items', () => {
36-
const doc = setupTextDocument('foo:\n - some\n ');
37-
const pos = Position.create(2, 2);
38-
const params = createParams(pos);
39-
const result = doDocumentOnTypeFormatting(doc, params);
40-
expect(result[0]).to.eqls(TextEdit.insert(pos, '- '));
41-
});
36+
it('should add indentation for mapping in array', () => {
37+
const doc = setupTextDocument('some:\n - arr:\n ');
38+
const pos = Position.create(2, 2);
39+
const params = createParams(pos);
40+
const result = doDocumentOnTypeFormatting(doc, params);
41+
expect(result).to.deep.include(TextEdit.insert(pos, ' '));
42+
});
4243

43-
it('should add indentation for mapping in array', () => {
44-
const doc = setupTextDocument('some:\n - arr:\n ');
45-
const pos = Position.create(2, 2);
46-
const params = createParams(pos);
47-
const result = doDocumentOnTypeFormatting(doc, params);
48-
expect(result).to.deep.include(TextEdit.insert(pos, ' '));
49-
});
44+
it('should replace all spaces in newline', () => {
45+
const doc = setupTextDocument('some:\n ');
46+
const pos = Position.create(1, 0);
47+
const params = createParams(pos);
48+
const result = doDocumentOnTypeFormatting(doc, params);
49+
expect(result).to.deep.include.members([
50+
TextEdit.del(Range.create(pos, Position.create(1, 3))),
51+
TextEdit.insert(pos, ' '),
52+
]);
53+
});
5054

51-
it('should replace all spaces in newline', () => {
52-
const doc = setupTextDocument('some:\n ');
53-
const pos = Position.create(1, 0);
54-
const params = createParams(pos);
55-
const result = doDocumentOnTypeFormatting(doc, params);
56-
expect(result).to.deep.include.members([TextEdit.del(Range.create(pos, Position.create(1, 3))), TextEdit.insert(pos, ' ')]);
57-
});
55+
it('should keep all non white spaces characters in newline', () => {
56+
const doc = setupTextDocument('some:\n foo');
57+
const pos = Position.create(1, 0);
58+
const params = createParams(pos);
59+
const result = doDocumentOnTypeFormatting(doc, params);
60+
expect(result).is.undefined;
61+
});
5862

59-
it('should keep all non white spaces characters in newline', () => {
60-
const doc = setupTextDocument('some:\n foo');
61-
const pos = Position.create(1, 0);
62-
const params = createParams(pos);
63-
const result = doDocumentOnTypeFormatting(doc, params);
64-
expect(result).is.undefined;
63+
it('should add indentation for multiline string', () => {
64+
const doc = setupTextDocument('some: |\n');
65+
const pos = Position.create(1, 0);
66+
const params = createParams(pos);
67+
const result = doDocumentOnTypeFormatting(doc, params);
68+
expect(result).to.deep.include(TextEdit.insert(pos, ' '));
69+
});
6570
});
6671

67-
it('should add indentation for multiline string', () => {
68-
const doc = setupTextDocument('some: |\n');
69-
const pos = Position.create(1, 0);
70-
const params = createParams(pos);
71-
const result = doDocumentOnTypeFormatting(doc, params);
72-
expect(result).to.deep.include(TextEdit.insert(pos, ' '));
72+
describe('On Tab Formatter', () => {
73+
it('should replace Tab with spaces', () => {
74+
const doc = setupTextDocument('some:\n\t');
75+
const pos = Position.create(1, 1);
76+
const params = createParams(pos);
77+
params.ch = '\t';
78+
const result = doDocumentOnTypeFormatting(doc, params);
79+
expect(result).to.deep.include(TextEdit.replace(Range.create(1, 0, 1, 1), ' '));
80+
});
7381
});
7482
});

0 commit comments

Comments
 (0)