How to stop Eclipse from auto-adding parentheses?

2020-05-20 07:38发布

If I for example type "Integer." and then hit CTRL+SPACE, and now choose "valueOf" from the list, Eclipse always adds "()" after the function name. That is really annoying to me because often times I have an already existing statement and want to wrap a function around it.

Example: I want to change

String x = "hello world";

to

String x = StringManipulator.uppercase("hello world");

If I write

String x = StringManipulator."hello world";

and hit CTRL+SPACE on the dot character, it inserts

String x = StringManipulator.uppercase()"hello world";

Is there any way to fix this behaviour? I don't need automatical parenthese-closing but just stopping Eclipse from autoinserting them would help me very much.

3条回答
我只想做你的唯一
2楼-- · 2020-05-20 08:03

I find this unrelated-seeming setting makes a big difference so I'll mention it.

The auto-completion feature in eclipse works somewhat differently if you choose "Completion overwrites" instead of "Completion inserts" on the "Content Assist" preferences.

(Let wrappingMethod() be the method that will be completed.)

wrappingMoriginalMethod()
With the cursor after the "gM" above, accepting the completion proposal gives:
wrappingMethod()

wrappingM(originalMethod()
With the cursor after the "gM" above, accepting the completion proposal gives:
wrappingMethod(originalMethod()
Notice I had to add the open paren to prevent the overwrite, but no extra parens.

wrappingM originalMethod()
With the cursor after the "gM" above, accepting the completion proposal gives:
wrappingMethod(parameterGuess) originalMethod()

I find this the most comfortable choice so far. I DO think that it would be nice if there were a "smart" parentheses insertion feature that would NOT insert the parens if completing right before an existing paren and since I am WISHING anyway... it would be even more awesome if it could check those existing parens to see if they have the args I am looking for or not and do something smart OR check the method after my insertion point to see if it should be an argument to my completion and just go ahead and DO that and put a closing paren after it and BEFORE the semi-colon if there was one there already.

Then again, if the IDE writes ALL of my code then I'll get bored pretty quickly.

查看更多
The star\"
3楼-- · 2020-05-20 08:03

Automatically close parentheses is for when you open a parentheses in this case you are autocompleting a method, in this case when the method you are calling has arguments it will autocreate the parentheses and the blocks where the arguments will be placed, there is no way to just autocomplete the method without the parentheses, but you could create a template to autocomplete:

  1. Go to Preferences: Java => Editor => Template
  2. New
  3. Write any relevant name
  4. In context choose Java statements
  5. Inside pattern write: StringManipulator.uppercase(${line_selection}${cursor});
  6. Ok and Ok again
  7. Now when you want to add the function just select the text to surround, press alt + shift + z, and choose the template you previously created

Hope it helps

查看更多
祖国的老花朵
4楼-- · 2020-05-20 08:13

Go to Preferences in eclipse -> into search box type: "parent" -> uncheck options that you don't like. That's what search is for in that mess (eclipse preferences).

查看更多
登录 后发表回答