继续这个问题HTML cursor position
按ENTER键展开css时如何获得正确的光标位置。
评论部分是我尝试更新函数以便为css工作的失败。
function! Expander()
let line = getline(".")
let col = col(".")
let first = line[col-2]
let second = line[col-1]
let third = line[col
if first ==# ">"
if second ==# "<" && third ==# "/"
return "\<CR>\<C-o>==\<C-o>O"
else
return "\<CR>"
endif
"else if first ==# "{"
"if second ==# "}"
"return "\<CR>\<C-o>==\<C-o>O"
else
return "\<CR>"
endif
endfunction
我对有或没有这个功能的解决方案持开放态度。 emmet和sparkup等插件非常适合用缩写文本创建html,但不能解决这个简单的问题。
我希望光标在这里输入。
#SomeID {
|
}
答案 0 :(得分:0)
这是我在链接答案中提出的功能的扩展版本。它会尝试展开()
,{}
,[]
和<tag></tag>
。
" the main function
function! SmartEnter()
" beware of the cmdline window
if &buftype == "nofile"
return "\<CR>"
endif
" get the character on the left of the cursor
let previous = getline(".")[col(".")-2]
" get the character on the right of the cursor
let next = getline(".")[col(".")-1]
" do the right thing
if previous ==# "{"
return PairExpander(previous, "}", next)
elseif previous ==# "["
return PairExpander(previous, "]", next)
elseif previous ==# "("
return PairExpander(previous, ")", next)
elseif previous ==# ">"
return TagExpander(next)
else
return "\<CR>"
endif
endfunction
" expands {}, (), []
function! PairExpander(left, right, next)
let pair_position = searchpairpos(a:left, "", a:right, "Wn")
if a:next !=# a:right && pair_position[0] == 0
return "\<CR>" . a:right . "\<C-o>==\<C-o>O"
elseif a:next !=# a:right && pair_position[0] != 0 && indent(pair_position[0]) != indent(".")
return "\<CR>" . a:right . "\<C-o>==\<C-o>O"
elseif a:next ==# a:right
return "\<CR>\<C-o>==\<C-o>O"
else
return "\<CR>"
endif
endfunction
" expands <tag></tag>
function! TagExpander(next)
if a:next ==# "<" && getline(".")[col(".")] ==# "/"
if getline(".")[searchpos("<", "bnW")[1]] ==# "/" || getline(".")[searchpos("<", "bnW")[1]] !=# getline(".")[col(".") + 1]
return "\<CR>"
else
return "\<CR>\<C-o>==\<C-o>O"
endif
else
return "\<CR>"
endif
endfunction