-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
179 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import GObject from "gi://GObject"; | ||
import Gtk from "gi://Gtk"; | ||
import Source from "gi://GtkSource"; | ||
|
||
import { rangeEquals } from "./util.js"; | ||
|
||
class RetroHoverProvider extends GObject.Object { | ||
constructor() { | ||
super(); | ||
this.diagnostics = []; | ||
} | ||
|
||
findDiagnostics(context) { | ||
const iter = new Gtk.TextIter(); | ||
context.get_iter(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, | ||
}); | ||
container.add_css_class("hoverdisplay"); | ||
container.add_css_class("osd"); | ||
container.add_css_class("frame"); | ||
|
||
for (const { message } of diagnostics) { | ||
const label = new Gtk.Label({ | ||
halign: Gtk.Align.START, | ||
label: `${message}`, | ||
}); | ||
label.add_css_class("body"); | ||
container.append(label); | ||
} | ||
|
||
display.append(container); | ||
} | ||
|
||
vfunc_populate(context, display) { | ||
try { | ||
const diagnostics = this.findDiagnostics(context); | ||
if (diagnostics.length < 1) return false; | ||
this.showDiagnostics(display, diagnostics); | ||
} catch (err) { | ||
logError(err); | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
} | ||
|
||
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 GObject.registerClass( | ||
{ | ||
GTypeName: "RetroHoverProvider", | ||
Implements: [Source.HoverProvider], | ||
}, | ||
RetroHoverProvider | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
export function rangeEquals(start, end) { | ||
return start.line === end.line && start.character === end.character; | ||
} | ||
|
||
export function getItersAtRange(buffer, { start, end }) { | ||
let start_iter; | ||
let end_iter; | ||
|
||
// Apply the tag on the whole line | ||
// if diagnostic start and end are equals such as | ||
// Blueprint-Error 13:12 to 13:12 Could not determine what kind of syntax is meant here | ||
if (rangeEquals(start, end)) { | ||
[, start_iter] = buffer.get_iter_at_line(start.line); | ||
[, end_iter] = buffer.get_iter_at_line(end.line); | ||
end_iter.forward_to_line_end(); | ||
start_iter.forward_find_char((char) => char !== "", end_iter); | ||
} else { | ||
[, start_iter] = buffer.get_iter_at_line_offset( | ||
start.line, | ||
start.character | ||
); | ||
[, end_iter] = buffer.get_iter_at_line_offset(end.line, end.character); | ||
} | ||
|
||
return [start_iter, end_iter]; | ||
} |