fix: Even more async syntax highlighting
This commit is contained in:
parent
8e498d75be
commit
ace85c6a5a
2 changed files with 34 additions and 22 deletions
|
@ -23,6 +23,11 @@ Changelog structure reference:
|
||||||
#### Error diagnostics (NEW)
|
#### Error diagnostics (NEW)
|
||||||
- Basic diagnostics info from LSP (mostly just trivial syntax errors)
|
- Basic diagnostics info from LSP (mostly just trivial syntax errors)
|
||||||
|
|
||||||
|
### Fixed
|
||||||
|
|
||||||
|
#### Syntax Highlighting
|
||||||
|
- Made the logic even more asynchronous, should lead to much less UI stuttering
|
||||||
|
|
||||||
## [0.3.1]
|
## [0.3.1]
|
||||||
|
|
||||||
### Added
|
### Added
|
||||||
|
|
|
@ -18,6 +18,9 @@ package com.falsepattern.zigbrains;
|
||||||
|
|
||||||
import com.falsepattern.zigbrains.lsp.ZLSEditorEventManager;
|
import com.falsepattern.zigbrains.lsp.ZLSEditorEventManager;
|
||||||
import com.intellij.openapi.application.ApplicationManager;
|
import com.intellij.openapi.application.ApplicationManager;
|
||||||
|
import com.intellij.openapi.application.ReadAction;
|
||||||
|
import com.intellij.openapi.application.WriteAction;
|
||||||
|
import com.intellij.openapi.editor.Editor;
|
||||||
import com.intellij.openapi.editor.markup.HighlighterLayer;
|
import com.intellij.openapi.editor.markup.HighlighterLayer;
|
||||||
import com.intellij.openapi.editor.markup.HighlighterTargetArea;
|
import com.intellij.openapi.editor.markup.HighlighterTargetArea;
|
||||||
import com.intellij.openapi.editor.markup.RangeHighlighter;
|
import com.intellij.openapi.editor.markup.RangeHighlighter;
|
||||||
|
@ -25,9 +28,12 @@ import com.intellij.openapi.util.Key;
|
||||||
import org.wso2.lsp4intellij.editor.EditorEventManager;
|
import org.wso2.lsp4intellij.editor.EditorEventManager;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collections;
|
||||||
import java.util.Comparator;
|
import java.util.Comparator;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.WeakHashMap;
|
||||||
|
import java.util.concurrent.locks.ReentrantLock;
|
||||||
import java.util.stream.Stream;
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
|
|
||||||
|
@ -36,40 +42,41 @@ public class HighlightingUtil {
|
||||||
|
|
||||||
public static void refreshHighlighting(EditorEventManager eem) {
|
public static void refreshHighlighting(EditorEventManager eem) {
|
||||||
var app = ApplicationManager.getApplication();
|
var app = ApplicationManager.getApplication();
|
||||||
if (!(eem instanceof ZLSEditorEventManager manager)) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
var editor = manager.editor;
|
|
||||||
if (editor == null) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
app.executeOnPooledThread(() -> {
|
app.executeOnPooledThread(() -> {
|
||||||
|
if (!(eem instanceof ZLSEditorEventManager manager)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
var editor = manager.editor;
|
||||||
|
if (editor == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
if (editor.isDisposed()) {
|
if (editor.isDisposed()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
var highlightRanges = manager.semanticHighlighting();
|
var highlightRanges = manager.semanticHighlighting();
|
||||||
var newHash = highlightRanges.hashCode();
|
var newHash = highlightRanges.hashCode();
|
||||||
app.invokeAndWait(() -> {
|
if (editor.isDisposed()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
var markup = editor.getMarkupModel();
|
||||||
|
var hash = markup.getUserData(HL_HASH);
|
||||||
|
if (hash != null && hash == newHash) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
markup.putUserData(HL_HASH, newHash);
|
||||||
|
var highlightersSorted = new ArrayList<>(Stream.of(WriteAction.computeAndWait(markup::getAllHighlighters))
|
||||||
|
.map(hl -> Map.entry(hl, hl.getStartOffset()))
|
||||||
|
.sorted(Comparator.comparingInt(Map.Entry::getValue))
|
||||||
|
.toList());
|
||||||
|
app.invokeLater(() -> {
|
||||||
if (editor.isDisposed()) {
|
if (editor.isDisposed()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
var markup = editor.getMarkupModel();
|
if (highlightRanges.size() == 1 && highlightRanges.get(0).start() == 0 && highlightRanges.get(0).remove() == -1) {
|
||||||
var hash = markup.getUserData(HL_HASH);
|
markup.removeAllHighlighters();
|
||||||
if (hash != null && hash == newHash) {
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
markup.putUserData(HL_HASH, newHash);
|
|
||||||
List<Map.Entry<RangeHighlighter, Integer>> highlightersSorted = null;
|
|
||||||
var documentLength = editor.getDocument().getTextLength();
|
var documentLength = editor.getDocument().getTextLength();
|
||||||
for (var range : highlightRanges) {
|
for (var range : highlightRanges) {
|
||||||
if (range.start() == 0 && range.remove() == -1 && highlightRanges.size() == 1) {
|
|
||||||
markup.removeAllHighlighters();
|
|
||||||
} else if (highlightersSorted == null) {
|
|
||||||
highlightersSorted = new ArrayList<>(Stream.of(markup.getAllHighlighters())
|
|
||||||
.map(hl -> Map.entry(hl, hl.getStartOffset()))
|
|
||||||
.sorted(Comparator.comparingInt(Map.Entry::getValue))
|
|
||||||
.toList());
|
|
||||||
}
|
|
||||||
var start = range.start();
|
var start = range.start();
|
||||||
var toRemove = range.remove();
|
var toRemove = range.remove();
|
||||||
if (toRemove > 0) {
|
if (toRemove > 0) {
|
||||||
|
|
Loading…
Add table
Reference in a new issue