I'd like to add a semicolon automatically in my current line. So I have this in my init.vim
:
function Test()
if matchstr(getline(line('.')), ';') != ';' && strlen(getline(line('.'))) > 4
call setline(line('.'), substitute(getline(line('.')), '$', ';', 'e'))
"execute 's/;\?$/;/e'
endif
endfunction
autocmd TextChangedI <buffer> call Test()
To sum it up, what this function does:
- Look, if there's already a semicolon (currently doesn't matter at which position it is)
- Look if the current line has more than 4 characters
- It both conditions are true, add a semicolon to the line
Now I have a problem with this function: It "breaks" my tabstops in coc
and my abbrevations.
Here's a demonstration:
ihu
is my abbreviation for unsigned short
(i stands for integer):
iab <buffer> ihu unsigned short
Now as you can see the abbreviation works in the beginning but after the placement of the semicolon my abbreviations don't work anymore.
You can also see in the function that I've tried to use
execute 's/;\?$/;/e'
but this produces the same problem.
Summarize
Do you have an idea how I can add the semicolon without breaking my abbreviations (and tabstops of some snippet-plugins)?
Edit
Ok, I found out that if it sets the semicolon earlier than after 4 characters, than I'll be able to use my abbreviations as usually. But I'd be happy if someone knows an answer to that question.
stridx()
, andcol('$')
). But the condition will need to become much more complex to handle blocks, control-statements, multi-line expressions, one-liner lambdas used in std algorithms... I'm afraid it'll be extremely complex to have an automatically inserted;
in an ergonomic way. :(