lsp-pi 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,178 @@
1
+ # LSP Extension
2
+
3
+ Language Server Protocol integration for pi-coding-agent.
4
+
5
+ ## Highlights
6
+
7
+ - **Hook** (`lsp.ts`): Auto-diagnostics (default at agent end; optional per `write`/`edit`)
8
+ - **Tool** (`lsp-tool.ts`): On-demand LSP queries (definitions, references, hover, symbols, diagnostics, signatures)
9
+ - Manages one LSP server per project root and reuses them across turns
10
+ - **Efficient**: Bounded memory usage via LRU cache and idle file cleanup
11
+ - Supports TypeScript/JavaScript, Vue, Svelte, Dart/Flutter, Python, Go, Kotlin, Swift, and Rust
12
+
13
+ ## Supported Languages
14
+
15
+ | Language | Server | Detection |
16
+ |----------|--------|-----------|
17
+ | TypeScript/JavaScript | `typescript-language-server` | `package.json`, `tsconfig.json` |
18
+ | Vue | `vue-language-server` | `package.json`, `vite.config.ts` |
19
+ | Svelte | `svelteserver` | `svelte.config.js` |
20
+ | Dart/Flutter | `dart language-server` | `pubspec.yaml` |
21
+ | Python | `pyright-langserver` | `pyproject.toml`, `requirements.txt` |
22
+ | Go | `gopls` | `go.mod` |
23
+ | Kotlin | `kotlin-ls` | `settings.gradle(.kts)`, `build.gradle(.kts)`, `pom.xml` |
24
+ | Swift | `sourcekit-lsp` | `Package.swift`, Xcode (`*.xcodeproj` / `*.xcworkspace`) |
25
+ | Rust | `rust-analyzer` | `Cargo.toml` |
26
+
27
+ ### Known Limitations
28
+
29
+ **rust-analyzer**: Very slow to initialize (30-60+ seconds) because it compiles the entire Rust project before returning diagnostics. This is a known rust-analyzer behavior, not a bug in this extension. For quick feedback, consider using `cargo check` directly.
30
+
31
+ ## Usage
32
+
33
+ ### Installation
34
+
35
+ Install the package and enable extensions:
36
+ ```bash
37
+ pi install git:github.com/prateekmedia/pi-hooks
38
+ pi config
39
+ ```
40
+
41
+ Dependencies are installed automatically during `pi install`.
42
+
43
+ ### Prerequisites
44
+
45
+ Install the language servers you need:
46
+
47
+ ```bash
48
+ # TypeScript/JavaScript
49
+ npm i -g typescript-language-server typescript
50
+
51
+ # Vue
52
+ npm i -g @vue/language-server
53
+
54
+ # Svelte
55
+ npm i -g svelte-language-server
56
+
57
+ # Python
58
+ npm i -g pyright
59
+
60
+ # Go (install gopls via go install)
61
+ go install golang.org/x/tools/gopls@latest
62
+
63
+ # Kotlin (kotlin-ls)
64
+ brew install JetBrains/utils/kotlin-lsp
65
+
66
+ # Swift (sourcekit-lsp; macOS)
67
+ # Usually available via Xcode / Command Line Tools
68
+ xcrun sourcekit-lsp --help
69
+
70
+ # Rust (install via rustup)
71
+ rustup component add rust-analyzer
72
+ ```
73
+
74
+ The extension spawns binaries from your PATH.
75
+
76
+ ## How It Works
77
+
78
+ ### Hook (auto-diagnostics)
79
+
80
+ 1. On `session_start`, warms up LSP for detected project type
81
+ 2. Tracks files touched by `write`/`edit`
82
+ 3. Default (`agent_end`): at agent end, sends touched files to LSP and posts a diagnostics message
83
+ 4. Optional (`edit_write`): per `write`/`edit`, appends diagnostics to the tool result
84
+ 5. Shows notification with diagnostic summary
85
+ 6. **Memory Management**: Keeps up to 30 files open per LSP server (LRU eviction) and automatically closes idle files (> 60s) to prevent memory bloat in long-running sessions.
86
+ 7. **Robustness**: Reuses cached diagnostics if a server doesn't re-publish them for unchanged files, avoiding false timeouts on re-analysis.
87
+
88
+ ### Tool (on-demand queries)
89
+
90
+ The `lsp` tool provides these actions:
91
+
92
+ | Action | Description | Requires |
93
+ |--------|-------------|----------|
94
+ | `definition` | Jump to definition | `file` + (`line`/`column` or `query`) |
95
+ | `references` | Find all references | `file` + (`line`/`column` or `query`) |
96
+ | `hover` | Get type/docs info | `file` + (`line`/`column` or `query`) |
97
+ | `symbols` | List symbols in file | `file`, optional `query` filter |
98
+ | `diagnostics` | Get single file diagnostics | `file`, optional `severity` filter |
99
+ | `workspace-diagnostics` | Get diagnostics for multiple files | `files` array, optional `severity` filter |
100
+ | `signature` | Get function signature | `file` + (`line`/`column` or `query`) |
101
+ | `rename` | Rename symbol across files | `file` + (`line`/`column` or `query`) + `newName` |
102
+ | `codeAction` | Get available quick fixes/refactors | `file` + `line`/`column`, optional `endLine`/`endColumn` |
103
+
104
+ **Query resolution**: For position-based actions, you can provide a `query` (symbol name) instead of `line`/`column`. The tool will find the symbol in the file and use its position.
105
+
106
+ **Severity filtering**: For `diagnostics` and `workspace-diagnostics` actions, use the `severity` parameter to filter results:
107
+ - `all` (default): Show all diagnostics
108
+ - `error`: Only errors
109
+ - `warning`: Errors and warnings
110
+ - `info`: Errors, warnings, and info
111
+ - `hint`: All including hints
112
+
113
+ **Workspace diagnostics**: The `workspace-diagnostics` action analyzes multiple files at once. Pass an array of file paths in the `files` parameter. Each file will be opened, analyzed by the appropriate LSP server, and diagnostics returned. Files are cleaned up after analysis to prevent memory bloat.
114
+
115
+ ```bash
116
+ # Find all TypeScript files and check for errors
117
+ find src -name "*.ts" -type f | xargs ...
118
+
119
+ # Example tool call
120
+ lsp action=workspace-diagnostics files=["src/index.ts", "src/utils.ts"] severity=error
121
+ ```
122
+
123
+ Example questions the LLM can answer using this tool:
124
+ - "Where is `handleSessionStart` defined in `lsp-hook.ts`?"
125
+ - "Find all references to `getManager`"
126
+ - "What type does `getDefinition` return?"
127
+ - "List symbols in `lsp-core.ts`"
128
+ - "Check all TypeScript files in src/ for errors"
129
+ - "Get only errors from `index.ts`"
130
+ - "Rename `oldFunction` to `newFunction`"
131
+ - "What quick fixes are available at line 10?"
132
+
133
+ ## Settings
134
+
135
+ Use `/lsp` to configure the auto diagnostics hook:
136
+ - Mode: default at agent end; can run after each edit/write or be disabled
137
+ - Scope: session-only or global (`~/.pi/agent/settings.json`)
138
+
139
+ To disable auto diagnostics, choose "Disabled" in `/lsp` or set in `~/.pi/agent/settings.json`:
140
+ ```json
141
+ {
142
+ "lsp": {
143
+ "hookMode": "disabled"
144
+ }
145
+ }
146
+ ```
147
+ Other values: `"agent_end"` (default) and `"edit_write"`.
148
+
149
+ Agent-end mode analyzes files touched during the full agent response (after all tool calls complete) and posts a diagnostics message only once. Disabling the hook does not disable the `/lsp` tool.
150
+
151
+ ## File Structure
152
+
153
+ | File | Purpose |
154
+ |------|---------|
155
+ | `lsp.ts` | Hook extension (auto-diagnostics; default at agent end) |
156
+ | `lsp-tool.ts` | Tool extension (on-demand LSP queries) |
157
+ | `lsp-core.ts` | LSPManager class, server configs, singleton manager |
158
+ | `package.json` | Declares both extensions via "pi" field |
159
+
160
+ ## Testing
161
+
162
+ ```bash
163
+ # Unit tests (root detection, configuration)
164
+ npm test
165
+
166
+ # Tool tests
167
+ npm run test:tool
168
+
169
+ # Integration tests (spawns real language servers)
170
+ npm run test:integration
171
+
172
+ # Run rust-analyzer tests (slow, disabled by default)
173
+ RUST_LSP_TEST=1 npm run test:integration
174
+ ```
175
+
176
+ ## License
177
+
178
+ MIT