|
| 1 | +# Sharpy LSP Server |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +Sharpy includes a built-in Language Server Protocol (LSP) server, accessible via `sharpyc lsp`. The server is implemented in the `Sharpy.Lsp` project using [OmniSharp.Extensions.LanguageServer](https://github.com/OmniSharp/csharp-language-server-protocol), providing IDE features to any editor that supports LSP. |
| 6 | + |
| 7 | +## Architecture |
| 8 | + |
| 9 | +The LSP server consists of three main layers: |
| 10 | + |
| 11 | +- **SharplyWorkspace** -- Manages document state (open files, edits, file versions). Tracks document content in memory and synchronizes with the compiler. |
| 12 | +- **CompilerApi** -- Provides analysis services (parsing, type checking, diagnostics) by invoking the Sharpy compiler pipeline on demand. |
| 13 | +- **Handlers** -- Implement individual LSP protocol methods, delegating to CompilerApi for analysis and returning results in the LSP wire format. |
| 14 | + |
| 15 | +``` |
| 16 | +Editor <-> stdio (JSON-RPC) <-> LSP Server |
| 17 | + | |
| 18 | + +-- Handlers (one per LSP method) |
| 19 | + | | |
| 20 | + | +-- CompilerApi (analysis) |
| 21 | + | | |
| 22 | + | +-- Sharpy Compiler Pipeline |
| 23 | + | |
| 24 | + +-- SharplyWorkspace (document state) |
| 25 | +``` |
| 26 | + |
| 27 | +## Supported Features |
| 28 | + |
| 29 | +### Document Synchronization |
| 30 | + |
| 31 | +| Method | Description | |
| 32 | +|--------|-------------| |
| 33 | +| `textDocument/didOpen` | Track newly opened documents | |
| 34 | +| `textDocument/didChange` | Apply incremental edits | |
| 35 | +| `textDocument/didClose` | Release document state | |
| 36 | +| `textDocument/didSave` | Trigger full re-analysis on save | |
| 37 | + |
| 38 | +### Diagnostics |
| 39 | + |
| 40 | +| Method | Description | |
| 41 | +|--------|-------------| |
| 42 | +| `textDocument/publishDiagnostics` | Push compiler errors, warnings, and info to the editor | |
| 43 | + |
| 44 | +Diagnostics are published after each document change (debounced) and include all `SPY`-prefixed diagnostic codes from the compiler. |
| 45 | + |
| 46 | +### Navigation |
| 47 | + |
| 48 | +| Method | Description | |
| 49 | +|--------|-------------| |
| 50 | +| `textDocument/definition` | Go-to-definition using `Symbol.DeclarationSpan` | |
| 51 | +| `textDocument/references` | Find all references using SemanticInfo reference tracking | |
| 52 | +| `textDocument/documentSymbol` | Hierarchical document outline (classes, functions, variables) | |
| 53 | +| `textDocument/documentHighlight` | Highlight all occurrences of a symbol in the current document | |
| 54 | +| `workspace/symbol` | Workspace-wide symbol search | |
| 55 | + |
| 56 | +### Intelligence |
| 57 | + |
| 58 | +| Method | Description | |
| 59 | +|--------|-------------| |
| 60 | +| `textDocument/hover` | Type information for identifiers, expressions, and function calls | |
| 61 | +| `textDocument/completion` | Scope-aware, member, and type completion | |
| 62 | +| `textDocument/signatureHelp` | Parameter hints during function calls | |
| 63 | +| `textDocument/inlayHint` | Inferred type and parameter name annotations | |
| 64 | + |
| 65 | +### Refactoring |
| 66 | + |
| 67 | +| Method | Description | |
| 68 | +|--------|-------------| |
| 69 | +| `textDocument/rename` | Symbol rename with validation (rejects invalid names) | |
| 70 | +| `textDocument/codeAction` | Quick fixes for naming conventions, unused imports, unused variables | |
| 71 | + |
| 72 | +### Display |
| 73 | + |
| 74 | +| Method | Description | |
| 75 | +|--------|-------------| |
| 76 | +| `textDocument/semanticTokens` | Semantic highlighting (types, functions, parameters, etc.) | |
| 77 | +| `textDocument/foldingRange` | Code folding for classes, functions, and block statements | |
| 78 | +| `textDocument/codeLens` | Reference counts on symbols; run buttons for entry points | |
| 79 | +| `textDocument/formatting` | Indentation normalization | |
| 80 | + |
| 81 | +## Transport |
| 82 | + |
| 83 | +The LSP server communicates over **stdio** (stdin/stdout) using the JSON-RPC 2.0 protocol. This is the standard transport for LSP and works with all major editors. |
| 84 | + |
| 85 | +```bash |
| 86 | +sharpyc lsp |
| 87 | +``` |
| 88 | + |
| 89 | +The server reads JSON-RPC messages from stdin and writes responses to stdout. Logging and diagnostics go to stderr. |
| 90 | + |
| 91 | +## Threading Model |
| 92 | + |
| 93 | +- **Document map**: `ConcurrentDictionary<Uri, Document>` for thread-safe document access |
| 94 | +- **Per-document lock**: `SemaphoreSlim` per document prevents concurrent analysis of the same file |
| 95 | +- **Debounce**: 300ms debounce on `didChange` events to avoid redundant re-analysis during rapid typing |
| 96 | + |
| 97 | +## Configuration |
| 98 | + |
| 99 | +The server accepts configuration via the LSP `workspace/didChangeConfiguration` notification. Settings are typically configured through the editor (e.g., VSCode settings). See [vscode-extension.md](vscode-extension.md) for the full settings reference. |
0 commit comments