Skip to content

Commit

Permalink
Can we have a "insertFinalNewline" option in "FormattingOptions"? Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
aeschli committed Nov 13, 2020
1 parent fd5fe07 commit f8bd282
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 5 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
3.0.0 2020-11-13
==================
* fixed API spec for `parseTree`. Can return `undefine` for empty input.
* added new API `FormattingOptions.insertFinalNewline`.


2.3.0 2020-07-03
==================
* new API `ModificationOptions.isArrayInsertion`: If `JSONPath` refers to an index of an array and `isArrayInsertion` is `true`, then `modify` will insert a new item at that location instead of overwriting its contents.
Expand Down
6 changes: 4 additions & 2 deletions src/impl/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export function format(documentText: string, range: Range | undefined, options:
}
let editOperations: Edit[] = [];
function addEdit(text: string, startOffset: number, endOffset: number) {
if (!hasError && startOffset < rangeEnd && endOffset > rangeStart && documentText.substring(startOffset, endOffset) !== text) {
if (!hasError && (!range || (startOffset < rangeEnd && endOffset > rangeStart)) && documentText.substring(startOffset, endOffset) !== text) {
editOperations.push({ offset: startOffset, length: endOffset - startOffset, content: text });
}
}
Expand Down Expand Up @@ -155,7 +155,9 @@ export function format(documentText: string, range: Range | undefined, options:
if (lineBreak && (secondToken === SyntaxKind.LineCommentTrivia || secondToken === SyntaxKind.BlockCommentTrivia)) {
replaceContent = newLineAndIndent();
}

}
if (secondToken === SyntaxKind.EOF) {
replaceContent = options.insertFinalNewline ? eol : '';
}
let secondTokenStart = scanner.getTokenOffset() + formatTextStart;
addEdit(replaceContent, firstTokenEnd, secondTokenStart);
Expand Down
4 changes: 4 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,10 @@ export interface FormattingOptions {
* The default 'end of line' character. If not set, '\n' is used as default.
*/
eol?: string;
/**
* If set, will add a new line at the end of the document.
*/
insertFinalNewline?: boolean;
}

/**
Expand Down
20 changes: 17 additions & 3 deletions src/test/format.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { Range } from '../main';

suite('JSON - formatter', () => {

function format(content: string, expected: string, insertSpaces = true) {
function format(content: string, expected: string, insertSpaces = true, insertFinalNewline = false) {
let range: Range | undefined = void 0;
var rangeStart = content.indexOf('|');
var rangeEnd = content.lastIndexOf('|');
Expand All @@ -19,7 +19,7 @@ suite('JSON - formatter', () => {
range = { offset: rangeStart, length: rangeEnd - rangeStart };
}

var edits = Formatter.format(content, range, { tabSize: 2, insertSpaces: insertSpaces, eol: '\n' });
var edits = Formatter.format(content, range, { tabSize: 2, insertSpaces, insertFinalNewline, eol: '\n' });

let lastEditOffset = content.length;
for (let i = edits.length - 1; i >= 0; i--) {
Expand Down Expand Up @@ -472,7 +472,7 @@ suite('JSON - formatter', () => {
format(content, expected);
});

test('line comment bug 33 ', () => {
test('line comment, enforce line comment ', () => {
var content = [
'{"settings": // This is some text',
'{',
Expand Down Expand Up @@ -504,4 +504,18 @@ suite('JSON - formatter', () => {

format(content, expected);
});

test('insertFinalNewline', () => {
var content = [
'{',
'}'
].join('\n');

var expected = [
'{}',
''
].join('\n');

format(content, expected, undefined, true);
});
});

0 comments on commit f8bd282

Please sign in to comment.