|
| 1 | +# LSP Support |
| 2 | + |
| 3 | +This document describes the Language Server Protocol features supported by the Ansible Language Server. |
| 4 | + |
| 5 | +## Supported Capabilities |
| 6 | + |
| 7 | +### Text Document Synchronization |
| 8 | + |
| 9 | +- **Method**: `textDocument/didOpen`, `textDocument/didChange`, `textDocument/didClose` |
| 10 | +- **Sync kind**: Incremental |
| 11 | + |
| 12 | +### Hover (`textDocument/hover`) |
| 13 | + |
| 14 | +Provides contextual information on hover for: |
| 15 | + |
| 16 | +| Target | Description | |
| 17 | +|---|---| |
| 18 | +| Module names | Module documentation from `ansible-doc` | |
| 19 | +| Module options/suboptions | Option type, description, default, choices | |
| 20 | +| Handler names (`notify`, `listen`) | Handler name with definition location | |
| 21 | +| Variables (`{{ var }}`) | Variable name with definition source (`register:`, `vars:`, `vars_prompt`) | |
| 22 | +| Variables from role `argument_specs` | Full documentation: type, description, default, choices | |
| 23 | +| File paths (`include_tasks`, `template src`, etc.) | Resolved absolute path | |
| 24 | +| Role names | `short_description` from `argument_specs.yml` | |
| 25 | + |
| 26 | +### Completion (`textDocument/completion`) |
| 27 | + |
| 28 | +- **Resolve support**: Yes (`completionItem/resolve`) |
| 29 | + |
| 30 | +Provides completion for: |
| 31 | + |
| 32 | +| Context | Items | |
| 33 | +|---|---| |
| 34 | +| Task keywords | Ansible task keywords (`name`, `when`, `loop`, `register`, etc.) | |
| 35 | +| Module names | Available modules from `ansible-doc` | |
| 36 | +| Module options/suboptions | Options with documentation from `ansible-doc` | |
| 37 | +| Jinja2 variables (`{{ }}`) | `register` vars, `vars:` keys, `vars_prompt` names, `vars_files` keys | |
| 38 | +| Role variables (inside role code) | Variables from `defaults/main.yml` + `vars/main.yml` | |
| 39 | +| Role variables (in playbook) | Variables from `argument_specs.yml` with `defaults/main.yml` fallback | |
| 40 | + |
| 41 | +### Go to Definition (`textDocument/definition`) |
| 42 | + |
| 43 | +| Source | Target | |
| 44 | +|---|---| |
| 45 | +| Module name (task key) | Module documentation source | |
| 46 | +| `notify` / `listen` value | Handler `name` definition (cross-file within role) | |
| 47 | +| Variable in `{{ }}` | `register:`, `vars:` key, or `defaults/main.yml` / `vars/main.yml` (cross-file within role) | |
| 48 | +| File path (`include_tasks`, `import_tasks`, `template src`, `copy src`, `include_vars`, `vars_files`) | Resolved target file | |
| 49 | +| Role name (`roles:`, `include_role`, `import_role`) | Role's `tasks/main.yml` | |
| 50 | + |
| 51 | +### Find References (`textDocument/references`) |
| 52 | + |
| 53 | +| Symbol kind | Scope | |
| 54 | +|---|---| |
| 55 | +| Handler names | Cross-file within role (`tasks/**/*.yml` + `handlers/**/*.yml`) | |
| 56 | +| Variables (`register`, `vars`, `vars_prompt`) | Cross-file within role (`tasks/**/*.yml` + `defaults/main.yml` + `vars/main.yml`) | |
| 57 | +| Module names | Single file | |
| 58 | +| Role names | Single file | |
| 59 | + |
| 60 | +### Rename (`textDocument/rename`) |
| 61 | + |
| 62 | +- **Prepare support**: Yes (`textDocument/prepareRename`) |
| 63 | +- Not supported for: module names, file paths, role names |
| 64 | + |
| 65 | +| Symbol kind | Scope | Details | |
| 66 | +|---|---|---| |
| 67 | +| Handler names | Cross-file within role | Rename matrix based on source: `notify` updates all; `name` updates `name` + `notify`; `listen` updates `listen` + `notify` | |
| 68 | +| Variables | Cross-file within role | Renames in definitions (`register`, `vars`, defaults, vars files) and usages (Jinja2 expressions) | |
| 69 | + |
| 70 | +### Document Symbols (`textDocument/documentSymbol`) |
| 71 | + |
| 72 | +Provides a hierarchical outline of Ansible documents: |
| 73 | + |
| 74 | +- Plays, blocks, tasks, handlers, roles |
| 75 | +- Supports nested structures (blocks within blocks, etc.) |
| 76 | + |
| 77 | +### Workspace Symbol (`workspace/symbol`) |
| 78 | + |
| 79 | +Provides symbol search by name query (substring match, case-insensitive). Returns definitions only (not usages). |
| 80 | + |
| 81 | +**Data sources:** |
| 82 | + |
| 83 | +- All open documents (AST parsed in-memory) |
| 84 | +- Role files on disk (`defaults/main.yml`, `vars/main.yml`, `handlers/**/*.yml`) when an open document is inside a role |
| 85 | + |
| 86 | +| Symbol kind | LSP SymbolKind | containerName | |
| 87 | +|---|---|---| |
| 88 | +| Handler names (`handlers[].name`) | Function | Play name | |
| 89 | +| Variables (`register: x`) | Variable | Task name | |
| 90 | +| Variables (`vars:` keys) | Variable | Play/block name | |
| 91 | +| Variables (`vars_prompt[].name`) | Variable | Play name | |
| 92 | +| Variables (`set_fact` keys) | Variable | Task name | |
| 93 | +| Variables (role `defaults/vars` top-level keys) | Variable | Role name | |
| 94 | +| Role names (`roles:`, `include_role`, `import_role`) | Package | Play name | |
| 95 | + |
| 96 | +Results are cached per document version (and by `mtime` for on-disk role files). |
| 97 | + |
| 98 | +This enables tools like Serena to find symbols by name and then call `textDocument/references` on the resolved position. |
| 99 | + |
| 100 | +**Limitations:** Only indexes roles that have at least one open file. Standalone files (`group_vars/`, `host_vars/`) are not indexed unless open. |
| 101 | + |
| 102 | +### Semantic Tokens (`textDocument/semanticTokens/full`) |
| 103 | + |
| 104 | +Provides semantic highlighting for Ansible-specific tokens. |
| 105 | + |
| 106 | +### Diagnostics (push model) |
| 107 | + |
| 108 | +Validation via `ansible-lint` (when available) with results published as `textDocument/publishDiagnostics`. |
| 109 | + |
| 110 | +## Cross-File Navigation |
| 111 | + |
| 112 | +When a file is located inside an Ansible role (detected by the `roles/<name>/` directory structure), several features operate across files within that role: |
| 113 | + |
| 114 | +- **Handler references/rename**: scans `tasks/**/*.yml` + `handlers/**/*.yml` |
| 115 | +- **Variable references/rename/definition**: scans `tasks/**/*.yml` + `defaults/main.yml` + `vars/main.yml` |
| 116 | + |
| 117 | +Role path resolution order: |
| 118 | + |
| 119 | +1. `./roles/<name>/` relative to the document |
| 120 | +2. `DEFAULT_ROLES_PATH` from `ansible-config dump` |
| 121 | +3. `~/.ansible/roles/` |
| 122 | +4. `/etc/ansible/roles/` |
| 123 | + |
| 124 | +## Role `argument_specs` Support |
| 125 | + |
| 126 | +When a role has `meta/argument_specs.yml`, the server uses it to provide: |
| 127 | + |
| 128 | +- **Completion**: role variables with type, description, default, and choices |
| 129 | +- **Hover**: full option documentation on role variables and `short_description` on role names |
| 130 | +- **Definition**: entry point resolution via `tasks_from` parameter (default: `main`) |
0 commit comments