mirror of
https://github.com/SpaceVim/SpaceVim.git
synced 2025-02-02 22:20:06 +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
|
||||
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
|
||||
return deepcopy(s:self)
|
||||
endfunction
|
||||
|
@ -11,6 +11,7 @@ scriptencoding utf-8
|
||||
let s:PASSWORD = SpaceVim#api#import('password')
|
||||
let s:NUMBER = SpaceVim#api#import('data#number')
|
||||
let s:LIST = SpaceVim#api#import('data#list')
|
||||
let s:VIM = SpaceVim#api#import('vim')
|
||||
|
||||
function! SpaceVim#layers#edit#plugins() abort
|
||||
let plugins = [
|
||||
@ -422,35 +423,35 @@ function! s:delete_extra_space() abort
|
||||
endfunction
|
||||
|
||||
function! s:set_justification_to(align) abort
|
||||
let l:startlinenr = line("'{")
|
||||
let l:endlinenr = line("'}")
|
||||
if getline(l:startlinenr) ==# ''
|
||||
let l:startlinenr += 1
|
||||
let l:startlinenr = line("'{")
|
||||
let l:endlinenr = line("'}")
|
||||
if getline(l:startlinenr) ==# ''
|
||||
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
|
||||
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
|
||||
endfor
|
||||
endfor
|
||||
|
||||
if a:align ==# 'left'
|
||||
execute l:startlinenr . ',' . l:endlinenr . ":left\<cr>"
|
||||
elseif a:align ==# 'center'
|
||||
execute l:startlinenr . ',' . l:endlinenr . ':center ' . l:maxlength . "\<cr>"
|
||||
elseif a:align ==# 'right'
|
||||
execute l:startlinenr . ',' . l:endlinenr . ':right ' . l:maxlength . "\<cr>"
|
||||
endif
|
||||
if a:align ==# 'left'
|
||||
execute l:startlinenr . ',' . l:endlinenr . ":left\<cr>"
|
||||
elseif a:align ==# 'center'
|
||||
execute l:startlinenr . ',' . l:endlinenr . ':center ' . l:maxlength . "\<cr>"
|
||||
elseif a:align ==# 'right'
|
||||
execute l:startlinenr . ',' . l:endlinenr . ':right ' . l:maxlength . "\<cr>"
|
||||
endif
|
||||
|
||||
unlet l:startlinenr
|
||||
unlet l:endlinenr
|
||||
unlet l:lineList
|
||||
unlet l:maxlength
|
||||
unlet l:startlinenr
|
||||
unlet l:endlinenr
|
||||
unlet l:lineList
|
||||
unlet l:maxlength
|
||||
endfunction
|
||||
|
||||
let s:local_lorem_ipsum = [
|
||||
@ -614,11 +615,15 @@ let s:ft_head_tp = {}
|
||||
function! s:add_buffer_head() abort
|
||||
if has_key(s:ft_head_tp, &ft) && getline(1) == '' && line('$') == 1
|
||||
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)
|
||||
endif
|
||||
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
|
||||
call extend(s:ft_head_tp, {a:ft : a:tamp})
|
||||
endfunction
|
||||
|
@ -23,12 +23,7 @@ function! SpaceVim#layers#lang#sh#config() abort
|
||||
let g:zsh_fold_enable = 1
|
||||
" }}}
|
||||
|
||||
call SpaceVim#layers#edit#add_ft_head_tamplate('sh',
|
||||
\ ['#!/usr/bin/env bash',
|
||||
\ '',
|
||||
\ ''
|
||||
\ ]
|
||||
\ )
|
||||
call SpaceVim#layers#edit#add_ft_head_tamplate('sh', s:bash_file_head)
|
||||
call SpaceVim#layers#edit#add_ft_head_tamplate('zsh', [
|
||||
\ '#!/usr/bin/env zsh',
|
||||
\ '',
|
||||
@ -64,3 +59,13 @@ function! s:go_to_def() abort
|
||||
call SpaceVim#lsp#go_to_def()
|
||||
endif
|
||||
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