0

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:

  1. Look, if there's already a semicolon (currently doesn't matter at which position it is)
  2. Look if the current line has more than 4 characters
  3. 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: 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.

3
  • 1
    With gvim 8.1.691 for windows, without COC, abbreviation expansion works. Jan 28 at 2:21
  • 1
    PS: you can simplify (for speed) the computations (use stridx(), and col('$')). 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. :( Jan 28 at 2:27
  • I'll try my best to do that (and to create a pull request to this one: github.com/lfilho/cosco.vim) ;)
    – TornaxO7
    Jan 28 at 8:50
-1

Ok, so it looks like that this problem doesn't occur anymore. I'd like to mark this question as "solved" or "finished" with this "answer".

This site is temporarily in read only mode and not accepting new answers.

Not the answer you're looking for? Browse other questions tagged .