Skip to content

Commit 895894b

Browse files
authored
Use GTKCssLanguageServer formatter (#860)
1 parent 47666fb commit 895894b

File tree

11 files changed

+84
-11030
lines changed

11 files changed

+84
-11030
lines changed

.github/CODEOWNERS

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
11
* @sonnyp
2+
3+
/src/langs/vala/ @lw64
24
*.vala @lw64
5+
6+
/src/langs/rust/ @lw64
37
*.rs @Hofer-Julian
48
Cargo.toml @Hofer-Julian
59
Cargo.lock @Hofer-Julian
10+
11+
/src/langs/python/ @theCapypara
12+
*.py @theCapypara
13+
614
*.c @andyholmes
715
*.h @andyholmes
8-
*.py @theCapypara

src/PanelStyle.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
import Gio from "gi://Gio";
22
import GObject from "gi://GObject";
33

4-
import { setup as setupCSS } from "./langs/css/css.js";
5-
6-
export default function PanelStyle({ builder, document_css, settings }) {
4+
export default function PanelStyle({ builder, settings }) {
75
const button_style = builder.get_object("button_style");
86
const panel_style = builder.get_object("panel_style");
97
settings.bind(
@@ -18,5 +16,4 @@ export default function PanelStyle({ builder, document_css, settings }) {
1816
"visible",
1917
GObject.BindingFlags.SYNC_CREATE,
2018
);
21-
setupCSS({ document: document_css });
2219
}

src/langs/css/CssDocument.js

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,33 @@
1-
import { format as prettier } from "../../lib/prettier.js";
2-
import prettier_postcss from "../../lib/prettier-postcss.js";
3-
41
import Document from "../../Document.js";
2+
import { applyTextEdits } from "../../lsp/sourceview.js";
3+
import { setup } from "./css.js";
54

65
export class CssDocument extends Document {
6+
constructor(...args) {
7+
super(...args);
8+
9+
this.lspc = setup({ document: this });
10+
}
11+
712
async format() {
8-
const code = await prettier(this.buffer.text, {
9-
parser: "css",
10-
plugins: [prettier_postcss],
13+
// https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textDocument_formatting
14+
const text_edits = await this.lspc.request("textDocument/formatting", {
15+
textDocument: {
16+
uri: this.file.get_uri(),
17+
},
18+
options: {
19+
tabSize: 2,
20+
insertSpaces: true,
21+
trimTrailingWhitespace: true,
22+
insertFinalNewline: true,
23+
trimFinalNewlines: true,
24+
},
1125
});
12-
this.code_view.replaceText(code, true);
26+
27+
// Biome doesn't support diff - it just returns one edit
28+
// we don't want to loose the cursor position so we use this
29+
const state = this.code_view.saveState();
30+
applyTextEdits(text_edits, this.buffer);
31+
await this.code_view.restoreState(state);
1332
}
1433
}

src/langs/css/css.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ export function setup({ document }) {
1313
if (!code_view.buffer.get_modified()) return;
1414
lspc.didChange().catch(console.error);
1515
});
16+
17+
return lspc;
1618
}
1719

1820
function createLSPClient({ code_view, file }) {

src/langs/css/meson.build

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
gjspack = find_program(meson.project_source_root() / 'troll/gjspack/bin/gjspack')
2+
custom_target('prettier',
3+
input: ['prettier.js'],
4+
output: ['prettier', 'prettier.src.gresource'],
5+
command: [
6+
gjspack,
7+
'--resource-root', meson.project_source_root() / 'src',
8+
'@INPUT0@',
9+
'@OUTDIR@',
10+
],
11+
install: true,
12+
install_dir: get_option('bindir'),
13+
build_always_stale: true,
14+
)
15+

src/langs/css/prettier.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#!/usr/bin/env -S gjs -m
2+
3+
// Shim to Node.js prettier CLI for GTKCssLanguageServer
4+
// ./src/langs/css/prettier.js --stdin-filepath src/style.css
5+
// or
6+
// ./troll/gjspack/bin/gjspack src/langs/css/prettier.js src/langs/css && ./src/langs/css/prettier --stdin-filepath src/style.css
7+
8+
import { exit } from "system";
9+
import Gio from "gi://Gio";
10+
11+
import { format } from "../../lib/prettier.js";
12+
import prettier_postcss from "../../lib/prettier-postcss.js";
13+
14+
const idx = ARGV.indexOf("--stdin-filepath");
15+
if (idx < 0) exit(1);
16+
const filename = ARGV[idx + 1];
17+
if (!filename) exit(1);
18+
19+
const file = Gio.File.new_for_path(filename);
20+
const [, contents] = file.load_contents(null);
21+
const text = new TextDecoder().decode(contents);
22+
23+
const formatted = await format(text, {
24+
parser: "css",
25+
plugins: [prettier_postcss],
26+
});
27+
28+
// eslint-disable-next-line no-restricted-globals
29+
print(formatted);

src/lib/prettier-babel.js

Lines changed: 0 additions & 18 deletions
This file was deleted.

src/lib/prettier-estree.js

Lines changed: 0 additions & 37 deletions
This file was deleted.

0 commit comments

Comments
 (0)