IDEA-94209 Completion in parameter list at array parameter position should insert braces after new Type

This commit is contained in:
peter
2012-11-10 23:05:30 +01:00
parent d92ec6bf10
commit 3f3c212594
11 changed files with 26 additions and 17 deletions
@@ -72,7 +72,7 @@ public class JavaSmartCompletionContributor extends CompletionContributor {
private static final ElementExtractorFilter THROWABLES_FILTER = new ElementExtractorFilter(new AssignableFromFilter(CommonClassNames.JAVA_LANG_THROWABLE));
@NonNls private static final String EXCEPTION_TAG = "exception";
static final ElementPattern<PsiElement> AFTER_NEW =
public static final ElementPattern<PsiElement> AFTER_NEW =
psiElement().afterLeaf(
psiElement().withText(PsiKeyword.NEW).andNot(
psiElement().afterLeaf(
@@ -92,6 +92,7 @@ public class PsiTypeLookupItem extends LookupItem {
PsiElement position = context.getFile().findElementAt(context.getStartOffset());
assert position != null;
boolean afterNew = JavaSmartCompletionContributor.AFTER_NEW.accepts(position);
int genericsStart = context.getTailOffset();
context.getDocument().insertString(genericsStart, JavaCompletionUtil.escapeXmlIfNeeded(context, calcGenerics(position, context)));
JavaCompletionUtil.shortenReference(context.getFile(), genericsStart - 1);
@@ -100,10 +101,15 @@ public class PsiTypeLookupItem extends LookupItem {
String braces = StringUtil.repeat("[]", getBracketsCount());
Editor editor = context.getEditor();
if (!braces.isEmpty()) {
context.getDocument().insertString(tail, braces);
editor.getCaretModel().moveToOffset(tail + 1);
if (context.getCompletionChar() == '[') {
context.setAddCompletionChar(false);
if (LookupEvent.isSpecialCompletionChar(context.getCompletionChar()) && afterNew) {
context.getDocument().insertString(tail, braces + "{}");
editor.getCaretModel().moveToOffset(tail + braces.length() + 1);
} else {
context.getDocument().insertString(tail, braces);
editor.getCaretModel().moveToOffset(tail + 1);
if (context.getCompletionChar() == '[') {
context.setAddCompletionChar(false);
}
}
}
else {
@@ -1,5 +1,5 @@
class Super {
void foo(String[] params, int... indices) {
foo(new String[<caret>], 0);
foo(new String[]{<caret>}, 0);
}
}
@@ -4,6 +4,6 @@ class MyException extends RuntimeException {
class XXX {
{
throw new MyException(new String[<caret>]);
throw new MyException(new String[]{<caret>});
}
}
@@ -2,6 +2,6 @@ class List<T> {}
class C {
void foo () {
List<? extends String>[] array = new List[<caret>];
List<? extends String>[] array = new List[]{<caret>};
}
}
@@ -2,6 +2,6 @@ class A {
<T> void foo(T[] ts) {}
{
foo(new Object[]);
foo(new Object[]{<caret>});
}
}
@@ -2,6 +2,7 @@ package com.intellij.codeInsight.completion;
import com.intellij.codeInsight.lookup.Lookup;
import com.intellij.codeInsight.lookup.LookupElement;
import com.intellij.codeInsight.lookup.LookupEvent;
import com.intellij.codeInsight.lookup.LookupManager;
import com.intellij.codeInsight.lookup.impl.LookupImpl;
import com.intellij.openapi.command.WriteCommandAction;
@@ -49,7 +50,7 @@ public abstract class LightFixtureCompletionTestCase extends LightCodeInsightFix
protected void selectItem(LookupElement item, final char completionChar) {
final LookupImpl lookup = getLookup();
lookup.setCurrentItem(item);
if (completionChar == 0 || completionChar == '\n' || completionChar == '\t' || completionChar == Lookup.COMPLETE_STATEMENT_SELECT_CHAR) {
if (LookupEvent.isSpecialCompletionChar(completionChar)) {
new WriteCommandAction.Simple(getProject()) {
@Override
protected void run() throws Throwable {
@@ -59,4 +59,9 @@ public class LookupEvent extends EventObject {
public boolean isCanceledExplicitly() {
return myCanceledExplicitly;
}
public static boolean isSpecialCompletionChar(char c) {
return c == Lookup.AUTO_INSERT_SELECT_CHAR || c == Lookup.COMPLETE_STATEMENT_SELECT_CHAR ||
c == Lookup.NORMAL_SELECT_CHAR || c == Lookup.REPLACE_SELECT_CHAR;
}
}
@@ -502,11 +502,7 @@ public class CompletionLookupArranger extends LookupArranger {
}
String withoutSpaces = StringUtil.replace(textInserted, new String[]{" ", "\t", "\n"}, new String[]{"", "", ""});
int spared = withoutSpaces.length() - indicator.getLookup().itemPattern(item).length();
if (completionChar != Lookup.NORMAL_SELECT_CHAR &&
completionChar != Lookup.REPLACE_SELECT_CHAR &&
completionChar != Lookup.AUTO_INSERT_SELECT_CHAR &&
completionChar != Lookup.COMPLETE_STATEMENT_SELECT_CHAR &&
withoutSpaces.contains(String.valueOf(completionChar))) {
if (!LookupEvent.isSpecialCompletionChar(completionChar) && withoutSpaces.contains(String.valueOf(completionChar))) {
spared--;
}
if (spared > 0) {
@@ -126,7 +126,7 @@ public abstract class BaseCompleteMacro extends Macro {
if (item == null) return;
char c = event.getCompletionChar();
if (c != Lookup.REPLACE_SELECT_CHAR && c != Lookup.NORMAL_SELECT_CHAR && c != Lookup.COMPLETE_STATEMENT_SELECT_CHAR) {
if (!LookupEvent.isSpecialCompletionChar(c)) {
return;
}
@@ -3,6 +3,7 @@ package com.intellij.codeInsight.completion;
import com.intellij.codeInsight.CodeInsightSettings;
import com.intellij.codeInsight.lookup.Lookup;
import com.intellij.codeInsight.lookup.LookupElement;
import com.intellij.codeInsight.lookup.LookupEvent;
import com.intellij.codeInsight.lookup.LookupManager;
import com.intellij.codeInsight.lookup.impl.LookupImpl;
import com.intellij.codeInsight.lookup.impl.LookupManagerImpl;
@@ -115,7 +116,7 @@ public class XmlSmartEnterTest extends LightCodeInsightTestCase {
}
private void select(final char c) {
if (c != '\n' && c != '\t' && c != Lookup.COMPLETE_STATEMENT_SELECT_CHAR) {
if (!LookupEvent.isSpecialCompletionChar(c)) {
type(c);
return;
}