mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-10-01 13:11:39 +07:00
Groovy: Completion map key: bug fix.
This commit is contained in:
+38
-2
@@ -20,6 +20,13 @@ import com.intellij.codeInsight.completion.InsertionContext;
|
||||
import com.intellij.codeInsight.lookup.LookupElement;
|
||||
import com.intellij.openapi.editor.Editor;
|
||||
import com.intellij.openapi.editor.ScrollType;
|
||||
import com.intellij.openapi.util.text.StringUtil;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap;
|
||||
import org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList;
|
||||
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* @author Maxim.Medvedev
|
||||
@@ -34,9 +41,38 @@ public class NamedArgumentInsertHandler implements InsertHandler<LookupElement>
|
||||
|
||||
public void handleInsert(InsertionContext context, LookupElement item) {
|
||||
int tailOffset = context.getTailOffset();
|
||||
|
||||
PsiElement argumentList = context.getFile().findElementAt(tailOffset - 1);
|
||||
while (argumentList != null && !(argumentList instanceof GrArgumentList) && !(argumentList instanceof GrListOrMap)) {
|
||||
argumentList = argumentList.getParent();
|
||||
}
|
||||
|
||||
final Editor editor = context.getEditor();
|
||||
editor.getDocument().insertString(tailOffset, ": ");
|
||||
editor.getCaretModel().moveToOffset(tailOffset + 2);
|
||||
|
||||
if (argumentList != null) {
|
||||
String argumentListText = argumentList.getText();
|
||||
|
||||
String s = argumentListText.substring(tailOffset - argumentList.getTextOffset());
|
||||
s = StringUtil.trimEnd(s, ")");
|
||||
|
||||
if (s.trim().length() == 0) {
|
||||
editor.getDocument().insertString(tailOffset, ": ");
|
||||
editor.getCaretModel().moveToOffset(tailOffset + 2);
|
||||
}
|
||||
else {
|
||||
Matcher m = Pattern.compile("(\\s*)(:)?(\\s*),?\\s?(\\s*)(.+)").matcher(s);
|
||||
if (!m.matches()) throw new RuntimeException("This pattern must matches on any non-empty string!");
|
||||
|
||||
if (m.group(2) != null) {
|
||||
editor.getCaretModel().moveToOffset(tailOffset + m.end(3));
|
||||
}
|
||||
else {
|
||||
editor.getDocument().replaceString(tailOffset, tailOffset + m.start(4), ": , ");
|
||||
editor.getCaretModel().moveToOffset(tailOffset + 2);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
editor.getScrollingModel().scrollToCaret(ScrollType.RELATIVE);
|
||||
editor.getSelectionModel().removeSelection();
|
||||
}
|
||||
|
||||
@@ -17,7 +17,8 @@
|
||||
package org.jetbrains.plugins.groovy;
|
||||
|
||||
import com.intellij.codeInsight.completion.CompletionType;
|
||||
import org.jetbrains.plugins.groovy.util.TestUtils;
|
||||
import org.jetbrains.plugins.groovy.util.TestUtils
|
||||
import com.intellij.codeInsight.lookup.LookupElement
|
||||
|
||||
/**
|
||||
* @author Maxim.Medvedev
|
||||
@@ -285,4 +286,41 @@ public class GroovyCompletionTest extends GroovyCompletionTestBase {
|
||||
defField<caret>
|
||||
"""
|
||||
}
|
||||
|
||||
public void testCompletionNamedArgument1() {
|
||||
def file = myFixture.configureByText(GroovyFileType.GROOVY_FILE_TYPE, """
|
||||
class A {
|
||||
public int m(arg) { return arg.arg111 + arg.arg222 + arg.arg333; }
|
||||
{ m(arg111: 1, arg<caret>: 2, arg333: 3) }
|
||||
}
|
||||
""")
|
||||
LookupElement[] lookupElements = myFixture.completeBasic()
|
||||
assertNull(lookupElements)
|
||||
|
||||
assertEquals """
|
||||
class A {
|
||||
public int m(arg) { return arg.arg111 + arg.arg222 + arg.arg333; }
|
||||
{ m(arg111: 1, arg222: 2, arg333: 3) }
|
||||
}
|
||||
""", file.text
|
||||
}
|
||||
|
||||
public void testCompletionNamedArgument2() {
|
||||
def file = myFixture.configureByText(GroovyFileType.GROOVY_FILE_TYPE, """
|
||||
class A {
|
||||
public int m(arg) { return arg.arg111 + arg.arg222 + arg.arg333; }
|
||||
{ m arg111: 1, arg<caret>: 2, arg333: 3 }
|
||||
}
|
||||
""")
|
||||
LookupElement[] lookupElements = myFixture.completeBasic()
|
||||
assertNull(lookupElements)
|
||||
|
||||
assertEquals """
|
||||
class A {
|
||||
public int m(arg) { return arg.arg111 + arg.arg222 + arg.arg333; }
|
||||
{ m arg111: 1, arg222: 2, arg333: 3 }
|
||||
}
|
||||
""", file.text
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user