Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/build/lib/formatter.js
3520 views
1
"use strict";
2
var __importDefault = (this && this.__importDefault) || function (mod) {
3
return (mod && mod.__esModule) ? mod : { "default": mod };
4
};
5
Object.defineProperty(exports, "__esModule", { value: true });
6
exports.format = format;
7
/*---------------------------------------------------------------------------------------------
8
* Copyright (c) Microsoft Corporation. All rights reserved.
9
* Licensed under the MIT License. See License.txt in the project root for license information.
10
*--------------------------------------------------------------------------------------------*/
11
const fs_1 = __importDefault(require("fs"));
12
const path_1 = __importDefault(require("path"));
13
const typescript_1 = __importDefault(require("typescript"));
14
class LanguageServiceHost {
15
files = {};
16
addFile(fileName, text) {
17
this.files[fileName] = typescript_1.default.ScriptSnapshot.fromString(text);
18
}
19
fileExists(path) {
20
return !!this.files[path];
21
}
22
readFile(path) {
23
return this.files[path]?.getText(0, this.files[path].getLength());
24
}
25
// for ts.LanguageServiceHost
26
getCompilationSettings = () => typescript_1.default.getDefaultCompilerOptions();
27
getScriptFileNames = () => Object.keys(this.files);
28
getScriptVersion = (_fileName) => '0';
29
getScriptSnapshot = (fileName) => this.files[fileName];
30
getCurrentDirectory = () => process.cwd();
31
getDefaultLibFileName = (options) => typescript_1.default.getDefaultLibFilePath(options);
32
}
33
const defaults = {
34
baseIndentSize: 0,
35
indentSize: 4,
36
tabSize: 4,
37
indentStyle: typescript_1.default.IndentStyle.Smart,
38
newLineCharacter: '\r\n',
39
convertTabsToSpaces: false,
40
insertSpaceAfterCommaDelimiter: true,
41
insertSpaceAfterSemicolonInForStatements: true,
42
insertSpaceBeforeAndAfterBinaryOperators: true,
43
insertSpaceAfterConstructor: false,
44
insertSpaceAfterKeywordsInControlFlowStatements: true,
45
insertSpaceAfterFunctionKeywordForAnonymousFunctions: false,
46
insertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis: false,
47
insertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets: false,
48
insertSpaceAfterOpeningAndBeforeClosingNonemptyBraces: true,
49
insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces: false,
50
insertSpaceAfterOpeningAndBeforeClosingJsxExpressionBraces: false,
51
insertSpaceAfterTypeAssertion: false,
52
insertSpaceBeforeFunctionParenthesis: false,
53
placeOpenBraceOnNewLineForFunctions: false,
54
placeOpenBraceOnNewLineForControlBlocks: false,
55
insertSpaceBeforeTypeAnnotation: false,
56
};
57
const getOverrides = (() => {
58
let value;
59
return () => {
60
value ??= JSON.parse(fs_1.default.readFileSync(path_1.default.join(__dirname, '..', '..', 'tsfmt.json'), 'utf8'));
61
return value;
62
};
63
})();
64
function format(fileName, text) {
65
const host = new LanguageServiceHost();
66
host.addFile(fileName, text);
67
const languageService = typescript_1.default.createLanguageService(host);
68
const edits = languageService.getFormattingEditsForDocument(fileName, { ...defaults, ...getOverrides() });
69
edits
70
.sort((a, b) => a.span.start - b.span.start)
71
.reverse()
72
.forEach(edit => {
73
const head = text.slice(0, edit.span.start);
74
const tail = text.slice(edit.span.start + edit.span.length);
75
text = `${head}${edit.newText}${tail}`;
76
});
77
return text;
78
}
79
//# sourceMappingURL=formatter.js.map
80