mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-18 09:34:34 +07:00
IDEA-85330 Literals completion
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
class Bar {
|
||||
String a = "abc.def"
|
||||
String b = "abde<caret>"
|
||||
}
|
||||
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
class Bar {
|
||||
String a = "abc.def"
|
||||
String b = "abc.def<caret>"
|
||||
}
|
||||
|
||||
@@ -110,4 +110,9 @@ public class WordCompletionTest extends CompletionTestCase {
|
||||
checkResultByFile(BASE_PATH + getTestName(false) + "_after.txt");
|
||||
}
|
||||
|
||||
public void testCompleteStringLiteralCopy() throws Throwable {
|
||||
configureByFile(BASE_PATH + getTestName(false) + ".java");
|
||||
checkResultByFile(BASE_PATH + getTestName(false) + "_after.java");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+62
-8
@@ -18,16 +18,18 @@ package com.intellij.codeInsight.completion;
|
||||
import com.intellij.codeInsight.lookup.LookupElement;
|
||||
import com.intellij.codeInsight.lookup.LookupElementBuilder;
|
||||
import com.intellij.lang.ASTNode;
|
||||
import com.intellij.lang.LanguageParserDefinitions;
|
||||
import com.intellij.lang.LanguageWordCompletion;
|
||||
import com.intellij.lang.ParserDefinition;
|
||||
import com.intellij.openapi.project.DumbAware;
|
||||
import com.intellij.openapi.project.DumbService;
|
||||
import com.intellij.openapi.util.Condition;
|
||||
import com.intellij.openapi.util.text.StringUtil;
|
||||
import com.intellij.psi.PlainTextTokenTypes;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.psi.PsiFile;
|
||||
import com.intellij.psi.PsiReference;
|
||||
import com.intellij.patterns.ElementPattern;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.impl.cache.impl.id.IdTableBuilding;
|
||||
import com.intellij.psi.tree.IElementType;
|
||||
import com.intellij.psi.util.PsiTreeUtil;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.Collections;
|
||||
@@ -35,6 +37,8 @@ import java.util.HashSet;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import static com.intellij.patterns.PlatformPatterns.psiElement;
|
||||
|
||||
/**
|
||||
* @author peter
|
||||
*/
|
||||
@@ -47,8 +51,8 @@ public class WordCompletionContributor extends CompletionContributor implements
|
||||
}
|
||||
}
|
||||
|
||||
public static void addWordCompletionVariants(CompletionResultSet result, CompletionParameters parameters, Set<String> excludes) {
|
||||
Set<String> realExcludes = new HashSet<String>(excludes);
|
||||
public static void addWordCompletionVariants(CompletionResultSet result, final CompletionParameters parameters, Set<String> excludes) {
|
||||
final Set<String> realExcludes = new HashSet<String>(excludes);
|
||||
for (String exclude : excludes) {
|
||||
String[] words = exclude.split("[ \\.-]");
|
||||
if (words.length > 0 && StringUtil.isNotEmpty(words[0])) {
|
||||
@@ -57,16 +61,66 @@ public class WordCompletionContributor extends CompletionContributor implements
|
||||
}
|
||||
|
||||
int startOffset = parameters.getOffset();
|
||||
PsiElement insertedElement = parameters.getPosition();
|
||||
final PsiElement position = parameters.getPosition();
|
||||
final CompletionResultSet javaResultSet = result.withPrefixMatcher(CompletionUtil.findJavaIdentifierPrefix(parameters));
|
||||
final CompletionResultSet plainResultSet = result.withPrefixMatcher(CompletionUtil.findAlphanumericPrefix(parameters));
|
||||
for (final String word : getAllWords(insertedElement, startOffset)) {
|
||||
for (final String word : getAllWords(position, startOffset)) {
|
||||
if (!realExcludes.contains(word)) {
|
||||
final LookupElement item = LookupElementBuilder.create(word);
|
||||
javaResultSet.addElement(item);
|
||||
plainResultSet.addElement(item);
|
||||
}
|
||||
}
|
||||
|
||||
addValuesFromOtherStringLiterals(result, parameters, realExcludes, position);
|
||||
}
|
||||
|
||||
private static void addValuesFromOtherStringLiterals(CompletionResultSet result,
|
||||
CompletionParameters parameters,
|
||||
final Set<String> realExcludes, PsiElement position) {
|
||||
ParserDefinition definition = LanguageParserDefinitions.INSTANCE.forLanguage(position.getLanguage());
|
||||
if (definition == null) {
|
||||
return;
|
||||
}
|
||||
final ElementPattern<PsiElement> pattern = psiElement().withElementType(definition.getStringLiteralElements());
|
||||
final PsiElement localString = PsiTreeUtil.findFirstParent(position, false, new Condition<PsiElement>() {
|
||||
@Override
|
||||
public boolean value(PsiElement element) {
|
||||
return pattern.accepts(element);
|
||||
}
|
||||
});
|
||||
if (localString == null) {
|
||||
return;
|
||||
}
|
||||
ElementManipulator<PsiElement> manipulator = ElementManipulators.getManipulator(localString);
|
||||
if (manipulator == null) {
|
||||
return;
|
||||
}
|
||||
int offset = manipulator.getRangeInElement(localString).getStartOffset();
|
||||
PsiFile file = position.getContainingFile();
|
||||
final CompletionResultSet fullStringResult = result.withPrefixMatcher( file.getText().substring(offset + localString.getTextRange().getStartOffset(), parameters.getOffset()));
|
||||
file.accept(new PsiRecursiveElementWalkingVisitor() {
|
||||
@Override
|
||||
public void visitElement(PsiElement element) {
|
||||
if (element == localString) {
|
||||
return;
|
||||
}
|
||||
if (pattern.accepts(element)) {
|
||||
element.accept(new PsiRecursiveElementWalkingVisitor() {
|
||||
@Override
|
||||
public void visitElement(PsiElement each) {
|
||||
String valueText = ElementManipulators.getValueText(each);
|
||||
if (StringUtil.isNotEmpty(valueText) && !realExcludes.contains(valueText)) {
|
||||
final LookupElement item = LookupElementBuilder.create(valueText);
|
||||
fullStringResult.addElement(item);
|
||||
}
|
||||
}
|
||||
});
|
||||
return;
|
||||
}
|
||||
super.visitElement(element);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private static boolean shouldPerformWordCompletion(CompletionParameters parameters) {
|
||||
|
||||
Reference in New Issue
Block a user