Skip to content

Commit 1c6d47c

Browse files
committed
Fix #9. Fix file extensions in type settings
1 parent 7cde2f3 commit 1c6d47c

File tree

3 files changed

+45
-49
lines changed

3 files changed

+45
-49
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ 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.1: 15 Mar 2016
59+
* Allow beautify on save to work with types in `beautify.*Files` settings. [Issue #9](https://github.com/HookyQR/VSCodeBeautify/issues/9)
60+
* Fix `beautify.*Files` settings requiring a `.` before the extension (both styles are now accepted).
61+
5862
### 0.1.0: 13 Mar 2016
5963
* Add beautify on save option. [Issue #5: Add Beautify on Save](https://github.com/HookyQR/VSCodeBeautify/issues/5)
6064
* Added `css`, and `html` beautifiers to the system range formatters. This means that beautify will run as the system `Format code` option.

extension.js

Lines changed: 38 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -42,55 +42,49 @@ const extMatch = n => ({
4242
pattern: n.startsWith("**/") ? n : ("**/" + n)
4343
});
4444

45-
const getBeautifyType = function(doc) {
45+
const getBeautifyType = function(doc, dontAsk) {
4646
if (doc.languageId === 'javascript') return 'js';
4747
if (doc.languageId === 'json') return 'js';
4848
if (doc.languageId === 'html') return 'html';
4949
if (doc.languageId === 'css') return 'css';
5050

51+
const type = doc.isUntitled ? "" : path.extname(doc.fileName)
52+
.toLowerCase();
53+
const cfg = vscode.workspace.getConfiguration('beautify');
54+
//if a type is set on the window, use that
55+
//check if the file is in the users json schema set
56+
const jsSchema = vscode.workspace.getConfiguration('json')
57+
.schemas;
58+
if (jsSchema.length) {
59+
let matcher = [];
60+
jsSchema.forEach(schema => {
61+
if (typeof schema.fileMatch === 'string') matcher.push(extMatch(schema.fileMatch));
62+
else matcher = matcher.concat(schema.fileMatch.map(extMatch));
63+
});
64+
if (vscode.languages.match(matcher, doc)) return "js";
65+
}
66+
if (cfg.HTMLfiles.indexOf(type) + 1 || cfg.HTMLfiles.indexOf(type.slice(1)) + 1) return 'html';
67+
else if (cfg.CSSfiles.indexOf(type) + 1 || cfg.CSSfiles.indexOf(type.slice(1)) + 1) return 'css';
68+
else if (cfg.JSfiles.indexOf(type) + 1 || cfg.JSfiles.indexOf(type.slice(1)) + 1) return 'js';
69+
if (dontAsk) return;
70+
5171
return new Promise((resolve, reject) => {
52-
const type = doc.isUntitled ? "" : path.extname(doc.fileName)
53-
.toLowerCase();
54-
const cfg = vscode.workspace.getConfiguration('beautify');
55-
//if a type is set on the window, use that
56-
//check if the file is in the users json schema set
57-
const jsSchema = vscode.workspace.getConfiguration('json')
58-
.schemas;
59-
if (jsSchema.length) {
60-
let matcher = [];
61-
jsSchema.forEach(schema => {
62-
if (typeof schema.fileMatch === 'string') matcher.push(extMatch(schema.fileMatch));
63-
else matcher = matcher.concat(schema.fileMatch.map(extMatch));
64-
});
65-
if (vscode.languages.match(matcher, doc)) return resolve("js");
66-
}
67-
if (cfg.HTMLfiles.indexOf(type) + 1) {
68-
//showDepWarning();
69-
return resolve("html");
70-
} else if (cfg.CSSfiles.indexOf(type) + 1) {
71-
//showDepWarning();
72-
return resolve("css");
73-
} else if (cfg.JSfiles.indexOf(type) + 1) {
74-
//showDepWarning();
75-
return resolve("js");
76-
} else {
77-
//Ask what they want to do:
78-
return vscode.window.showQuickPick([{
79-
label: "JS",
80-
description: "Does JavaScript and JSON"
72+
//Ask what they want to do:
73+
return vscode.window.showQuickPick([{
74+
label: "JS",
75+
description: "Does JavaScript and JSON"
8176
}, {
82-
label: "CSS"
77+
label: "CSS"
8378
}, {
84-
label: "HTML"
79+
label: "HTML"
8580
}], {
86-
matchOnDescription: true,
87-
placeHolder: "Couldn't determine type to beautify, please choose."
88-
})
89-
.then(function(choice) {
90-
if (!choice || !choice.label) return reject('no beautify type selected');
91-
return resolve(choice.label.toLowerCase());
92-
});
93-
}
81+
matchOnDescription: true,
82+
placeHolder: "Couldn't determine type to beautify, please choose."
83+
})
84+
.then(function(choice) {
85+
if (!choice || !choice.label) return reject('no beautify type selected');
86+
return resolve(choice.label.toLowerCase());
87+
});
9488
});
9589
};
9690

@@ -173,8 +167,10 @@ function beautifyOnSave(doc) {
173167
let refType = doc.languageId;
174168

175169
if (refType === 'javascript') refType = 'js';
176-
if (['json', 'js', 'html', 'css'].indexOf(refType) === -1) return;
177-
170+
if (['json', 'js', 'html', 'css'].indexOf(refType) === -1) {
171+
refType = getBeautifyType(doc, true);
172+
if (!refType) return;
173+
}
178174
if (cfg.onSave === true || (
179175
Array.isArray(cfg.onSave) && cfg.onSave.indexOf(refType) >= 0
180176
)) {

package.json

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "beautify",
33
"displayName": "beautify",
44
"description": "Beautify code in place for VS Code",
5-
"version": "0.1.0",
5+
"version": "0.1.1",
66
"publisher": "HookyQR",
77
"engines": {
88
"vscode": "^0.10.1"
@@ -12,11 +12,7 @@
1212
"Languages"
1313
],
1414
"activationEvents": [
15-
"onCommand:HookyQR.beautify",
16-
"onLanguage:javascript",
17-
"onLanguage:json",
18-
"onLanguage:html",
19-
"onLanguage:css"
15+
"*"
2016
],
2117
"icon": "icon.svg",
2218
"galleryBanner": {
@@ -78,7 +74,7 @@
7874
},
7975
"dependencies": {
8076
"js-beautify": "^1.6.2",
81-
"minimatch": "^3.0.0"
77+
"minimatch": "^3.0.0"
8278
},
8379
"repository": {
8480
"type": "git",

0 commit comments

Comments
 (0)