mirror of
https://github.com/Microsoft/vscode
synced 2024-08-27 21:09:43 +00:00
fixes #5269: [json] "Format Code" folds braces into preceding comments.
This commit is contained in:
parent
41ec572ca0
commit
fc59387aba
|
@ -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--;
|
||||
|
|
|
@ -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 = [
|
||||
|
|
|
@ -66,14 +66,15 @@ export function format(model: EditorCommon.IMirrorModel, range: EditorCommon.IRa
|
|||
var firstTokenEnd = model.getPositionFromOffset(scanner.getTokenOffset() + scanner.getTokenLength() + rangeOffset);
|
||||
var 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
|
||||
var commentTokenStart = model.getPositionFromOffset(scanner.getTokenOffset() + rangeOffset);
|
||||
addEdit(' ', { startLineNumber: firstTokenEnd.lineNumber, startColumn: firstTokenEnd.column, endLineNumber: commentTokenStart.lineNumber, endColumn: commentTokenStart.column });
|
||||
firstTokenEnd = model.getPositionFromOffset(scanner.getTokenOffset() + scanner.getTokenLength() + rangeOffset);
|
||||
replaceContent = secondToken === Json.SyntaxKind.LineCommentTrivia ? newLineAndIndent() : '';
|
||||
secondToken = scanNext();
|
||||
}
|
||||
var replaceContent = '';
|
||||
if (secondToken === Json.SyntaxKind.CloseBraceToken) {
|
||||
if (firstToken !== Json.SyntaxKind.OpenBraceToken) {
|
||||
indentLevel--;
|
||||
|
|
|
@ -253,6 +253,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*/ ',
|
||||
|
@ -308,6 +321,19 @@ 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 = [
|
||||
|
|
Loading…
Reference in a new issue