Skip to content

Commit 5365837

Browse files
committed
Use workspace indenting by default.
1 parent 1c6d47c commit 5365837

File tree

3 files changed

+48
-30
lines changed

3 files changed

+48
-30
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ If you wish to include the files that are included by default, set `"beautify.on
5555
Embedded version of js-beautify is v1.6.2.
5656

5757
## Changes:
58+
### 0.1.2: 20 Mar 2016
59+
* Beautify with no .jsbeautifyrc file in path tree will use workspace settings for tabs/spaces indent. [Issue #11](https://github.com/HookyQR/VSCodeBeautify/issues/11)<br>Will use the editor setting if the file being beautified is visible, or workspace/user setting if it is not visible. (Beautify of a non-visible file can be envoked when beautify on save is enabled.)
60+
5861
### 0.1.1: 15 Mar 2016
5962
* Allow beautify on save to work with types in `beautify.*Files` settings. [Issue #9](https://github.com/HookyQR/VSCodeBeautify/issues/9)
6063
* Fix `beautify.*Files` settings requiring a `.` before the extension (both styles are now accepted).

extension.js

Lines changed: 36 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const dumpError = e => {
99
if (e) console.log('beautify err:', e);
1010
return [];
1111
};
12+
1213
const dropComments = inText => inText.replace(/(\/\*.*\*\/)|\/\/.*(?:[\r\n]|$)/g, "");
1314

1415
const mergeOpts = function(opts, type) {
@@ -73,54 +74,53 @@ const getBeautifyType = function(doc, dontAsk) {
7374
return vscode.window.showQuickPick([{
7475
label: "JS",
7576
description: "Does JavaScript and JSON"
76-
}, {
77+
}, {
7778
label: "CSS"
78-
}, {
79+
}, {
7980
label: "HTML"
80-
}], {
81+
}], {
8182
matchOnDescription: true,
8283
placeHolder: "Couldn't determine type to beautify, please choose."
8384
})
8485
.then(function(choice) {
8586
if (!choice || !choice.label) return reject('no beautify type selected');
8687
return resolve(choice.label.toLowerCase());
87-
});
88+
}, () => 0);
8889
});
8990
};
9091

91-
function getConfigFor(doc, type) {
92+
function getConfigFor(doc, defaultOptions, type) {
9293

9394
let base = vscode.workspace.rootPath;
9495

9596
if (!doc.isUntitled) base = path.dirname(doc.fileName);
96-
if (!base) return Promise.resolve({});
97+
if (!base) return Promise.resolve(defaultOptions);
9798
let configFile = findRecursive(base, '.jsbeautifyrc');
98-
if (!configFile) return Promise.resolve({});
99+
if (!configFile) return Promise.resolve(defaultOptions);
99100
return new Promise(resolve => {
100101
fs.readFile(configFile, 'utf8', (e, d) => {
101-
if (!d) d = '{}';
102-
let opts = {};
102+
let opts = defaultOptions;
103+
if (!d) return resolve(opts);
103104
try {
104105
const unCommented = dropComments(d.toString());
105106
opts = JSON.parse(unCommented);
106107
opts = mergeOpts(opts, type);
107108
} catch (e) {
108109
vscode.window.showWarningMessage(`Found a .jsbeautifyrc file [${configFile}], but it didn't parse correctly.`);
109-
opts = {}; //just use the default opts
110110
}
111111
resolve(opts);
112112
});
113113
});
114114
}
115115

