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.
16 package com
.intellij
.codeInsight
.daemon
.impl
;
18 import com
.intellij
.codeHighlighting
.BackgroundEditorHighlighter
;
19 import com
.intellij
.codeHighlighting
.Pass
;
20 import com
.intellij
.codeHighlighting
.TextEditorHighlightingPass
;
21 import com
.intellij
.codeInsight
.daemon
.DaemonCodeAnalyzer
;
22 import com
.intellij
.openapi
.editor
.Document
;
23 import com
.intellij
.openapi
.editor
.Editor
;
24 import com
.intellij
.openapi
.project
.Project
;
25 import com
.intellij
.psi
.PsiCompiledElement
;
26 import com
.intellij
.psi
.PsiDocumentManager
;
27 import com
.intellij
.psi
.PsiFile
;
28 import com
.intellij
.psi
.impl
.PsiFileEx
;
29 import com
.intellij
.util
.ArrayUtil
;
30 import org
.jetbrains
.annotations
.NotNull
;
32 import java
.util
.Collections
;
33 import java
.util
.List
;
35 public class TextEditorBackgroundHighlighter
implements BackgroundEditorHighlighter
{
36 private static final int[] EXCEPT_OVERRIDDEN
= {
42 Pass
.LOCAL_INSPECTIONS
,
46 private final Editor myEditor
;
47 private final Document myDocument
;
48 private PsiFile myFile
;
49 private final Project myProject
;
50 private boolean myCompiled
;
51 private static final int[] EXCEPT_VISIBLE
= {
53 Pass
.UPDATE_OVERRIDEN_MARKERS
,
54 Pass
.LOCAL_INSPECTIONS
,
58 public TextEditorBackgroundHighlighter(@NotNull Project project
, @NotNull Editor editor
) {
61 myDocument
= myEditor
.getDocument();
65 private void renewFile() {
66 if (myFile
== null || !myFile
.isValid()) {
67 myFile
= PsiDocumentManager
.getInstance(myProject
).getPsiFile(myDocument
);
68 myCompiled
= myFile
instanceof PsiCompiledElement
;
70 myFile
= (PsiFile
)((PsiCompiledElement
)myFile
).getMirror();
72 if (myFile
!= null && !myFile
.isValid()) {
78 myFile
.putUserData(PsiFileEx
.BATCH_REFERENCE_PROCESSING
, Boolean
.TRUE
);
82 private List
<TextEditorHighlightingPass
> getPasses(int[] passesToIgnore
) {
83 if (myProject
.isDisposed()) return Collections
.emptyList();
84 PsiDocumentManager
.getInstance(myProject
).commitAllDocuments();
86 if (myFile
== null || !myFile
.isPhysical()) return Collections
.emptyList();
88 passesToIgnore
= EXCEPT_OVERRIDDEN
;
90 else if (!DaemonCodeAnalyzer
.getInstance(myProject
).isHighlightingAvailable(myFile
)) {
91 return Collections
.emptyList();
94 TextEditorHighlightingPassRegistrarEx passRegistrar
= TextEditorHighlightingPassRegistrarEx
.getInstanceEx(myProject
);
96 return passRegistrar
.instantiatePasses(myFile
, myEditor
, passesToIgnore
);
100 public TextEditorHighlightingPass
[] createPassesForVisibleArea() {
101 List
<TextEditorHighlightingPass
> passes
= getPasses(EXCEPT_VISIBLE
);
102 int updateAllIndex
= -1;
103 int updateVisibleIndex
= -1;
104 for (int i
= 0, passesSize
= passes
.size(); i
< passesSize
; i
++) {
105 TextEditorHighlightingPass pass
= passes
.get(i
);
106 if (pass
instanceof GeneralHighlightingPass
) {
107 if (pass
.getId() == Pass
.UPDATE_ALL
) updateAllIndex
= i
;
108 if (pass
.getId() == Pass
.UPDATE_VISIBLE
) updateVisibleIndex
= i
;
112 if (updateVisibleIndex
!= -1 && updateAllIndex
== -1) {
115 else if (updateVisibleIndex
== -1 && updateAllIndex
!= -1) {
116 // use updateAll pass instead of updateVisible
118 else if (updateVisibleIndex
!= -1 && updateAllIndex
!= -1) {
119 // run only updateVisible, not both
120 passes
.remove(updateAllIndex
);
122 return passes
.isEmpty() ? TextEditorHighlightingPass
.EMPTY_ARRAY
: passes
.toArray(new TextEditorHighlightingPass
[passes
.size()]);
126 public TextEditorHighlightingPass
[] createPassesForEditor() {
127 List
<TextEditorHighlightingPass
> passes
= getPasses(ArrayUtil
.EMPTY_INT_ARRAY
);
128 return passes
.isEmpty() ? TextEditorHighlightingPass
.EMPTY_ARRAY
: passes
.toArray(new TextEditorHighlightingPass
[passes
.size()]);