Skip to content

Commit 79e3bc8

Browse files
committed
test: Fix ci newlines
1 parent bd0b9cd commit 79e3bc8

2 files changed

Lines changed: 14 additions & 10 deletions

File tree

src/Providers/DocumentFormattingEditProvider.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,16 @@ type formattingStyle = {
77
tabType: ' ' | '\t';
88
tabSize: number;
99
wsBrackets: string;
10+
eol: '\r\n' | '\n';
1011
};
1112

12-
function getFormattingStyle(options?: vscode.FormattingOptions): formattingStyle {
13+
function getFormattingStyle(options?: vscode.FormattingOptions, document?: vscode.TextDocument): formattingStyle {
1314
const bracketStyle = <'tight' | 'default'>vscode.workspace.getConfiguration('json.textmate').get('formattingStyle');
1415
const style: formattingStyle = {
1516
tabType: options?.insertSpaces ? ' ' : '\t',
1617
tabSize: options?.insertSpaces ? options?.tabSize : 1,
1718
wsBrackets: bracketStyle == 'tight' ? '' : ' ',
19+
eol: document?.eol == vscode.EndOfLine.CRLF ? '\r\n' : '\n',
1820
};
1921
return style;
2022
}
@@ -27,7 +29,7 @@ export const DocumentFormattingEditProvider: vscode.DocumentFormattingEditProvid
2729
const jsonTree = trees.jsonTree;
2830
const textEdits: vscode.TextEdit[] = [];
2931

30-
const style = getFormattingStyle(options);
32+
const style = getFormattingStyle(options, document);
3133

3234
// const start = performance.now();
3335
formatChildren(jsonTree.rootNode, textEdits, 0, style);
@@ -45,7 +47,7 @@ export const DocumentRangeFormattingEditProvider: vscode.DocumentRangeFormatting
4547
const jsonTree = trees.jsonTree;
4648
const textEdits: vscode.TextEdit[] = [];
4749

48-
const style = getFormattingStyle(options);
50+
const style = getFormattingStyle(options, document);
4951

5052
const startPoint = toPoint(range.start);
5153
const endPoint = toPoint(range.end);
@@ -114,7 +116,7 @@ export const OnTypeFormattingEditProvider: vscode.OnTypeFormattingEditProvider =
114116
return;
115117
}
116118

117-
const style = getFormattingStyle(options);
119+
const style = getFormattingStyle(options, document);
118120

119121
let level = 0;
120122
let parent = node.parent;
@@ -224,7 +226,7 @@ function formatChildren(parentNode: webTreeSitter.Node, textEdits: vscode.TextEd
224226
nextSibling?.startPosition ?? parentNode.endPosition,
225227
),
226228
expand ?
227-
node.type + '\n'.padEnd(indent + 1, style.tabType) :
229+
node.type + style.eol + ''.padEnd(indent, style.tabType) :
228230
node.type + style.wsBrackets,
229231
),
230232
);
@@ -241,7 +243,7 @@ function formatChildren(parentNode: webTreeSitter.Node, textEdits: vscode.TextEd
241243
node.endPosition,
242244
),
243245
expand ?
244-
'\n'.padEnd(indent + 1, style.tabType) + node.type :
246+
style.eol + ''.padEnd(indent, style.tabType) + node.type :
245247
style.wsBrackets + node.type,
246248
),
247249
);
@@ -264,7 +266,7 @@ function formatChildren(parentNode: webTreeSitter.Node, textEdits: vscode.TextEd
264266
nextSibling?.startPosition ?? parentNode.endPosition,
265267
),
266268
expand ?
267-
',\n'.padEnd(indent + 2, style.tabType) :
269+
',' + style.eol + ''.padEnd(indent, style.tabType) :
268270
', ',
269271
),
270272
);

src/test/suite/extension.test.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ suite('Extension Tests', async () => {
8484
}
8585
},
8686
'\t'
87-
).replaceAll(/\r?\n/g, '\r\n') + '\r\n';
87+
).replaceAll(/[\r\n]+/g, '\r\n') + '\r\n';
8888

8989
const file = vscode.Uri.joinPath(baselinesUri, filename);
9090

@@ -104,7 +104,7 @@ suite('Extension Tests', async () => {
104104
// VSCode's IRange is presented differently compared to how its actually stored
105105
assert.equal(
106106
actualStringified,
107-
JSON.stringify(expected, null, '\t').replaceAll(/\r?\n/g, '\r\n') + '\r\n',
107+
JSON.stringify(expected, null, '\t').replaceAll(/[\r\n]+/g, '\r\n') + '\r\n',
108108
);
109109
assert.deepEqual(
110110
JSON.parse(actualStringified),
@@ -332,6 +332,7 @@ suite('Extension Tests', async () => {
332332
editBuilder => {
333333
const minifiedText = editorFormatted.document.getText().replaceAll(/\s*[\r\n]+\s*/gm, '');
334334
editBuilder.replace(new vscode.Range(0, 0, editorFormatted.document.lineCount, 0), minifiedText + '\r\n');
335+
editBuilder.setEndOfLine(vscode.EndOfLine.CRLF);
335336
}
336337
);
337338
// TODO: TreeSitter broken. can't handle partially minified JSON
@@ -418,7 +419,7 @@ suite('Extension Tests', async () => {
418419
const editor = await vscode.window.showTextDocument(uri, showTextDocumentOptions);
419420

420421
type CodeAction = vscode.CodeAction & {
421-
cacheId?: number[];
422+
cacheId: number[];
422423
isAI: boolean;
423424
edit?: vscode.WorkspaceEdit & {
424425
edits: {
@@ -446,6 +447,7 @@ suite('Extension Tests', async () => {
446447
test('DocumentSymbolProvider', async () => {
447448
const uri = vscode.Uri.joinPath(fixturesUri, 'JSON.tmLanguage.json');
448449
const editor = await vscode.window.showTextDocument(uri, showTextDocumentOptions);
450+
await editor.edit(editBuilder => editBuilder.setEndOfLine(vscode.EndOfLine.CRLF));
449451

450452
const documentSymbolsActual = await vscode.commands.executeCommand(
451453
'_executeDocumentSymbolProvider', uri

0 commit comments

Comments
 (0)