Skip to content

Commit 5fabd10

Browse files
committed
use lsp for formatting
1 parent 4de12a6 commit 5fabd10

File tree

3 files changed

+23
-27
lines changed

3 files changed

+23
-27
lines changed

.vscode/extensions.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@
99
"prince781.vala",
1010
"bodil.blueprint-gtk",
1111
"dbaeumer.vscode-eslint",
12-
"ms-python.black-formatter"
12+
"charliermarsh.ruff"
1313
]
1414
}

src/langs/python/PythonDocument.js

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,34 @@
1-
import Gio from "gi://Gio";
1+
import { setup } from "./python.js";
22

33
import Document from "../../Document.js";
4-
import { setup } from "./python.js";
4+
import { applyTextEdits } from "../../lsp/sourceview.js";
55

66
export class PythonDocument extends Document {
77
constructor(...args) {
88
super(...args);
99

10-
/// XXX: We should await setup, but we can't in a Constructor. This would require bigger refactoring.
1110
this.lspc = setup({ document: this });
1211
}
1312

1413
async format() {
15-
const code = await formatPythonCode(this.buffer.text);
16-
this.code_view.replaceText(code, true);
14+
// https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textDocument_formatting
15+
const text_edits = await this.lspc.request("textDocument/formatting", {
16+
textDocument: {
17+
uri: this.file.get_uri(),
18+
},
19+
options: {
20+
tabSize: 4,
21+
insertSpaces: true,
22+
trimTrailingWhitespace: true,
23+
insertFinalNewline: true,
24+
trimFinalNewlines: true,
25+
},
26+
});
27+
28+
// lsp Ruff doesn't support diff - it just returns one edit
29+
// we don't want to loose the cursor position so we use this
30+
const state = this.code_view.saveState();
31+
applyTextEdits(text_edits, this.buffer);
32+
await this.code_view.restoreState(state);
1733
}
1834
}
19-
20-
function formatPythonCode(text) {
21-
const ruffLauncher = Gio.SubprocessLauncher.new(
22-
Gio.SubprocessFlags.STDIN_PIPE |
23-
Gio.SubprocessFlags.STDOUT_PIPE |
24-
Gio.SubprocessFlags.STDERR_PIPE,
25-
);
26-
27-
const ruffProcess = ruffLauncher.spawnv(["ruff", "format", "--quiet", "-"]);
28-
29-
const [success, stdout, stderr] = ruffProcess.communicate_utf8(text, null);
30-
31-
if (!success || stderr !== "") {
32-
console.error(`Error running ruff format: ${stderr}`);
33-
return text;
34-
}
35-
36-
return stdout;
37-
}

src/langs/python/python.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,12 @@ const LSP_CONFIG = {
1515
},
1616
};
1717

18-
export async function setup({ document }) {
18+
export function setup({ document }) {
1919
const { file, buffer, code_view } = document;
2020

2121
const lspc = createLSPClient({
2222
lang: getLanguage("python"),
2323
root_uri: file.get_parent().get_uri(),
24-
quiet: true,
2524
});
2625

2726
lspc.request("workspace/didChangeConfiguration", {

0 commit comments

Comments
 (0)