-
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathDocumentMarkupModelScanner.java
More file actions
216 lines (173 loc) · 7.9 KB
/
DocumentMarkupModelScanner.java
File metadata and controls
216 lines (173 loc) · 7.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package org.overengineer.inlineproblems;
import com.intellij.codeInsight.daemon.impl.HighlightInfo;
import com.intellij.openapi.Disposable;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.impl.DocumentMarkupModel;
import com.intellij.openapi.editor.markup.RangeHighlighter;
import com.intellij.openapi.fileEditor.FileEditorManager;
import com.intellij.openapi.fileEditor.TextEditor;
import com.intellij.openapi.project.ProjectManager;
import com.intellij.openapi.util.Disposer;
import com.intellij.util.concurrency.AppExecutorUtil;
import com.intellij.util.ui.update.MergingUpdateQueue;
import com.intellij.util.ui.update.Update;
import org.overengineer.inlineproblems.entities.InlineProblem;
import org.overengineer.inlineproblems.entities.enums.Listener;
import org.overengineer.inlineproblems.listeners.HighlightProblemListener;
import org.overengineer.inlineproblems.settings.SettingsState;
import org.overengineer.inlineproblems.utils.FileNameUtil;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
public class DocumentMarkupModelScanner implements Disposable {
private final ProblemManager problemManager = ApplicationManager.getApplication().getService(ProblemManager.class);
private final SettingsState settingsState;
private final Logger logger = Logger.getInstance(DocumentMarkupModelScanner.class);
private int delayMilliseconds = HighlightProblemListener.ADDITIONAL_MANUAL_SCAN_DELAY_MILLIS;
private static DocumentMarkupModelScanner instance;
private final MergingUpdateQueue mergingUpdateQueue;
private ScheduledFuture<?> scheduledFuture;
public static final String NAME = "ManualScanner";
private DocumentMarkupModelScanner() {
Disposer.register(problemManager, this);
settingsState = SettingsState.getInstance();
mergingUpdateQueue = new MergingUpdateQueue(
"DocumentMarkupModelScannerQueue",
10,
true,
null,
this,
null,
true
);
SettingsState settingsState = SettingsState.getInstance();
if (settingsState.getEnabledListener() == Listener.MANUAL_SCANNING) {
delayMilliseconds = settingsState.getManualScannerDelay();
}
createAndStartScheduledFuture();
}
public static DocumentMarkupModelScanner getInstance() {
if (instance == null)
instance = new DocumentMarkupModelScanner();
return instance;
}
@Override
public void dispose() {
mergingUpdateQueue.cancelAllUpdates();
}
public void scanForProblemsManually() {
ProjectManager projectManager = ProjectManager.getInstanceIfCreated();
if (projectManager != null) {
List<InlineProblem> problems = new ArrayList<>();
for (var project : projectManager.getOpenProjects()) {
if (!project.isInitialized() || project.isDisposed())
continue;
FileEditorManager fileEditorManager = FileEditorManager.getInstance(project);
for (var editor : fileEditorManager.getAllEditors()) {
if (editor.getFile() == null || FileNameUtil.ignoreFile(editor.getFile().getName())) {
continue;
}
if (editor instanceof TextEditor) {
var textEditor = (TextEditor) editor;
problems.addAll(getProblemsInEditor(textEditor));
}
}
}
problemManager.updateFromNewActiveProblems(problems);
}
}
/**
* This function is queued in the mergingUpdateQueue because it is called frequently, this can be multiple times per
* millisecond if the HighlightProblemListener is used.
*/
public void scanForProblemsManuallyInTextEditor(TextEditor textEditor) {
if (textEditor.getFile() == null || FileNameUtil.ignoreFile(textEditor.getFile().getName())) {
return;
}
mergingUpdateQueue.queue(new Update("scan") {
@Override
public void run() {
List<InlineProblem> problems = getProblemsInEditor(textEditor);
problemManager.updateFromNewActiveProblemsForProjectAndFile(
problems,
textEditor.getEditor().getProject(),
textEditor.getFile().getPath()
);
}
});
}
private List<InlineProblem> getProblemsInEditor(TextEditor textEditor) {
Editor editor = textEditor.getEditor();
Document document = editor.getDocument();
List<InlineProblem> problems = new ArrayList<>();
int lineCount = document.getLineCount();
if (lineCount <= 0) {
return problems;
}
int fileEndOffset = document.getLineEndOffset(lineCount - 1);
RangeHighlighter[] highlighters = DocumentMarkupModel
.forDocument(document, editor.getProject(), false)
.getAllHighlighters();
List<String> problemTextBeginningFilterList = new ArrayList<>(
Arrays.asList(SettingsState.getInstance().getProblemFilterList().split(";"))
);
Arrays.stream(highlighters)
.filter(h -> {
if (h.isValid() && h.getErrorStripeTooltip() instanceof HighlightInfo) {
HighlightInfo highlightInfo = (HighlightInfo) h.getErrorStripeTooltip();
return highlightInfo.getDescription() != null &&
!highlightInfo.getDescription().isEmpty() &&
problemTextBeginningFilterList.stream()
.noneMatch(f -> highlightInfo.getDescription().stripLeading().toLowerCase().startsWith(f.toLowerCase())) &&
fileEndOffset >= highlightInfo.getStartOffset()
;
}
return false;
})
.forEach(h -> {
HighlightInfo highlightInfo = (HighlightInfo) h.getErrorStripeTooltip();
InlineProblem newProblem = new InlineProblem(
document.getLineNumber(highlightInfo.getStartOffset()),
textEditor.getFile().getPath(),
highlightInfo,
textEditor,
h,
settingsState
);
problemManager.applyCustomSeverity(newProblem);
if (problemManager.shouldProblemBeIgnored(newProblem.getSeverity())) {
return;
}
problems.add(newProblem);
});
return problems;
}
public void restartManualScan() {
cancelScheduledFuture();
createAndStartScheduledFuture();
}
public void setDelayMilliseconds(int newDelayMilliseconds) {
delayMilliseconds = newDelayMilliseconds;
restartManualScan();
}
private void cancelScheduledFuture() {
if (!scheduledFuture.cancel(false)) {
if (!scheduledFuture.cancel(true)) {
logger.warn("Unable to cancel scheduledFuture");
}
}
}
private void createAndStartScheduledFuture() {
scheduledFuture = AppExecutorUtil.getAppScheduledExecutorService().scheduleWithFixedDelay(
() -> ApplicationManager.getApplication().invokeAndWait(this::scanForProblemsManually),
2000,
delayMilliseconds,
TimeUnit.MILLISECONDS
);
}
}