-
-
Notifications
You must be signed in to change notification settings - Fork 90
Expand file tree
/
Copy pathWorkbenchHoverProvider.js
More file actions
86 lines (70 loc) · 2.15 KB
/
WorkbenchHoverProvider.js
File metadata and controls
86 lines (70 loc) · 2.15 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
import GObject from "gi://GObject";
import Gtk from "gi://Gtk";
import Source from "gi://GtkSource";
import { rangeEquals } from "./lsp/LSP.js";
import { registerClass } from "./overrides.js";
class WorkbenchHoverProvider extends GObject.Object {
constructor() {
super();
this.diagnostics = [];
}
findDiagnostics(context) {
const [, iter] = context.get_iter();
const line = iter.get_line();
// Looks like line_offset starts at 0
// Blueprint starts at 1
const character = iter.get_line_offset() + 1;
return findDiagnostics(this.diagnostics, { line, character });
}
showDiagnostics(display, diagnostics) {
const container = new Gtk.Box({
orientation: Gtk.Orientation.VERTICAL,
spacing: 4,
css_classes: ["hoverdisplay", "osd", "frame"],
});
for (const { message } of diagnostics) {
const label = new Gtk.Label({
halign: Gtk.Align.START,
label: `${message}`,
css_classes: ["body"],
});
container.append(label);
}
display.append(container);
}
vfunc_populate(context, display) {
try {
const diagnostics = this.findDiagnostics(context);
if (diagnostics.length < 1) return [false, null];
this.showDiagnostics(display, diagnostics);
} catch (err) {
console.error(err);
return [false, null];
}
return [true, null];
}
}
function findDiagnostics(diagnostics, position) {
return diagnostics.filter((diagnostic) => {
return isDiagnosticInRange(diagnostic, position);
});
}
export function isDiagnosticInRange(diagnostic, { line, character }) {
const { start, end } = diagnostic.range;
// The tag is applied on the whole line
// when diagnostic start and end ranges are equals
if (rangeEquals(start, end) && line === start.line) return true;
if (line < start.line) return false;
if (line > end.line) return false;
return (
(line >= start.line && character >= start.character - 1) ||
(line <= end.line && character <= end.character + 1)
);
}
export default registerClass(
{
GTypeName: "WorkbenchHoverProvider",
Implements: [Source.HoverProvider],
},
WorkbenchHoverProvider,
);