KotlinLanguageServer

Intelligent Kotlin support for any editor/IDE using the Language Server Protocol

Github星跟踪图

Kotlin Language Server

A language server that provides smart code completion, diagnostics, hover, document symbols, definition lookup, method signature help and more for Kotlin.

Release
Build Status
Downloads
Gitter

Icon

Any editor conforming to LSP is supported, including VSCode and Atom.

Getting Started

This repository needs your help!

The original author created this project while he was considering using Kotlin in his work. He ended up deciding not to and is not really using Kotlin these days though this is a pretty fully-functional language server that just needs someone to use it every day for a while and iron out the last few pesky bugs.

There are two hard parts of implementing a language server:

  • Figuring out the dependencies
  • Incrementally re-compiling as the user types

The project uses the internal APIs of the Kotlin compiler.

Dependencies are determined by the findClassPath function, which invokes Maven or Gradle and tells it to output a list of dependencies. Currently, both Maven and Gradle projects are supported.

I get incremental compilation at the file-level by keeping the same KotlinCoreEnvironment alive between compilations in Compiler.kt. There is a performance benchmark in OneFilePerformance.kt that verifies this works.

Getting incremental compilation at the expression level is a bit more complicated:

  • Fully compile a file and store in CompiledFile:
    • val content: String A snapshot of the source code
    • val parse: KtFile The parsed AST
    • val compile: BindingContext Additional information about the AST from typechecking
  • After the user edits the file:
    • Find the smallest section the encompasses all the user changes
    • Get the LexicalScope encompassing this region from the BindingContext that was generated by the full-compile
    • Create a fake, in-memory .kt file with just the expression we want to re-compile
      • Add space at the top of the file so the line numbers match up
    • Re-compile this tiny fake file

The incremental expression compilation logic is all in CompiledFile.kt. The Kotlin AST has a built-in repair API, which seems to be how IntelliJ works, but as far as I can tell this API does not work if the surrounding IntelliJ machinery is not present. Hence I created the "fake tiny file" incremental-compilation mechanism, which seems to be quite fast and predictable.

There is an extensive suite of behavioral tests, which are all implemented in terms of the language server protocol, so you should be able to refactor the code any way you like and the tests should still work.

Modules, Name, Description, ----, -----------, server, The language server executable, shared, Classpath resolution and utilities, ## Scripts, Name, Command, Description, ----, -------, -----------, update_kt_version.py, python3 scripts/update_kt_version.py, Finds and updates the Kotlin compiler/plugin version for this project, bump_version.py, python3 scripts/bump_version.py, Increments the project version and creates a new tag, ## Protocol Extensions

The Kotlin language server supports some non-standard requests through LSP. See KotlinProtocolExtensions for a description of the interface. The general syntax for these methods is kotlin/someCustomMethod.

Features

Autocomplete

Autocomplete

Signature help

Signature Help

Hover

Hover

Go-to-definition, find all references

Find all references

Document symbols

Document symbols

Global symbols

Global symbols

Authors

主要指标

概览
名称与所有者fwcd/kotlin-language-server
主编程语言Kotlin
编程语言Kotlin (语言数: 4)
平台
许可证MIT License
所有者活动
创建于2018-05-28 12:40:14
推送于2025-06-02 12:44:02
最后一次提交
发布数40
最新版本名称1.3.13 (发布于 2025-01-18 23:39:54)
第一版名称0.1.3 (发布于 )
用户参与
星数1.9k
关注者数31
派生数233
提交数1.6k
已启用问题?
问题数359
打开的问题数182
拉请求数205
打开的拉请求数35
关闭的拉请求数26
项目设置
已启用Wiki?
已存档?
是复刻?
已锁定?
是镜像?
是私有?