2 * Copyright 2000-2009 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package com
.intellij
.codeInspection
;
19 import com
.intellij
.analysis
.AnalysisScope
;
20 import com
.intellij
.codeHighlighting
.HighlightDisplayLevel
;
21 import com
.intellij
.codeInsight
.daemon
.impl
.analysis
.DefaultHighlightVisitor
;
22 import com
.intellij
.codeInsight
.highlighting
.HighlightErrorFilter
;
23 import com
.intellij
.openapi
.extensions
.Extensions
;
24 import com
.intellij
.openapi
.util
.TextRange
;
25 import com
.intellij
.psi
.*;
26 import org
.jetbrains
.annotations
.Nls
;
27 import org
.jetbrains
.annotations
.NotNull
;
29 import java
.util
.List
;
34 public class SyntaxErrorInspection
extends GlobalInspectionTool
{
36 public boolean isGraphNeeded() {
42 public HighlightDisplayLevel
getDefaultLevel() {
43 return HighlightDisplayLevel
.ERROR
;
47 public void runInspection(AnalysisScope scope
,
48 final InspectionManager manager
,
49 final GlobalInspectionContext globalContext
,
50 final ProblemDescriptionsProcessor problemDescriptionsProcessor
) {
51 scope
.accept(new MyPsiRecursiveElementVisitor(manager
, globalContext
, problemDescriptionsProcessor
));
57 public String
getGroupDisplayName() {
64 public String
getDisplayName() {
65 return "Syntax error";
70 public String
getShortName() {
74 private static class MyPsiRecursiveElementVisitor
extends PsiRecursiveElementVisitor
implements PsiLanguageInjectionHost
.InjectedPsiVisitor
{
75 private final InspectionManager manager
;
76 private final GlobalInspectionContext globalContext
;
77 private final ProblemDescriptionsProcessor problemDescriptionsProcessor
;
78 private final HighlightErrorFilter
[] errorFilters
;
80 public MyPsiRecursiveElementVisitor(InspectionManager manager
, GlobalInspectionContext globalContext
,
81 ProblemDescriptionsProcessor problemDescriptionsProcessor
) {
82 this.manager
= manager
;
83 this.globalContext
= globalContext
;
84 this.problemDescriptionsProcessor
= problemDescriptionsProcessor
;
85 this.errorFilters
= Extensions
.getExtensions(DefaultHighlightVisitor
.FILTER_EP_NAME
, manager
.getProject());
89 public void visitElement(PsiElement element
) {
90 super.visitElement(element
);
91 if (element
instanceof PsiLanguageInjectionHost
) {
92 ((PsiLanguageInjectionHost
)element
).processInjectedPsi(this);
97 public void visitErrorElement(PsiErrorElement element
) {
98 super.visitErrorElement(element
);
99 for (final HighlightErrorFilter errorFilter
: errorFilters
) {
100 if (!errorFilter
.shouldHighlightErrorElement(element
)) return;
103 CommonProblemDescriptor descriptor
;
104 final TextRange textRange
= element
.getTextRange();
105 if (textRange
.getLength() > 0) {
106 descriptor
= manager
.createProblemDescriptor(
108 GlobalInspectionUtil
.createInspectionMessage(element
.getErrorDescription()),
109 ProblemHighlightType
.ERROR
,
113 PsiElement parent
= element
;
115 parent
= parent
.getParent();
116 if (parent
== null) break;
117 TextRange r
= parent
.getTextRange();
118 if (r
== null) return; // no place to attach the problem descriptor to
119 if (r
.getLength() > 0) {
123 if (parent
== null) return;
124 int offset
= element
.getTextRange().getStartOffset() - parent
.getTextRange().getStartOffset();
125 descriptor
= manager
.createProblemDescriptor(parent
,
126 new TextRange(offset
, offset
+1),
127 GlobalInspectionUtil
.createInspectionMessage(element
.getErrorDescription()),
128 ProblemHighlightType
.ERROR
, false);
131 problemDescriptionsProcessor
.addProblemElement(GlobalInspectionUtil
.retrieveRefElement(element
, globalContext
), descriptor
);
134 public void visit(@NotNull PsiFile injectedPsi
, @NotNull List
<PsiLanguageInjectionHost
.Shred
> places
) {
135 injectedPsi
.acceptChildren(this);