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/api/vim/buffer.vim b/autoload/SpaceVim/api/vim/buffer.vim index 2642bdf79..4dcfbe5e9 100644 --- a/autoload/SpaceVim/api/vim/buffer.vim +++ b/autoload/SpaceVim/api/vim/buffer.vim @@ -12,46 +12,75 @@ 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) + 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 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 415bc569c..7737d8aad 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 0709a578c..3cf3634b7 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#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', + \ '# -*- 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..c9d90eeca --- /dev/null +++ b/autoload/SpaceVim/plugins/runner.vim @@ -0,0 +1,113 @@ +"============================================================================= +" 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:JOB = SpaceVim#api#import('job') +let s:BUFFER = SpaceVim#api#import('vim#buffer') +let s:STRING = SpaceVim#api#import('data#string') + +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 :call SpaceVim#plugins#runner#close() + let s:bufnr = bufnr('%') + wincmd p +endfunction + + +function! s:async_run(runner) abort + let cmd = printf(a:runner, bufname('%')) + 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'), + \ }) +endfunction + + +function! s:update_statusline() abort + redrawstatus! +endfunction + +function! SpaceVim#plugins#runner#reg_runner(ft, runner) abort + let s:runners[a:ft] = a:runner +endfunction + +function! SpaceVim#plugins#runner#open() abort + 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() + call s:async_run(runner) + call s:update_statusline() + 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) + call s:update_statusline() +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 + +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() abort + if s:status.is_running == 1 + elseif s:status.is_exit == 1 + 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 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']