Skip to content

Commit f03bb1d

Browse files
evidolobdatho7561
authored andcommitted
Add on type conversion of tab char to spaces
Signed-off-by: Yevhen Vydolob <[email protected]> Signed-off-by: David Thompson <[email protected]>
1 parent 2a2e614 commit f03bb1d

File tree

5 files changed

+71
-46
lines changed

5 files changed

+71
-46
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
},
2828
"dependencies": {
2929
"ajv": "^8.11.0",
30+
"fast-uri": "^3.0.6",
3031
"lodash": "4.17.21",
3132
"prettier": "^3.0.0",
3233
"request-light": "^0.5.7",

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
@@ -105,6 +105,7 @@ export class YAMLServerInit {
105105
documentFormattingProvider: false,
106106
documentOnTypeFormattingProvider: {
107107
firstTriggerCharacter: '\n',
108+
moreTriggerCharacter: ['\t'],
108109
},
109110
documentRangeFormattingProvider: false,
110111
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
});

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1517,6 +1517,11 @@ fast-levenshtein@^2.0.6:
15171517
resolved "https://registry.npmjs.org/fast-levenshtein/-/fast-levenshtein-2.0.6.tgz"
15181518
integrity sha1-PYpcZog6FqMMqGQ+hR8Zuqd5eRc=
15191519

1520+
fast-uri@^3.0.6:
1521+
version "3.0.6"
1522+
resolved "https://registry.yarnpkg.com/fast-uri/-/fast-uri-3.0.6.tgz#88f130b77cfaea2378d56bf970dea21257a68748"
1523+
integrity sha512-Atfo14OibSv5wAp4VWNsFYE1AchQRTv9cBGWET4pZWHzYshFSS9NQI6I57rdKn9croWVMbYFbLhJ+yJvmZIIHw==
1524+
15201525
fastq@^1.6.0:
15211526
version "1.13.0"
15221527
resolved "https://registry.yarnpkg.com/fastq/-/fastq-1.13.0.tgz#616760f88a7526bdfc596b7cab8c18938c36b98c"

0 commit comments

Comments
 (0)