-
Notifications
You must be signed in to change notification settings - Fork 180
/
options.js
175 lines (161 loc) · 6.03 KB
/
options.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
'use strict';
const vscode = require('vscode'),
path = require('path'),
os = require('os'),
fs = require('fs'),
editorconfig = require('editorconfig');
const dropComments = inText => inText
.replace(/\/\*.*\*\//g, '')
.replace(/("(?:[^\\"\r\n]|\\")*?")|(?:\/\/.*(?=[\r\n]|$))/g, (_, str) => str || '');
const mergeOpts = (opts, kind) => {
const finOpts = {};
for (let a in opts) {
if (a !== 'js' && a !== 'html' && a !== 'css') {
finOpts[a] = opts[a];
}
}
//merge in the per type settings
if (kind in opts) {
for (let b in opts[kind]) {
if (b === 'allowed_file_extensions') continue;
finOpts[b] = opts[kind][b];
}
}
return finOpts;
};
const findRecursive = (dir, fileName, root) => {
const fullPath = path.join(dir, fileName);
const nextDir = path.dirname(dir);
let result = fs.existsSync(fullPath) ? fullPath : null;
if (!result && nextDir !== dir && dir !== root) {
result = findRecursive(nextDir, fileName, root);
}
return result;
};
const optionsFromVSCode = (doc, formattingOptions, type) => {
const config = vscode.workspace.getConfiguration();
if (!formattingOptions) {
formattingOptions = vscode.workspace.getConfiguration('editor');
//if this document is open, use the settings from that window
vscode.window.visibleTextEditors.some(editor => {
if (editor.document && editor.document.fileName === doc.fileName) {
return (formattingOptions = editor.options);
}
});
}
const options = {
indent_with_tabs: formattingOptions.insertSpaces === undefined ? true : !formattingOptions.insertSpaces,
indent_size: formattingOptions.tabSize,
indent_char: ' ',
end_with_newline: config.files.insertFinalNewLine,
eol: config.files.eol,
space_after_anon_function: config.javascript.format.insertSpaceAfterFunctionKeywordForAnonymousFunctions,
space_in_paren: config.javascript.format.insertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis
};
if (type === 'html') {
options.end_with_newline = config.html.format.endWithNewline;
if (typeof config.html.format.extraLiners === 'string') {
options.extra_liners = config.html.format.extraLiners
.split(',')
.map(s => s.trim());
}
options.indent_handlebars = config.html.format.indentHandlebars;
options.indent_inner_html = config.html.format.indentInnerHtml;
options.max_preserve_newlines = config.html.format.maxPreserveNewLines;
options.preserve_newlines = config.html.format.preserveNewLines;
options.wrap_attributes = config.html.format.wrapAttributes;
if (typeof config.html.format.unformatted === 'string') {
options.unformatted = config.html.format.unformatted
.split(',')
.map(s => s.trim());
}
options.wrap_line_length = config.html.format.wrapLineLength;
}
return options;
};
/* set_file_editorconfig_opts directly from js-beautify/lib/cli.js */
function set_file_editorconfig_opts(file, config) {
try {
const eConfigs = editorconfig.parseSync(file);
if (eConfigs.indent_style === 'tab') {
config.indent_with_tabs = true;
config.indent_char = '\t';
} else if (eConfigs.indent_style === 'space') {
config.indent_with_tabs = false;
config.indent_char = ' ';
}
if (eConfigs.indent_size && eConfigs.indent_size !== 'tab') {
config.indent_size = eConfigs.indent_size;
}
if (eConfigs.max_line_length) {
if (eConfigs.max_line_length === 'off') {
config.wrap_line_length = 0;
} else {
config.wrap_line_length = parseInt(eConfigs.max_line_length);
}
}
if (eConfigs.insert_final_newline === true) {
config.end_with_newline = true;
} else if (eConfigs.insert_final_newline === false) {
config.end_with_newline = false;
}
if (eConfigs.end_of_line) {
if (eConfigs.end_of_line === 'cr') {
config.eol = '\r';
} else if (eConfigs.end_of_line === 'lf') {
config.eol = '\n';
} else if (eConfigs.end_of_line === 'crlf') {
config.eol = '\r\n';
}
}
} catch (e) {}
}
const getWorkspaceRoot = doc => {
if (!vscode.workspace.workspaceFolders || vscode.workspace.workspaceFolders.length === 0) return;
if (!doc || doc.isUntitled) return vscode.workspace.workspaceFolders[0].uri.fsPath;
const folder = vscode.workspace.getWorkspaceFolder(doc.uri);
if (!folder) return;
return folder.uri.fsPath;
};
module.exports = (doc, type, formattingOptions) => {
let root = getWorkspaceRoot(doc) || vscode.workspace.rootPath;
let dir = doc.isUntitled ? root : path.dirname(doc.fileName);
let opts = optionsFromVSCode(doc, formattingOptions, type);
set_file_editorconfig_opts(doc.fileName, opts); // this does nothing if no ec file was found
let configFile = dir ? findRecursive(dir, '.jsbeautifyrc', root) : null;
if (!configFile) {
let beautify_config = vscode.workspace.getConfiguration('beautify')
.config;
if (beautify_config) {
if (typeof beautify_config === 'object') {
return Promise.resolve(mergeOpts(beautify_config, type));
} else if (typeof beautify_config === 'string') {
if (path.isAbsolute(beautify_config)) configFile = beautify_config;
else configFile = path.resolve(root, beautify_config);
configFile = fs.existsSync(configFile) ? configFile : null;
}
}
}
if (!configFile && root) {
configFile = findRecursive(path.dirname(root), '.jsbeautifyrc');
}
if (!configFile) {
configFile = path.join(os.homedir(), '.jsbeautifyrc');
if (!fs.existsSync(configFile)) return Promise.resolve(opts);
}
return new Promise((resolve, reject) => {
fs.readFile(configFile, 'utf8', (e, d) => {
if (!d || !d.length) return resolve(opts);
try {
const unCommented = dropComments(d.toString());
opts = JSON.parse(unCommented);
opts = mergeOpts(opts, type);
resolve(opts);
} catch (e) {
vscode.window.showWarningMessage(
`Found a .jsbeautifyrc file [${configFile}], but it didn't parse correctly.`);
reject();
}
});
});
};