-
-
Notifications
You must be signed in to change notification settings - Fork 89
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Rust analyzer support #868
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
49ad910
Add Rust analyzer support
sonnyp e9ff826
f
sonnyp 8e359db
Merge branch 'main' into rust-language-server
sonnyp ab0ca69
rust 4 tabSize
sonnyp 9f05898
debug
sonnyp face6c5
Merge branch 'main' into rust-language-server
sonnyp be7852d
f
sonnyp 3196ebc
f
sonnyp File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will copy Rust template files such as
Cargo.toml
andworkbench.rs
when creating a new session from a demo.We already do this on compile but if we want diagnostics to work before pressing "Run" we need this here too.