IDEA-27091 When finishing method lookup item with '(', put caret after this '(' even if method has no parameters

IDEA-51789 When "Insert pair bracket" on Editor->Smart keys page is off, IDEA still inserts both parentheses on autocomplete lookup, but overtype doesn't work
This commit is contained in:
peter
2010-03-09 12:54:15 +00:00
parent c4fdc0f5af
commit eed9138dbd
8 changed files with 72 additions and 16 deletions
@@ -53,24 +53,26 @@ public class PsiMethodInsertHandler implements InsertHandler<LookupItem<PsiMetho
final TailType tailType = getTailType(item, context);
final Document document = editor.getDocument();
final PsiFile file = context.getFile();
final int offset = editor.getCaretModel().getOffset();
context.setAddCompletionChar(false);
final LookupElement[] allItems = context.getElements();
boolean signatureSelected = allItems.length > 1 || item.getUserData(LookupItem.FORCE_SHOW_SIGNATURE_ATTR) != null;
final boolean overloadsMatter = allItems.length == 1 && item.getUserData(LookupItem.FORCE_SHOW_SIGNATURE_ATTR) == null;
int offset = editor.getCaretModel().getOffset();
final boolean hasParams = MethodParenthesesHandler.hasParams(item, allItems, overloadsMatter, myMethod);
final boolean needLeftParenth = isToInsertParenth(file.findElementAt(context.getStartOffset()));
final boolean hasParams = MethodParenthesesHandler.hasParams(item, allItems, !signatureSelected, myMethod);
final boolean needRightParenth = shouldInsertRParenth(completionChar, tailType, hasParams);
if (needLeftParenth) {
final CodeStyleSettings styleSettings = CodeStyleSettingsManager.getSettings(context.getProject());
new MethodParenthesesHandler(myMethod, !signatureSelected,
new MethodParenthesesHandler(myMethod, overloadsMatter,
styleSettings.SPACE_BEFORE_METHOD_CALL_PARENTHESES,
styleSettings.SPACE_WITHIN_METHOD_CALL_PARENTHESES && hasParams,
shouldInsertRightParenthesis(tailType)
needRightParenth
).handleInsert(context, item);
}
insertExplicitTypeParams(item, document, offset, file);
final PsiType type = myMethod.getReturnType();
@@ -86,14 +88,25 @@ public class PsiMethodInsertHandler implements InsertHandler<LookupItem<PsiMetho
if (needLeftParenth && hasParams) {
// Invoke parameters popup
AutoPopupController.getInstance(myMethod.getProject()).autoPopupParameterInfo(editor, signatureSelected ? myMethod : null);
AutoPopupController.getInstance(myMethod.getProject()).autoPopupParameterInfo(editor, overloadsMatter ? null : myMethod);
}
if (tailType == TailType.SMART_COMPLETION || needLeftParenth && needRightParenth) {
tailType.processTail(editor, context.getTailOffset());
}
tailType.processTail(editor, context.getTailOffset());
editor.getScrollingModel().scrollToCaret(ScrollType.RELATIVE);
}
protected static boolean shouldInsertRightParenthesis(TailType tailType) {
return tailType != TailType.SMART_COMPLETION;
private boolean shouldInsertRParenth(char completionChar, TailType tailType, boolean hasParams) {
if (tailType == TailType.SMART_COMPLETION) {
return false;
}
if (completionChar == '(' && !hasParams) {
//it's highly probable that the user will type ')' next and it may not be overwritten if the flag is off
return CodeInsightSettings.getInstance().AUTOINSERT_PAIR_BRACKET;
}
return true;
}
@NotNull
@@ -0,0 +1,8 @@
class MyClass {
void foo() {}
{
fo<caret>
}
}
@@ -0,0 +1,8 @@
class MyClass {
void foo() {}
{
foo(<caret>
}
}
@@ -0,0 +1,8 @@
class MyClass {
void foo() {}
{
foo();<caret>
}
}
@@ -3,6 +3,6 @@ class MyClass {
void foo() {}
{
foo();<caret>
foo(<caret>);
}
}
@@ -308,6 +308,25 @@ public class NormalCompletionTest extends LightCompletionTestCase {
checkResultByFile("/codeInsight/completion/normal/MethodWithLeftParTailType2_after.java");
}
public void testMethodWithLeftParTailTypeNoPairBrace() throws Exception {
final boolean old = CodeInsightSettings.getInstance().AUTOINSERT_PAIR_BRACKET;
CodeInsightSettings.getInstance().AUTOINSERT_PAIR_BRACKET = false;
try {
configureByFile("/codeInsight/completion/normal/" + getTestName(false) + ".java");
selectItem(myItems[0], '(');
checkResultByFile("/codeInsight/completion/normal/" + getTestName(false) + "_after.java");
//no tail type should work the normal way
configureByFile("/codeInsight/completion/normal/" + getTestName(false) + ".java");
selectItem(myItems[0]);
checkResultByFile("/codeInsight/completion/normal/" + getTestName(false) + "_after2.java");
}
finally {
CodeInsightSettings.getInstance().AUTOINSERT_PAIR_BRACKET = old;
}
}
public void testExcessSpaceInTypeCast() throws Throwable {
configureByFile("/codeInsight/completion/normal/" + getTestName(false) + ".java");
selectItem(myItems[0]);
@@ -72,9 +72,9 @@ public abstract class ParenthesesInsertHandler<T extends LookupElement> implemen
final Document document = editor.getDocument();
PsiElement element = findNextToken(context);
final boolean hasParams = placeCaretInsideParentheses(context, item);
final char completionChar = context.getCompletionChar();
final boolean putCaretInside = completionChar == '(' || placeCaretInsideParentheses(context, item);
if (completionChar == '(') {
context.setAddCompletionChar(false);
}
@@ -99,7 +99,7 @@ public abstract class ParenthesesInsertHandler<T extends LookupElement> implemen
if (isToken(last, ")")) {
int rparenthOffset = last.getTextRange().getStartOffset();
context.setTailOffset(rparenthOffset + 1);
if (!hasParams) {
if (!putCaretInside) {
for (int i = lparenthOffset + 1; i < rparenthOffset; i++) {
if (!Character.isWhitespace(document.getCharsSequence().charAt(i))) {
return;
@@ -132,7 +132,7 @@ public abstract class ParenthesesInsertHandler<T extends LookupElement> implemen
tailOffset = TailType.insertChar(editor, tailOffset, ' ');
}
document.insertString(tailOffset, ")");
editor.getCaretModel().moveToOffset(hasParams ? caret : context.getTailOffset());
editor.getCaretModel().moveToOffset(putCaretInside ? caret : context.getTailOffset());
}
@Nullable
@@ -3,4 +3,4 @@ class Foo {
def bar
}
new Foo().getBar()<caret>
new Foo().getBar(<caret>)