mirror of
https://github.com/SpaceVim/SpaceVim.git
synced 2025-02-09 09:40:04 +08:00
Improve file head support (#3034)
This commit is contained in:
parent
af1b4d1f29
commit
a19a5db8f0
@ -46,6 +46,29 @@ function! s:self.jumps() abort
|
|||||||
return result
|
return result
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
|
function! s:self.parse_string(line) abort
|
||||||
|
let expr = '`[^`]*`'
|
||||||
|
let i = 0
|
||||||
|
let line = []
|
||||||
|
while i < strlen(a:line) || i != -1
|
||||||
|
let [rst, m, n] = matchstrpos(a:line, expr, i)
|
||||||
|
if m == -1
|
||||||
|
call add(line, a:line[i:-1])
|
||||||
|
break
|
||||||
|
else
|
||||||
|
call add(line, a:line[i:m-1])
|
||||||
|
try
|
||||||
|
let rst = eval(rst[1:-2])
|
||||||
|
catch
|
||||||
|
let rst = ''
|
||||||
|
endtry
|
||||||
|
call add(line, rst)
|
||||||
|
endif
|
||||||
|
let i = n
|
||||||
|
endwhile
|
||||||
|
return join(line, '')
|
||||||
|
endfunction
|
||||||
|
|
||||||
function! SpaceVim#api#vim#get() abort
|
function! SpaceVim#api#vim#get() abort
|
||||||
return deepcopy(s:self)
|
return deepcopy(s:self)
|
||||||
endfunction
|
endfunction
|
||||||
|
@ -11,6 +11,7 @@ scriptencoding utf-8
|
|||||||
let s:PASSWORD = SpaceVim#api#import('password')
|
let s:PASSWORD = SpaceVim#api#import('password')
|
||||||
let s:NUMBER = SpaceVim#api#import('data#number')
|
let s:NUMBER = SpaceVim#api#import('data#number')
|
||||||
let s:LIST = SpaceVim#api#import('data#list')
|
let s:LIST = SpaceVim#api#import('data#list')
|
||||||
|
let s:VIM = SpaceVim#api#import('vim')
|
||||||
|
|
||||||
function! SpaceVim#layers#edit#plugins() abort
|
function! SpaceVim#layers#edit#plugins() abort
|
||||||
let plugins = [
|
let plugins = [
|
||||||
@ -422,35 +423,35 @@ function! s:delete_extra_space() abort
|
|||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
function! s:set_justification_to(align) abort
|
function! s:set_justification_to(align) abort
|
||||||
let l:startlinenr = line("'{")
|
let l:startlinenr = line("'{")
|
||||||
let l:endlinenr = line("'}")
|
let l:endlinenr = line("'}")
|
||||||
if getline(l:startlinenr) ==# ''
|
if getline(l:startlinenr) ==# ''
|
||||||
let l:startlinenr += 1
|
let l:startlinenr += 1
|
||||||
|
endif
|
||||||
|
if getline(l:endlinenr) ==# ''
|
||||||
|
let l:endlinenr -= 1
|
||||||
|
endif
|
||||||
|
let l:lineList = map(getline(l:startlinenr, l:endlinenr), 'trim(v:val)')
|
||||||
|
let l:maxlength = 0
|
||||||
|
for l:line in l:lineList
|
||||||
|
let l:length = strdisplaywidth(l:line)
|
||||||
|
if l:length > l:maxlength
|
||||||
|
let l:maxlength = l:length
|
||||||
endif
|
endif
|
||||||
if getline(l:endlinenr) ==# ''
|
endfor
|
||||||
let l:endlinenr -= 1
|
|
||||||
endif
|
|
||||||
let l:lineList = map(getline(l:startlinenr, l:endlinenr), 'trim(v:val)')
|
|
||||||
let l:maxlength = 0
|
|
||||||
for l:line in l:lineList
|
|
||||||
let l:length = strdisplaywidth(l:line)
|
|
||||||
if l:length > l:maxlength
|
|
||||||
let l:maxlength = l:length
|
|
||||||
endif
|
|
||||||
endfor
|
|
||||||
|
|
||||||
if a:align ==# 'left'
|
if a:align ==# 'left'
|
||||||
execute l:startlinenr . ',' . l:endlinenr . ":left\<cr>"
|
execute l:startlinenr . ',' . l:endlinenr . ":left\<cr>"
|
||||||
elseif a:align ==# 'center'
|
elseif a:align ==# 'center'
|
||||||
execute l:startlinenr . ',' . l:endlinenr . ':center ' . l:maxlength . "\<cr>"
|
execute l:startlinenr . ',' . l:endlinenr . ':center ' . l:maxlength . "\<cr>"
|
||||||
elseif a:align ==# 'right'
|
elseif a:align ==# 'right'
|
||||||
execute l:startlinenr . ',' . l:endlinenr . ':right ' . l:maxlength . "\<cr>"
|
execute l:startlinenr . ',' . l:endlinenr . ':right ' . l:maxlength . "\<cr>"
|
||||||
endif
|
endif
|
||||||
|
|
||||||
unlet l:startlinenr
|
unlet l:startlinenr
|
||||||
unlet l:endlinenr
|
unlet l:endlinenr
|
||||||
unlet l:lineList
|
unlet l:lineList
|
||||||
unlet l:maxlength
|
unlet l:maxlength
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
let s:local_lorem_ipsum = [
|
let s:local_lorem_ipsum = [
|
||||||
@ -614,11 +615,15 @@ let s:ft_head_tp = {}
|
|||||||
function! s:add_buffer_head() abort
|
function! s:add_buffer_head() abort
|
||||||
if has_key(s:ft_head_tp, &ft) && getline(1) == '' && line('$') == 1
|
if has_key(s:ft_head_tp, &ft) && getline(1) == '' && line('$') == 1
|
||||||
let head = s:ft_head_tp[&ft]
|
let head = s:ft_head_tp[&ft]
|
||||||
call setline(1, head)
|
call setline(1, map(head, 's:parse(v:val)'))
|
||||||
call cursor(len(head), 0)
|
call cursor(len(head), 0)
|
||||||
endif
|
endif
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
|
function! s:parse(line) abort
|
||||||
|
return s:VIM.parse_string(a:line)
|
||||||
|
endfunction
|
||||||
|
|
||||||
function! SpaceVim#layers#edit#add_ft_head_tamplate(ft, tamp) abort
|
function! SpaceVim#layers#edit#add_ft_head_tamplate(ft, tamp) abort
|
||||||
call extend(s:ft_head_tp, {a:ft : a:tamp})
|
call extend(s:ft_head_tp, {a:ft : a:tamp})
|
||||||
endfunction
|
endfunction
|
||||||
|
@ -23,12 +23,7 @@ function! SpaceVim#layers#lang#sh#config() abort
|
|||||||
let g:zsh_fold_enable = 1
|
let g:zsh_fold_enable = 1
|
||||||
" }}}
|
" }}}
|
||||||
|
|
||||||
call SpaceVim#layers#edit#add_ft_head_tamplate('sh',
|
call SpaceVim#layers#edit#add_ft_head_tamplate('sh', s:bash_file_head)
|
||||||
\ ['#!/usr/bin/env bash',
|
|
||||||
\ '',
|
|
||||||
\ ''
|
|
||||||
\ ]
|
|
||||||
\ )
|
|
||||||
call SpaceVim#layers#edit#add_ft_head_tamplate('zsh', [
|
call SpaceVim#layers#edit#add_ft_head_tamplate('zsh', [
|
||||||
\ '#!/usr/bin/env zsh',
|
\ '#!/usr/bin/env zsh',
|
||||||
\ '',
|
\ '',
|
||||||
@ -64,3 +59,13 @@ function! s:go_to_def() abort
|
|||||||
call SpaceVim#lsp#go_to_def()
|
call SpaceVim#lsp#go_to_def()
|
||||||
endif
|
endif
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
|
|
||||||
|
let s:bash_file_head = ['#!/usr/bin/env bash',
|
||||||
|
\ '',
|
||||||
|
\ ''
|
||||||
|
\ ]
|
||||||
|
|
||||||
|
function! SpaceVim#layers#lang#sh#set_variable(var) abort
|
||||||
|
let s:bash_file_head = get(a:var, 'bash-file-head', s:bash_file_head)
|
||||||
|
endfunction
|
||||||
|
5
test/api/vim.vader
Normal file
5
test/api/vim.vader
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
Execute ( SpaceVim api: vim ):
|
||||||
|
let &encoding = 'utf-8'
|
||||||
|
let vimapi = SpaceVim#api#import('vim')
|
||||||
|
AssertEqual vimapi.parse_string('sss `strftime("%Y-%m-%d")` sss'), 'sss ' . strftime("%Y-%m-%d") . ' sss'
|
||||||
|
unlet vimapi
|
Loading…
Reference in New Issue
Block a user