use of com.intellij.codeInsight.daemon.impl.HighlightInfo in project intellij-community by JetBrains.
the class LightAdvHighlightingTest method testUnusedInspectionNonPrivateMembersReferencedFromText.
public void testUnusedInspectionNonPrivateMembersReferencedFromText() {
doTest(true);
WriteCommandAction.runWriteCommandAction(null, () -> {
PsiDirectory directory = myFile.getParent();
assertNotNull(myFile.toString(), directory);
PsiFile txt = directory.createFile("x.txt");
VirtualFile vFile = txt.getVirtualFile();
assertNotNull(txt.toString(), vFile);
try {
VfsUtil.saveText(vFile, "XXX");
} catch (IOException e) {
throw new RuntimeException(e);
}
});
List<HighlightInfo> infos = doHighlighting(HighlightSeverity.WARNING);
assertEmpty(infos);
}
use of com.intellij.codeInsight.daemon.impl.HighlightInfo in project intellij-community by JetBrains.
the class ImportHelperTest method testAutoImportSkipsClassReferenceInMethodPosition.
public void testAutoImportSkipsClassReferenceInMethodPosition() throws Throwable {
@NonNls String text = "package x; import java.util.HashMap; class S { HashMap<String,String> f(){ return Hash<caret>Map <String, String >();} } ";
configureByText(StdFileTypes.JAVA, text);
boolean old = CodeInsightSettings.getInstance().ADD_UNAMBIGIOUS_IMPORTS_ON_THE_FLY;
CodeInsightSettings.getInstance().ADD_UNAMBIGIOUS_IMPORTS_ON_THE_FLY = true;
DaemonCodeAnalyzerSettings.getInstance().setImportHintEnabled(true);
try {
List<HighlightInfo> errs = highlightErrors();
assertTrue(errs.size() > 1);
PsiJavaFile javaFile = (PsiJavaFile) getFile();
assertEquals(1, javaFile.getImportList().getAllImportStatements().length);
PsiReference ref = javaFile.findReferenceAt(getEditor().getCaretModel().getOffset());
ImportClassFix fix = new ImportClassFix((PsiJavaCodeReferenceElement) ref);
assertFalse(fix.isAvailable(getProject(), getEditor(), getFile()));
} finally {
CodeInsightSettings.getInstance().ADD_UNAMBIGIOUS_IMPORTS_ON_THE_FLY = old;
}
}
use of com.intellij.codeInsight.daemon.impl.HighlightInfo in project intellij-community by JetBrains.
the class HighlightUtil method checkUnhandledExceptions.
@Nullable
static HighlightInfo checkUnhandledExceptions(@NotNull final PsiElement element, @Nullable TextRange textRange) {
final List<PsiClassType> unhandledExceptions = ExceptionUtil.getUnhandledExceptions(element);
if (unhandledExceptions.isEmpty())
return null;
final HighlightInfoType highlightType = getUnhandledExceptionHighlightType(element);
if (highlightType == null)
return null;
if (textRange == null)
textRange = element.getTextRange();
final String description = getUnhandledExceptionsDescriptor(unhandledExceptions);
HighlightInfo errorResult = HighlightInfo.newHighlightInfo(highlightType).range(textRange).descriptionAndTooltip(description).create();
registerUnhandledExceptionFixes(element, errorResult, unhandledExceptions);
return errorResult;
}
use of com.intellij.codeInsight.daemon.impl.HighlightInfo in project intellij-community by JetBrains.
the class HighlightUtil method checkVariableAlreadyDefined.
@Nullable
static HighlightInfo checkVariableAlreadyDefined(@NotNull PsiVariable variable) {
if (variable instanceof ExternallyDefinedPsiElement)
return null;
boolean isIncorrect = false;
PsiElement declarationScope = null;
if (variable instanceof PsiLocalVariable || variable instanceof PsiParameter && ((declarationScope = ((PsiParameter) variable).getDeclarationScope()) instanceof PsiCatchSection || declarationScope instanceof PsiForeachStatement || declarationScope instanceof PsiLambdaExpression)) {
@SuppressWarnings("unchecked") PsiElement scope = PsiTreeUtil.getParentOfType(variable, PsiFile.class, PsiMethod.class, PsiClassInitializer.class, PsiResourceList.class);
VariablesNotProcessor proc = new VariablesNotProcessor(variable, false) {
@Override
protected boolean check(final PsiVariable var, final ResolveState state) {
return (var instanceof PsiLocalVariable || var instanceof PsiParameter) && super.check(var, state);
}
};
PsiIdentifier identifier = variable.getNameIdentifier();
assert identifier != null : variable;
PsiScopesUtil.treeWalkUp(proc, identifier, scope);
if (scope instanceof PsiResourceList && proc.size() == 0) {
scope = PsiTreeUtil.getParentOfType(variable, PsiFile.class, PsiMethod.class, PsiClassInitializer.class);
PsiScopesUtil.treeWalkUp(proc, identifier, scope);
}
if (proc.size() > 0) {
isIncorrect = true;
} else if (declarationScope instanceof PsiLambdaExpression) {
isIncorrect = checkSameNames(variable);
}
} else if (variable instanceof PsiField) {
PsiField field = (PsiField) variable;
PsiClass aClass = field.getContainingClass();
if (aClass == null)
return null;
PsiField fieldByName = aClass.findFieldByName(variable.getName(), false);
if (fieldByName != null && fieldByName != field) {
isIncorrect = true;
}
} else {
isIncorrect = checkSameNames(variable);
}
if (isIncorrect) {
String description = JavaErrorMessages.message("variable.already.defined", variable.getName());
PsiIdentifier identifier = variable.getNameIdentifier();
assert identifier != null : variable;
HighlightInfo highlightInfo = HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(identifier).descriptionAndTooltip(description).create();
if (variable instanceof PsiLocalVariable) {
QuickFixAction.registerQuickFixAction(highlightInfo, QUICK_FIX_FACTORY.createReuseVariableDeclarationFix((PsiLocalVariable) variable));
}
return highlightInfo;
}
return null;
}
use of com.intellij.codeInsight.daemon.impl.HighlightInfo in project intellij-community by JetBrains.
the class HighlightUtil method checkAssignmentOperatorApplicable.
@Nullable
static HighlightInfo checkAssignmentOperatorApplicable(@NotNull PsiAssignmentExpression assignment) {
PsiJavaToken operationSign = assignment.getOperationSign();
IElementType eqOpSign = operationSign.getTokenType();
IElementType opSign = TypeConversionUtil.convertEQtoOperation(eqOpSign);
if (opSign == null)
return null;
final PsiType lType = assignment.getLExpression().getType();
final PsiExpression rExpression = assignment.getRExpression();
if (rExpression == null)
return null;
final PsiType rType = rExpression.getType();
HighlightInfo errorResult = null;
if (!TypeConversionUtil.isBinaryOperatorApplicable(opSign, lType, rType, true)) {
String operatorText = operationSign.getText().substring(0, operationSign.getText().length() - 1);
String message = JavaErrorMessages.message("binary.operator.not.applicable", operatorText, JavaHighlightUtil.formatType(lType), JavaHighlightUtil.formatType(rType));
errorResult = HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(assignment).descriptionAndTooltip(message).create();
}
return errorResult;
}
Aggregations