lsp.mdx 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. ---
  2. title: LSP Servers
  3. description: OpenCode integrates with your LSP servers.
  4. ---
  5. OpenCode integrates with your Language Server Protocol (LSP) to help the LLM interact with your codebase. It uses diagnostics to provide feedback to the LLM.
  6. ---
  7. ## Built-in
  8. OpenCode comes with several built-in LSP servers for popular languages:
  9. | LSP Server | Extensions | Requirements |
  10. | ------------------ | ---------------------------------------------------- | ------------------------------------------------------------ |
  11. | typescript | .ts, .tsx, .js, .jsx, .mjs, .cjs, .mts, .cts | `typescript` dependency in project |
  12. | deno | .ts, .tsx, .js, .jsx, .mjs | `deno` command available (auto-detects deno.json/deno.jsonc) |
  13. | eslint | .ts, .tsx, .js, .jsx, .mjs, .cjs, .mts, .cts, .vue | `eslint` dependency in project |
  14. | gopls | .go | `go` command available |
  15. | ruby-lsp (rubocop) | .rb, .rake, .gemspec, .ru | `ruby` and `gem` commands available |
  16. | pyright | .py, .pyi | `pyright` dependency installed |
  17. | elixir-ls | .ex, .exs | `elixir` command available |
  18. | zls | .zig, .zon | `zig` command available |
  19. | csharp | .cs | `.NET SDK` installed |
  20. | vue | .vue | Auto-installs for Vue projects |
  21. | rust | .rs | `rust-analyzer` command available |
  22. | clangd | .c, .cpp, .cc, .cxx, .c++, .h, .hpp, .hh, .hxx, .h++ | Auto-installs for C/C++ projects |
  23. | svelte | .svelte | Auto-installs for Svelte projects |
  24. | astro | .astro | Auto-installs for Astro projects |
  25. | yaml-ls | .yaml, .yml | Auto-installs Red Hat yaml-language-server |
  26. | jdtls | .java | `Java SDK (version 21+)` installed |
  27. | lua-ls | .lua | Auto-installs for Lua projects |
  28. | sourcekit-lsp | .swift, .objc, .objcpp | `swift` installed (`xcode` on macOS) |
  29. | php intelephense | .php | Auto-installs for PHP projects |
  30. LSP servers are automatically enabled when one of the above file extensions are detected and the requirements are met.
  31. :::note
  32. You can disable automatic LSP server downloads by setting the `OPENCODE_DISABLE_LSP_DOWNLOAD` environment variable to `true`.
  33. :::
  34. ---
  35. ## How It Works
  36. When opencode opens a file, it:
  37. 1. Checks the file extension against all enabled LSP servers.
  38. 2. Starts the appropriate LSP server if not already running.
  39. ---
  40. ## Configure
  41. You can customize LSP servers through the `lsp` section in your opencode config.
  42. ```json title="opencode.json"
  43. {
  44. "$schema": "https://opencode.ai/config.json",
  45. "lsp": {}
  46. }
  47. ```
  48. Each LSP server supports the following:
  49. | Property | Type | Description |
  50. | ---------------- | -------- | ------------------------------------------------- |
  51. | `disabled` | boolean | Set this to `true` to disable the LSP server |
  52. | `command` | string[] | The command to start the LSP server |
  53. | `extensions` | string[] | File extensions this LSP server should handle |
  54. | `env` | object | Environment variables to set when starting server |
  55. | `initialization` | object | Initialization options to send to the LSP server |
  56. Let's look at some examples.
  57. ---
  58. ### Disabling LSP servers
  59. To disable a specific LSP server, set `disabled` to `true`:
  60. ```json title="opencode.json" {5}
  61. {
  62. "$schema": "https://opencode.ai/config.json",
  63. "lsp": {
  64. "typescript": {
  65. "disabled": true
  66. }
  67. }
  68. }
  69. ```
  70. ---
  71. ### Custom LSP servers
  72. You can add custom LSP servers by specifying the command and file extensions:
  73. ```json title="opencode.json" {4-7}
  74. {
  75. "$schema": "https://opencode.ai/config.json",
  76. "lsp": {
  77. "custom-lsp": {
  78. "command": ["custom-lsp-server", "--stdio"],
  79. "extensions": [".custom"]
  80. }
  81. }
  82. }
  83. ```
  84. ---
  85. ## Additional Information
  86. ### PHP Intelephense
  87. PHP Intelephense offers premium features through a license key. You can provide a license key by placing (only) the key in a text file at:
  88. - On macOS/Linux: `$HOME/intelephense/licence.txt`
  89. - On Windows: `%USERPROFILE%/intelephense/licence.txt`
  90. The file should contain only the license key with no additional content.