/
Highlighter.java
601 lines (521 loc) · 22.7 KB
/
Highlighter.java
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
/*
* Copyright 2003-2022 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package jetbrains.mps.nodeEditor;
import com.intellij.ide.PowerSaveMode;
import com.intellij.openapi.Disposable;
import com.intellij.openapi.application.ApplicationListener;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.command.CommandEvent;
import com.intellij.openapi.components.ProjectComponent;
import com.intellij.openapi.fileEditor.FileEditorManager;
import com.intellij.openapi.project.DumbService;
import com.intellij.openapi.project.IndexNotReadyException;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Disposer;
import com.intellij.util.ConcurrencyUtil;
import com.intellij.util.concurrency.EdtExecutorService;
import com.intellij.util.messages.MessageBusConnection;
import jetbrains.mps.RuntimeFlags;
import jetbrains.mps.classloading.ClassLoaderManager;
import jetbrains.mps.classloading.DeployListener;
import jetbrains.mps.ide.MPSCoreComponents;
import jetbrains.mps.ide.ThreadUtils;
import jetbrains.mps.ide.project.ProjectHelper;
import jetbrains.mps.logging.Logger;
import jetbrains.mps.make.MakeServiceComponent;
import jetbrains.mps.module.ReloadableModule;
import jetbrains.mps.nodeEditor.checking.EditorChecker;
import jetbrains.mps.nodeEditor.highlighter.EditorCheckerWrapper;
import jetbrains.mps.nodeEditor.highlighter.EditorComponentCreateListener;
import jetbrains.mps.nodeEditor.highlighter.HighlighterEditorList;
import jetbrains.mps.nodeEditor.highlighter.HighlighterEditorTracker;
import jetbrains.mps.nodeEditor.highlighter.HighlighterEventCollector;
import jetbrains.mps.nodeEditor.highlighter.HighlighterUpdateSession;
import jetbrains.mps.nodeEditor.highlighter.IHighlighter;
import jetbrains.mps.openapi.editor.Editor;
import jetbrains.mps.openapi.editor.message.EditorMessageOwner;
import jetbrains.mps.project.MPSProject;
import jetbrains.mps.smodel.event.SModelEvent;
import jetbrains.mps.smodel.event.SModelReplacedEvent;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.mps.openapi.model.SModelReference;
import org.jetbrains.mps.openapi.repository.CommandListener;
import org.jetbrains.mps.openapi.util.ProgressMonitor;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.Future;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;
public class Highlighter implements IHighlighter, ProjectComponent {
private static final Logger LOG = Logger.getLogger(Highlighter.class);
private volatile boolean myPaused;
private final com.intellij.openapi.command.CommandListener myCommandListener = new PauseDuringCommandOrUndoTransparentAction();
private final ClassLoaderManager myClassLoaderManager;
private ScheduledExecutorService myBackgroundExecutor;
private ScheduleHighlighterUpdate myScheduleHighlighterUpdate;
// no idea if shall register with project as parent disposable
private final Disposable myDisposable = Disposer.newDisposable(Highlighter.class.getName());
private final List<EditorCheckerWrapper> myCheckers = new CopyOnWriteArrayList<>();
/**
* Whether to force running all checkers in power-save mode. Accessed from the highlighter thread only, therefore non-volatile.
*/
private boolean myForceUpdateInPowerSaveModeFlag = false;
private InspectorTool myInspectorTool;
private final DeployListener myClassesListener = new DeployListener() {
@Override
public void onUnloaded(@NotNull Set<ReloadableModule> modules, @NotNull ProgressMonitor monitor) {
addPendingAction(() -> {
myEditorTracker.markEverythingUnchecked();
myEditorList.clearAdditionalEditors();
});
}
};
private final Project myProject;
private final MPSProject myMPSProject;
private final CommandWatcher myCommandWatcher = new CommandWatcher();
private final HighlighterEditorList myEditorList;
private final HighlighterEventCollector myEventCollector = new HighlighterEventCollector();
// Keeps track of which editors may be checked incrementally. Must only be accessed from the highlighter background thread.
private final HighlighterEditorTracker myEditorTracker = new HighlighterEditorTracker();
private final MakeServiceComponent myMakeComponent;
public static Highlighter getInstance(MPSProject mpsProject) {
return mpsProject.getComponent(Highlighter.class);
}
public Highlighter(Project project) {
// it's important that MPSProject is initialized before this component, and un-initialized after
myMPSProject = ProjectHelper.fromIdeaProjectOrFail(project);
myProject = project;
myEditorList = new HighlighterEditorList(FileEditorManager.getInstance(project));
MPSCoreComponents coreComponents = MPSCoreComponents.getInstance();
myClassLoaderManager = coreComponents.getClassLoaderManager();
myMakeComponent = coreComponents.getPlatform().findComponent(MakeServiceComponent.class);
}
@Override
public void projectOpened() {
myClassLoaderManager.addListener(myClassesListener);
myEventCollector.startListening(myMPSProject.getRepository());
myInspectorTool = myProject.getComponent(InspectorTool.class);
// perhaps, should register myDisposable with myProject as parent not to rely
// solely on projectClose()?
MessageBusConnection mbCon = myProject.getMessageBus().connect(myDisposable);
mbCon.subscribe(EditorComponentCreateListener.EDITOR_COMPONENT_CREATION, new EditorComponentCreateListener() {
@Override
public void editorComponentCreated(@NotNull EditorComponent editorComponent) {
}
@Override
public void editorComponentDisposed(@NotNull final EditorComponent editorComponent) {
if (myEditorTracker.isInspector(editorComponent)) {
addPendingAction(myEditorTracker::markInspectorUnchecked);
}
}
});
ApplicationManager.getApplication().addApplicationListener(new PauseDuringWriteAction(), myDisposable);
mbCon.subscribe(com.intellij.openapi.command.CommandListener.TOPIC, myCommandListener);
myMPSProject.getModelAccess().addCommandListener(myCommandWatcher);
}
@Override
public void projectClosed() {
myMPSProject.getModelAccess().removeCommandListener(myCommandWatcher);
// remove all listeners associated with our own Disposable instance, including those from message buss
Disposer.dispose(myDisposable);
myEventCollector.stopListening(myMPSProject.getRepository());
myClassLoaderManager.removeListener(myClassesListener);
myInspectorTool = null;
}
@Override
@NonNls
@NotNull
public String getComponentName() {
return "MPS Highlighter";
}
@Override
public void initComponent() {
startUpdater();
}
@Override
public void disposeComponent() {
stopUpdater();
}
@Override
public MPSProject getProject() {
return myMPSProject;
}
private Future<?> addPendingAction(Runnable action) {
myCommandWatcher.resetGracePeriod(); // grace period may grow large with multiple extendGracePeriod() calls, wake the thread up
return myBackgroundExecutor.submit(action);
}
private <T> Future<T> addPendingAction(Callable<T> action) {
return myBackgroundExecutor.submit(action);
}
public void addChecker(@NotNull final EditorChecker checker) {
if (RuntimeFlags.isTestMode() || ApplicationManager.getApplication().isHeadlessEnvironment()) {
return;
}
addPendingAction(() -> {
myCheckers.add(new EditorCheckerWrapper(checker));
myEditorTracker.markEverythingUnchecked();
});
}
/**
* Removes a checker from the set of active checkers. Also removes its messages from any known open editors. Must be called from EDT.
*
* @param checker the checker to remove
*/
public void removeChecker(@NotNull final EditorChecker checker) {
if (RuntimeFlags.isTestMode() || ApplicationManager.getApplication().isHeadlessEnvironment()) {
return;
}
ThreadUtils.assertEDT();
// Checker removal is done in three steps:
//
// 1. Remove the checker's wrapper from the internal list of checkers and stop it.
EditorCheckerWrapper wrapper = findWrapperFor(checker);
if (wrapper == null) {
return;
}
myCheckers.remove(wrapper);
EditorMessageOwner messageOwner = wrapper.getEditorMessageOwner();
// After dispose() completes it is guaranteed that the highlighter will not run the checker in the background anymore.
wrapper.stop();
// 2. In EDT (since UI access is required) get a list of all editors that are currently open.
final List<EditorComponent> editors = myEditorList.getAllEditors();
if (editors.isEmpty()) {
return;
}
// 3. In the highlighter thread again (actually, any background thread would do), go through the list retrieved in the previous step and remove
// the checker's messages from each editor.
addPendingAction(() -> {
long time = System.currentTimeMillis();
for (EditorComponent component : editors) {
component.getHighlightManager().clearForOwner(messageOwner, true);
}
if (LOG.isDebugLevel()) {
long elapsed = System.currentTimeMillis() - time;
LOG.debug(String.format("Removing %s messages from %d editors took %d ms", messageOwner, editors.size(), elapsed));
}
});
}
@Nullable
private EditorCheckerWrapper findWrapperFor(@NotNull EditorChecker checker) {
EditorCheckerWrapper wrapper = null;
for (EditorCheckerWrapper candidate : myCheckers) {
if (candidate.isWrapping(checker)) {
wrapper = candidate;
break;
}
}
return wrapper;
}
public void addAdditionalEditorComponent(EditorComponent additionalEditorComponent) {
myEditorList.addAdditionalEditorComponent(additionalEditorComponent);
}
public void removeAdditionalEditorComponent(EditorComponent additionalEditorComponent) {
myEditorList.removeAdditionalEditorComponent(additionalEditorComponent);
}
public void addAdditionalEditor(Editor additionalEditor) {
myEditorList.addAdditionalEditor(additionalEditor);
}
public void removeAdditionalEditor(Editor additionalEditor) {
myEditorList.removeAdditionalEditor(additionalEditor);
}
private void startUpdater() {
if (myBackgroundExecutor != null && !myBackgroundExecutor.isShutdown()) {
LOG.error("Highlighter is already initialized", new Throwable());
return;
}
myBackgroundExecutor = ConcurrencyUtil.newSingleScheduledThreadExecutor("Highlighter");
myScheduleHighlighterUpdate = new ScheduleHighlighterUpdate(EdtExecutorService.getScheduledExecutorInstance(), DumbService.getInstance(myProject));
if (!RuntimeFlags.isTestMode() && !ApplicationManager.getApplication().isHeadlessEnvironment()) {
myScheduleHighlighterUpdate.scheduleNext();
}
}
public void stopUpdater() {
myScheduleHighlighterUpdate = null;
myBackgroundExecutor.shutdown();
try {
myBackgroundExecutor.awaitTermination(100, TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {
LOG.error(e);
}
}
@NotNull
private HighlighterUpdateSession createUpdateSession(List<EditorComponent> activeEditors, boolean essentialOnly) {
processAccumulatedEvents();
final Set<EditorCheckerWrapper> checkers = new LinkedHashSet<>();
if (!PowerSaveMode.isEnabled() || myForceUpdateInPowerSaveModeFlag) {
// calling checkers only if we are not in powerSafeMode or updateEditorFlag was set by
// explicit update action (available in powerSafeMode only)
for (EditorCheckerWrapper checker : myCheckers) {
if (!essentialOnly || checker.isEssential()) {
checkers.add(checker);
}
}
checkers.addAll(myCheckers);
myForceUpdateInPowerSaveModeFlag = false;
}
if (PowerSaveMode.isEnabled()) {
// if we are in powerSaveMode then next editor checkers execution should
// recheck all editors completely
myEditorTracker.markEverythingUnchecked();
} else {
myEditorTracker.markOnlyEditorsChecked(activeEditors);
}
return new HighlighterUpdateSession(Highlighter.this, checkers, activeEditors, getInspector());
}
public void resetCheckedStateInBackground(final EditorComponent editorComponent) {
addPendingAction(() -> {
myForceUpdateInPowerSaveModeFlag = true;
myEditorTracker.markUnchecked(editorComponent);
if (myEditorTracker.isInspector(editorComponent)) {
return;
}
for (EditorCheckerWrapper checker : myCheckers) {
checker.forceAutofix(editorComponent);
}
});
}
@Override
public boolean isStopping() {
return myBackgroundExecutor.isShutdown();
}
@NotNull
@Override
public HighlighterEditorTracker getEditorTracker() {
return myEditorTracker;
}
private EditorComponent getInspector() {
if (myInspectorTool == null) {
return null;
}
return myInspectorTool.getInspector();
}
/**
* Feeds events collected at this point to all registered checkers for processing. Must be called on the highlighter thread because the collection of all
* checkers is accessed.
*/
private void processAccumulatedEvents() {
List<SModelEvent> events = myEventCollector.drainEvents();
for (SModelEvent event : events) {
if (event instanceof SModelReplacedEvent) {
final SModelReference mref = event.getModel().getReference();
myEditorTracker.markEditorsOfModelUnchecked(mref);
}
}
for (EditorCheckerWrapper checker : myCheckers) {
checker.processEvents(events);
}
}
private void pauseUpdater() {
myPaused = true;
}
private void resumeUpdater() {
myPaused = false;
}
@Override
public boolean isPausedOrStopping() {
return myPaused || isStopping();
}
// XXX why not ModelAccess's WriteActionListener and mps.CommandListener?
private class PauseDuringWriteAction implements ApplicationListener {
@Override
public void beforeWriteActionStart(@NotNull Object action) {
pauseUpdater();
}
@Override
public void writeActionFinished(@NotNull Object action) {
resumeUpdater();
}
}
private class PauseDuringCommandOrUndoTransparentAction implements com.intellij.openapi.command.CommandListener {
private int myLevel = 0;
@Override
public void commandStarted(CommandEvent event) {
increaseLevel();
}
@Override
public void commandFinished(CommandEvent event) {
decreaseLevel();
}
@Override
public void undoTransparentActionStarted() {
increaseLevel();
}
@Override
public void undoTransparentActionFinished() {
decreaseLevel();
}
private void increaseLevel() {
if (myLevel == 0) {
pauseUpdater();
}
myLevel++;
}
private void decreaseLevel() {
myLevel--;
if (myLevel == 0) {
resumeUpdater();
}
}
}
/**
* Runs in EDT
* FIXME In fact, there's no true need to perform isGoodTimeToUpdate from an EDT thread.
* If we would do it from another thread, we can avoid 'heartbeat' check and to fire off
* EDT read that accesses active editors on regular Java monitor wake up (or any other thread
* synch mechanism). That would simplify grace period management a lot (no need to extend)
*/
private class ScheduleHighlighterUpdate implements Runnable {
private final ScheduledExecutorService myEdtExecutor;
private final DumbService myDumbService;
public ScheduleHighlighterUpdate(ScheduledExecutorService edtExecutor, DumbService dumbService) {
myEdtExecutor = edtExecutor;
myDumbService = dumbService;
}
@Override
public void run() {
if (!isGoodTimeToUpdate()) {
if (!isStopping()) {
scheduleNext();
}
return;
}
List<EditorComponent> activeEditors = myEditorList.getActiveEditors(); // Must be called in EDT
// don't use addPendingAction() as it resets grace period
myBackgroundExecutor.submit(() -> {
try {
update(activeEditors);
} catch (Throwable t) {
LOG.error("Error occurred during highlighter update", t);
}
});
}
private boolean isGoodTimeToUpdate() {
return !isPausedOrStopping() && !myDumbService.isDumb() && !myMakeComponent.isSessionActive() && myCommandWatcher.isGracePeriodExpired();
}
private void update(List<EditorComponent> activeEditors) {
try {
final boolean updateAllCheckers = myCommandWatcher.getAndClearHasChangesFlag();
// there had been changes, ensure we check with all possible checkers and do it asap.
if (updateAllCheckers) {
// I reset grace period here, prior to update to ensure we fire check again soon in case something goes wrong
myCommandWatcher.resetGracePeriod();
}
createUpdateSession(activeEditors, !updateAllCheckers).update();
// XXX in fact, would be better to check if update() had succeeded and only then extend grace period.
myCommandWatcher.extendGracePeriod();
} catch (IndexNotReadyException ex) {
myCommandWatcher.resetGracePeriod();
myEditorTracker.markEverythingUnchecked();
} finally {
scheduleNext();
}
}
private void scheduleNext() {
myEdtExecutor.schedule(this, myCommandWatcher.timeToCheck4ExpiredGracePeriod(), TimeUnit.MILLISECONDS);
}
}
/**
* The idea is to delay highlighter when user actively modifies a model.
* When a command comes, we enlarge grace period till the next highlighter start ({@link #timeToCheck4ExpiredGracePeriod()}.
* Otherwise, when there are no commands, there is highlighter 'heartbeat' that asks {@link EditorChecker#needsUpdate(EditorComponent)}.
* Unfortunately, there are few EditorChecker implementations, that always tell {@code true} and perform internal result caching; therefore
* one can observe constant activity of highlighter thread even when there's no user activity.
* <p>
* Thread safe.
*/
private static class CommandWatcher implements CommandListener {
// base time unit for checks, don't run more often than once in that period
private static final int DEFAULT_GRACE_PERIOD_MS = 150;
// this is how long one could look at the editor and don't perceive highlighter hang
private static final int MAX_TOLERABLE_DELAY_MS = DEFAULT_GRACE_PERIOD_MS * 7;
// frequency to check grace period expiration.
// When a command comes during long grace period, we don't want to wait for the period to end normally,
// but there's no mechanism to notify ScheduleHighlighterUpdate, therefore we ask it to get back to us and check often enough.
private static final int EXPIRATION_HEARTBEAT_MS = DEFAULT_GRACE_PERIOD_MS * 2;
// the moment in time we track grace period from
private final AtomicLong myGracePeriodResetTime = new AtomicLong();
// period of time starting from myGracePeriodResetTime we'd like to perform NO highlighting, increases if a user doesn't modify models.
private final AtomicLong myGracePeriod = new AtomicLong();
private volatile boolean myHasChanges;
CommandWatcher() {
myHasChanges = true;
resetGracePeriod();
}
boolean isGracePeriodExpired() {
final long time = System.currentTimeMillis();
long delta = time - myGracePeriodResetTime.get();
return delta >= myGracePeriod.get();
}
boolean getAndClearHasChangesFlag() {
final boolean rv = myHasChanges;
myHasChanges = false;
return rv;
}
// tell there's no need to run highlighter any time soon
void extendGracePeriod() {
// XXX perhaps, shall use geometric progression (although capped)
myGracePeriod.getAndAdd(MAX_TOLERABLE_DELAY_MS);
myGracePeriodResetTime.set(System.currentTimeMillis());
}
void resetGracePeriod() {
myGracePeriod.set(DEFAULT_GRACE_PERIOD_MS);
myGracePeriodResetTime.set(System.currentTimeMillis());
}
long timeToCheck4ExpiredGracePeriod() {
final long time = System.currentTimeMillis();
final long delta = time - myGracePeriodResetTime.get();
assert delta >= 0;
final long left = myGracePeriod.get() - delta;
// max(left,DEFAULT_GRACE_PERIOD_MS) ensures we don't scheduleNext one by one with 0 delay just because there's e.g. make session
// and isGoodTimeToUpdate() == false.
// EXPIRATION_HEARTBEAT_MS helps to react promptly to grace period change due to commands, i.e.
// we don't want to wait for a whole extended grace period once there's a change in a model.
// Clients shall check #isGracePeriodExpired() once told delay is over to make sure the period is in fact over
return Math.min(Math.max(left, DEFAULT_GRACE_PERIOD_MS), EXPIRATION_HEARTBEAT_MS);
}
@Override
public void commandStarted() {
final long time = System.currentTimeMillis();
long delta = time - myGracePeriodResetTime.get();
// there are 2 scenarios:
// active modification, time between old and new command is small here, we need to ensure grace period is not shorter not to intervene with editing.
// long pause between commands (user inactivity), time period between old previous and actual command is huge
//
if (delta > MAX_TOLERABLE_DELAY_MS) {
// if there's long delay from last reset, check soon
delta = DEFAULT_GRACE_PERIOD_MS;
}
// extend grace period with to reflect frequency of events, so that we don't report isGracePeriodExpired right away
// twice the delta as one delta value amounts to time already passed to command start.
myGracePeriod.getAndSet(Math.max(delta * 2, DEFAULT_GRACE_PERIOD_MS));
// guess, the idea of this piece of code was to avoid highlighter to start for at least amount of time close to pause between
// two commands, so that if a user types (issues commands) with more or less constant speed, highlighter doesn't jump in
}
@Override
public void commandFinished() {
myHasChanges = true;
final long time = System.currentTimeMillis();
myGracePeriodResetTime.set(time);
// we keep whatever value we have accumulated so far in myGracePeriod, which presumably accounts for possible subsequent command
}
}
}