skip to Main Content

In VSCode if you type Java code such as this …

new ArrayList(

… while using autocomplete you end up with:

new ArrayList<>((

Some kind of "feature" is automatically adding parentheses. However, this "feature" interferes with regular flow of typing and therefore is extremely annoying.


Edit:

Sorry, as I use autocomplete automatically now for decades, I forgot to mention this in the original text above. It is so naturally to use it, at some point you stop thinking about it. Perhaps that was the reason for the confusion surrounding this question.

However, you start thinking about autocomplete only if it does not behave as you might expect it to behave. That’s what this question is about.

I’ll try to explain the problem in more detail in the next sections.

The typical Java situation

As I mentioned above to reproduce this behavior you only need to type the name of class within a code block, e.g. something like this:

Key typed Text inserted
n n_
e ne_
w new_
<space> new _
A new A_
r new Ar_
r new Arr_
a new Arra_
y new Array_
L new ArrayL_
<tab> new ArrayList<>(_)
( new ArrayList<>((_)

The underscore marks the position of the cursor.

(Sorry, I’m not capable of fancy animations, so instead I use tables with each editing step to depict the situation here.)

As you can see above: In contrast to other autocomplete implementations for VSCode some special "feature" in the Red Hat Java extension adds parenthesis automatically.

The Python (and C) situation

Interestingly in other programming languages – e.g. Python – the autocomplete feature is implemented differently:

Key typed Text inserted
A A_
S AS_
T AST_
N ASTN_
<tab> ASTNode_
( ASTNode(_

So after autocomplete there is no additional parenthesis inserted. In contrast to the same code situation in Java. ArrayList and ASTNode are both classes. However, autocomplete behaves differently in both cases.

More insights into Java autocomplete

Consider these primitive class definitions:

public class Class1 {
    public Class1(int foo) {
    }
}

public class Class0 {
}

If you try to instantiate Class1 within a method autocomplete behaves like this:

Key typed Text inserted
n n_
e ne_
w new_
<space> new _
C new C_
<select-Class1-with-cursor> new C_
<tab> new Class1(_)

However if you try to instantiate Class0:

Key typed Text inserted
n n_
e ne_
w new_
<space> new _
C new C_
<select-Class0-with-cursor> new C_
<tab> new Class0()_

So the placement of the cursor is inconsistent: It depends on the kind of constructor(s) present in the class.

Even more insights into Java autocomplete

And it’s even more complicated. Consider this example:

public class Test
{

    public static void foo()
    {
    }

    public static void main(String[] args)
    {
        _
    }

}

The underscore marks the cursor position. Now if you’re at this position and type …

Key typed Text inserted
f f_
<tab> foo();_

In this situation autocomplete is behaving completely differently again and even adds a semicolon.

However, if you do the equivalent in C no parenthesis (and no semicolon) are added at all.

I stop here and do not analyze more programming languages for now as it would bring us to far away from the question.

The question

Autocomplete behaving differently in different situations (and differently across different languages) is not very helpful. So the question is:

Can this kind of autocomplete "feature" be configured somehow? (Here: For the Java extension.) Can this "feature" that is trying to add parenthesis be deactivated somehow? (Because to my experience this is the most common situation.)

(And of course: Without losing autocomplete all together. Just deactivating Java language support all together is not an option.)

I was not able to find anything in the settings. I was not able to find anything on Google (so far) – but maybe you have. Do you know a way how to modify (= streamline) this behavior? I’m interested in having a more consistent behavior across different programming languages. I know there are fine grained configurations in other IDEs so I guess there is a way to configure this in VSCode as well. But how?

2

Answers


  1. This works perfectly fine for me without any issues.

    enter image description here

    Please check your settings or restore all default settings. Also check if there are other extensions causing this issue.

    Login or Signup to reply.
  2. I don’t see what the problem is. This is perfectly useful behaviour for continuous typing.

    You’ve shown that the cursor will skip past the closing parenthesis for accepting a suggestion for calling a constructor when the class has no constructors that take arguments. Perfectly useful. No point really in landing the cursor in a place where nothing goes. Same for calling a function that has no overloads that take arguments.

    I don’t know how to disable this. I don’t think there is a way to disable it. If it did exist, it would probably have a setting ID starting with java.completion.

    I suggest that you stop fighting it and just embrace it. If you really want a setting to disable this, then raise a feature-request to the maintainers of the extension / the maintainers of the Eclipse JDT Language Server which that extension is built upon.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search