Skip to content

Commit 7dbd61c

Browse files
committed
Add comment strip
1 parent 99d35a3 commit 7dbd61c

File tree

3 files changed

+58
-18
lines changed

3 files changed

+58
-18
lines changed

README.md

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,34 @@
22

33
[![Build Status](https://api.travis-ci.org/HookyQR/VSCodeBeautify.svg?branch=master)](https://travis-ci.org/HookyQR/VSCodeBeautify)
44

5-
VS Code uses js-beautify internally, bit it lacks the ability to modify the style you wish to use. This extension enables running [js-beautify](http://jsbeautifier.org/) in VS Code, _AND_ honouring any `.jsbeautifyrc` file in the open file's path tree to load *your* code styling. Run with **⌘⇧P** `Beautify`.
5+
VS Code uses js-beautify internally, bit it lacks the ability to modify the style you wish to use. This extension enables running [js-beautify](http://jsbeautifier.org/) in VS Code, _AND_ honouring any `.jsbeautifyrc` file in the open file's path tree to load *your* code styling. Run with **F1** `Beautify`.
66

7-
This package now includes hints when editing your `.jsbeautifyrc`. Only the first file found will be used. If the format is bad, the default js-beautify settings will be used.
7+
This package includes hints when editing your `.jsbeautifyrc`. Only the first file found will be used. If the format is bad, the default js-beautify settings will be used, but a warning will be issued to let you know. Comments in your settings file are acceptable (they're removed before the file is parsed). The embedded schema for `.jsbeautifyrc` has also been published at [JSON Schema Store](http://schemastore.org) which allows users of VSCode 0.10.3 to add it manually to their user or workspace settings:
88

9-
Also runs http and css beautify from the same package, as determined by the file extension. If the file is unsaved, or the type is undetermined, you'll be prompted for which beautifier to use.
9+
```json
10+
"json.schemas": [
11+
{
12+
"fileMatch": ["**/.jsbeautifyrc"],
13+
"url": "http://json.schemastore.org/jsbeautifyrc"
14+
}
15+
]
16+
```
17+
18+
Also runs http and css beautify from the same package, as determined by the file extension. The schema indicates which beautifier each of the settings pertains to.
19+
20+
If the file is unsaved, or the type is undetermined, you'll be prompted for which beautifier to use.
1021

1122
Extra (permanent) file extension may be added under user or workspace settings.
1223

24+
1325
Embedded version of js-beautify is v1.5.10.
1426

1527
## Changes:
28+
### 0.0.5: 24 Dec 2015
29+
* Schema published at http://json.schemastore.org/jsbeautifyrc.
30+
* Added README details for schema install for users of VSCode < v0.10.5
31+
* Added comments remover before JSON parse. Fixes [Issue #2: .jsbeautifyrc file not being used](https://github.com/HookyQR/VSCodeBeautify/issues/2)
32+
1633
### 0.0.4: 19 Dec 2015
1734
* Changed default (unknown) processing to ask you what you want to use.
1835
* Fixed [Issue #1: No handler found for the command: 'HookyQR.beautify'](https://github.com/HookyQR/VSCodeBeautify/issues/1)

extension.js

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,29 @@ function findRecursive(dir, fileName) {
1313
}
1414
return result;
1515
}
16+
17+
var dropWithRegEx = function(text, re) {
18+
if (!re.global) //I'm not doing that for ever
19+
return text;
20+
var oText = "";
21+
var match = re.exec(text);
22+
var lastEnd = 0;
23+
while (match) {
24+
if (lastEnd < match.index)
25+
oText += text.slice(lastEnd, match.index);
26+
lastEnd = match.index + match[0].length;
27+
match = re.exec(text);
28+
}
29+
if (lastEnd < text.length) oText += text.slice(lastEnd, text.length);
30+
return oText;
31+
}
32+
var dropMultiLineComments = inText => dropWithRegEx(inText, /\/\*.*\*\//g);
33+
var dropSingleLineComments = inText => dropWithRegEx(inText, /\/\/.*(?:[\r\n]|$)/g);
34+
var dropComments = inText => dropSingleLineComments(dropMultiLineComments(inText));
35+
1636
//register on activation
1737
function activate(context) {
18-
38+
1939
var doBeautify = function(active, doc, opts) {
2040
var original = doc.getText();
2141
var type = doc.isUntitled ? "" : doc.fileName.split('.')
@@ -52,13 +72,10 @@ function activate(context) {
5272
return;
5373
}
5474
}
55-
if (cfg.HTMLfiles.indexOf(type) + 1) {
56-
result = beautify.html(original, opts);
57-
} else if (cfg.CSSfiles.indexOf(type) + 1) {
58-
result = beautify.css(original, opts);
59-
} else if (cfg.JSfiles.indexOf(type) + 1) {
60-
result = beautify.js(original, opts);
61-
} else {
75+
if (cfg.HTMLfiles.indexOf(type) + 1) result = beautify.html(original, opts);
76+
else if (cfg.CSSfiles.indexOf(type) + 1) result = beautify.css(original, opts);
77+
else if (cfg.JSfiles.indexOf(type) + 1) result = beautify.js(original, opts);
78+
else {
6279
//Ask what they want to do:
6380
vscode.window.showQuickPick([{
6481
label: "JS",
@@ -73,16 +90,16 @@ function activate(context) {
7390
})
7491
.then(function(choice) {
7592
if (!choice || !choice.label) return;
76-
result=beautify[choice.label.toLowerCase()](original, opts);
77-
93+
result = beautify[choice.label.toLowerCase()](original, opts);
7894
active.edit(editor => editor.replace(range, result));
7995
});
8096
return;
8197
}
8298
//and make the change:
8399
active.edit(editor => editor.replace(range, result));
84100
};
85-
101+
//it's ok to build and pass the re from outside of here, we always run
102+
//to completion.
86103
var disposable = vscode.commands.registerCommand('HookyQR.beautify', function() {
87104
var active = vscode.window.activeTextEditor;
88105
if (!active) return;
@@ -97,11 +114,14 @@ function activate(context) {
97114

98115
//walk to find a .jsbeautifyrc
99116
if (beautFile) fs.readFile(beautFile, function(ee, d) {
100-
if (ee && !d) d = "{}";
117+
if (!d) d = "{}";
101118
var opts = {};
102119
try {
103-
opts = JSON.parse(d.toString());
120+
var unCommented = dropComments(d.toString());
121+
opts = JSON.parse(unCommented);
104122
} catch (e) {
123+
//put a warning in here
124+
vscode.window.showWarningMessage("Found a .jsbeautifyrc file, but it didn't parse correctly.");
105125
opts = {}; //just use the default opts
106126
}
107127
doBeautify(active, doc, opts);

package.json

Lines changed: 5 additions & 2 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.0.4",
5+
"version": "0.0.5",
66
"publisher": "HookyQR",
77
"engines": {
88
"vscode": "^0.10.1"
@@ -60,5 +60,8 @@
6060
"repository": {
6161
"type": "git",
6262
"url": "https://github.com/HookyQR/VSCodeBeautify"
63-
}
63+
},
64+
"bugs":{
65+
"url":"https://github.com/HookyQR/VSCodeBeautify/issues"
66+
}
6467
}

0 commit comments

Comments
 (0)