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 1 commit into
base: master
Choose a base branch
from

Conversation

shashi1687
Copy link

What this PR does

This adds a new “head-start” LSP server example under:
crates/rust-analyzer/examples/rust_analyzer_lsp_example/
It demonstrates how to:

  • Capture document edits with textDocument/didChange and log the URI
  • Stub out textDocument/completion (returns an empty list)
  • Stub out textDocument/formatting (returns no edits)

What the example does

  1. Initialization
    • Opens JSON-RPC over stdio
    • Advertises full text sync, a completion provider, and a formatting provider
  2. Main loop
    • Logs every buffer change (didChange)
    • Replies to completion requests with {"isIncomplete":false,"items":[]}
    • Replies to formatting requests with []
    • Handles clean shutdown on shutdown/exit

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

About the author’s note

“The current LSP example is a good, minimal demonstration. It shows how to respond to textDocument/definition. I used it to make an example that includes capturing content from textDocument/didChange and using it to provide textDocument/completion and textDocument/formatting functionality…”

  • We’ve taken that idea and upstreamed it as a fresh, in-tree example.
  • No external code was copied verbatim—just a clean re-implementation.

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

@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.

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