Skip to content

Commit

Permalink
Add Blueprint formatter support
Browse files Browse the repository at this point in the history
  • Loading branch information
sonnyp committed Sep 9, 2023
1 parent 7b0f9b4 commit 59a65a5
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 10 deletions.
6 changes: 3 additions & 3 deletions build-aux/modules/blueprint-compiler.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
"builddir": true,
"sources": [
{
"type": "archive",
"url": "https://gitlab.gnome.org/jwestman/blueprint-compiler/-/archive/v0.10.0/blueprint-compiler-v0.10.0.tar.gz",
"sha512": "1f2a9097e3d6a030385c371a01e32caefab6f7ede1979b43492255b504250ac7f44ae22a97656a7c33c0e518236b95d6074baf8f87c53df1fbb1e42eceb39a87"
"type": "git",
"url": "https://gitlab.gnome.org/gregorni/blueprint-compiler.git",
"branch": "formatter"
}
]
}
2 changes: 1 addition & 1 deletion build-aux/re.sonny.Workbench.Devel.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
"sources": [
{
"type": "dir",
"path": "."
"path": "../.."
}
],
"post-install": [
Expand Down
2 changes: 1 addition & 1 deletion build-aux/re.sonny.Workbench.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
"sources": [
{
"type": "dir",
"path": "."
"path": "../.."
}
],
"post-install": [
Expand Down
1 change: 1 addition & 0 deletions src/PanelUI.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ export default function PanelUI({
panel.stop = stop;
panel.update = update;
panel.panel = panel_ui;
panel.blueprint = blueprint;

return panel;
}
42 changes: 42 additions & 0 deletions src/langs/blueprint/blueprint.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import GLib from "gi://GLib";
// import Source from "gi://GtkSource";

import LSPClient from "../../lsp/LSPClient.js";

Expand Down Expand Up @@ -40,9 +41,49 @@ export function setup({ document }) {
});
return blp;
},
async format(text) {
// https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textDocument_formatting
const text_edits = await lspc.request("textDocument/formatting", {
textDocument: {
uri: file.get_uri(),
},
options: {
tabSize: 2,
insertSpaces: true,
trimTrailingWhitespace: true,
insertFinalNewline: true,
trimFinalNewlines: true,
},
});

console.log(text_edits);
if (!text_edits) return text;

let new_text = text;
for (const text_edit of text_edits) {
new_text = applyTextEdit(text_edit, code_view.buffer, new_text);
}

return new_text;
},
};
}

function applyTextEdit({ range, newText }, buffer, text) {
const { start, end } = range;
const [, start_iter] = buffer.get_iter_at_line_offset(
start.line,
start.character,
);
const [, end_iter] = buffer.get_iter_at_line_offset(end.line, end.character);

return (
text.slice(0, start_iter.get_offset()) +
newText +
text.slice(end_iter.get_offset())
);
}

const SYSLOG_IDENTIFIER = pkg.name;

export function logBlueprintError(err) {
Expand Down Expand Up @@ -72,6 +113,7 @@ function createLSPClient({ code_view, file }) {
uri,
languageId: "blueprint",
buffer: code_view.buffer,
quiet: false,
});

lspc.connect("exit", () => {
Expand Down
10 changes: 5 additions & 5 deletions src/lsp/LSPClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const clientInfo = {
};

export default class LSPClient {
constructor(argv, { rootUri, uri, languageId, buffer }) {
constructor(argv, { rootUri, uri, languageId, buffer, quiet = true }) {
this.argv = argv;
this.started = false;
this.proc = null;
Expand All @@ -30,6 +30,7 @@ export default class LSPClient {
this.version = 0;
this.buffer = buffer;
this.ready = false;
this.quiet = quiet;

// https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#clientCapabilities
this.capabilities = {
Expand Down Expand Up @@ -132,10 +133,9 @@ export default class LSPClient {
_start_process() {
let flags =
Gio.SubprocessFlags.STDIN_PIPE | Gio.SubprocessFlags.STDOUT_PIPE;
// vala-language-server and blueprint are pretty verbose
// https://github.com/vala-lang/vala-language-server/issues/274
// comment this to debug LSP
flags = flags | Gio.SubprocessFlags.STDERR_SILENCE;
if (this.quiet) {
flags = flags | Gio.SubprocessFlags.STDERR_SILENCE;
}

this.proc = Gio.Subprocess.new(this.argv, flags);
this.proc
Expand Down
3 changes: 3 additions & 0 deletions src/window.js
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,9 @@ export default function Window({ application, session }) {
await format(langs.xml.document.code_view, (text) => {
return xml.format(text, 2);
});
await format(langs.blueprint.document.code_view, async (text) => {
return panel_ui.blueprint.format(text);
});
}
}

Expand Down

0 comments on commit 59a65a5

Please sign in to comment.