$npx -y skills add lsp-client/lsp-skill --skill lsp-code-analysisSemantic code analysis via LSP. Navigate code (definitions, references, implementations), search symbols, preview refactorings, and get file outlines. Use for exploring unfamiliar codebases or performing safe refactoring.
| 1 | # LSP Code Analysis |
| 2 | |
| 3 | ## IMPORTANT: PREREQUISITE |
| 4 | |
| 5 | To use this skill, you **MUST** follow these steps: |
| 6 | |
| 7 | 1. **Check for updates**: Run the [update script](scripts/update.sh) to ensure you are using the latest version of the tool. |
| 8 | 2. **Verify project support**: Run `lsp server start <project_path>` to start the LSP server and confirm the project is supported. |
| 9 | |
| 10 | **IF YOU DO NOT PERFORM THESE STEPS, YOU ARE NOT ALLOWED TO USE THIS SKILL.** |
| 11 | |
| 12 | ## Abstract |
| 13 | |
| 14 | This document specifies the operational requirements and best practices for the `lsp-code-analysis` skill. It provides a semantic interface to codebase navigation, analysis and refactoring via the Language Server Protocol (LSP). |
| 15 | |
| 16 | ## Overview |
| 17 | |
| 18 | You are provided with `lsp` CLI tool for semantic code navigation and analysis. It SHOULD be preferred over `read` or `grep` for most code understanding tasks. |
| 19 | |
| 20 | Usages: |
| 21 | |
| 22 | - **Semantic navigation**: Jump to definitions, find references, locate implementations - understands code structure, not just text patterns. |
| 23 | - **Language-aware**: Distinguishes between variables, functions, classes, types - eliminates false positives from text search. |
| 24 | - **Cross-file intelligence**: Trace dependencies, refactor safely across entire codebase - knows what imports what. |
| 25 | - **Type-aware**: Get precise type information, signatures, documentation - without reading implementation code. |
| 26 | |
| 27 | ### Tool Selection |
| 28 | |
| 29 | **Guideline**: You SHOULD prioritize LSP commands for code navigation and analysis. Agents MAY use `read` or `grep` ONLY when semantic analysis is not applicable (e.g., searching for comments or literal strings). |
| 30 | |
| 31 | | Task | Traditional Tool | Recommended LSP Command | |
| 32 | | ------------------- | ---------------- | ----------------------------------------------- | |
| 33 | | **Find Definition** | `grep`, `read` | [`definition`](#definition-navigate-to-source) | |
| 34 | | **Find Usages** | `grep -r` | [`reference`](#reference-find-all-usages) | |
| 35 | | **Understand File** | `read` | [`outline`](#outline-file-structure) | |
| 36 | | **View Docs/Types** | `read` | [`doc`](#doc-get-documentation) | |
| 37 | | **Refactor** | `sed` | See [Refactoring Guide](references/refactor.md) | |
| 38 | |
| 39 | ## Commands |
| 40 | |
| 41 | All commands support `-h` or `--help`. |
| 42 | |
| 43 | ### Locating Symbols |
| 44 | |
| 45 | Most commands use a unified locating syntax via the `--scope` and `--find` options. |
| 46 | |
| 47 | **Arguments**: `<file_path>` |
| 48 | |
| 49 | **Options**: |
| 50 | |
| 51 | - `--scope`: Narrow search to a symbol body or line range. |
| 52 | - `--find`: Text pattern to find within the scope. |
| 53 | |
| 54 | **Scope Formats**: |
| 55 | |
| 56 | - `<line>`: Single line number (e.g., `42`). |
| 57 | - `<start>,<end>`: Line range (e.g., `10,20`). Use `0` for end to mean till EOF (e.g., `10,0`). |
| 58 | - `<symbol_path>`: Symbol path with dots (e.g., `MyClass.my_method`). |
| 59 | |
| 60 | **Find Pattern (`--find`)**: |
| 61 | |
| 62 | The `--find` option narrows the target to a **text pattern within the selected scope**: |
| 63 | |
| 64 | - The scope is determined by `--scope` (line/range/symbol). If no `--scope` is given, the entire file is the scope. |
| 65 | - Pattern matching is **whitespace-insensitive**: differences in spaces, tabs, and newlines are ignored. |
| 66 | - You MAY include the cursor marker `<|>` inside the pattern to specify the **exact position of interest** within the match (for example, on a variable name, keyword, or operator). |
| 67 | - If `--find` is omitted, the command uses the start of the scope (or a tool-specific default) as the navigation target. |
| 68 | |
| 69 | **Cursor Marker (`<|>`)**: |
| 70 | |
| 71 | The `<|>` marker indicates the exact position for symbol resolution. It represents the character immediately to its right. Use it within the find pattern to point to a specific element (e.g., `user.<|>name` to target the `name` property). |
| 72 | |
| 73 | **Examples**: |
| 74 | |
| 75 | - `lsp doc foo.py --find "self.<|>"` - Find `self.` in entire file, position at the character after the dot (typically for completion or member access) |
| 76 | - `lsp doc foo.py --scope 42 --find "return <|>result"` - Find `return result` on line 42, position at `r` of `result` |
| 77 | - `lsp doc foo.py --scope 10,20 --find "if <|>condition"` - Find `if condition` in lines 10-20, position at `c` of `condition` |
| 78 | - `lsp doc foo.py --scope MyClass.my_method --find "self.<|>"` - Find `self.` within `MyClass.my_method`, position after the dot |
| 79 | - `lsp doc foo.py --scope MyClass` - Target the `MyClass` symbol directly |
| 80 | |
| 81 | **Guideline for Scope vs. Find**: |
| 82 | |
| 83 | - Use `--scope <symbol_path>` (e.g., `--scope MyClass`, `--scope MyClass.my_method`) to target **classes, functions, or methods**. This is the most robust and preferred way to target symbol. |
| 84 | - Use `--find` (often combined with `--scope`) to target variables or specific positions. Use this when the target is not a uniquely named symbol or when you nee |