LucHermitte / lh-brackets

LH's bracketing system for vim
Other
51 stars 2 forks source link

Using lh-brackets with Go #23

Closed renkam closed 4 years ago

renkam commented 4 years ago

Is your feature request related to a problem? Please describe. I am annoyed with how vim works with brackets when programming in Go. I think that lh-bracket might be in help here but it looks like I don't understand how to configure it. Main problem is when I try to create a new struct. When I type opening bracket { and hit I get what I've expected. Unfortunately after typing all member I've left with unwanted mark after closing bracket }.

Describe the solution you'd like I would like that when I type all the members and then hit <cr>} I have cursor after closing bracket } and no mark after it.

Describe alternatives you've considered I am able to have expected result If I hit <C-J>, but this is counter intuitive for me.

Additional context Situation I am describing is when struct looks like below and cursor is in marked position <cursor>

type struct foo {
    member int<cursor>
}

Now hitting <cr> and } (or even shorter only }) I would like to have cursor after closing } bracket and no placeholder mark.

LucHermitte commented 4 years ago

The marker/placeholder can be completely removed by having

:let g:usemarks = 0

in your .vimrc for instance.

Regarding the enhancement for having } jump after the closing brackets on the next line, it will need more tweaking though :Bracket -close option. I've just added this feature to C and C++.

As I'm not writing any Go, I'm not sure what specific mappings would make sense.

The idea is to provide a go ftplugin that'll closely look like after/ftplugin/c/c_brackets.vim. The function lh#cpp#brackets#square_close() could be adapted for Go context if having semicolon after } really doesn't make an sense and you don't wish to support this feature.

Let me know if this solves your problem. Also, if you have other specific requirements for Go, I can provide an official ftplugin for this language.

renkam commented 4 years ago

Thanks for some suggestions. I'll try to look into C/C++ ftplugin and hopefully I'll came up with something useful.

Regarding marks, I think that completely disabling marks is not a good idea. They are useful in other situations. I am probably looking for a smart way that plugin could detect a situation that I want to close struct, that typically I do by pressing <cr>} and do <C-J> instead. Is there a way to detect that a mark is placed? Then it might be possible to write a imap for <cr>} that do <C-J> if mark is placed and do a normal}` otherwise.

LucHermitte commented 4 years ago

Having a mapping on <CR>} is a bad idea. I did similar things in the past and I've abandoned this way since then.

The problem is that every time you hit <cr>, Vim will be waiting a little while to see whether you're hitting } or something else. That's why I don't have a mapping on {<cr>, but two mappings, with <CR> one that analyses it context.

The change I've committed on Friday is such that now } analyses whether what follows is }\(mark\)\? or \_s\+}\(mark\)\?. In both cases the cursor will jump after the } character and remove any following mark. The difference is that the first situation will permit the sequence to be re-doable. When multiple line are involved (second case) re-doability becomes more complex, and it cannot be achieved without relying on another plugin.

renkam commented 4 years ago

I totally forgot about blocking/waiting for next input on multiple character mappings. This would be annoying.

If I understand correctly I need to write my own version of lh#cpp#brackets#curly_close() for golang. Is that right?

LucHermitte commented 4 years ago

Not necessarily. In a first approximation, you could start with the following in a goland ftplugin

  :Brackets { } -visual=0 -insert=1
        \ -clos=function('lh#cpp#brackets#curly_close')

" Note that you may also want to have line-wise surrounding with <localleader>{

  :Brackets { } -visual=1 -insert=0 -nl -trigger=<localleader>{

The catch, is that it will also work if the closing curly bracket is followed by a semi-colon, which, IIRC, should never happen in Go. If this is not an issue, no need to duplicate and adapt lh#cpp#brackets#curly_close.

renkam commented 4 years ago

Ok, I can't get it working. Looks like my vimscript skills are not enough to get it done right. First I need to mention that fot C++ files everything working as expected.

Then I've tried to issue by hand

:Brackets { } -visual=0 -insert=1 -clos=function('lh#cpp#brackets#curly_close')

but get error after pressing {

Error detected while processing function lh#brackets#opener: line   24:
E704: Funcref variable name must start with a capital: close

Then to workaround this, I've copied from plugin lh-brackets/autoload/lh/cpp/brackets.vim file into ~/.vim/autoload/lh/go and replaced all the #cpp# strings to #go#. Then added two lines you suggested (with lh#go#brackets#curly_close for -clos param). This didn't complain when triggered by hitting {, but also do nothing. It just inserts another { at cursor.

I've even tried to copy this customized brackets file into plugin's autoload dir under go, but this didn't help either.

LucHermitte commented 4 years ago

That's odd the issue is triggered only in that case. I've just pushed a work around Vim required naming policy for funcrefs. Let me know if it solves your issue.

renkam commented 4 years ago

Now after calling :Brackets { } -visual=0 -insert=1 -clos=function('lh#cpp#brackets#curly_close') I still get error.

Error detected while processing function lh#brackets#opener:                                                                                                                                                                                   
line   72:                                                                                                                                                                                                                                     
E729: using Funcref as a String                                                                                                                                                                                                                
Error detected while processing function lh#brackets#opener:                                                                                                                                                                                   
line   72:                                                                                                                                                                                                                                     
E116: Invalid arguments for function call                                                                                                                                                                                                      
Error detected while processing function lh#brackets#opener:                                                                                                                                                                                   
line   72:                                                                                                                                                                                                                                     
E15: Invalid expression: call('lh#map#insert_seq',[a:trigger, l:Open.'!cursorhere!'.l:Close.'!mark!']+a:000)
LucHermitte commented 4 years ago

Hum. I see the mistake I've made. I'll see to implement a proper solution.

renkam commented 4 years ago

Looks like last commit fixes the error. Thank you for your great support!

LucHermitte commented 4 years ago

You're welcome. And thank you for your inputs.