116-
function beautifyDoc(doc, range, type) {
116+
function beautifyDoc(doc, range, defaultOptions, type) {
117117
if (!doc) {
118118
vscode.window.showInformationMessage(
119119
"Beautify can't get the file information because the editor won't supply it. (File probably too large)");
120120
throw "";
121121
}
122-
return (Promise.resolve(type ? type : getBeautifyType(doc)))
123-
.then(type => getConfigFor(doc, type)
122+
return Promise.resolve(type ? type : getBeautifyType(doc))
123+
.then(type => getConfigFor(doc, defaultOptions, type)
124124
.then(config => {
125125
const original = doc.getText(doc.validateRange(range));
126126
return beautify[type](original, config);
@@ -136,12 +136,19 @@ function extendRange(doc, rng) {
136136
return r;
137137
}
138138

139+
function optionsFromFormat(formattingOptions) {
140+
return {
141+
indent_with_tabs: !formattingOptions.insertSpaces,
142+
indent_size: formattingOptions.tabSize,
143+
indent_char: ' '
144+
};
145+
}
146+
139147
function rangeEditByType(type) {
140-
return (doc, rng) => {
148+
return (doc, rng, formattingOptions) => {
141149
rng = extendRange(doc, rng);
142-
return beautifyDoc(doc, rng, type)
143-
.then(newText => documentEdit(rng, newText))
144-
.catch(dumpError);
150+
return beautifyDoc(doc, rng, optionsFromFormat(formattingOptions), type)
151+
.then(newText => documentEdit(rng, newText), dumpError);
145152
};
146153
}
147154

@@ -175,7 +182,15 @@ function beautifyOnSave(doc) {
175182
Array.isArray(cfg.onSave) && cfg.onSave.indexOf(refType) >= 0
176183
)) {
177184
const range = new vscode.Range(0, 0, Number.MAX_VALUE, Number.MAX_VALUE);
178-
beautifyDoc(doc, range)
185+
//determine a default options
186+
let defaultOptions = optionsFromFormat(vscode.workspace.getConfiguration('editor'));
187+
//if this document is open, use the settings from that window
188+
vscode.window.visibleTextEditors.some(editor => {
189+
if (editor.document && editor.document.fileName === doc.fileName) {
190+
return (defaultOptions = optionsFromFormat(editor.options));
191+
}
192+
});
193+
beautifyDoc(doc, range, defaultOptions, refType)
179194
.then(newText => {
180195
let we = new vscode.WorkspaceEdit();
181196
we.replace(doc.uri, range, newText);
@@ -194,9 +209,9 @@ function activate(context) {
194209
if (!active) return;
195210
if (!active.document) return;
196211
const range = new vscode.Range(0, 0, Number.MAX_VALUE, Number.MAX_VALUE);
197-
beautifyDoc(active.document, range)
198-
.then(newText => active.edit(editor => editor.replace(range, newText)))
199-
.catch(dumpError);
212+
213+
beautifyDoc(active.document, range, optionsFromFormat(active.options))
214+
.then(newText => active.edit(editor => editor.replace(range, newText)), dumpError);
200215
}));
201216

202217
//VS Code won't allow the formatters to run for json, or js. The inbuild

package.json

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,18 @@
22
"name": "beautify",
33
"displayName": "beautify",
44
"description": "Beautify code in place for VS Code",
5-
"version": "0.1.1",
5+
"version": "0.1.2",
66
"publisher": "HookyQR",
77
"engines": {
88
"vscode": "^0.10.1"
99
},
1010
"categories": [
11-
"Other",
12-
"Languages"
13-
],
11+
"Other",
12+
"Languages"
13+
],
1414
"activationEvents": [
15-
"*"
16-
],
15+
"*"
16+
],
1717
"icon": "icon.svg",
1818
"galleryBanner": {
1919
"color": "#e8e030",
@@ -26,15 +26,15 @@
2626
"id": "json",
2727
"aliases": ["JSON"],
2828
"filenames": [".jsbeautifyrc"]
29-
}],
29+
}],
3030
"jsonValidation": [{
3131
"fileMatch": ".jsbeautifyrc",
3232
"url": "./schema/beautifyrc.json"
33-
}],
33+
}],
3434
"commands": [{
3535
"command": "HookyQR.beautify",
3636
"title": "Beautify"
37-
}],
37+
}],
3838
"configuration": {
3939
"type": "object",
4040
"title": "Beautify config",

0 commit comments

Comments
 (0)