fixes #5269: [json] "Format Code" folds braces into preceding comments.

This commit is contained in:
Martin Aeschlimann
2016-04-22 15:57:04 +02:00
parent 41ec572ca0
commit fc59387aba
4 changed files with 58 additions and 2 deletions

View File

@@ -74,14 +74,16 @@ export function format(document: ITextDocument, range: Range, options: Formattin
let firstTokenEnd = scanner.getTokenOffset() + scanner.getTokenLength() + rangeOffset;
let secondToken = scanNext();
let replaceContent = '';
while (!lineBreak && (secondToken === Json.SyntaxKind.LineCommentTrivia || secondToken === Json.SyntaxKind.BlockCommentTrivia)) {
// comments on the same line: keep them on the same line, but ignore them otherwise
let commentTokenStart = scanner.getTokenOffset() + rangeOffset;
addEdit(' ', firstTokenEnd, commentTokenStart);
firstTokenEnd = scanner.getTokenOffset() + scanner.getTokenLength() + rangeOffset;
replaceContent = secondToken === Json.SyntaxKind.LineCommentTrivia ? newLineAndIndent() : '';
secondToken = scanNext();
}
let replaceContent = '';
if (secondToken === Json.SyntaxKind.CloseBraceToken) {
if (firstToken !== Json.SyntaxKind.OpenBraceToken) {
indentLevel--;

View File

@@ -240,6 +240,19 @@ suite('JSON Formatter', () => {
format(content, expected);
});
test('single line comment on same line 2', () => {
var content = [
'{ //comment',
'}'
].join('\n');
var expected = [
'{ //comment',
'}'
].join('\n');
format(content, expected);
});
test('block comment on same line', () => {
var content = [
'{ "a": {}, /*comment*/ ',
@@ -295,6 +308,20 @@ suite('JSON Formatter', () => {
format(content, expected);
});
test('multiple mixed comments on same line', () => {
var content = [
'[ /*comment*/ /*comment*/ // comment ',
']'
].join('\n');
var expected = [
'[ /*comment*/ /*comment*/ // comment ',
']'
].join('\n');
format(content, expected);
});
test('range', () => {
var content = [