lsp.mdx 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 | .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. | jdtls | .java | `Java SDK (version 21+)` installed |
  26. | lua-ls | .lua | Auto-installs for Lua projects |
  27. LSP servers are automatically enabled when one of the above file extensions are detected and the requirements are met.
  28. :::note
  29. You can disable automatic LSP server downloads by setting the `OPENCODE_DISABLE_LSP_DOWNLOAD` environment variable to `true`.
  30. :::
  31. ---
  32. ## How It Works
  33. When opencode opens a file, it:
  34. 1. Checks the file extension against all enabled LSP servers.
  35. 2. Starts the appropriate LSP server if not already running.
  36. ---
  37. ## Configure
  38. You can customize LSP servers through the `lsp` section in your opencode config.
  39. ```json title="opencode.json"
  40. {
  41. "$schema": "https://opencode.ai/config.json",
  42. "lsp": {}
  43. }
  44. ```
  45. Each LSP server supports the following:
  46. | Property | Type | Description |
  47. | ---------------- | -------- | ------------------------------------------------- |
  48. | `disabled` | boolean | Set this to `true` to disable the LSP server |
  49. | `command` | string[] | The command to start the LSP server |
  50. | `extensions` | string[] | File extensions this LSP server should handle |
  51. | `env` | object | Environment variables to set when starting server |
  52. | `initialization` | object | Initialization options to send to the LSP server |
  53. Let's look at some examples.
  54. ---
  55. ### Disabling LSP servers
  56. To disable a specific LSP server, set `disabled` to `true`:
  57. ```json title="opencode.json" {5}
  58. {
  59. "$schema": "https://opencode.ai/config.json",
  60. "lsp": {
  61. "typescript": {
  62. "disabled": true
  63. }
  64. }
  65. }
  66. ```
  67. ---
  68. ### Custom LSP servers
  69. You can add custom LSP servers by specifying the command and file extensions:
  70. ```json title="opencode.json" {4-7}
  71. {
  72. "$schema": "https://opencode.ai/config.json",
  73. "lsp": {
  74. "custom-lsp": {
  75. "command": ["custom-lsp-server", "--stdio"],
  76. "extensions": [".custom"]
  77. }
  78. }
  79. }
  80. ```