Skip to content

Commit

Permalink
Add comment strip
Browse files Browse the repository at this point in the history
  • Loading branch information
HookyQR committed Dec 23, 2015
1 parent 99d35a3 commit 7dbd61c
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 18 deletions.
23 changes: 20 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,34 @@

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

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`.
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`.

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.
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:

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.
```json
"json.schemas": [
{
"fileMatch": ["**/.jsbeautifyrc"],
"url": "http://json.schemastore.org/jsbeautifyrc"
}
]
```

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.

If the file is unsaved, or the type is undetermined, you'll be prompted for which beautifier to use.

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


Embedded version of js-beautify is v1.5.10.

## Changes:
### 0.0.5: 24 Dec 2015
* Schema published at http://json.schemastore.org/jsbeautifyrc.
* Added README details for schema install for users of VSCode < v0.10.5
* Added comments remover before JSON parse. Fixes [Issue #2: .jsbeautifyrc file not being used](https://github.com/HookyQR/VSCodeBeautify/issues/2)

### 0.0.4: 19 Dec 2015
* Changed default (unknown) processing to ask you what you want to use.
* Fixed [Issue #1: No handler found for the command: 'HookyQR.beautify'](https://github.com/HookyQR/VSCodeBeautify/issues/1)
Expand Down
46 changes: 33 additions & 13 deletions extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,29 @@ function findRecursive(dir, fileName) {
}
return result;
}

var dropWithRegEx = function(text, re) {
if (!re.global) //I'm not doing that for ever
return text;
var oText = "";
var match = re.exec(text);
var lastEnd = 0;
while (match) {
if (lastEnd < match.index)
oText += text.slice(lastEnd, match.index);
lastEnd = match.index + match[0].length;
match = re.exec(text);
}
if (lastEnd < text.length) oText += text.slice(lastEnd, text.length);
return oText;
}
var dropMultiLineComments = inText => dropWithRegEx(inText, /\/\*.*\*\//g);
var dropSingleLineComments = inText => dropWithRegEx(inText, /\/\/.*(?:[\r\n]|$)/g);
var dropComments = inText => dropSingleLineComments(dropMultiLineComments(inText));

//register on activation
function activate(context) {

var doBeautify = function(active, doc, opts) {
var original = doc.getText();
var type = doc.isUntitled ? "" : doc.fileName.split('.')
Expand Down Expand Up @@ -52,13 +72,10 @@ function activate(context) {
return;
}
}
if (cfg.HTMLfiles.indexOf(type) + 1) {
result = beautify.html(original, opts);
} else if (cfg.CSSfiles.indexOf(type) + 1) {
result = beautify.css(original, opts);
} else if (cfg.JSfiles.indexOf(type) + 1) {
result = beautify.js(original, opts);
} else {
if (cfg.HTMLfiles.indexOf(type) + 1) result = beautify.html(original, opts);
else if (cfg.CSSfiles.indexOf(type) + 1) result = beautify.css(original, opts);
else if (cfg.JSfiles.indexOf(type) + 1) result = beautify.js(original, opts);
else {
//Ask what they want to do:
vscode.window.showQuickPick([{
label: "JS",
Expand All @@ -73,16 +90,16 @@ function activate(context) {
})
.then(function(choice) {
if (!choice || !choice.label) return;
result=beautify[choice.label.toLowerCase()](original, opts);

result = beautify[choice.label.toLowerCase()](original, opts);
active.edit(editor => editor.replace(range, result));
});
return;
}
//and make the change:
active.edit(editor => editor.replace(range, result));
};

//it's ok to build and pass the re from outside of here, we always run
//to completion.
var disposable = vscode.commands.registerCommand('HookyQR.beautify', function() {
var active = vscode.window.activeTextEditor;
if (!active) return;
Expand All @@ -97,11 +114,14 @@ function activate(context) {

//walk to find a .jsbeautifyrc
if (beautFile) fs.readFile(beautFile, function(ee, d) {
if (ee && !d) d = "{}";
if (!d) d = "{}";
var opts = {};
try {
opts = JSON.parse(d.toString());
var unCommented = dropComments(d.toString());
opts = JSON.parse(unCommented);
} catch (e) {
//put a warning in here
vscode.window.showWarningMessage("Found a .jsbeautifyrc file, but it didn't parse correctly.");
opts = {}; //just use the default opts
}
doBeautify(active, doc, opts);
Expand Down
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "beautify",
"displayName": "beautify",
"description": "Beautify code in place for VS Code",
"version": "0.0.4",
"version": "0.0.5",
"publisher": "HookyQR",
"engines": {
"vscode": "^0.10.1"
Expand Down Expand Up @@ -60,5 +60,8 @@
"repository": {
"type": "git",
"url": "https://github.com/HookyQR/VSCodeBeautify"
}
},
"bugs":{
"url":"https://github.com/HookyQR/VSCodeBeautify/issues"
}
}

0 comments on commit 7dbd61c

Please sign in to comment.