Skip to content

Commit 6072e9f

Browse files
committed
Add tests + fix #14
1 parent 5365837 commit 6072e9f

File tree

20 files changed

+190
-24
lines changed

20 files changed

+190
-24
lines changed

.gitignore

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
node_modules
22
*.vsix
33
.vscodeignore
4-
typings
4+
.vscode-test
55
.vscode
6-
.jshintrc
6+
.jshintrc
7+
typings

.travis.yml

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,21 @@
1-
language: node_js
2-
node_js:
3-
- "4.1"
4-
- "stable"
1+
os:
2+
- osx
3+
- linux
4+
5+
before_install:
6+
- if [ $TRAVIS_OS_NAME == "linux" ]; then
7+
export CXX="g++-4.9" CC="gcc-4.9" DISPLAY=:99.0;
8+
sh -e /etc/init.d/xvfb start;
9+
sleep 3;
10+
fi
11+
12+
env:
13+
- CODE_VERSION="0.10.1"
14+
- CODE_VERSION="0.10.9"
15+
- CODE_VERSION="1.0.0"
16+
17+
install:
18+
- npm install
19+
20+
script:
21+
- npm test --silent

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.3: 08 May 2016
59+
* Fix [Issue #14: Full file beautify doubles text on version 1.1.0](https://github.com/HookyQR/VSCodeBeautify/issues/11)
60+
* Add tests for supported formats and nested settings.
61+
5862
### 0.1.2: 20 Mar 2016
5963
* 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.)
6064

