diff --git a/.gitignore b/.gitignore index 28a2e04..a5282ff 100755 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ node_modules *.vsix .vscodeignore -typings +.vscode-test .vscode -.jshintrc \ No newline at end of file +.jshintrc +typings diff --git a/.travis.yml b/.travis.yml index 9ba38e4..d0fd497 100755 --- a/.travis.yml +++ b/.travis.yml @@ -1,4 +1,21 @@ -language: node_js -node_js: -- "4.1" -- "stable" +os: + - osx + - linux + +before_install: + - if [ $TRAVIS_OS_NAME == "linux" ]; then + export CXX="g++-4.9" CC="gcc-4.9" DISPLAY=:99.0; + sh -e /etc/init.d/xvfb start; + sleep 3; + fi + +env: + - CODE_VERSION="0.10.1" + - CODE_VERSION="0.10.9" + - CODE_VERSION="1.0.0" + +install: + - npm install + +script: + - npm test --silent \ No newline at end of file diff --git a/README.md b/README.md index ee581d5..a814135 100755 --- a/README.md +++ b/README.md @@ -55,6 +55,10 @@ 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.3: 08 May 2016 +* Fix [Issue #14: Full file beautify doubles text on version 1.1.0](https://github.com/HookyQR/VSCodeBeautify/issues/11) +* Add tests for supported formats and nested settings. + ### 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)
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.) diff --git a/extension.js b/extension.js index c2005e5..7567b2f 100755 --- a/extension.js +++ b/extension.js @@ -12,7 +12,7 @@ const dumpError = e => { const dropComments = inText => inText.replace(/(\/\*.*\*\/)|\/\/.*(?:[\r\n]|$)/g, ""); -const mergeOpts = function(opts, type) { +const mergeOpts = function(opts, kind) { const finOpts = {}; for (let a in opts) { if (a !== 'js' && a !== 'html' && a !== 'css') { @@ -20,10 +20,10 @@ const mergeOpts = function(opts, type) { } } //merge in the per type settings - if (type in opts) { - for (let b in opts[type]) { + if (kind in opts) { + for (let b in opts[kind]) { if (b === 'allowed_file_extensions') continue; - finOpts[b] = opts[type][b]; + finOpts[b] = opts[kind][b]; } } return finOpts; @@ -64,9 +64,9 @@ const getBeautifyType = function(doc, dontAsk) { }); if (vscode.languages.match(matcher, doc)) return "js"; } - if (cfg.HTMLfiles.indexOf(type) + 1 || cfg.HTMLfiles.indexOf(type.slice(1)) + 1) return 'html'; - else if (cfg.CSSfiles.indexOf(type) + 1 || cfg.CSSfiles.indexOf(type.slice(1)) + 1) return 'css'; - else if (cfg.JSfiles.indexOf(type) + 1 || cfg.JSfiles.indexOf(type.slice(1)) + 1) return 'js'; + if (cfg.HTMLfiles.includes(type) || cfg.HTMLfiles.includes(type.slice(1))) return 'html'; + else if (cfg.CSSfiles.includes(type) || cfg.CSSfiles.includes(type.slice(1))) return 'css'; + else if (cfg.JSfiles.includes(type) || cfg.JSfiles.includes(type.slice(1))) return 'js'; if (dontAsk) return; return new Promise((resolve, reject) => { @@ -178,10 +178,9 @@ function beautifyOnSave(doc) { refType = getBeautifyType(doc, true); if (!refType) return; } - if (cfg.onSave === true || ( - Array.isArray(cfg.onSave) && cfg.onSave.indexOf(refType) >= 0 - )) { - const range = new vscode.Range(0, 0, Number.MAX_VALUE, Number.MAX_VALUE); + if (cfg.onSave === true || (Array.isArray(cfg.onSave) && cfg.onSave.indexOf(refType) >= 0)) { + let range = new vscode.Range(0, 0, Number.MAX_VALUE, Number.MAX_VALUE); + range = doc.validateRange(range); //determine a default options let defaultOptions = optionsFromFormat(vscode.workspace.getConfiguration('editor')); //if this document is open, use the settings from that window @@ -208,14 +207,14 @@ function activate(context) { const active = vscode.window.activeTextEditor; if (!active) return; if (!active.document) return; - const range = new vscode.Range(0, 0, Number.MAX_VALUE, Number.MAX_VALUE); - - beautifyDoc(active.document, range, optionsFromFormat(active.options)) + let range = new vscode.Range(0, 0, Number.MAX_VALUE, Number.MAX_VALUE); + + range = active.document.validateRange(range); + + return 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 - //js-beautify runs instead context.subscriptions.push( vscode.languages.registerDocumentRangeFormattingEditProvider('html', { provideDocumentRangeFormattingEdits: rangeEditByType('html') @@ -223,6 +222,8 @@ function activate(context) { context.subscriptions.push(vscode.languages.registerDocumentRangeFormattingEditProvider('css', { provideDocumentRangeFormattingEdits: rangeEditByType('css') })); + //VS Code won't allow the formatters to run for json, or js. The inbuild + //js-beautify runs instead context.subscriptions.push(vscode.languages.registerDocumentRangeFormattingEditProvider('javascript', { provideDocumentRangeFormattingEdits: rangeEditByType('js') })); @@ -230,6 +231,5 @@ function activate(context) { provideDocumentRangeFormattingEdits: rangeEditByType('js') })); vscode.workspace.onDidSaveTextDocument(beautifyOnSave); - } exports.activate = activate; diff --git a/package.json b/package.json index bbd5597..5dc001f 100755 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "beautify", "displayName": "beautify", "description": "Beautify code in place for VS Code", - "version": "0.1.2", + "version": "0.1.3", "publisher": "HookyQR", "engines": { "vscode": "^0.10.1" @@ -76,6 +76,14 @@ "js-beautify": "^1.6.2", "minimatch": "^3.0.0" }, + "devDependencies": { + "vscode": "^0.11.x", + "mocha": "^2.4.5", + "expect.js": "~0.3.1" + }, + "scripts": { + "test": "node ./node_modules/vscode/bin/test" + }, "repository": { "type": "git", "url": "https://github.com/HookyQR/VSCodeBeautify" diff --git a/test/data/.jsbeautifyrc b/test/data/.jsbeautifyrc new file mode 100644 index 0000000..6324cb8 --- /dev/null +++ b/test/data/.jsbeautifyrc @@ -0,0 +1,14 @@ +{ + "indent_with_tabs": true, + "css": { + "selector_separator_newline": true + }, + "js": { + "break_chained_methods": true, + "max_preserve_newlines": 2 + }, + "html": { + "brace_style": "none", + "preserve_newlines": false + } +} \ No newline at end of file diff --git a/test/data/in.css b/test/data/in.css new file mode 100644 index 0000000..afd3a4f --- /dev/null +++ b/test/data/in.css @@ -0,0 +1 @@ +.a, #b{border: 1px solid green} \ No newline at end of file diff --git a/test/data/in.html b/test/data/in.html new file mode 100644 index 0000000..c97c493 --- /dev/null +++ b/test/data/in.html @@ -0,0 +1,4 @@ +abcbeautify for VS Code + + + \ No newline at end of file diff --git a/test/data/in.js b/test/data/in.js new file mode 100644 index 0000000..886a317 --- /dev/null +++ b/test/data/in.js @@ -0,0 +1 @@ +var a=1;function b(x){return x;}b(a).toString(); \ No newline at end of file diff --git a/test/data/in.json b/test/data/in.json new file mode 100644 index 0000000..b771d35 --- /dev/null +++ b/test/data/in.json @@ -0,0 +1,5 @@ +{"test": 1, "test2": "test3", + + +"test4":["test5",6] +} \ No newline at end of file diff --git a/test/data/out.2.css b/test/data/out.2.css new file mode 100644 index 0000000..f2f5a05 --- /dev/null +++ b/test/data/out.2.css @@ -0,0 +1,4 @@ +.a, +#b { + border: 1px solid green +} \ No newline at end of file diff --git a/test/data/out.2.html b/test/data/out.2.html new file mode 100644 index 0000000..e0f999b --- /dev/null +++ b/test/data/out.2.html @@ -0,0 +1,7 @@ + + + + abc + + +beautify for VS Code \ No newline at end of file diff --git a/test/data/out.2.js b/test/data/out.2.js new file mode 100644 index 0000000..fbe1530 --- /dev/null +++ b/test/data/out.2.js @@ -0,0 +1,7 @@ +var a = 1; + +function b(x) { + return x; +} +b(a) + .toString(); \ No newline at end of file diff --git a/test/data/out.2.json b/test/data/out.2.json new file mode 100644 index 0000000..d85e992 --- /dev/null +++ b/test/data/out.2.json @@ -0,0 +1,6 @@ +{ + "test": 1, + "test2": "test3", + + "test4": ["test5", 6] +} \ No newline at end of file diff --git a/test/data/out.css b/test/data/out.css new file mode 100644 index 0000000..953e69b --- /dev/null +++ b/test/data/out.css @@ -0,0 +1,4 @@ +.a, +#b { + border: 1px solid green +} \ No newline at end of file diff --git a/test/data/out.html b/test/data/out.html new file mode 100644 index 0000000..07ea84a --- /dev/null +++ b/test/data/out.html @@ -0,0 +1,10 @@ + + + + abc + + +beautify for VS Code + + + \ No newline at end of file diff --git a/test/data/out.js b/test/data/out.js new file mode 100644 index 0000000..db6da97 --- /dev/null +++ b/test/data/out.js @@ -0,0 +1,6 @@ +var a = 1; + +function b(x) { + return x; +} +b(a).toString(); \ No newline at end of file diff --git a/test/data/out.json b/test/data/out.json new file mode 100644 index 0000000..8a72485 --- /dev/null +++ b/test/data/out.json @@ -0,0 +1,7 @@ +{ + "test": 1, + "test2": "test3", + + + "test4": ["test5", 6] +} \ No newline at end of file diff --git a/test/extension.test.js b/test/extension.test.js new file mode 100644 index 0000000..839c325 --- /dev/null +++ b/test/extension.test.js @@ -0,0 +1,52 @@ +'use strict'; +let vscode = require('vscode'), + expect = require('expect.js'), + path = require('path'), + fs = require('fs'); + +let root = path.join(path.dirname(__filename), 'data'); + +describe('with empty .jsbeautify', () => { + beforeEach(() => fs.writeFileSync(path.join(root, '.jsbeautifyrc'), "{}")); + + ['.js', '.html', '.json', '.css'].forEach(extension => + it('beautify of "' + extension + "'", () => vscode.workspace.openTextDocument(path.join(root, 'in' + extension)) + .then(doc => vscode.window.showTextDocument(doc) + .then(() => vscode.commands.executeCommand('HookyQR.beautify') + .then(() => expect(doc.getText()) + .to.be(fs.readFileSync(path.join(root, 'out' + extension), 'utf8'))))))); + + ['.html', '.css'].forEach(extension => + it('format of "' + extension + "'", () => vscode.workspace.openTextDocument(path.join(root, 'in' + extension)) + .then(doc => vscode.window.showTextDocument(doc) + .then(() => vscode.commands.executeCommand('editor.action.format') + .then(() => expect(doc.getText()) + .to.be(fs.readFileSync(path.join(root, 'out' + extension), 'utf8'))))))); +}); + +describe('with nested options in .jsbeautify', () => { + beforeEach(() => fs.writeFileSync(path.join(root, '.jsbeautifyrc'), + `{ + "indent_with_tabs": true, + "css": { + "selector_separator_newline": true + }, + "js": { + "break_chained_methods": true, + "max_preserve_newlines": 2 + }, + "html": { + "brace_style": "none", + "preserve_newlines": false + } +}` + )); + + ['.js', '.html', '.json', '.css'].forEach(extension => + it('beautify of "' + extension + "'", () => vscode.workspace.openTextDocument(path.join(root, 'in' + extension)) + .then(doc => vscode.window.showTextDocument(doc) + .then(() => vscode.commands.executeCommand('HookyQR.beautify') + .then(() => expect(doc.getText()) + .to.be(fs.readFileSync(path.join(root, 'out.2' + extension), 'utf8'))))))); + +}); diff --git a/test/index.js b/test/index.js new file mode 100644 index 0000000..2bc0614 --- /dev/null +++ b/test/index.js @@ -0,0 +1,8 @@ +'use strict'; +let runner = require('vscode/lib/testrunner'); + +runner.configure({ + useColors: true +}); + +module.exports = runner; \ No newline at end of file