use of com.intellij.lang.annotation.AnnotationHolder in project intellij-community by JetBrains.
the class GlobalAnnotator method visitPyGlobalStatement.
@Override
public void visitPyGlobalStatement(final PyGlobalStatement node) {
PyFunction function = PsiTreeUtil.getParentOfType(node, PyFunction.class);
if (function != null) {
PyParameterList paramList = function.getParameterList();
// collect param names
final Set<String> paramNames = new HashSet<>();
ParamHelper.walkDownParamArray(paramList.getParameters(), new ParamHelper.ParamVisitor() {
@Override
public void visitNamedParameter(PyNamedParameter param, boolean first, boolean last) {
paramNames.add(param.getName());
}
});
// check globals
final AnnotationHolder holder = getHolder();
for (PyTargetExpression expr : node.getGlobals()) {
final String expr_name = expr.getReferencedName();
if (paramNames.contains(expr_name)) {
holder.createErrorAnnotation(expr.getTextRange(), PyBundle.message("ANN.$0.both.global.and.param", expr_name));
}
}
}
}
use of com.intellij.lang.annotation.AnnotationHolder in project intellij-community by JetBrains.
the class GroovyAnnotator method checkThisOrSuperReferenceExpression.
private static void checkThisOrSuperReferenceExpression(final GrReferenceExpression ref, AnnotationHolder holder) {
PsiElement nameElement = ref.getReferenceNameElement();
if (nameElement == null)
return;
IElementType elementType = nameElement.getNode().getElementType();
if (!(elementType == GroovyTokenTypes.kSUPER || elementType == GroovyTokenTypes.kTHIS))
return;
final GrExpression qualifier = ref.getQualifier();
if (qualifier instanceof GrReferenceExpression) {
final PsiElement resolved = ((GrReferenceExpression) qualifier).resolve();
if (resolved instanceof PsiClass) {
GrTypeDefinition containingClass = PsiTreeUtil.getParentOfType(ref, GrTypeDefinition.class, true, GroovyFile.class);
if (elementType == GroovyTokenTypes.kSUPER && containingClass != null && GrTraitUtil.isTrait((PsiClass) resolved)) {
PsiClassType[] superTypes = containingClass.getSuperTypes();
if (ContainerUtil.find(superTypes, type -> ref.getManager().areElementsEquivalent(type.resolve(), resolved)) != null) {
holder.createInfoAnnotation(nameElement, null).setTextAttributes(GroovySyntaxHighlighter.KEYWORD);
// reference to trait method
return;
}
}
if (containingClass == null || containingClass.getContainingClass() == null && !containingClass.isAnonymous()) {
holder.createErrorAnnotation(ref, GroovyBundle.message("qualified.0.is.allowed.only.in.nested.or.inner.classes", nameElement.getText()));
return;
}
if (PsiTreeUtil.isAncestor(resolved, ref, true)) {
if (PsiUtil.hasEnclosingInstanceInScope((PsiClass) resolved, ref, true)) {
holder.createInfoAnnotation(nameElement, null).setTextAttributes(GroovySyntaxHighlighter.KEYWORD);
}
} else {
String qname = ((PsiClass) resolved).getQualifiedName();
assert qname != null;
holder.createErrorAnnotation(ref, GroovyBundle.message("is.not.enclosing.class", qname));
}
}
} else if (qualifier == null) {
if (elementType == GroovyTokenTypes.kSUPER) {
final GrMember container = PsiTreeUtil.getParentOfType(ref, GrMethod.class, GrClassInitializer.class);
if (container != null && container.hasModifierProperty(PsiModifier.STATIC)) {
holder.createErrorAnnotation(ref, GroovyBundle.message("super.cannot.be.used.in.static.context"));
}
}
}
}
use of com.intellij.lang.annotation.AnnotationHolder in project android by JetBrains.
the class AndroidColorAnnotatorTest method findAnnotation.
@NotNull
private Annotation findAnnotation(VirtualFile virtualFile, String target, Class<? extends PsiElement> elementClass) {
PsiFile file = PsiManager.getInstance(getProject()).findFile(virtualFile);
assertThat(file).isNotNull();
int caretOffset = target.indexOf('|');
if (caretOffset != -1) {
target = target.substring(0, caretOffset) + target.substring(caretOffset + 1);
} else {
caretOffset = 0;
}
String source = file.getText();
int dot = source.indexOf(target);
assertThat(dot).isNotEqualTo(-1);
dot += caretOffset;
PsiElement element = PsiTreeUtil.findElementOfClassAtOffset(file, dot, elementClass, false);
assertThat(element).isNotNull();
final List<Annotation> annotations = Lists.newArrayList();
AnnotationHolder holder = Mockito.mock(AnnotationHolder.class);
Mockito.when(holder.createInfoAnnotation(Matchers.any(PsiElement.class), Matchers.anyString())).thenAnswer(invocation -> {
PsiElement e = (PsiElement) invocation.getArguments()[0];
String message = (String) invocation.getArguments()[1];
Annotation annotation = new Annotation(e.getTextRange().getStartOffset(), e.getTextRange().getEndOffset(), HighlightSeverity.INFORMATION, message, null);
annotations.add(annotation);
return annotation;
});
AndroidColorAnnotator annotator = new AndroidColorAnnotator();
annotator.annotate(element, holder);
Mockito.reset(holder);
assertThat(annotations).isNotEmpty();
return annotations.get(0);
}
use of com.intellij.lang.annotation.AnnotationHolder in project intellij-community by JetBrains.
the class DaemonRespondToChangesTest method testHighlightingDoesWaitForEmbarrassinglySlowExternalAnnotatorsToFinish.
public void testHighlightingDoesWaitForEmbarrassinglySlowExternalAnnotatorsToFinish() throws Exception {
configureByText(JavaFileType.INSTANCE, "class X { int f() { int gg<caret> = 11; return 0;} }");
final AtomicBoolean run = new AtomicBoolean();
final int SLEEP = 20000;
ExternalAnnotator<Integer, Integer> annotator = new ExternalAnnotator<Integer, Integer>() {
@Nullable
@Override
public Integer collectInformation(@NotNull PsiFile file) {
return 0;
}
@Nullable
@Override
public Integer doAnnotate(final Integer collectedInfo) {
TimeoutUtil.sleep(SLEEP);
return 0;
}
@Override
public void apply(@NotNull final PsiFile file, final Integer annotationResult, @NotNull final AnnotationHolder holder) {
run.set(true);
}
};
ExternalLanguageAnnotators.INSTANCE.addExplicitExtension(JavaLanguage.INSTANCE, annotator);
try {
long start = System.currentTimeMillis();
List<HighlightInfo> errors = filter(CodeInsightTestFixtureImpl.instantiateAndRun(getFile(), getEditor(), new int[0], false), HighlightSeverity.ERROR);
long elapsed = System.currentTimeMillis() - start;
assertEquals(0, errors.size());
if (!run.get()) {
fail(ThreadDumper.dumpThreadsToString());
}
assertTrue("Elapsed: " + elapsed, elapsed >= SLEEP);
} finally {
ExternalLanguageAnnotators.INSTANCE.removeExplicitExtension(JavaLanguage.INSTANCE, annotator);
}
}
use of com.intellij.lang.annotation.AnnotationHolder in project intellij-community by JetBrains.
the class GroovyPostHighlightingPass method doApplyInformationToEditor.
@Override
public void doApplyInformationToEditor() {
if (myUnusedDeclarations == null || myUnusedImports == null) {
return;
}
AnnotationHolder annotationHolder = new AnnotationHolderImpl(new AnnotationSession(myFile));
List<HighlightInfo> infos = new ArrayList<>(myUnusedDeclarations);
for (GrImportStatement unusedImport : myUnusedImports) {
Annotation annotation = annotationHolder.createWarningAnnotation(calculateRangeToUse(unusedImport), GroovyInspectionBundle.message("unused.import"));
annotation.setHighlightType(ProblemHighlightType.LIKE_UNUSED_SYMBOL);
annotation.registerFix(GroovyQuickFixFactory.getInstance().createOptimizeImportsFix(false));
infos.add(HighlightInfo.fromAnnotation(annotation));
}
UpdateHighlightersUtil.setHighlightersToEditor(myProject, myDocument, 0, myFile.getTextLength(), infos, getColorsScheme(), getId());
if (myUnusedImports != null && !myUnusedImports.isEmpty()) {
IntentionAction fix = GroovyQuickFixFactory.getInstance().createOptimizeImportsFix(true);
if (fix.isAvailable(myProject, myEditor, myFile) && myFile.isWritable()) {
fix.invoke(myProject, myEditor, myFile);
}
}
}
Aggregations