From 89d7e5324d54ddada02925ca19871801421a29b2 Mon Sep 17 00:00:00 2001 From: wsdjeg Date: Thu, 5 Oct 2017 19:10:01 +0800 Subject: [PATCH 1/5] Add language specified mappings for python --- autoload/SpaceVim/layers/lang/python.vim | 40 ++++++++++++++---------- autoload/SpaceVim/plugins/runner.vim | 33 +++++++++++++++++++ 2 files changed, 57 insertions(+), 16 deletions(-) create mode 100644 autoload/SpaceVim/plugins/runner.vim diff --git a/autoload/SpaceVim/layers/lang/python.vim b/autoload/SpaceVim/layers/lang/python.vim index 0709a578c..e42ae85cc 100644 --- a/autoload/SpaceVim/layers/lang/python.vim +++ b/autoload/SpaceVim/layers/lang/python.vim @@ -8,25 +8,33 @@ " < function! SpaceVim#layers#lang#python#plugins() abort - let plugins = [] - " python - if has('nvim') - call add(plugins, ['zchee/deoplete-jedi', { 'on_ft' : 'python'}]) - else - call add(plugins, ['davidhalter/jedi-vim', { 'on_ft' : 'python', - \ 'if' : has('python') || has('python3')}]) - endif - call add(plugins, ['Vimjas/vim-python-pep8-indent', - \ { 'on_ft' : 'python'}]) - return plugins + let plugins = [] + " python + if has('nvim') + call add(plugins, ['zchee/deoplete-jedi', { 'on_ft' : 'python'}]) + else + call add(plugins, ['davidhalter/jedi-vim', { 'on_ft' : 'python', + \ 'if' : has('python') || has('python3')}]) + endif + call add(plugins, ['Vimjas/vim-python-pep8-indent', + \ { 'on_ft' : 'python'}]) + return plugins endfunction function! SpaceVim#layers#lang#python#config() abort - call SpaceVim#layers#edit#add_ft_head_tamplate('python', - \ ['#!/usr/bin/env python', - \ '# -*- coding: utf-8 -*-', - \ ''] - \ ) + call SpaceVim#mapping#space#regesit_lang_mappings('python', funcref('s:language_specified_mappings')) + call SpaceVim#layers#edit#add_ft_head_tamplate('python', + \ ['#!/usr/bin/env python', + \ '# -*- coding: utf-8 -*-', + \ ''] + \ ) endfunction + +function! s:language_specified_mappings() abort + + call SpaceVim#mapping#space#langSPC('nmap', ['l','r'], + \ 'call SpaceVim#plugins#runner#open()', + \ 'execute current file', 1) +endfunction diff --git a/autoload/SpaceVim/plugins/runner.vim b/autoload/SpaceVim/plugins/runner.vim new file mode 100644 index 000000000..1d01094c2 --- /dev/null +++ b/autoload/SpaceVim/plugins/runner.vim @@ -0,0 +1,33 @@ +"============================================================================= +" runner.vim --- code runner for SpaceVim +" Copyright (c) 2016-2017 Shidong Wang & Contributors +" Author: Shidong Wang < wsdjeg at 163.com > +" URL: https://spacevim.org +" License: MIT license +"============================================================================= + +let s:runners = {} + +function! s:open_win() abort + +endfunction + + +function! s:async_run(runner) abort + +endfunction + + +function! s:update_statusline() abort + +endfunction + + +function! SpaceVim#plugins#runner#open() + let runner = get(s:runners, &filetype, '') + if !empty(runner) + call s:open_win() + call s:async_run(runner) + call s:update_statusline() + endif +endfunction From 228beb038dec9513c996bff7e0c8ab6f166950ae Mon Sep 17 00:00:00 2001 From: wsdjeg Date: Thu, 5 Oct 2017 20:26:17 +0800 Subject: [PATCH 2/5] Add SpaceVimRunner --- autoload/SpaceVim/api/vim/buffer.vim | 60 +++++++++++--------- autoload/SpaceVim/layers/core/statusline.vim | 2 + autoload/SpaceVim/layers/lang/python.vim | 2 +- autoload/SpaceVim/plugins/runner.vim | 57 ++++++++++++++++++- 4 files changed, 91 insertions(+), 30 deletions(-) diff --git a/autoload/SpaceVim/api/vim/buffer.vim b/autoload/SpaceVim/api/vim/buffer.vim index 2642bdf79..e70ff3a95 100644 --- a/autoload/SpaceVim/api/vim/buffer.vim +++ b/autoload/SpaceVim/api/vim/buffer.vim @@ -12,46 +12,54 @@ else endif function! s:self.open(opts) abort - let buf = get(a:opts, 'bufname', '') - let mode = get(a:opts, 'mode', 'vertical topleft split') - let Initfunc = get(a:opts, 'initfunc', '') - let cmd = get(a:opts, 'cmd', '') - if empty(buf) - exe mode | enew - else - exe mode buf - endif - if !empty(Initfunc) - call call(Initfunc, []) - endif + let buf = get(a:opts, 'bufname', '') + let mode = get(a:opts, 'mode', 'vertical topleft split') + let Initfunc = get(a:opts, 'initfunc', '') + let cmd = get(a:opts, 'cmd', '') + if empty(buf) + exe mode | enew + else + exe mode buf + endif + if !empty(Initfunc) + call call(Initfunc, []) + endif - if !empty(cmd) - exe cmd - endif + if !empty(cmd) + exe cmd + endif endfunction func! s:self.resize(size, ...) abort - let cmd = get(a:000, 0, 'vertical') - exe cmd 'resize' a:size + let cmd = get(a:000, 0, 'vertical') + exe cmd 'resize' a:size endf function! s:self.listed_buffers() abort - return filter(range(1, bufnr('$')), 'buflisted(v:val)') + return filter(range(1, bufnr('$')), 'buflisted(v:val)') endfunction function! s:self.filter_do(expr) abort - let buffers = range(1, bufnr('$')) - for f_expr in a:expr.expr - let buffers = filter(buffers, f_expr) - endfor - for b in buffers - exe printf(a:expr.do, b) - endfor + let buffers = range(1, bufnr('$')) + for f_expr in a:expr.expr + let buffers = filter(buffers, f_expr) + endfor + for b in buffers + exe printf(a:expr.do, b) + endfor +endfunction + + +" just same as nvim_buf_set_lines +function! s:self.buf_set_lines(buffer, start, end, strict_indexing, replacement) abort + if exists('*nvim_buf_set_lines') + call nvim_buf_set_lines(a:buffer, a:start, a:end, a:strict_indexing, a:replacement) + endif endfunction fu! SpaceVim#api#vim#buffer#get() abort - return deepcopy(s:self) + return deepcopy(s:self) endf diff --git a/autoload/SpaceVim/layers/core/statusline.vim b/autoload/SpaceVim/layers/core/statusline.vim index 3b6b1879f..59248cbca 100644 --- a/autoload/SpaceVim/layers/core/statusline.vim +++ b/autoload/SpaceVim/layers/core/statusline.vim @@ -251,6 +251,8 @@ function! SpaceVim#layers#core#statusline#get(...) abort return '%#SpaceVim_statusline_a# Transient State %#SpaceVim_statusline_a_SpaceVim_statusline_b#' elseif &filetype ==# 'HelpDescribe' return '%#SpaceVim_statusline_a# HelpDescribe %#SpaceVim_statusline_a_SpaceVim_statusline_b#' + elseif &filetype ==# 'SpaceVimRunner' + return '%#SpaceVim_statusline_a# Runner %#SpaceVim_statusline_a_SpaceVim_statusline_b# %{SpaceVim#plugins#runner#status()}' endif if a:0 > 0 return s:active() diff --git a/autoload/SpaceVim/layers/lang/python.vim b/autoload/SpaceVim/layers/lang/python.vim index e42ae85cc..3cf3634b7 100644 --- a/autoload/SpaceVim/layers/lang/python.vim +++ b/autoload/SpaceVim/layers/lang/python.vim @@ -22,7 +22,7 @@ function! SpaceVim#layers#lang#python#plugins() abort endfunction function! SpaceVim#layers#lang#python#config() abort - + call SpaceVim#plugins#runner#reg_runner('python', 'python %s') call SpaceVim#mapping#space#regesit_lang_mappings('python', funcref('s:language_specified_mappings')) call SpaceVim#layers#edit#add_ft_head_tamplate('python', \ ['#!/usr/bin/env python', diff --git a/autoload/SpaceVim/plugins/runner.vim b/autoload/SpaceVim/plugins/runner.vim index 1d01094c2..708fa8b13 100644 --- a/autoload/SpaceVim/plugins/runner.vim +++ b/autoload/SpaceVim/plugins/runner.vim @@ -6,24 +6,49 @@ " License: MIT license "============================================================================= +let s:JOB = SpaceVim#api#import('job') +let s:BUFFER = SpaceVim#api#import('vim#buffer') + let s:runners = {} +let s:bufnr = 0 + function! s:open_win() abort - + botright split __runner__ + let lines = &lines * 30 / 100 + exe 'resize ' . lines + setlocal buftype=nofile bufhidden=wipe nobuflisted nolist noswapfile nowrap cursorline nospell nonu norelativenumber + set filetype=SpaceVimRunner + let s:bufnr = bufnr('%') endfunction function! s:async_run(runner) abort - + let cmd = printf(a:runner, bufname('#')) + call s:JOB.start(cmd,{ + \ 'on_stdout' : function('s:on_stdout'), + \ 'on_stderr' : function('s:on_stderr'), + \ 'on_exit' : function('s:on_exit'), + \ }) endfunction function! s:update_statusline() abort - + redrawstatus endfunction +function! SpaceVim#plugins#runner#reg_runner(ft, runner) + let s:runners[a:ft] = a:runner +endfunction function! SpaceVim#plugins#runner#open() + let s:lines = 0 + let s:status = { + \ 'is_running' : 0, + \ 'is_exit' : 0, + \ 'has_errors' : 0, + \ 'exit_code' : 0 + \ } let runner = get(s:runners, &filetype, '') if !empty(runner) call s:open_win() @@ -31,3 +56,29 @@ function! SpaceVim#plugins#runner#open() call s:update_statusline() endif endfunction + +function! s:on_stdout(job_id, data, event) abort + call s:BUFFER.buf_set_lines(s:bufnr, s:lines , s:lines + 1, 0, a:data) + let s:lines += len(a:data) + call s:update_statusline() +endfunction + +function! s:on_stderr(job_id, data, event) abort + let s:status.has_errors = 1 + call s:update_statusline() +endfunction + +function! s:on_exit(job_id, data, event) abort + let s:status.is_exit = 1 + let s:status.exit_code = a:data + call s:update_statusline() + +endfunction + + +function! SpaceVim#plugins#runner#status() + if s:status.is_running == 1 + elseif s:status.is_exit == 1 + return 'exit code : ' . s:status.exit_code + endif +endfunction From eb7d940de883646f25be71af230d149b64dad420 Mon Sep 17 00:00:00 2001 From: wsdjeg Date: Thu, 5 Oct 2017 20:50:38 +0800 Subject: [PATCH 3/5] Fix runner --- autoload/SpaceVim.vim | 2 +- autoload/SpaceVim/plugins/runner.vim | 16 ++++++++++++++-- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/autoload/SpaceVim.vim b/autoload/SpaceVim.vim index 11ef17a47..987d98d49 100644 --- a/autoload/SpaceVim.vim +++ b/autoload/SpaceVim.vim @@ -348,7 +348,7 @@ let g:spacevim_enable_vimfiler_gitstatus = 0 " Enable/Disable filetypeicon colum in vimfiler buffer, default is 0. let g:spacevim_enable_vimfiler_filetypeicon = 0 let g:spacevim_smartcloseignorewin = ['__Tagbar__' , 'vimfiler:default'] -let g:spacevim_smartcloseignoreft = ['help', 'tagbar', 'vimfiler'] +let g:spacevim_smartcloseignoreft = ['help', 'tagbar', 'vimfiler', 'SpaceVimRunner'] let g:spacevim_altmoveignoreft = ['Tagbar' , 'vimfiler'] let g:spacevim_enable_javacomplete2_py = 0 let g:spacevim_src_root = 'E:\sources\' diff --git a/autoload/SpaceVim/plugins/runner.vim b/autoload/SpaceVim/plugins/runner.vim index 708fa8b13..e5c5705bb 100644 --- a/autoload/SpaceVim/plugins/runner.vim +++ b/autoload/SpaceVim/plugins/runner.vim @@ -14,17 +14,22 @@ let s:runners = {} let s:bufnr = 0 function! s:open_win() abort + if s:bufnr != 0 && bufexists(s:bufnr) + exe 'bd ' . s:bufnr + endif botright split __runner__ let lines = &lines * 30 / 100 exe 'resize ' . lines setlocal buftype=nofile bufhidden=wipe nobuflisted nolist noswapfile nowrap cursorline nospell nonu norelativenumber set filetype=SpaceVimRunner + nnoremap q :bd let s:bufnr = bufnr('%') + wincmd p endfunction function! s:async_run(runner) abort - let cmd = printf(a:runner, bufname('#')) + let cmd = printf(a:runner, bufname('%')) call s:JOB.start(cmd,{ \ 'on_stdout' : function('s:on_stdout'), \ 'on_stderr' : function('s:on_stderr'), @@ -34,7 +39,7 @@ endfunction function! s:update_statusline() abort - redrawstatus + redrawstatus! endfunction function! SpaceVim#plugins#runner#reg_runner(ft, runner) @@ -65,6 +70,8 @@ endfunction function! s:on_stderr(job_id, data, event) abort let s:status.has_errors = 1 + call s:BUFFER.buf_set_lines(s:bufnr, s:lines , s:lines + 1, 0, a:data) + let s:lines += len(a:data) call s:update_statusline() endfunction @@ -75,10 +82,15 @@ function! s:on_exit(job_id, data, event) abort endfunction +function! s:debug_info() abort + return [] +endfunction + function! SpaceVim#plugins#runner#status() if s:status.is_running == 1 elseif s:status.is_exit == 1 return 'exit code : ' . s:status.exit_code endif + return '' endfunction From c5aee19c89335fd7ff240b58ab9262ba18bcef07 Mon Sep 17 00:00:00 2001 From: wsdjeg Date: Thu, 5 Oct 2017 21:26:24 +0800 Subject: [PATCH 4/5] Fix lint --- autoload/SpaceVim/plugins/runner.vim | 29 ++++++++++++++++++++++------ 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/autoload/SpaceVim/plugins/runner.vim b/autoload/SpaceVim/plugins/runner.vim index e5c5705bb..c9d90eeca 100644 --- a/autoload/SpaceVim/plugins/runner.vim +++ b/autoload/SpaceVim/plugins/runner.vim @@ -8,6 +8,7 @@ let s:JOB = SpaceVim#api#import('job') let s:BUFFER = SpaceVim#api#import('vim#buffer') +let s:STRING = SpaceVim#api#import('data#string') let s:runners = {} @@ -22,7 +23,7 @@ function! s:open_win() abort exe 'resize ' . lines setlocal buftype=nofile bufhidden=wipe nobuflisted nolist noswapfile nowrap cursorline nospell nonu norelativenumber set filetype=SpaceVimRunner - nnoremap q :bd + nnoremap q :call SpaceVim#plugins#runner#close() let s:bufnr = bufnr('%') wincmd p endfunction @@ -30,7 +31,8 @@ endfunction function! s:async_run(runner) abort let cmd = printf(a:runner, bufname('%')) - call s:JOB.start(cmd,{ + let s:start_time = reltime() + let s:job_id = s:JOB.start(cmd,{ \ 'on_stdout' : function('s:on_stdout'), \ 'on_stderr' : function('s:on_stderr'), \ 'on_exit' : function('s:on_exit'), @@ -42,11 +44,11 @@ function! s:update_statusline() abort redrawstatus! endfunction -function! SpaceVim#plugins#runner#reg_runner(ft, runner) +function! SpaceVim#plugins#runner#reg_runner(ft, runner) abort let s:runners[a:ft] = a:runner endfunction -function! SpaceVim#plugins#runner#open() +function! SpaceVim#plugins#runner#open() abort let s:lines = 0 let s:status = { \ 'is_running' : 0, @@ -62,6 +64,9 @@ function! SpaceVim#plugins#runner#open() endif endfunction +" @vimlint(EVL103, 1, a:job_id) +" @vimlint(EVL103, 1, a:data) +" @vimlint(EVL103, 1, a:event) function! s:on_stdout(job_id, data, event) abort call s:BUFFER.buf_set_lines(s:bufnr, s:lines , s:lines + 1, 0, a:data) let s:lines += len(a:data) @@ -76,21 +81,33 @@ function! s:on_stderr(job_id, data, event) abort endfunction function! s:on_exit(job_id, data, event) abort + let s:end_time = reltime(s:start_time) let s:status.is_exit = 1 let s:status.exit_code = a:data call s:update_statusline() endfunction +" @vimlint(EVL103, 0, a:job_id) +" @vimlint(EVL103, 0, a:data) +" @vimlint(EVL103, 0, a:event) function! s:debug_info() abort return [] endfunction -function! SpaceVim#plugins#runner#status() +function! SpaceVim#plugins#runner#status() abort if s:status.is_running == 1 elseif s:status.is_exit == 1 - return 'exit code : ' . s:status.exit_code + return 'exit code : ' . s:status.exit_code + \ . ' time: ' . s:STRING.trim(reltimestr(s:end_time)) endif return '' endfunction + +function! SpaceVim#plugins#runner#close() abort + if s:status.is_exit == 0 + call s:JOB.close(s:job_id) + endif + exe 'bd' s:bufnr +endfunction From 44ebec2fe85d592ccee090d55ec54905e4af49f8 Mon Sep 17 00:00:00 2001 From: wsdjeg Date: Thu, 5 Oct 2017 22:27:10 +0800 Subject: [PATCH 5/5] Add test for buf_set_lines --- autoload/SpaceVim/api/vim/buffer.vim | 21 +++++++++++++++++++++ test/api/vim/buffer.vader | 10 +++++++++- 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/autoload/SpaceVim/api/vim/buffer.vim b/autoload/SpaceVim/api/vim/buffer.vim index e70ff3a95..4dcfbe5e9 100644 --- a/autoload/SpaceVim/api/vim/buffer.vim +++ b/autoload/SpaceVim/api/vim/buffer.vim @@ -56,6 +56,27 @@ endfunction function! s:self.buf_set_lines(buffer, start, end, strict_indexing, replacement) abort if exists('*nvim_buf_set_lines') call nvim_buf_set_lines(a:buffer, a:start, a:end, a:strict_indexing, a:replacement) + elseif has('python') + py import vim + py import string + if bufexists(a:buffer) + py bufnr = string.atoi(vim.eval("a:buffer")) + py start_line = string.atoi(vim.eval("a:start")) + py end_line = string.atoi(vim.eval("a:end")) + py lines = vim.eval("a:replacement") + py vim.buffers[bufnr][start_line:end_line] = lines + endif + elseif has('python3') + py3 import vim + py3 import string + if bufexists(a:buffer) + py3 bufnr = string.atoi(vim.eval("a:buffer")) + py3 start_line = string.atoi(vim.eval("a:start")) + py3 end_line = string.atoi(vim.eval("a:end")) + py3 lines = vim.eval("a:replacement") + py3 vim.buffers[bufnr][start_line:end_line] = lines + endif + else endif endfunction diff --git a/test/api/vim/buffer.vader b/test/api/vim/buffer.vader index c60f19aa2..5bd1cc405 100644 --- a/test/api/vim/buffer.vader +++ b/test/api/vim/buffer.vader @@ -1,6 +1,14 @@ -Execute ( SpaceVim api: vim#buffer ): +Execute ( SpaceVim api: vim#buffer open ): new let buffer = SpaceVim#api#import('vim#buffer') call buffer.open({'bufname':'foo', 'cmd' : 'setl buftype=nofile bufhidden=wipe'}) AssertEqual bufname('%'), 'foo' AssertEqual &buftype, 'nofile' + +Execute ( SpaceVim api: vim#buffer buf_set_lines): + new + let buffer = SpaceVim#api#import('vim#buffer') + let nr = bufnr('%') + new + call buffer.buf_set_lines(nr, 0, 1, 0, ['line 1', 'line 2', 'line 3']) + AssertEqual getbufline(nr, 1, '$'), ['line 1', 'line 2', 'line 3']