Skip to content

Commit

Permalink
Fix issue #1 + add query on unknown type.
Browse files Browse the repository at this point in the history
  • Loading branch information
HookyQR committed Dec 19, 2015
1 parent f040b1f commit 99d35a3
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 25 deletions.
17 changes: 10 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,25 @@

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

VS Code has its own code formater. But 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 searches for `.jsbeautifyrc` file in the files 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 **⌘⇧P** `Beautify`.

See [js-beautify on gitHub](https://github.com/beautify-web/js-beautify) for available options in the rc file. The file must be valid JSON to be used. Only the first file of the correct name found will be used. If the format is bad, the default js-beautify settings will be used.
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.

Also runs http and css beautify from the same package, as determined by the file extension. If the file is unsaved, js-beautify will be attempted by default.
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.

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

Embeded version of js-beautify is v1.5.10.
Embedded version of js-beautify is v1.5.10.

## Changes:
### 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)

### 0.0.3: 19 Dec 2015
* _Tries_ to mark any elements in `json.schema` settings as JSON, and thus beautify as JS.
* Added schema for `.jsbeautifyrc` file.
* Added schema for `.jsbeautifyrc` file. (Requires VS Code v0.10.5+ see [Issue #1](https://github.com/HookyQR/VSCodeBeautify/issues/1))
* Added language type so `.jsbeautifyrc` is recognised as JSON.

### 0.0.2: 17 Dec 2015
* Add options for other file extensions.

48 changes: 34 additions & 14 deletions extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,20 @@ function findRecursive(dir, fileName) {
}
//register on activation
function activate(context) {

var doBeautify = function(active, doc, opts) {
var better = doc.getText();
var type = doc.isUntitled ? "js" : doc.fileName.split('.')
var original = doc.getText();
var type = doc.isUntitled ? "" : doc.fileName.split('.')
.pop()
.toLowerCase();
var cfg = vscode.workspace.getConfiguration('beautify');
//if a type is set on the window, use that
console.log(vscode.window.activeTextEditor);
//check if the file is in the users json schema set
var jsSchema = vscode.workspace.getConfiguration('json')
.schemas;
var range;
//get the whole file:
var range = new vscode.Range(new vscode.Position(0, 0), doc.positionAt(Infinity));
var result;
if (jsSchema) {
var matcher = [];
var extMatch = n => ({
Expand All @@ -43,24 +44,43 @@ function activate(context) {
});
if (vscode.languages.match(matcher, doc)) {
//beautify as javascript
better = beautify.js(better, opts);
result = beautify.js(original, opts);
//get the whole file:
range = new vscode.Range(new vscode.Position(0, 0), doc.positionAt(Infinity));
//and make the change:
active.edit(editor => editor.replace(range, better));
active.edit(editor => editor.replace(range, result));
return;
}
}
if (cfg.HTMLfiles.indexOf(type) + 1) {
better = beautify.html(better, opts);
result = beautify.html(original, opts);
} else if (cfg.CSSfiles.indexOf(type) + 1) {
better = beautify.css(better, opts);
result = beautify.css(original, opts);
} else if (cfg.JSfiles.indexOf(type) + 1) {
better = beautify.js(better, opts);
} else return;
//get the whole file:
range = new vscode.Range(new vscode.Position(0, 0), doc.positionAt(Infinity));
result = beautify.js(original, opts);
} else {
//Ask what they want to do:
vscode.window.showQuickPick([{
label: "JS",
description: "Does JavaScript and JSON"
}, {
label: "CSS"
}, {
label: "HTML"
}], {
matchOnDescription: true,
placeHolder: "Couldn't determine type to beautify, pleae choose."
})
.then(function(choice) {
if (!choice || !choice.label) return;
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, better));
active.edit(editor => editor.replace(range, result));
};

var disposable = vscode.commands.registerCommand('HookyQR.beautify', function() {
Expand Down
5 changes: 1 addition & 4 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.3",
"version": "0.0.4",
"publisher": "HookyQR",
"engines": {
"vscode": "^0.10.1"
Expand All @@ -15,9 +15,6 @@
],
"license": "MIT",
"main": "./extension",
"extensionDependencies": [
"vscode.json"
],
"contributes": {
"languages": [{
"id": "json",
Expand Down

0 comments on commit 99d35a3

Please sign in to comment.