extension.js

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,18 @@ const dumpError = e => {
1212

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

15-
const mergeOpts = function(opts, type) {
15+
const mergeOpts = function(opts, kind) {
1616
const finOpts = {};
1717
for (let a in opts) {
1818
if (a !== 'js' && a !== 'html' && a !== 'css') {
1919
finOpts[a] = opts[a];
2020
}
2121
}
2222
//merge in the per type settings
23-
if (type in opts) {
24-
for (let b in opts[type]) {
23+
if (kind in opts) {
24+
for (let b in opts[kind]) {
2525
if (b === 'allowed_file_extensions') continue;
26-
finOpts[b] = opts[type][b];
26+
finOpts[b] = opts[kind][b];
2727
}
2828
}
2929
return finOpts;
@@ -64,9 +64,9 @@ const getBeautifyType = function(doc, dontAsk) {
6464
});
6565
if (vscode.languages.match(matcher, doc)) return "js";
6666
}
67-
if (cfg.HTMLfiles.indexOf(type) + 1 || cfg.HTMLfiles.indexOf(type.slice(1)) + 1) return 'html';
68-
else if (cfg.CSSfiles.indexOf(type) + 1 || cfg.CSSfiles.indexOf(type.slice(1)) + 1) return 'css';
69-
else if (cfg.JSfiles.indexOf(type) + 1 || cfg.JSfiles.indexOf(type.slice(1)) + 1) return 'js';
67+
if (cfg.HTMLfiles.includes(type) || cfg.HTMLfiles.includes(type.slice(1))) return 'html';
68+
else if (cfg.CSSfiles.includes(type) || cfg.CSSfiles.includes(type.slice(1))) return 'css';
69+
else if (cfg.JSfiles.includes(type) || cfg.JSfiles.includes(type.slice(1))) return 'js';
7070
if (dontAsk) return;
7171

7272
return new Promise((resolve, reject) => {
@@ -178,10 +178,9 @@ function beautifyOnSave(doc) {
178178
refType = getBeautifyType(doc, true);
179179
if (!refType) return;
180180
}
181-
if (cfg.onSave === true || (
182-
Array.isArray(cfg.onSave) && cfg.onSave.indexOf(refType) >= 0
183-
)) {
184-
const range = new vscode.Range(0, 0, Number.MAX_VALUE, Number.MAX_VALUE);
181+
if (cfg.onSave === true || (Array.isArray(cfg.onSave) && cfg.onSave.indexOf(refType) >= 0)) {
182+
let range = new vscode.Range(0, 0, Number.MAX_VALUE, Number.MAX_VALUE);
183+
range = doc.validateRange(range);
185184
//determine a default options
186185
let defaultOptions = optionsFromFormat(vscode.workspace.getConfiguration('editor'));
187186
//if this document is open, use the settings from that window
@@ -208,28 +207,29 @@ function activate(context) {
208207
const active = vscode.window.activeTextEditor;
209208
if (!active) return;
210209
if (!active.document) return;
211-
const range = new vscode.Range(0, 0, Number.MAX_VALUE, Number.MAX_VALUE);
212-
213-
beautifyDoc(active.document, range, optionsFromFormat(active.options))
210+
let range = new vscode.Range(0, 0, Number.MAX_VALUE, Number.MAX_VALUE);
211+
212+
range = active.document.validateRange(range);
213+
214+
return beautifyDoc(active.document, range, optionsFromFormat(active.options))
214215
.then(newText => active.edit(editor => editor.replace(range, newText)), dumpError);
215216
}));
216217

217-
//VS Code won't allow the formatters to run for json, or js. The inbuild
218-
//js-beautify runs instead
219218
context.subscriptions.push(
220219
vscode.languages.registerDocumentRangeFormattingEditProvider('html', {
221220
provideDocumentRangeFormattingEdits: rangeEditByType('html')
222221
}));
223222
context.subscriptions.push(vscode.languages.registerDocumentRangeFormattingEditProvider('css', {
224223
provideDocumentRangeFormattingEdits: rangeEditByType('css')
225224
}));
225+
//VS Code won't allow the formatters to run for json, or js. The inbuild
226+
//js-beautify runs instead
226227
context.subscriptions.push(vscode.languages.registerDocumentRangeFormattingEditProvider('javascript', {
227228
provideDocumentRangeFormattingEdits: rangeEditByType('js')
228229
}));
229230
context.subscriptions.push(vscode.languages.registerDocumentRangeFormattingEditProvider('json', {
230231
provideDocumentRangeFormattingEdits: rangeEditByType('js')
231232
}));
232233
vscode.workspace.onDidSaveTextDocument(beautifyOnSave);
233-
234234
}
235235
exports.activate = activate;

package.json

Lines changed: 9 additions & 1 deletion
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.2",
5+
"version": "0.1.3",
66
"publisher": "HookyQR",
77
"engines": {
88
"vscode": "^0.10.1"
@@ -76,6 +76,14 @@
7676
"js-beautify": "^1.6.2",
7777
"minimatch": "^3.0.0"
7878
},
79+
"devDependencies": {
80+
"vscode": "^0.11.x",
81+
"mocha": "^2.4.5",
82+
"expect.js": "~0.3.1"
83+
},
84+
"scripts": {
85+
"test": "node ./node_modules/vscode/bin/test"
86+
},
7987
"repository": {
8088
"type": "git",
8189
"url": "https://github.com/HookyQR/VSCodeBeautify"

test/data/.jsbeautifyrc

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"indent_with_tabs": true,
3+
"css": {
4+
"selector_separator_newline": true
5+
},
6+
"js": {
7+
"break_chained_methods": true,
8+
"max_preserve_newlines": 2
9+
},
10+
"html": {
11+
"brace_style": "none",
12+
"preserve_newlines": false
13+
}
14+
}

test/data/in.css

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.a, #b{border: 1px solid green}

test/data/in.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<html><head><title>abc</title></head><body><a href='https://github.com/HookyQR/VSCodeBeautify' data-blank="blank">beautify for VS Code</a>
2+
3+
4+
</body>

test/data/in.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
var a=1;function b(x){return x;}b(a).toString();

test/data/in.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{"test": 1, "test2": "test3",
2+
3+
4+
"test4":["test5",6]
5+
}

0 commit comments

Comments
 (0)