-
-
Notifications
You must be signed in to change notification settings - Fork 90
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
10 changed files
with
124 additions
and
39 deletions.
There are no files selected for viewing
Validating CODEOWNERS rules …
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
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
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
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 |
---|---|---|
@@ -1,36 +1,30 @@ | ||
import Gio from "gi://Gio"; | ||
import { setup } from "./rust.js"; | ||
|
||
import Document from "../../Document.js"; | ||
import { applyTextEdits } from "../../lsp/sourceview.js"; | ||
|
||
export class RustDocument extends Document { | ||
async format() { | ||
const code = await formatRustCode(this.buffer.text); | ||
this.code_view.replaceText(code, true); | ||
} | ||
} | ||
|
||
function formatRustCode(text) { | ||
const rustfmtLauncher = Gio.SubprocessLauncher.new( | ||
Gio.SubprocessFlags.STDIN_PIPE | | ||
Gio.SubprocessFlags.STDOUT_PIPE | | ||
Gio.SubprocessFlags.STDERR_PIPE, | ||
); | ||
constructor(...args) { | ||
super(...args); | ||
|
||
const rustfmtProcess = rustfmtLauncher.spawnv([ | ||
"rustfmt", | ||
"--quiet", | ||
"--emit", | ||
"stdout", | ||
"--edition", | ||
"2021", | ||
]); | ||
this.lspc = setup({ document: this }); | ||
} | ||
|
||
const [success, stdout, stderr] = rustfmtProcess.communicate_utf8(text, null); | ||
async format() { | ||
// https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textDocument_formatting | ||
const text_edits = await this.lspc.request("textDocument/formatting", { | ||
textDocument: { | ||
uri: this.file.get_uri(), | ||
}, | ||
options: { | ||
tabSize: 4, | ||
insertSpaces: true, | ||
trimTrailingWhitespace: true, | ||
insertFinalNewline: true, | ||
trimFinalNewlines: true, | ||
}, | ||
}); | ||
|
||
if (!success || stderr !== "") { | ||
console.error(`Error running rustfmt: ${stderr}`); | ||
return text; | ||
applyTextEdits(text_edits, this.buffer); | ||
} | ||
|
||
return stdout; | ||
} |
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,37 @@ | ||
import Gio from "gi://Gio"; | ||
|
||
import { createLSPClient } from "../../common.js"; | ||
import { getLanguage } from "../../util.js"; | ||
|
||
export function setup({ document }) { | ||
const { file, buffer, code_view } = document; | ||
|
||
const lspc = createLSPClient({ | ||
lang: getLanguage("rust"), | ||
root_uri: file.get_parent().get_uri(), | ||
}); | ||
lspc.buffer = buffer; | ||
lspc.uri = file.get_uri(); | ||
lspc.connect( | ||
"notification::textDocument/publishDiagnostics", | ||
(_self, params) => { | ||
if (params.uri !== file.get_uri()) { | ||
return; | ||
} | ||
code_view.handleDiagnostics(params.diagnostics); | ||
}, | ||
); | ||
|
||
lspc.start().catch(console.error); | ||
|
||
buffer.connect("modified-changed", () => { | ||
if (!buffer.get_modified()) return; | ||
lspc.didChange().catch(console.error); | ||
}); | ||
|
||
return lspc; | ||
} | ||
|
||
export const rust_template_dir = Gio.File.new_for_path( | ||
pkg.pkgdatadir, | ||
).resolve_relative_path("langs/rust/template"); |
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