aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormitermayer <mitermayer.reis@gmail.com>2019-08-25 21:43:46 -0700
committermitermayer <mitermayer.reis@gmail.com>2019-08-25 21:43:46 -0700
commit394f4220f570092486d33ccbed7af7fa8edf21bc (patch)
tree348f36737ce5d1a58e956d4e5cef3465c6be050f
parent6a5dbb150f1362c6c4600b6f4e7ae5611ac0ae64 (diff)
parent8601e0090ccbf202c15a151305cdf211e52d8f71 (diff)
downloadvim-prettier-394f4220f570092486d33ccbed7af7fa8edf21bc.tar.xz
Merge branch 'release/1.x' into shore/master-rebasing
-rw-r--r--autoload/prettier/job/async/vim.vim19
-rw-r--r--autoload/prettier/resolver/config.vim1
-rw-r--r--autoload/prettier/utils/buffer.vim11
-rw-r--r--package.json9
-rw-r--r--tests/fixtures/foo.js1
-rw-r--r--tests/formatting.test.js118
6 files changed, 147 insertions, 12 deletions
diff --git a/autoload/prettier/job/async/vim.vim b/autoload/prettier/job/async/vim.vim
index ab0fbef..29774b9 100644
--- a/autoload/prettier/job/async/vim.vim
+++ b/autoload/prettier/job/async/vim.vim
@@ -8,13 +8,15 @@ function! prettier#job#async#vim#run(cmd, startSelection, endSelection) abort
let l:bufferName = bufname('%')
- call job_start([&shell, &shellcmdflag, a:cmd], {
- \ 'in_io': 'buffer',
- \ 'in_top': a:startSelection,
- \ 'in_bot': a:endSelection,
- \ 'in_name': l:bufferName,
+ let l:job = job_start([&shell, &shellcmdflag, a:cmd], {
+ \ 'out_io': 'buffer',
\ 'err_cb': {channel, msg -> s:onError(msg)},
\ 'close_cb': {channel -> s:onClose(channel, a:startSelection, a:endSelection, l:bufferName)}})
+
+ let l:stdin = job_getchannel(l:job)
+
+ call ch_sendraw(l:stdin, join(getbufline(bufnr(l:bufferName), a:startSelection, a:endSelection), "\n"))
+ call ch_close_in(l:stdin)
endfunction
function! s:onError(msg) abort
@@ -27,9 +29,9 @@ function! s:onClose(channel, startSelection, endSelection, bufferName) abort
let l:currentBufferName = bufname('%')
let l:isInsideAnotherBuffer = a:bufferName != l:currentBufferName ? 1 : 0
- while ch_status(a:channel) ==# 'buffered'
- call add(l:out, ch_read(a:channel))
- endwhile
+ let l:buff = ch_getbufnr(a:channel, 'out')
+ let l:out = getbufline(l:buff, 2, '$')
+ execute 'bd!' . l:buff
" we have no prettier output so lets exit
if len(l:out) == 0 | return | endif
@@ -37,6 +39,7 @@ function! s:onClose(channel, startSelection, endSelection, bufferName) abort
" nothing to update
if (prettier#utils#buffer#willUpdatedLinesChangeBuffer(l:out, a:startSelection, a:endSelection) == 0)
let s:prettier_job_running = 0
+ redraw!
return
endif
diff --git a/autoload/prettier/resolver/config.vim b/autoload/prettier/resolver/config.vim
index b6bacb2..2db11b5 100644
--- a/autoload/prettier/resolver/config.vim
+++ b/autoload/prettier/resolver/config.vim
@@ -27,7 +27,6 @@ function! prettier#resolver#config#resolve(config, hasSelection, start, end) abo
\ get(a:config, 'proseWrap', g:prettier#config#prose_wrap) .
\ ' --stdin-filepath=' .
\ simplify(expand('%:p')) .
- \ ' --no-editorconfig '.
\ ' --loglevel error '.
\ ' --stdin '
return l:cmd
diff --git a/autoload/prettier/utils/buffer.vim b/autoload/prettier/utils/buffer.vim
index 258c615..c22fec7 100644
--- a/autoload/prettier/utils/buffer.vim
+++ b/autoload/prettier/utils/buffer.vim
@@ -9,10 +9,17 @@ function! prettier#utils#buffer#replace(lines, startSelection, endSelection) abo
endif
" delete all lines on the current buffer
- silent! execute len(l:newBuffer) . ',' . line('$') . 'delete _'
+ silent! execute '%delete _'
" replace all lines from the current buffer with output from prettier
- call setline(1, l:newBuffer)
+ let l:idx = 0
+ for l:line in l:newBuffer
+ silent! call append(l:idx, l:line)
+ let l:idx += 1
+ endfor
+
+ " delete trailing newline introduced by the above append procedure
+ silent! execute '$delete _'
" Restore view
call winrestview(l:winview)
diff --git a/package.json b/package.json
index 3dbd7b3..eb53112 100644
--- a/package.json
+++ b/package.json
@@ -8,13 +8,20 @@
"type": "git",
"url": "git://github.com/prettier/vim-prettier.git"
},
+ "scripts": {
+ "test": "LOG_LEVEL=error jest"
+ },
"dependencies": {
- "prettier": "^1.16.4",
"@prettier/plugin-lua": "0.0.1",
"@prettier/plugin-php": "^0.10.2",
"@prettier/plugin-python": "prettier/plugin-python",
"@prettier/plugin-ruby": "^0.8.0",
"@prettier/plugin-swift": "prettier/plugin-swift",
"prettier": "^1.16.4"
+ },
+ "devDependencies": {
+ "colors": "^1.3.2",
+ "jest": "^23.6.0",
+ "vim-driver": "^1.0.0"
}
}
diff --git a/tests/fixtures/foo.js b/tests/fixtures/foo.js
new file mode 100644
index 0000000..82955ab
--- /dev/null
+++ b/tests/fixtures/foo.js
@@ -0,0 +1 @@
+const a = () => { return 2; };
diff --git a/tests/formatting.test.js b/tests/formatting.test.js
new file mode 100644
index 0000000..d627921
--- /dev/null
+++ b/tests/formatting.test.js
@@ -0,0 +1,118 @@
+const fs = require('fs');
+const path = require('path');
+const HeadlessRemoteClient = require('vim-driver/dist/HeadlessRemoteClient');
+const Server = require('vim-driver/dist/Server');
+
+const HOST = '127.0.0.1';
+const PORT = 1337;
+const FIXTURES_DIR = `${__dirname}/fixtures`;
+
+let server;
+let remote;
+
+const getBufferContents = async remote =>
+ (await remote.call('getline', [1, '$'])).join('\n');
+
+const sleep = ms => new Promise(resolve => setTimeout(resolve, ms));
+const waitUntil = (condition, timeout = 4000) => {
+ return new Promise(resolve => {
+ let isTimedOut = false;
+ let timeoutId = null;
+
+ const check = () => {
+ const promise = condition();
+ promise.then(result => {
+ if (!isTimedOut && result === true) {
+ clearTimeout(timeoutId);
+ resolve();
+ } else if (!isTimedOut) {
+ check();
+ }
+ });
+ };
+
+ timeoutId = setTimeout(() => {
+ isTimedOut = true;
+ resolve();
+ }, timeout);
+ });
+};
+
+const assertFormatting = (file) => {
+ const filename = path.basename(file);
+ test(`Prettier formats ${filename} file with :Prettier command`, async () => {
+ await remote.edit(`${FIXTURES_DIR}/${file}`);
+
+ const lines = await getBufferContents(remote);
+
+ // run sync formatting
+ await remote.execute('Prettier');
+
+ const updatedLines = await getBufferContents(remote);
+
+ // we now check that we have indeed formatted the code
+ expect(updatedLines).not.toBe(lines);
+ });
+
+ test(`Prettier formats ${filename} file with :PrettierAsync command`, async () => {
+ await remote.edit(`${FIXTURES_DIR}/${file}`);
+
+ const lines = await getBufferContents(remote);
+
+ // run async formatting
+ await remote.execute('PrettierAsync');
+
+ const unmodifiedLines = await getBufferContents(remote);
+
+ // async should not happen immediatly so content should still remain the same
+ expect(unmodifiedLines).toBe(lines);
+
+ // we now will wait until prettier has finally updated the content async
+ await waitUntil(async () => (await getBufferContents(remote)) !== lines);
+
+ const updatedLines = await getBufferContents(remote);
+
+ // we now check that we have indeed formatted the code
+ expect(lines).not.toBe(updatedLines);
+ });
+};
+
+beforeAll(async () => {
+ server = new Server();
+ await server.listen(HOST, PORT);
+});
+
+afterAll(async () => {
+ await server.close();
+});
+
+// should ensure that we cache original fixture contents and
+// restore it on the afterEach
+beforeEach(async () => {
+ remote = new HeadlessRemoteClient({host: HOST, port: PORT});
+ await remote.connect(server);
+});
+
+afterEach(async () => {
+ if (remote.isConnected()) {
+ try {
+ const filename = await remote.call('expand', ['%:p']);
+
+ if (filename) {
+ // restore the file
+ await remote.execute('earlier 1d | noautocmd | write');
+ }
+ } catch (e) {
+ } finally {
+ await remote.close();
+ }
+ }
+});
+
+//test('PrettierVersion returns pluggin version', async () => {
+// const result = await remote.execute('PrettierVersion');
+// expect(result).toMatchSnapshot();
+//});
+
+// run formatting tests in all fixtures
+fs.readdirSync(FIXTURES_DIR).forEach(file => assertFormatting(file));