use of com.intellij.lang.annotation.Annotation in project intellij-plugins by JetBrains.
the class DartAnnotator method applyServerHighlighting.
private static void applyServerHighlighting(@NotNull final VirtualFile file, @NotNull final AnnotationHolder holder) {
final PsiFile psiFile = holder.getCurrentAnnotationSession().getFile();
final DartAnalysisServerService das = DartAnalysisServerService.getInstance(psiFile.getProject());
for (DartServerData.DartError error : das.getErrors(file)) {
if (shouldIgnoreMessageFromDartAnalyzer(file.getPath(), error.getAnalysisErrorFileSD()))
continue;
final Annotation annotation = createAnnotation(holder, error, psiFile.getTextLength());
if (annotation != null) {
final DartQuickFixSet quickFixSet = new DartQuickFixSet(psiFile.getManager(), file, error.getOffset(), error.getCode(), error.getSeverity());
for (IntentionAction quickFix : quickFixSet.getQuickFixes()) {
annotation.registerFix(quickFix);
}
if (error.getCode() != null) {
annotation.setProblemGroup(new DartProblemGroup(error.getCode(), error.getSeverity()));
}
}
}
for (DartServerData.DartHighlightRegion region : das.getHighlight(file)) {
final String attributeKey = HIGHLIGHTING_TYPE_MAP.get(region.getType());
if (attributeKey != null) {
final TextRange textRange = new TextRange(region.getOffset(), region.getOffset() + region.getLength());
holder.createInfoAnnotation(textRange, null).setTextAttributes(TextAttributesKey.find(attributeKey));
}
}
}
use of com.intellij.lang.annotation.Annotation in project intellij-plugins by JetBrains.
the class ActionScriptAnnotatingVisitor method reportStaticMethodProblem.
private void reportStaticMethodProblem(JSAttributeList attributeList, String key) {
final ASTNode astNode = attributeList.getNode().findChildByType(JSTokenTypes.STATIC_KEYWORD);
final Annotation annotation = myHolder.createErrorAnnotation(astNode, JSBundle.message(key));
annotation.registerFix(new RemoveASTNodeFix("javascript.fix.remove.static.modifier", astNode));
}
use of com.intellij.lang.annotation.Annotation in project intellij-plugins by JetBrains.
the class ActionScriptAnnotatingVisitor method checkFunctionDeclaration.
@Override
protected void checkFunctionDeclaration(@NotNull final JSFunction node) {
super.checkFunctionDeclaration(node);
final ASTNode nameIdentifier = node.findNameIdentifier();
if (nameIdentifier == null)
return;
PsiElement parent = node.getParent();
if (parent instanceof JSFile) {
parent = JSResolveUtil.getClassReferenceForXmlFromContext(parent);
final String name = node.getName();
if (parent instanceof JSClass && name != null && name.equals(((JSClass) parent).getName()) && !isNative(node) && JavaScriptSupportLoader.isFlexMxmFile(parent.getContainingFile())) {
final Annotation annotation = myHolder.createErrorAnnotation(nameIdentifier, JSBundle.message("javascript.validation.message.constructor.in.mxml.is.not.allowed"));
annotation.registerFix(new RemoveASTNodeFix("javascript.fix.remove.constructor", node.getNode()));
}
}
if (parent instanceof JSPackageStatement) {
checkNamedObjectIsInCorrespondingFile(node);
}
if (parent instanceof JSClass && !node.isConstructor()) {
final JSAttributeList attributeList = node.getAttributeList();
final JSClass clazz = (JSClass) parent;
if (attributeList == null || !attributeList.hasModifier(JSAttributeList.ModifierType.STATIC) && (attributeList.getAccessType() != JSAttributeList.AccessType.PRIVATE || attributeList.hasModifier(JSAttributeList.ModifierType.OVERRIDE))) {
final String qName = clazz.getQualifiedName();
final boolean hasOverride = attributeList != null && attributeList.hasModifier(JSAttributeList.ModifierType.OVERRIDE);
final Ref<JSFunction> set = new Ref<>();
boolean b = JSResolveUtil.iterateType(node, parent, qName, new JSOverrideHandler() {
public boolean process(@NotNull final List<JSPsiElementBase> elements, final PsiElement scope, final String className) {
//noinspection StringEquality
if (qName == className || qName != null && qName.equals(className))
return true;
JSFunction value = (JSFunction) elements.iterator().next();
set.set(value);
DialectOptionHolder holder;
if ("Object".equals(className)) {
if (hasOverride && !attributeList.hasModifier(JSAttributeList.ModifierType.NATIVE)) {
/*native modifier is written always*/
final ASTNode astNode = attributeList.getNode().findChildByType(JSTokenTypes.OVERRIDE_KEYWORD);
final Annotation annotation = myHolder.createErrorAnnotation(astNode, JSBundle.message("javascript.validation.message.function.override.for.object.method"));
annotation.registerFix(new RemoveASTNodeFix("javascript.fix.remove.override.modifier", astNode));
}
return false;
} else if (!hasOverride && (holder = myHighlighter.getDialectOptionsHolder()) != null && holder.isECMA4) {
final Annotation annotation = myHolder.createErrorAnnotation(nameIdentifier, JSBundle.message("javascript.validation.message.function.override.without.override.modifier", className));
annotation.registerFix(new AddOverrideIntentionAction(node));
} else {
JSAttributeList attrList = value.getAttributeList();
JSAttributeList parentAttrList = ((JSAttributeListOwner) scope).getAttributeList();
if (attrList != null && attrList.hasModifier(JSAttributeList.ModifierType.FINAL) || parentAttrList != null && parentAttrList.hasModifier(JSAttributeList.ModifierType.FINAL)) {
myHolder.createErrorAnnotation(attributeList.getNode().findChildByType(JSTokenTypes.OVERRIDE_KEYWORD), JSBundle.message("javascript.validation.message.can.not.override.final.method", className));
}
}
if (clazz.isInterface()) {
myHolder.createErrorAnnotation(nameIdentifier, JSBundle.message("javascript.validation.message.function.override.for.interface", className));
}
return false;
}
}, true);
if (b && hasOverride) {
final ASTNode astNode = attributeList.getNode().findChildByType(JSTokenTypes.OVERRIDE_KEYWORD);
final Annotation annotation = myHolder.createErrorAnnotation(astNode, JSBundle.message("javascript.validation.message.function.override.without.parent.method"));
annotation.registerFix(new RemoveASTNodeFix("javascript.fix.remove.override.modifier", astNode));
}
if (!b && hasOverride) {
final JSFunction override = set.get();
final JSAttributeList overrideAttrList = override.getAttributeList();
String overrideNs = null;
if (attributeList.getAccessType() != overrideAttrList.getAccessType() || (overrideNs = JSResolveUtil.getNamespaceValue(overrideAttrList)) != null && !overrideNs.equals(JSResolveUtil.getNamespaceValue(attributeList))) {
String newVisibility;
IntentionAction fix;
if (overrideNs != null) {
newVisibility = overrideNs;
fix = JSFixFactory.getInstance().createChangeVisibilityFix(node, null, overrideNs);
} else {
newVisibility = JSFormatUtil.formatVisibility(overrideAttrList.getAccessType());
fix = JSFixFactory.getInstance().createChangeVisibilityFix(node, overrideAttrList.getAccessType(), null);
}
final Annotation annotation = myHolder.createErrorAnnotation(findElementForAccessModifierError(node, attributeList), JSBundle.message("javascript.validation.message.function.override.incompatible.access.modifier", newVisibility));
annotation.registerFix(fix);
}
final SignatureMatchResult incompatibleSignature = checkCompatibleSignature(node, override);
if (incompatibleSignature == SignatureMatchResult.PARAMETERS_DIFFERS) {
final JSParameterList nodeParameterList = node.getParameterList();
final JSParameterList overrideParameterList = override.getParameterList();
final Annotation annotation = myHolder.createErrorAnnotation(nodeParameterList != null ? nodeParameterList.getNode() : node.findNameIdentifier(), JSBundle.message("javascript.validation.message.function.override.incompatible.signature", overrideParameterList != null ? overrideParameterList.getText() : "()"));
annotation.registerFix(new ChangeSignatureFix(node, overrideParameterList, false) {
@NotNull
public String getText() {
return JSBundle.message("javascript.fix.message.change.parameters.to.expected");
}
});
annotation.registerFix(new ChangeSignatureFix(override, nodeParameterList, true));
} else if (incompatibleSignature == SignatureMatchResult.RETURN_TYPE_DIFFERS) {
PsiElement returnTypeExpr = node.getReturnTypeElement();
JSType type = override.getReturnType();
final String baseReturnType = type != null ? type.getResolvedTypeText() : null;
String msg = JSBundle.message("javascript.validation.message.function.override.incompatible.signature2", StringUtil.notNullize(baseReturnType));
final Annotation annotation = myHolder.createErrorAnnotation(returnTypeExpr != null ? returnTypeExpr.getNode() : node.findNameIdentifier(), msg);
annotation.registerFix(new ChangeTypeFix(node, baseReturnType, "javascript.fix.message.change.return.type.to.expected"));
annotation.registerFix(createChangeBaseMethodSignatureFix(override, node));
} else if (incompatibleSignature == SignatureMatchResult.FUNCTION_KIND_DIFFERS) {
String msg = JSBundle.message("javascript.validation.message.function.override.incompatible.signature3", override.getKind().toString());
final Annotation annotation = myHolder.createErrorAnnotation(node.findNameIdentifier(), msg);
//annotation.registerFix();
}
}
} else if (attributeList.hasModifier(JSAttributeList.ModifierType.STATIC)) {
if (clazz.isInterface()) {
reportStaticMethodProblem(attributeList, "javascript.validation.message.static.method.in.interface");
}
if (attributeList.hasModifier(JSAttributeList.ModifierType.OVERRIDE)) {
reportStaticMethodProblem(attributeList, "javascript.validation.message.static.method.with.override");
}
}
}
}
use of com.intellij.lang.annotation.Annotation in project intellij-plugins by JetBrains.
the class ActionScriptAnnotatingVisitor method checkPackageStatement.
private void checkPackageStatement(final JSPackageStatement packageStatement) {
final String s = packageStatement.getQualifiedName();
final PsiFile containingFile = packageStatement.getContainingFile();
final String expected = JSResolveUtil.getExpectedPackageNameFromFile(containingFile.getVirtualFile(), containingFile.getProject());
if (expected != null && (s == null && expected.length() != 0 || s != null && !expected.equals(s))) {
final ASTNode nameIdentifier = packageStatement.findNameIdentifier();
final Annotation annotation = myHolder.createErrorAnnotation(nameIdentifier != null ? nameIdentifier : packageStatement.getFirstChild().getNode(), JSBundle.message("javascript.validation.message.incorrect.package.name", s, expected));
annotation.registerFix(new IntentionAction() {
@NotNull
public String getText() {
return JSBundle.message("javascript.fix.package.name", expected);
}
@NotNull
public String getFamilyName() {
return getText();
}
public boolean isAvailable(@NotNull final Project project, final Editor editor, final PsiFile file) {
return packageStatement.isValid();
}
public void invoke(@NotNull final Project project, final Editor editor, final PsiFile file) throws IncorrectOperationException {
JSPackageStatementImpl.doChangeName(project, packageStatement, expected);
}
public boolean startInWriteAction() {
return true;
}
});
}
final Set<JSNamedElement> elements = new THashSet<>();
for (JSSourceElement statement : packageStatement.getStatements()) {
if (statement instanceof JSNamedElement && !(statement instanceof JSImportStatement)) {
elements.add((JSNamedElement) statement);
} else if (statement instanceof JSVarStatement) {
ContainerUtil.addAll(elements, ((JSVarStatement) statement).getVariables());
}
}
if (elements.size() > 1) {
for (JSNamedElement el : elements) {
if (!(el instanceof JSAttributeListOwner))
continue;
JSAttributeList attributeList = ((JSAttributeListOwner) el).getAttributeList();
if (attributeList != null && attributeList.getConditionalCompileVariableReference() != null)
continue;
final ASTNode nameIdentifier = el.findNameIdentifier();
myHolder.createErrorAnnotation(nameIdentifier != null ? nameIdentifier : el.getFirstChild().getNode(), JSBundle.message("javascript.validation.message.more.than.one.externally.visible.symbol")).registerFix(new RemoveASTNodeFix("javascript.fix.remove.externally.visible.symbol", el.getNode()));
}
}
checkFileUnderSourceRoot(packageStatement, new SimpleErrorReportingClient());
}
use of com.intellij.lang.annotation.Annotation in project intellij-plugins by JetBrains.
the class ActionScriptTypeChecker method checkExpressionIsAssignableToVariable.
@Override
public void checkExpressionIsAssignableToVariable(JSVariable p, final JSExpression expr, PsiFile containingFile, @PropertyKey(resourceBundle = JSBundle.BUNDLE) String problemKey, boolean allowChangeVariableTypeFix) {
final JSType type = p.getType();
Pair<Annotation, String> annotationAndExprType = checkExpressionIsAssignableToType(expr, type, problemKey, allowChangeVariableTypeFix ? p : null);
if (annotationAndExprType != null && p.getParent() instanceof JSParameterList && expr.getParent() instanceof JSArgumentList && !JSCommonTypeNames.VOID_TYPE_NAME.equals(annotationAndExprType.second)) {
JSFunction method = (JSFunction) p.getParent().getParent();
JSFunction topMethod = JSInheritanceUtil.findTopMethods(method).iterator().next();
annotationAndExprType.first.registerFix(new ChangeSignatureFix(topMethod, (JSArgumentList) expr.getParent()));
}
PsiElement _fun;
if (annotationAndExprType == null && type != null && FUNCTION_CLASS_NAME.equals(type.getResolvedTypeText()) && p instanceof JSParameter && isAddEventListenerMethod((JSFunction) p.getParent().getParent()) && ((expr instanceof JSReferenceExpression && (_fun = ((JSReferenceExpression) expr).resolve()) instanceof JSFunction) || (expr instanceof JSFunctionExpression && (_fun = expr) != null))) {
JSFunction fun = (JSFunction) _fun;
JSParameterList parameterList = fun.getParameterList();
if (parameterList != null) {
JSParameter[] parameters = parameterList.getParameterVariables();
boolean invalidArgs = parameters.length == 0;
if (!invalidArgs && parameters.length > 1) {
for (int i = parameters.length - 1; i > 0; --i) {
if (!parameters[i].isRest() && parameters[i].getInitializer() == null) {
invalidArgs = true;
break;
}
}
}
Computable.NotNullCachedComputable<JSParameterList> expectedParameterListForEventListener = new Computable.NotNullCachedComputable<JSParameterList>() {
@NotNull
@Override
protected JSParameterList internalCompute() {
JSClass jsClass = calcNontrivialExpectedEventType(expr);
ASTNode treeFromText = JSChangeUtil.createJSTreeFromText(expr.getProject(), "function f(event:" + (jsClass != null ? jsClass.getQualifiedName() : FlexCommonTypeNames.FLASH_EVENT_FQN) + ") {}", JavaScriptSupportLoader.ECMA_SCRIPT_L4);
return ((JSFunction) treeFromText.getPsi()).getParameterList();
}
};
if (invalidArgs) {
PsiElement expr_;
if (expr instanceof JSFunctionExpression) {
expr_ = ((JSFunctionExpression) expr).getParameterList();
} else {
expr_ = expr;
}
registerProblem(expr_, JSBundle.message("javascript.callback.signature.mismatch"), ProblemHighlightType.WEAK_WARNING, new ChangeSignatureFix(fun, expectedParameterListForEventListener));
} else {
final JSClass expectedEventClass = calcNontrivialExpectedEventType(expr);
JSType paramType = parameters[0].getType();
final String actualParameterType = paramType != null ? paramType.getResolvedTypeText() : null;
if (expectedEventClass == null) {
if (!JSResolveUtil.isAssignableType(FlexCommonTypeNames.FLASH_EVENT_FQN, actualParameterType, parameters[0]) && !JSResolveUtil.isAssignableType(FlexCommonTypeNames.STARLING_EVENT_FQN, actualParameterType, parameters[0])) {
registerProblem(expr instanceof JSFunctionExpression ? parameters[0] : expr, JSBundle.message("javascript.callback.signature.mismatch"), ProblemHighlightType.WEAK_WARNING, new ChangeSignatureFix(fun, expectedParameterListForEventListener));
}
} else {
if (!JSResolveUtil.isAssignableType(actualParameterType, expectedEventClass.getQualifiedName(), parameters[0])) {
registerProblem(expr instanceof JSFunctionExpression ? parameters[0] : expr, JSBundle.message("javascript.callback.signature.mismatch.event.class", expectedEventClass.getQualifiedName()), ProblemHighlightType.WEAK_WARNING, new ChangeSignatureFix(fun, expectedParameterListForEventListener));
}
}
}
}
}
}
Aggregations