Skip to content

Add richer LSP server example (#20017) #20187

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

shashi1687
Copy link

@shashi1687 shashi1687 commented Jul 6, 2025

Summary

Adds a new “head-start” LSP server example inside crates/lsp-server/examples/. This fresh example shows how to:

  • Log document edits (textDocument/didChange)
  • Provide a stubbed completion handler (returns an empty list)
  • Provide a stubbed formatting handler (returns no edits)

Details

  1. Setup

    • Launches JSON-RPC over stdio
    • Advertises full-text sync, completion, and formatting capabilities
  2. Message Loop

    • Logs each didChange URI
    • Responds to completion with {"isIncomplete":false,"items":[]}
    • Responds to formatting with []
    • Cleans up on shutdown/exit

This minimal template makes it easy to embed rust-analyzer as an LSP server with basic completion and formatting hooks.


Testing & Validation

  1. Build
    cargo build -p rust-analyzer --example rust_analyzer_lsp_example

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jul 6, 2025
@shashi1687 shashi1687 force-pushed the add-lsp-server-example branch 2 times, most recently from 00ccfee to fc7a459 Compare July 6, 2025 16:39
@shashi1687 shashi1687 force-pushed the add-lsp-server-example branch from fc7a459 to b39376c Compare July 6, 2025 17:02
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this example should be located under lib/lsp-server/examples/ like the previous one

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for catching that! You’re absolutely right—I’ve now moved the example into lib/lsp-server/examples/ alongside goto_def.rs. The file is renamed to completion_and_formatting.rs, and I’ve updated the crates/lsp-server/Cargo.toml manifest accordingly. Let me know if you spot anything else!

@ShoyuVanilla
Copy link
Member

ShoyuVanilla commented Jul 8, 2025

Well, I think that the original intention of the OP in #20017 is asking opinions about adding their existing lsp-server example codes to rust-analyzer repository, not implementing new examples, though I don't have strong opinion on it.

What the example does

...
This gives anyone embedding rust-analyzer a minimal “plug-in” template for completions and formatting.

Besides that, I think this example is not that "richer" than existing goto_def.rs one because most of the scaffolding codes are almost same, and I can't really understand what is the intention of the message handling examples in this PR: It's just a personal thought but I think it would be better to be somewhat more exhaustive on different kinds of messages or more focused or detailed on a specific category of messages.
In short, I'd like to ask "What would be the proper filename for this when located in the same directory with goto_def.rs example?"

@lnicola
Copy link
Member

lnicola commented Jul 8, 2025

Yeah, I think a single example would be better.

- Move the completion/formatting example from
  crates/rust-analyzer/examples/ to
  crates/lsp-server/examples/
- Rename file to completion_and_formatting.rs to match snake_case
  and clearly indicate both features
- Add anyhow = "1.0" under [dev-dependencies] in
  crates/lsp-server/Cargo.toml so the example compiles without error
@shashi1687
Copy link
Author

Well, I think that the original intention of the OP in #20017 is asking opinions about adding their existing lsp-server example codes to rust-analyzer repository, not implementing new examples, though I don't have strong opinion on it.

What the example does

...
This gives anyone embedding rust-analyzer a minimal “plug-in” template for completions and formatting.

Besides that, I think this example is not that "richer" than existing goto_def.rs one because most of the scaffolding codes are almost same, and I can't really understand what is the intention of the message handling examples in this PR: It's just a personal thought but I think it would be better to be somewhat more exhaustive on different kinds of messages or more focused or detailed on a specific category of messages. In short, I'd like to ask "What would be the proper filename for this when located in the same directory with goto_def.rs example?"

Thank you so much for raising these points—I really appreciate your insight! 🙏
You’re absolutely right that a richer sample could handle additional messages (hover, semantic tokens, etc.), and I think that would make a great follow-up PR once this one is settled.

As for the filename, would you be comfortable with completion_and_formatting.rs? It follows our snake_case style and clearly indicates the two features it covers, sitting neatly beside goto_def.rs. If you’d prefer a different name or any other tweaks, I’m more than happy to update—just let me know what you’d like!

@shashi1687
Copy link
Author

Yeah, I think a single example would be better.

Thank you again for all your feedback! 🙏

At this point, would you prefer that I close this PR, or is there anything else you’d like me to adjust before we merge it? Please let me know what you’d recommend, and I’ll act accordingly.

@shashi1687 shashi1687 requested a review from ShoyuVanilla July 8, 2025 18:48
@ShoyuVanilla
Copy link
Member

ShoyuVanilla commented Jul 9, 2025

@lnicola and I pondered upon this and we think in current status, this would be best to be merged into existing goto_def.rs as they share much scaffolding codes in common - of course, the filename goto_def.rs should be adjusted`.

I'll comment on some details though if you take the "merging" suggestion most of them wouldn't be needed 😅


[[example]]
name = "completion_and_formatting"
path = "examples/completion_and_formatting.rs"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need this because the new example code is located inside examples/

//! rust-analyzer LSP example
//!
//! A minimal Language Server Protocol server demonstrating how to embed
//! `rust-analyzer` as a library. To run it:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"embed rust-analyzer" is not a right term here. This lsp-server is just a crate that rust-analyzer dogfoods on to handle lsp requests and responses.
So, the examples are not for rust-analyer or embedding. They just show how to use this "library"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants