GithubHelp home page GithubHelp logo

kangwang1988 / kotlin-language-server Goto Github PK

View Code? Open in Web Editor NEW

This project forked from fwcd/kotlin-language-server

0.0 1.0 0.0 37.11 MB

Kotlin code completion, linting and more for any editor/IDE using the Language Server Protocol

License: MIT License

Python 2.01% Java 0.28% Kotlin 97.61% Dockerfile 0.09%

kotlin-language-server's Introduction

Kotlin Language Server

Release Build Downloads Chat

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

Icon

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

Getting Started

Packaging

Packaging status

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.

Figuring out the dependencies

Dependencies are determined by the DefaultClassPathResolver.kt, which invokes Maven or Gradle to get a list of classpath JARs. Alternatively, projects can also 'manually' provide a list of dependencies through a shell script, located either at [project root]/kotlinLspClasspath.{sh,bat,cmd} or [config root]/KotlinLanguageServer/classpath.{sh,bat,cmd}, which outputs a list of JARs.

  • Example of the ~/.config/KotlinLanguageServer/classpath.sh on Linux:
#!/bin/bash
echo /my/path/kotlin-compiler-1.4.10/lib/kotlin-stdlib.jar:/my/path/my-lib.jar
  • Example of the %HOMEPATH%\.config\KotlinLanguageServer\classpath.bat on Windows:
@echo off
echo C:\my\path\kotlin-compiler-1.4.10\lib\kotlin-stdlib.jar;C:\my\path\my-lib.jar

Incrementally re-compiling as the user types

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
release_version.py python3 scripts/release_version.py Creates a tag for the current version and bumps the development version

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

kotlin-language-server's People

Contributors

fwcd avatar georgewfraser avatar themkat avatar daplf avatar donniewest avatar strum355 avatar koush avatar skirwan avatar jlahoda avatar arturbosch avatar goooler avatar jeremylcarter avatar deltatre-johann avatar wikoion avatar kuro46 avatar svermeulen avatar nycto avatar mtdl9 avatar norefle avatar kerkmann avatar a-stewart avatar kkkiio avatar geoffatsource avatar damienstanton avatar chrisg123 avatar whym avatar lenguyenthanh avatar pajatopmr avatar fastfrwrd avatar kadengordon avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.