Can code completion append closing character automatically?

49 views
Skip to first unread message

magict4

unread,
Apr 28, 2012, 7:38:43 AM4/28/12
to eclim-user
Hi,

Eclim is a great plugin for Java developers.

However, I have a problem with code completion function.

e.g

System.out.prin <C-X><C-X> the result is System.out.print(|

but I wish eclim can append closing character as well, and move cursor
between parentheses, like

System.out.print(|)

I have used inoremap ( ()<LEFT> and a plugin called auto-close. If i
just type '(' , they work well. But they cannot work with code
completion function.

My English is poor, wish you understand what I mean. ^_^

Eric Van Dewoestine

unread,
Apr 29, 2012, 1:43:35 PM4/29/12
to eclim...@googlegroups.com
Unfortunately vim's code completion doesn't provide the hooks to
really accomplish this. All I can do is feed vim the potential
completion strings and from there I have no control over cursor
placement nor are there any hooks to be notified when the user selects
a completion.

While I agree that auto adding the closing paren would be nice, I just
can't think of a reliable way to accomplish it with vim' current code
completion functionality.

--
eric

John Reiter

unread,
Apr 30, 2012, 10:54:46 AM4/30/12
to eclim...@googlegroups.com
I don't think this is a problem with eclim specifically, but on the
topic of code completion... does anyone else have any issues with
slowness scrolling through the completion results? The completion
options always appear very quickly for me, but actually scrolling
through them is always very slow.

John
> --
> You received this message because you are subscribed to the Google Groups "eclim-user" group.
> To post to this group, send email to eclim...@googlegroups.com.
> To unsubscribe from this group, send email to eclim-user+...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/eclim-user?hl=en.
>

Eric Van Dewoestine

unread,
May 1, 2012, 10:55:01 PM5/1/12
to eclim...@googlegroups.com
On 2012-04-30 10:54:46, John Reiter wrote:
> I don't think this is a problem with eclim specifically, but on the
> topic of code completion... does anyone else have any issues with
> slowness scrolling through the completion results? The completion
> options always appear very quickly for me, but actually scrolling
> through them is always very slow.
>
> John

I run into laggy scrolling here and there but I've never taken the
time to see exactly what was causing it... until today ;)

I first made sure that I could reliably reproduce laggy scrolling with
a simple completion function completely outside the scope of eclim,
that just returns a large list of static completions. I then went
through a lot of commenting out settings and such until I narrowed the
issue down to the following line in my ~/.vim/ftplugin/java/java.vim
file:

setlocal foldmethod=syntax

Without that line scrolling through the completions is now very smooth
for me. I'm not exactly sure what that triggers under the covers
(probably recalculating folds in the file for every text change, and
hence every time you scroll up/down the completion list), but my first
hint that this was syntax related was noticing that the scrolling
became very smooth if I turned off syntax highlighting altogether
(:syntax off).

You can check if you have this particular option set by running:

:verbose set foldmethod?

That will tell you what that option is set to, and if set in one of
your vim files, it will also tell you which file set it. If you
haven't set that option or it's set to something other than 'syntax',
you might want to play with commenting out any syntax related options
you may have set until you find the culprit.

Let me know what you find or if you need help narrowing the issue
down.

In the future I'd also prefer if you created a new email thread for
unrelated issues... it makes it easier to find things later based on
the email subject.

--
eric

John Reiter

unread,
May 3, 2012, 9:59:02 AM5/3/12
to eclim...@googlegroups.com
It looks like it was folding for me as well. With foldmethod set to
manual, my scrolling is nice and smooth. Thanks Eric!

> In the future I'd also prefer if you created a new email thread for
> unrelated issues... it makes it easier to find things later based on
> the email subject.

Oops, sorry about that. I'll be sure to create new threads in the future.

John

magict4

unread,
May 27, 2012, 1:27:43 AM5/27/12
to eclim-user
I figure out a "stupid" method to implement this function, it is not
elegant, but really works.

put this in .vimrc, when you use code completion and enter 'a', it
automatically appends the closing character and you can continue
typing.
You can replace a with b,c,d,e...z.

inoremap <expr> a strpart(getline('.'), col('.')-2, 1) == "(" &&
strpart(getline('.'),col('.')-1,1) != ")" ? "a)\<Left>" : "a"

if you don't want to type any param, just want to finish the line with
';' try this

inoremap <expr> ; strpart(getline('.'), col('^'), 3) == "for" ? ";" :
"<END>;"

On Apr 30, 1:43 am, Eric Van Dewoestine <ervan...@gmail.com> wrote:
> On 2012-04-28 04:38:43, magict4 wrote:
>
>
>
>
>
>
>
>
>
> > Hi,
>
> > Eclim is a great plugin for Java developers.
>
> > However, I have a problem withcodecompletionfunction.
>
> > e.g
>
> > System.out.prin <C-X><C-X> the result is System.out.print(|
>
> > but I wish eclim can append closing character as well, and move cursor
> > between parentheses, like
>
> > System.out.print(|)
>
> > I have used inoremap ( ()<LEFT> and a plugin called auto-close. If i
> > just type '(' , they work well. But they cannot work withcode
> >completionfunction.
>
> > My English is poor, wish you understand what I mean. ^_^
>
> Unfortunately vim'scodecompletiondoesn't provide the hooks to
> really accomplish this. All I can do is feed vim the potentialcompletionstrings and from there I have no control over cursor

Eric Van Dewoestine

unread,
May 28, 2012, 7:18:40 PM5/28/12
to eclim...@googlegroups.com
On 2012-05-26 22:27:43, magict4 wrote:
> I figure out a "stupid" method to implement this function, it is not
> elegant, but really works.
>
> put this in .vimrc, when you use code completion and enter 'a', it
> automatically appends the closing character and you can continue
> typing.
> You can replace a with b,c,d,e...z.
>
> inoremap <expr> a strpart(getline('.'), col('.')-2, 1) == "(" &&
> strpart(getline('.'),col('.')-1,1) != ")" ? "a)\<Left>" : "a"
>
> if you don't want to type any param, just want to finish the line with
> ';' try this
>
> inoremap <expr> ; strpart(getline('.'), col('^'), 3) == "for" ? ";" :
> "<END>;"

I like this semicolon mapping, that is handy! I did have to tweak it
to make it compatible with my matchem plugin (probably have the same
issue with delimitMate as well), and I used regex over strpart for my
own preference:

inoremap <expr> ; <SID>EndSemicolon()
function s:EndSemicolon()
if getline('.') =~ '^\s*for\>'
return ';'
endif
call feedkeys("\<END>")
call feedkeys(';', 'n')
return ''
endfunction

Thanks for sharing! If you don't mind I might actually bundle the
above with my matchem plugin since I think it compliments it well.

>
> On Apr 30, 1:43�am, Eric Van Dewoestine <ervan...@gmail.com> wrote:
> > On 2012-04-28 04:38:43, magict4 wrote:
> >
> >
> >
> >
> >
> >
> >
> >
> >
> > > Hi,
> >
> > > Eclim is a great plugin for Java developers.
> >
> > > However, I have a problem withcodecompletionfunction.
> >
> > > e.g
> >
> > > System.out.prin <C-X><C-X> the result is System.out.print(|
> >
> > > but I wish eclim can append closing character as well, and move cursor
> > > between parentheses, like
> >
> > > System.out.print(|)
> >
> > > I have used inoremap ( ()<LEFT> and a plugin called auto-close. If i
> > > just type '(' , they work well. But they cannot work withcode
> > >completionfunction.
> >
> > > My English is poor, wish you understand what I mean. ^_^
> >
> > Unfortunately vim'scodecompletiondoesn't provide the hooks to
> > really accomplish this. All I can do is feed vim the potentialcompletionstrings and from there I have no control over cursor
> > placement nor are there any hooks to be notified when the user selects
> > acompletion.
> >
> > While I agree that auto adding the closing paren would be nice, I just
> > can't think of a reliable way to accomplish it with vim' currentcodecompletionfunctionality.
> >
> > --
> > eric
>
> --
> You received this message because you are subscribed to the Google Groups "eclim-user" group.
> To post to this group, send email to eclim...@googlegroups.com.
> To unsubscribe from this group, send email to eclim-user+...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/eclim-user?hl=en.
>

--
eric

magict4

unread,
May 29, 2012, 10:40:37 AM5/29/12
to eclim-user
OK!
Reply all
Reply to author
Forward
0 new messages