Repository navigation
Integrated Language Server - #10723
Conversation
|
@fdncred, @jokeyrhyme I tested something in nushell and integrated |
|
@schrieveslaach This seems like a good start to integrating What I'd like to see here is both the I haven't really used |
|
I'm on my phone but will check this out a bit later I did use |
|
Thanks for getting a start on this! <3 I think there was a previous suggestion about having a plain Having "start" in there makes me think a background daemon will be created and then the command will terminate, which might be what we want, but then we'll probably want an accompanying Otherwise, if the command is intended to stay alive and serve LSP requests, then maybe drop the "start"? 🤷 |
|
Is it worth using the |
|
Related: nushell/vscode-nushell-lang#162 |
c723f35 to
9bc05d4
Compare
|
@fdncred, @jokeyrhyme, I updated the PR to keep the previous behavior and to provide a command line option |
|
Thanks for the roadmap/checklist :) I wonder if there's a way we can collaborate a little more closely? Should I raise PRs against your branch? There's great work here that effectively covers jokeyrhyme/nuls#7 , but then the rest of There might be a way we can avoid duplication of effort, although some duplication is probably unavoidable |
|
Oh, also, I'm not sure if you've checked it out, but there's a room dedicated to editor support in the official Discord: |
|
I've updated the draft PR for the Visual Studio Code extension, so that there's now a setting to point it at |
|
From a nushell core-team perspective, we'd definitely like to see these two approaches to a language server converge into one excellent approach. We appreciate all the effort from @schrieveslaach and @jokeyrhyme. Thanks @jokeyrhyme for getting the vscode extension setup to use We definitely like this PR's approach of just calling the nushell functions instead of shelling out. We also like that it's using So, if we can just merge our efforts here, I think we can get to the goal of having a lsp pretty quickly. No need to have it perfect on the first merge, now that we have the extension setup to handle it, it should be pretty easy to switch back and forth between lsps for testing. Really great work so far y'all! |
|
@fdncred, I'm very happy to read your appreciation statement and I'm glad to help here.
I specifically chose lsp-server instead of tower due to minimal set of dependencies. 😉
What about then making this PR into the initial PR that adds the corresponding LSP responses for Shall we create a tracking issue then? |
No, I didn't because I'm into Discord. Is that something you think is really required? |
Not especially (and I'm usually on the Matrix room rather than the Discord server, as a preference), I was just thinking out loud |
The nushell team is not opposed to an incremental approach The Code extension will have a switch in it, so there's no expectation for a completed LSP server here as part of this PR Once this PR is ready, we can raise issues for any follow-up items That way we can use issue assignments, etc |
|
@schrieveslaach Yes, please create a tracking issue with the tasks as markdown checkboxes. Yes, we can land this when the first pieces are ready, but the entirety isn't complete. Since this PR is really entirely separate from nushell (but built in) we should feel free to merge it whenever we think the first step is ready. And then followup PRs documented a little bit in the tracking issue could follow on behind this one. One thing we need to make sure of though is that we do not break the existing lsp functionality. So, all the Ping me when the tracking issue is created and we're ready to merge this PR. |
|
@schrieveslaach I think we were wanting it as a separate crate before we merge, but if it's easier to start as a mod, then that's fine. I kind of think what you're suggesting is better, adding all the lsp support stuff separate to the ide commands, because it should easily allow us to have both coexist, which is what we want at this point. |
|
FYI, I started from scratch where I build the LSP support in a separate crate. I also started with test driven development so that the LSP support has some proper tests. |
oh, nice. TDD should provide ample tests then. Good idea. I guess we can close this PR when you open your new one. Thanks! |
|
Exciting stuff @schrieveslaach, thanks for the effort! |
@fdncred, I wanted to update this PR. Do you want me to create a new one instead? I was thinking that updating this one is better because it has been referenced by other issues and PRs. |
|
@schrieveslaach I'm fine with using this PR still. I just thought you meant you were going to close it when you said you started from scratch. |
|
Yep, but nushell itself could have declared that anything other than UNIX line endings is invalid and not a valid nushell script, refusing to execute anything else That would mean nobody would have Windows line endings anywhere for us to deal with here (unless they wanted to curate their own collection of scripts that nushell cannot execute, for some reason) This is a hypothetical lament, of course, seeing as the ship has sailed :) |
|
Ah, a LineIterator. That's an interesting approach. Hopefully it work better with some tweaking. |
|
I could be way off here, but after looking at this a little bit, I'm wondering if In our ide.rs code, In nushell, if you do I could be way off too. I've just looked at a few sections of your code so far. I'm leaning towards me being wrong after trying to consume more code. It's like we're trying to convert a span to a position, which is a little different since position has line and character and span is just an index range in an array. |
|
The vscode extension also converts spans to positions but it goes about it a little different. Just posting this here in case you haven't seen it. Not sure how helpful it is. function convertSpan(utf8_offset: number, lineBreaks: Array<number>): Position {
const lineBreakIndex = lowerBoundBinarySearch(lineBreaks, utf8_offset);
const start_of_line_offset =
lineBreakIndex == -1 ? 0 : lineBreaks[lineBreakIndex] + 1;
const character = Math.max(0, utf8_offset - start_of_line_offset);
return { line: lineBreakIndex + 1, character };
}
function lowerBoundBinarySearch(arr: number[], num: number): number {
let low = 0;
let mid = 0;
let high = arr.length - 1;
if (num >= arr[high]) return high;
while (low < high) {
// Bitshift to avoid floating point division
mid = (low + high) >> 1;
if (arr[mid] < num) {
low = mid + 1;
} else {
high = mid;
}
}
return low - 1;
}and it uses this to find the linebreaks function findLineBreaks(utf16_text: string): Array<number> {
const utf8_text = new TextEncoder().encode(utf16_text);
const lineBreaks: Array<number> = [];
for (let i = 0; i < utf8_text.length; ++i) {
if (utf8_text[i] == 0x0a) {
lineBreaks.push(i);
}
}
return lineBreaks;
} |
|
In let resp = goto_definition(script.clone(), 4, 1);
eprintln!("response: {:?}", resp);I've changed the fn goto_definition(uri: Url, line: u32, character: u32) -> Message {
let (client_connection, _recv) = initialize_language_server();
let method_name = GotoDefinition::METHOD.to_string();
eprintln!("method_name: {}", method_name);
let method_params = serde_json::to_value(GotoDefinitionParams {
text_document_position_params: TextDocumentPositionParams {
text_document: TextDocumentIdentifier { uri },
position: lsp_types::Position { line, character },
},
work_done_progress_params: Default::default(),
partial_result_params: Default::default(),
})
.unwrap();
eprintln!("method_params: {:?}", method_params);
let mess_req = Message::Request(lsp_server::Request {
id: 2.into(),
method: method_name,
params: method_params,
});
eprintln!("goto_def_mess_req: {:?}", mess_req);
client_connection.sender.send(mess_req).unwrap();
client_connection
.receiver
.recv_timeout(std::time::Duration::from_secs(5))
.unwrap()
}What makes it even more confusing is |
|
Sorry for all the debug spam here @schrieveslaach. I think I may be on to something here. I changed fn lsp_position_to_location(position: &lsp_types::Position, file: &[u8]) -> usize {
let content = String::from_utf8_lossy(file);
eprintln!("content: {:?}", content);
for (p, char_index) in LineIterator::from_file(&content) {
let ch = match content.chars().nth(char_index) {
Some('\n') => "LF".to_string(),
Some('\r') => "CR".to_string(),
Some(v) => v.to_string(),
_ => "err".to_string(),
};
eprintln!("p: {:?}, char_index: {}, char: {}", p, char_index, ch);
if p == *position {
eprintln!("found match: p: {:?} char_index: {}", p, char_index);
return char_index;
}
}
eprintln!("not found");
content.len() - 1
}So I could see how it was checking the data. It looks to me that the LineIterator isn't quite working right because of this output. Notice that I changed "4, 1" to "4, 5" in let resp = goto_definition(script.clone(), 4, 5);
eprintln!("response: {:?}", resp);Which means that if I change the assert to this assert_json_eq!(
result,
serde_json::json!({
"uri": script,
"range": {
"start": { "line": 0, "character": 17 },
"end": { "line": 4, "character": 0 }
}
})
);it passes on Windows. That's probably a good thing but I'm also guessing that it would fail on Linux. So, I'm still thinking these errors are all about line endings. I can't really mess with this anymore for now. Hope something here helps! |
59d0019 to
5769fe2
Compare
This commit integrates a language server into nushell so that IDEs don't have to convert CLI option back and forth.
|
@fdncred, thank you very much for looking into this. During the day I came to the conclusion that maybe a rope would be a good data structure for the rescue because a) it provides indexing and conversion out of the box and b) when start working on messages of Maybe, it is also worthwhile to use this structure at the core of Nushell but that is out of scope of this PR. I just wanted to start a conversation on it. I'll go ahead and mark this PR as ready. |
|
Let's go! |
|
Amazing, great work! <3 |
|
Thanks a lot for accepting my contribution. |
|
@schrieveslaach People are already testing it and trying to integrate it into nvim and helix. If you waant to participate in these chats, please come to our #editor-support on Discord. |
|
Very cool! This might make it easier for us (Zed) to integrate the language server! |
@JosephTLyons Hopefully so, this is very early and things don't work right yet. I didn't have any problems with |
# Description This commit integrates a language server into nushell so that IDEs don't have to convert CLI option back and forth. - fixes nushell/vscode-nushell-lang#117 - fixes jokeyrhyme/nuls#8 Tracking tasks - [x] [textDocument/hover](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textDocument_hover) -> `nu --ide-hover` - [x] [textDocument/completion](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textDocument_completion) -> `nu --ide-complete` - [x] [textDocument/definition](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textDocument_definition) -> `nu --ide-goto-def` - ~~[ ] [textDocument/didChange](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textDocument_didChange), [textDocument/didClose](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textDocument_didClose), and [textDocument/didOpen](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textDocument_didOpen)~~ (will be done in a follow-up PR) - ~~[ ] [textDocument/inlayHint](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textDocument_inlayHint) -> `nu --ide-check`~~ (will be done in a follow-up PR) - ~~[ ] [textDocument/publishDiagnostics](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textDocument_publishDiagnostics) -> `nu --ide-check`~~ (will be done in a follow-up PR) - ~~[ ] [workspace/configuration](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#workspace_configuration)~~ (will be done in a follow-up PR) - ~~[ ] [workspace/didChangeConfiguration](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#workspace_didChangeConfiguration)~~ (will be done in a follow-up PR) # User-Facing Changes The command line options `--lsp` will start a LSP server. # Tests + Formatting <!-- Don't forget to add tests that cover your changes. Make sure you've run and fixed any issues with these commands: - `cargo fmt --all -- --check` to check standard code formatting (`cargo fmt --all` applies these changes) - `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used` to check that you're using the standard code style - `cargo test --workspace` to check that all tests pass (on Windows make sure to [enable developer mode](https://learn.microsoft.com/en-us/windows/apps/get-started/developer-mode-features-and-debugging)) - `cargo run -- -c "use std testing; testing run-tests --path crates/nu-std"` to run the tests for the standard library > **Note** > from `nushell` you can also use the `toolkit` as follows > ```bash > use toolkit.nu # or use an `env_change` hook to activate it automatically > toolkit check pr > ``` --> # After Submitting <!-- If your PR had any user-facing changes, update [the documentation](https://github.com/nushell/nushell.github.io) after the PR is merged, if necessary. This will help us keep the docs up to date. -->
# Description This commit integrates a language server into nushell so that IDEs don't have to convert CLI option back and forth. - fixes nushell/vscode-nushell-lang#117 - fixes jokeyrhyme/nuls#8 Tracking tasks - [x] [textDocument/hover](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textDocument_hover) -> `nu --ide-hover` - [x] [textDocument/completion](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textDocument_completion) -> `nu --ide-complete` - [x] [textDocument/definition](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textDocument_definition) -> `nu --ide-goto-def` - ~~[ ] [textDocument/didChange](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textDocument_didChange), [textDocument/didClose](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textDocument_didClose), and [textDocument/didOpen](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textDocument_didOpen)~~ (will be done in a follow-up PR) - ~~[ ] [textDocument/inlayHint](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textDocument_inlayHint) -> `nu --ide-check`~~ (will be done in a follow-up PR) - ~~[ ] [textDocument/publishDiagnostics](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textDocument_publishDiagnostics) -> `nu --ide-check`~~ (will be done in a follow-up PR) - ~~[ ] [workspace/configuration](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#workspace_configuration)~~ (will be done in a follow-up PR) - ~~[ ] [workspace/didChangeConfiguration](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#workspace_didChangeConfiguration)~~ (will be done in a follow-up PR) # User-Facing Changes The command line options `--lsp` will start a LSP server. # Tests + Formatting <!-- Don't forget to add tests that cover your changes. Make sure you've run and fixed any issues with these commands: - `cargo fmt --all -- --check` to check standard code formatting (`cargo fmt --all` applies these changes) - `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used` to check that you're using the standard code style - `cargo test --workspace` to check that all tests pass (on Windows make sure to [enable developer mode](https://learn.microsoft.com/en-us/windows/apps/get-started/developer-mode-features-and-debugging)) - `cargo run -- -c "use std testing; testing run-tests --path crates/nu-std"` to run the tests for the standard library > **Note** > from `nushell` you can also use the `toolkit` as follows > ```bash > use toolkit.nu # or use an `env_change` hook to activate it automatically > toolkit check pr > ``` --> # After Submitting <!-- If your PR had any user-facing changes, update [the documentation](https://github.com/nushell/nushell.github.io) after the PR is merged, if necessary. This will help us keep the docs up to date. -->
Description
This commit integrates a language server into nushell so that IDEs don't have to convert CLI option back and forth.
nuexecutable jokeyrhyme/nuls#8Tracking tasks
nu --ide-hovernu --ide-completenu --ide-goto-def[ ] textDocument/didChange, textDocument/didClose, and textDocument/didOpen(will be done in a follow-up PR)[ ] textDocument/inlayHint ->(will be done in a follow-up PR)nu --ide-check[ ] textDocument/publishDiagnostics ->(will be done in a follow-up PR)nu --ide-check[ ] workspace/configuration(will be done in a follow-up PR)[ ] workspace/didChangeConfiguration(will be done in a follow-up PR)User-Facing Changes
The command line options
--lspwill start a LSP server.Tests + Formatting
After Submitting