mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-01-08 15:09:39 +07:00
SSR: improve & test JavaStructuralSearchProfile.shouldShowProblem()
GitOrigin-RevId: 10cc80084f7f29437af41f09a70548dd22cdc8a0
This commit is contained in:
committed by
intellij-monorepo-bot
parent
3addcac0eb
commit
66b28af3f1
@@ -13,6 +13,7 @@ import com.intellij.lang.java.JavaLanguage;
|
||||
import com.intellij.openapi.fileTypes.LanguageFileType;
|
||||
import com.intellij.openapi.fileTypes.StdFileTypes;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.util.Key;
|
||||
import com.intellij.openapi.util.registry.Registry;
|
||||
import com.intellij.openapi.util.text.StringUtil;
|
||||
import com.intellij.psi.*;
|
||||
@@ -65,6 +66,10 @@ public class JavaStructuralSearchProfile extends StructuralSearchProfile {
|
||||
PsiKeyword.CHAR, PsiKeyword.BYTE
|
||||
));
|
||||
|
||||
private static final Key<List<PsiErrorElement>> ERRORS = new Key<>("STRUCTURAL_SEARCH_ERRORS");
|
||||
private static final Comparator<PsiErrorElement> ERROR_COMPARATOR =
|
||||
Comparator.comparingInt(PsiErrorElement::getTextOffset).thenComparing(PsiErrorElement::getErrorDescription);
|
||||
|
||||
@Override
|
||||
public String getText(PsiElement match, int start, int end) {
|
||||
if (match instanceof PsiIdentifier) {
|
||||
@@ -490,7 +495,7 @@ public class JavaStructuralSearchProfile extends StructuralSearchProfile {
|
||||
return false;
|
||||
}
|
||||
|
||||
final PsiErrorElement error = findErrorElementAt(file, highlightInfo.startOffset);
|
||||
final PsiErrorElement error = findErrorElementAt(file, highlightInfo.startOffset, highlightInfo.getDescription());
|
||||
if (error == null) {
|
||||
return false;
|
||||
}
|
||||
@@ -539,36 +544,24 @@ public class JavaStructuralSearchProfile extends StructuralSearchProfile {
|
||||
return true;
|
||||
}
|
||||
|
||||
private static PsiErrorElement findErrorElementAt(PsiFile file, int offset) {
|
||||
final PsiElement element1 = file.findElementAt(offset);
|
||||
if (element1 != null) {
|
||||
final PsiElement leaf = PsiTreeUtil.prevLeaf(element1);
|
||||
if (leaf instanceof PsiErrorElement) {
|
||||
return (PsiErrorElement)leaf;
|
||||
}
|
||||
final PsiErrorElement parent = PsiTreeUtil.getParentOfType(element1, PsiErrorElement.class);
|
||||
if (parent != null && parent.getTextRange().getStartOffset() == offset) {
|
||||
return parent;
|
||||
}
|
||||
}
|
||||
if (offset > 0) {
|
||||
final PsiElement element2 = file.findElementAt(offset - 1);
|
||||
if (element2 != null) {
|
||||
PsiElement leaf = PsiTreeUtil.nextLeaf(element2);
|
||||
if (leaf instanceof PsiErrorElement) {
|
||||
return (PsiErrorElement)leaf;
|
||||
}
|
||||
if (leaf != null && leaf.getTextLength() == 0) {
|
||||
leaf = PsiTreeUtil.nextLeaf(leaf);
|
||||
}
|
||||
if (leaf instanceof PsiErrorElement) {
|
||||
return (PsiErrorElement)leaf;
|
||||
}
|
||||
private static PsiErrorElement findErrorElementAt(PsiFile file, int offset, String description) {
|
||||
final List<PsiErrorElement> errorList = ERRORS.get(file, findErrors(file));
|
||||
for (PsiErrorElement element : errorList) {
|
||||
if (element.getTextOffset() == offset && description.equals(element.getErrorDescription())) {
|
||||
return element;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private static List<PsiErrorElement> findErrors(PsiFile file) {
|
||||
final Collection<PsiErrorElement> errors = PsiTreeUtil.findChildrenOfType(file, PsiErrorElement.class);
|
||||
final List<PsiErrorElement> errorList = new ArrayList<>(errors);
|
||||
Collections.sort(errorList, ERROR_COMPARATOR);
|
||||
file.putUserData(ERRORS, errorList);
|
||||
return errorList;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void checkSearchPattern(CompiledPattern pattern) {
|
||||
final ValidatingVisitor visitor = new ValidatingVisitor();
|
||||
|
||||
@@ -0,0 +1,59 @@
|
||||
// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
|
||||
package com.intellij.structuralsearch.plugin.ui;
|
||||
|
||||
import com.intellij.openapi.editor.Document;
|
||||
import com.intellij.openapi.editor.Editor;
|
||||
import com.intellij.psi.JavaCodeFragment;
|
||||
import com.intellij.psi.JavaCodeFragmentFactory;
|
||||
import com.intellij.structuralsearch.JavaStructuralSearchProfile;
|
||||
import com.intellij.testFramework.fixtures.LightJavaCodeInsightFixtureTestCase;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
/**
|
||||
* @see StructuralSearchHighlightInfoFilter
|
||||
* @see JavaStructuralSearchProfile#shouldShowProblem(com.intellij.codeInsight.daemon.impl.HighlightInfo, com.intellij.psi.PsiFile, com.intellij.structuralsearch.PatternContext)
|
||||
* @author Bas Leijdekkers
|
||||
*/
|
||||
public class JavaShouldShowProblemTest extends LightJavaCodeInsightFixtureTestCase {
|
||||
|
||||
public void testSimpleError() {
|
||||
doTest("class A {<EOLError descr=\"'}' expected\"></EOLError>");
|
||||
}
|
||||
|
||||
public void testClassContent() {
|
||||
doTest("class $X$ { $Content$ }");
|
||||
}
|
||||
|
||||
public void testNakedTry() {
|
||||
doTest("try { $st$; }");
|
||||
}
|
||||
|
||||
public void testSymbol() {
|
||||
doTest("BigDecimal");
|
||||
}
|
||||
|
||||
public void testAnnotation() {
|
||||
doTest("@SuppressWarnings");
|
||||
}
|
||||
|
||||
public void testType() {
|
||||
doTest("List<String>");
|
||||
}
|
||||
|
||||
public void testUnexpectedToken() {
|
||||
doTest("java.math.BigDecimal<error descr=\"Unexpected token\">)</error>");
|
||||
}
|
||||
|
||||
private void doTest(@NotNull String code) {
|
||||
doTest(code, "");
|
||||
}
|
||||
|
||||
private void doTest(@NotNull String code, String contextId) {
|
||||
final JavaCodeFragment fragment = JavaCodeFragmentFactory.getInstance(getProject()).createCodeBlockCodeFragment(code, null, true);
|
||||
myFixture.configureFromExistingVirtualFile(fragment.getVirtualFile());
|
||||
final Editor editor = myFixture.getEditor();
|
||||
final Document document = editor.getDocument();
|
||||
document.putUserData(StructuralSearchDialog.STRUCTURAL_SEARCH_PATTERN_CONTEXT_ID, contextId);
|
||||
myFixture.testHighlighting(false, false, false);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user