Kotlin Language Server

repository·main·Indexed 25 days ago

https://github.com/kotlin/kotlin-lsp

An official Language Server Protocol (LSP) implementation for Kotlin by JetBrains. It provides IntelliJ-powered intelligence, diagnostics, and refactoring capabilities to LSP-compatible editors, including Visual Studio Code. Supported features include code completion, navigation (go to definition, call hierarchy), rename refactoring, and semantic highlighting. It supports JVM projects managed by Gradle and Maven, with experimental support for the Android Gradle Plugin.

Tokens
9K
Snippets
18
Records
60
Agent score
86%

What's inside kotlin-lsp

  1. Supported Features in Kotlin by JetBrains

    main

    The extension provides IntelliJ-powered features for Kotlin development, including:

    • Code Intelligence: Up-to-date Kotlin language support, code completion, signature help, diagnostics, inspections, and quick fixes for Kotlin and kotlinx libraries.
    • Navigation: Go to definition, references, type definition, implementation, call hierarchy, and documentation hover support.
    • Refactoring & Editing: Rename refactoring, organize imports, code formatting, code folding, and file templates for new Kotlin files.
    • Visual Aids: Semantic highlighting, inlay hints for types and parameter names, and document/workspace symbols.
    • Build Systems: Support for JVM projects using Gradle, Maven, and experimental Android Gradle Plugin support. (Note: Kotlin Multiplatform (KMP) support is planned for future releases).
  2. Supported Features of kotlin-lsp

    main

    The Kotlin Language Server provides the following features based on IntelliJ IDEA implementation:

    • Language Support: Up-to-date Kotlin language versions and semantic highlighting.
    • IntelliJ-powered Intelligence: Code completion, diagnostics, and quick fixes for Kotlin and kotlinx libraries.
    • Refactoring & Editing: Rename refactoring, organize imports, code formatting, and code folding.
    • Navigation: Documentation navigation, hover support, and Call Hierarchy.
    • Build Systems: Support for JVM projects using Gradle and Maven. Experimental support for the Android Gradle Plugin is available. (Note: Kotlin Multiplatform (KMP) support is planned for future releases).
  3. Bootstrap and run the development environment

    main

    To set up the development environment, install dependencies using pnpm install. To run the extension in a development host, open the project in VS Code and press F5 (selecting the Extension launch configuration). This triggers a watch task.

    When making changes to files in src/, Rspack will rebuild automatically. To apply changes in the development host after a rebuild, use Cmd+R (or Ctrl+R on Windows/Linux) to reload the window.

    pnpm install
  4. Rebuild tree-sitter-kotlin.wasm grammar artifact

    main

    If you need to rebuild the tree-sitter-kotlin.wasm artifact after updating the source, follow these steps:

    1. Check out the desired commit from the tree-sitter-kotlin source repository.
    2. Run the tree-sitter build command using the --wasm flag, pointing to the path of the checked-out source.
    3. This process requires Docker with the emscripten/emsdk image.
    4. Note that the package uses a pre-generated src/parser.c, so a tree-sitter generate step is not required.
    5. Copy the resulting tree-sitter-kotlin.wasm file into the current directory and update the commit reference in the documentation.
    tree-sitter build --wasm <path-to-tree-sitter-kotlin-source>
  5. Quick Start for Kotlin by JetBrains in VS Code

    main

    To use the Kotlin language server in Visual Studio Code, follow these steps:

    1. Install the extension in Visual Studio Code.
    2. Open a folder containing a Kotlin JVM project managed by Gradle or Maven.
    3. Open any .kt file to activate the extension and the language server.

    Important Migration Note: If you have an older extension named jetbrains.kotlin installed from GitHub, you must uninstall it when prompted by the dialog. The new jetbrains.kotlin-server extension will not activate correctly if the old extension is still present. After uninstalling, reload the VS Code window.

    1. Wait for the project import and indexing process to complete before using features.
  6. Quick Start for VS Code

    main

    To use Kotlin support in Visual Studio Code, you can either install the official extension from the Marketplace or manually install a VSIX build.

    Marketplace Installation

    Install the extension directly from the VS Code Marketplace. The extension activates automatically when you open a Kotlin file in a workspace.

    Manual VSIX Installation

    1. Download the required VSIX file from the releases page.
    2. In VS Code, go to Extensions | More Actions | Install from VSIX or drag-and-drop the VSIX file into the Extensions tool window.

    Important: Extension ID Migration

    The extension ID has changed from jetbrains.kotlin to jetbrains.kotlin-server. If you have the old version installed, you must uninstall it when prompted by the new extension to ensure proper activation.

  7. Build a VSIX package

    main

    To build a VSIX package, ensure that server artifacts are built first, then execute the ./build.sh script.

    If building this package within a pnpm workspace, you may need to enable pnpm 11 using corepack first:

    ./build.sh
    
    # If in a pnpm workspace, enable pnpm 11:
    corepack enable
    corepack prepare pnpm@11.4.0 --activate
  8. Configure kotlin-lsp via TCP

    main

    To connect Neovim to a kotlin-lsp instance already running and listening on a network port (default is 9999), configure nvim.lsp using vim.lsp.rpc.connect.

    {
      cmd = vim.lsp.rpc.connect('127.0.0.1', tonumber(9999))
      single_file_support = true,
      filetypes = { "kotlin" },
      root_markers = { "build.gradle", "build.gradle.kts", "pom.xml" },
    }
  9. Configure kotlin-lsp with nvim-lspconfig

    main

    If you use the nvim-lspconfig plugin, you can use the built-in preset for kotlin_lsp. Ensure the kotlin-lsp CLI is installed and available in your $PATH before configuring.

    -- enable the language server
    vim.lsp.enable('kotlin_lsp')
    
    -- configure language server's options
    vim.lsp.config('kotlin_lsp', {
        single_file_support = false,
    })
  10. Configure kotlin-lsp via stdio

    main

    To run kotlin-lsp using the stdio communication method, follow these steps:

    1. Install socat and netcat on your system.
    2. Ensure the kotlin-lsp.sh script is executable: chmod +x $KOTLIN_LSP_DIR/kotlin-lsp.sh
    3. Create a symlink to kotlin-lsp.sh in your $PATH (e.g., $HOME/.local/bin/kotlin-ls).
    4. Configure nvim.lsp with the following settings:
    {
      cmd = { "kotlin-ls", "--stdio" },
      single_file_support = true,
      filetypes = { "kotlin" },
      root_markers = { "build.gradle", "build.gradle.kts", "pom.xml" },
    }
  11. Configure Helix editor to use kotlin-lsp

    main

    To use the Kotlin Language Server with the Helix editor, ensure the kotlin-lsp CLI is installed and available in your $PATH. Then, add the following configuration to your languages.toml file (typically located at ~/.config/helix/languages.toml):

    [[language]]
    name = "kotlin"
    language-servers = [ "kotlin-lsp" ]