Skip to content

Add on type conversion of tab char to spaces #1056

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
},
"dependencies": {
"ajv": "^8.11.0",
"fast-uri": "^3.0.6",
"lodash": "4.17.21",
"prettier": "^3.0.0",
"request-light": "^0.5.7",
Expand Down
10 changes: 10 additions & 0 deletions src/languageservice/services/yamlOnTypeFormatting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,14 @@ export function doDocumentOnTypeFormatting(
return [TextEdit.insert(position, ' ')];
}
}

if (params.ch === '\t' && params.options.insertSpaces) {
return [
TextEdit.replace(
Range.create(position.line, position.character - 1, position.line, position.character),
' '.repeat(params.options.tabSize)
),
];
}
return;
}
1 change: 1 addition & 0 deletions src/yamlServerInit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ export class YAMLServerInit {
documentFormattingProvider: false,
documentOnTypeFormattingProvider: {
firstTriggerCharacter: '\n',
moreTriggerCharacter: ['\t'],
},
documentRangeFormattingProvider: false,
definitionProvider: true,
Expand Down
100 changes: 54 additions & 46 deletions test/yamlOnTypeFormatting.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,58 +17,66 @@ function createParams(position: Position): DocumentOnTypeFormattingParams {
};
}
describe('YAML On Type Formatter', () => {
it('should react on "\n" only', () => {
const doc = setupTextDocument('foo:');
const params = createParams(Position.create(1, 0));
params.ch = '\t';
const result = doDocumentOnTypeFormatting(doc, params);
expect(result).is.undefined;
});
describe('On Enter Formatter', () => {
it('should add indentation for mapping', () => {
const doc = setupTextDocument('foo:\n');
const params = createParams(Position.create(1, 0));
const result = doDocumentOnTypeFormatting(doc, params);
expect(result).to.deep.include(TextEdit.insert(Position.create(1, 0), ' '));
});

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

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

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

it('should replace all spaces in newline', () => {
const doc = setupTextDocument('some:\n ');
const pos = Position.create(1, 0);
const params = createParams(pos);
const result = doDocumentOnTypeFormatting(doc, params);
expect(result).to.deep.include.members([TextEdit.del(Range.create(pos, Position.create(1, 3))), TextEdit.insert(pos, ' ')]);
});
it('should keep all non white spaces characters in newline', () => {
const doc = setupTextDocument('some:\n foo');
const pos = Position.create(1, 0);
const params = createParams(pos);
const result = doDocumentOnTypeFormatting(doc, params);
expect(result).is.undefined;
});

it('should keep all non white spaces characters in newline', () => {
const doc = setupTextDocument('some:\n foo');
const pos = Position.create(1, 0);
const params = createParams(pos);
const result = doDocumentOnTypeFormatting(doc, params);
expect(result).is.undefined;
it('should add indentation for multiline string', () => {
const doc = setupTextDocument('some: |\n');
const pos = Position.create(1, 0);
const params = createParams(pos);
const result = doDocumentOnTypeFormatting(doc, params);
expect(result).to.deep.include(TextEdit.insert(pos, ' '));
});
});

it('should add indentation for multiline string', () => {
const doc = setupTextDocument('some: |\n');
const pos = Position.create(1, 0);
const params = createParams(pos);
const result = doDocumentOnTypeFormatting(doc, params);
expect(result).to.deep.include(TextEdit.insert(pos, ' '));
describe('On Tab Formatter', () => {
it('should replace Tab with spaces', () => {
const doc = setupTextDocument('some:\n\t');
const pos = Position.create(1, 1);
const params = createParams(pos);
params.ch = '\t';
const result = doDocumentOnTypeFormatting(doc, params);
expect(result).to.deep.include(TextEdit.replace(Range.create(1, 0, 1, 1), ' '));
});
});
});
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1517,6 +1517,11 @@ fast-levenshtein@^2.0.6:
resolved "https://registry.npmjs.org/fast-levenshtein/-/fast-levenshtein-2.0.6.tgz"
integrity sha1-PYpcZog6FqMMqGQ+hR8Zuqd5eRc=

fast-uri@^3.0.6:
version "3.0.6"
resolved "https://registry.yarnpkg.com/fast-uri/-/fast-uri-3.0.6.tgz#88f130b77cfaea2378d56bf970dea21257a68748"
integrity sha512-Atfo14OibSv5wAp4VWNsFYE1AchQRTv9cBGWET4pZWHzYshFSS9NQI6I57rdKn9croWVMbYFbLhJ+yJvmZIIHw==

fastq@^1.6.0:
version "1.13.0"
resolved "https://registry.yarnpkg.com/fastq/-/fastq-1.13.0.tgz#616760f88a7526bdfc596b7cab8c18938c36b98c"
Expand Down