Skip to content

Commit

Permalink
Use workspace indenting by default.
Browse files Browse the repository at this point in the history
  • Loading branch information
HookyQR committed Mar 20, 2016
1 parent 1c6d47c commit 5365837
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 30 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ If you wish to include the files that are included by default, set `"beautify.on
Embedded version of js-beautify is v1.6.2.

## Changes:
### 0.1.2: 20 Mar 2016
* 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.)

### 0.1.1: 15 Mar 2016
* Allow beautify on save to work with types in `beautify.*Files` settings. [Issue #9](https://github.com/HookyQR/VSCodeBeautify/issues/9)
* Fix `beautify.*Files` settings requiring a `.` before the extension (both styles are now accepted).
Expand Down
57 changes: 36 additions & 21 deletions extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const dumpError = e => {
if (e) console.log('beautify err:', e);
return [];
};

const dropComments = inText => inText.replace(/(\/\*.*\*\/)|\/\/.*(?:[\r\n]|$)/g, "");

const mergeOpts = function(opts, type) {
Expand Down Expand Up @@ -73,54 +74,53 @@ const getBeautifyType = function(doc, dontAsk) {
return vscode.window.showQuickPick([{
label: "JS",
description: "Does JavaScript and JSON"
}, {
}, {
label: "CSS"
}, {
}, {
label: "HTML"
}], {
}], {
matchOnDescription: true,
placeHolder: "Couldn't determine type to beautify, please choose."
})
.then(function(choice) {
if (!choice || !choice.label) return reject('no beautify type selected');
return resolve(choice.label.toLowerCase());
});
}, () => 0);
});
};

function getConfigFor(doc, type) {
function getConfigFor(doc, defaultOptions, type) {

let base = vscode.workspace.rootPath;

if (!doc.isUntitled) base = path.dirname(doc.fileName);
if (!base) return Promise.resolve({});
if (!base) return Promise.resolve(defaultOptions);
let configFile = findRecursive(base, '.jsbeautifyrc');
if (!configFile) return Promise.resolve({});
if (!configFile) return Promise.resolve(defaultOptions);
return new Promise(resolve => {
fs.readFile(configFile, 'utf8', (e, d) => {
if (!d) d = '{}';
let opts = {};
let opts = defaultOptions;
if (!d) return resolve(opts);
try {
const unCommented = dropComments(d.toString());
opts = JSON.parse(unCommented);
opts = mergeOpts(opts, type);
} catch (e) {
vscode.window.showWarningMessage(`Found a .jsbeautifyrc file [${configFile}], but it didn't parse correctly.`);
opts = {}; //just use the default opts
}
resolve(opts);
});
});
}

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

function optionsFromFormat(formattingOptions) {
return {
indent_with_tabs: !formattingOptions.insertSpaces,
indent_size: formattingOptions.tabSize,
indent_char: ' '
};
}

function rangeEditByType(type) {
return (doc, rng) => {
return (doc, rng, formattingOptions) => {
rng = extendRange(doc, rng);
return beautifyDoc(doc, rng, type)
.then(newText => documentEdit(rng, newText))
.catch(dumpError);
return beautifyDoc(doc, rng, optionsFromFormat(formattingOptions), type)
.then(newText => documentEdit(rng, newText), dumpError);
};
}

Expand Down Expand Up @@ -175,7 +182,15 @@ function beautifyOnSave(doc) {
Array.isArray(cfg.onSave) && cfg.onSave.indexOf(refType) >= 0
)) {
const range = new vscode.Range(0, 0, Number.MAX_VALUE, Number.MAX_VALUE);
beautifyDoc(doc, range)
//determine a default options
let defaultOptions = optionsFromFormat(vscode.workspace.getConfiguration('editor'));
//if this document is open, use the settings from that window
vscode.window.visibleTextEditors.some(editor => {
if (editor.document && editor.document.fileName === doc.fileName) {
return (defaultOptions = optionsFromFormat(editor.options));
}
});
beautifyDoc(doc, range, defaultOptions, refType)
.then(newText => {
let we = new vscode.WorkspaceEdit();
we.replace(doc.uri, range, newText);
Expand All @@ -194,9 +209,9 @@ function activate(context) {
if (!active) return;
if (!active.document) return;
const range = new vscode.Range(0, 0, Number.MAX_VALUE, Number.MAX_VALUE);
beautifyDoc(active.document, range)
.then(newText => active.edit(editor => editor.replace(range, newText)))
.catch(dumpError);

beautifyDoc(active.document, range, optionsFromFormat(active.options))
.then(newText => active.edit(editor => editor.replace(range, newText)), dumpError);
}));

//VS Code won't allow the formatters to run for json, or js. The inbuild
Expand Down
18 changes: 9 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@
"name": "beautify",
"displayName": "beautify",
"description": "Beautify code in place for VS Code",
"version": "0.1.1",
"version": "0.1.2",
"publisher": "HookyQR",
"engines": {
"vscode": "^0.10.1"
},
"categories": [
"Other",
"Languages"
],
"Other",
"Languages"
],
"activationEvents": [
"*"
],
"*"
],
"icon": "icon.svg",
"galleryBanner": {
"color": "#e8e030",
Expand All @@ -26,15 +26,15 @@
"id": "json",
"aliases": ["JSON"],
"filenames": [".jsbeautifyrc"]
}],
}],
"jsonValidation": [{
"fileMatch": ".jsbeautifyrc",
"url": "./schema/beautifyrc.json"
}],
}],
"commands": [{
"command": "HookyQR.beautify",
"title": "Beautify"
}],
}],
"configuration": {
"type": "object",
"title": "Beautify config",
Expand Down

0 comments on commit 5365837

Please sign in to comment.