Skip to content

Commit

Permalink
Merge pull request #2 from fwcd/main
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaoyunwu authored Sep 25, 2022
2 parents b347c7d + 86fd15d commit daccc1b
Show file tree
Hide file tree
Showing 46 changed files with 1,370 additions and 251 deletions.
7 changes: 6 additions & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
name: Build
on: [ push, pull_request ]

on:
push:
branches:
- main
pull_request:

jobs:
build:
Expand Down
9 changes: 9 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@
"request": "attach",
"hostName": "localhost",
"port": 5005
},
{
"type": "extensionHost",
"name": "Run Grammar Dev Extension",
"request": "launch",
"args": [
"--disable-extension=fwcd.kotlin",
"--extensionDevelopmentPath=${workspaceFolder}/grammars/vscode-grammar-dev"
]
}
]
}
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ All notable changes to the language server will be documented in this file.

Check [Keep a Changelog](http://keepachangelog.com/) for recommendations on how to structure this file.

## [1.3.1]
- Add support for run/debug code lenses
- Add definition lookup support for JDT symbols
- Add quick fix for implementing abstract functions
- Add experimental JDT.LS integration

## [1.3.0]
- Bump to Kotlin 1.6
- Support JDK 17
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ Any editor conforming to LSP is supported, including [VSCode](https://github.com
* See [Kotlin Debug Adapter](https://github.com/fwcd/kotlin-debug-adapter) for debugging support on JVM
* See [tree-sitter-kotlin](https://github.com/fwcd/tree-sitter-kotlin) for an experimental [Tree-Sitter](https://tree-sitter.github.io/tree-sitter/) grammar

## Packaging

[![Packaging status](https://repology.org/badge/vertical-allrepos/kotlin-language-server.svg)](https://repology.org/project/kotlin-language-server/versions)

## This repository needs your help!

[The original author](https://github.com/georgewfraser) 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.
Expand Down
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
projectVersion=1.4.0
projectVersion=1.3.2
kotlinVersion=1.6.10
exposedVersion=0.37.3
lsp4jVersion=0.12.0
lsp4jVersion=0.15.0
javaVersion=11
Binary file modified gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.3.1-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-7.5-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
6 changes: 6 additions & 0 deletions gradlew
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,12 @@ set -- \
org.gradle.wrapper.GradleWrapperMain \
"$@"

# Stop when "xargs" is not available.
if ! command -v xargs >/dev/null 2>&1
then
die "xargs is not available"
fi

# Use "xargs" to parse quoted args.
#
# With -n1 it outputs one arg per line, with the quotes and backslashes removed.
Expand Down
14 changes: 8 additions & 6 deletions gradlew.bat
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
@rem limitations under the License.
@rem

@if "%DEBUG%" == "" @echo off
@if "%DEBUG%"=="" @echo off
@rem ##########################################################################
@rem
@rem Gradle startup script for Windows
Expand All @@ -25,7 +25,7 @@
if "%OS%"=="Windows_NT" setlocal

set DIRNAME=%~dp0
if "%DIRNAME%" == "" set DIRNAME=.
if "%DIRNAME%"=="" set DIRNAME=.
set APP_BASE_NAME=%~n0
set APP_HOME=%DIRNAME%

Expand All @@ -40,7 +40,7 @@ if defined JAVA_HOME goto findJavaFromJavaHome

set JAVA_EXE=java.exe
%JAVA_EXE% -version >NUL 2>&1
if "%ERRORLEVEL%" == "0" goto execute
if %ERRORLEVEL% equ 0 goto execute

echo.
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
Expand Down Expand Up @@ -75,13 +75,15 @@ set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar

:end
@rem End local scope for the variables with windows NT shell
if "%ERRORLEVEL%"=="0" goto mainEnd
if %ERRORLEVEL% equ 0 goto mainEnd

:fail
rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
rem the _cmd.exe /c_ return code!
if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
exit /b 1
set EXIT_CODE=%ERRORLEVEL%
if %EXIT_CODE% equ 0 set EXIT_CODE=1
if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE%
exit /b %EXIT_CODE%

:mainEnd
if "%OS%"=="Windows_NT" endlocal
Expand Down
9 changes: 7 additions & 2 deletions grammars/Kotlin.tmLanguage.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"$schema": "https://raw.githubusercontent.com/martinring/tmlanguage/master/tmlanguage.json",
"name": "Kotlin",
"scopeName": "source.kotlin",
"patterns": [
Expand Down Expand Up @@ -475,10 +476,14 @@
"end": "(\\})",
"name": "meta.template.expression.kotlin",
"beginCaptures": {
"1": "punctuation.definition.template-expression.begin"
"1": {
"name": "punctuation.definition.template-expression.begin"
}
},
"endCaptures": {
"1": "punctuation.definition.template-expression.begin"
"1": {
"name": "punctuation.definition.template-expression.end"
}
},
"patterns": [
{
Expand Down
4 changes: 4 additions & 0 deletions grammars/vscode-grammar-dev/.vscodeignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.vscode/**
.vscode-test/**
.gitignore
vsc-extension-quickstart.md
11 changes: 11 additions & 0 deletions grammars/vscode-grammar-dev/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Grammar Development Extension for VSCode

A small VSCode extension for development of the Kotlin grammar.

> **Note**: The actual Kotlin support for VSCode, including the language client, is located in the [`vscode-kotlin` repository](https://github.com/fwcd/vscode-kotlin).
## Usage

The most convenient way to run the extension is to simply use the debug configuration `Run Grammar Dev` in this repo.

> **Note**: VSCode might show a warning that grammar paths are located outside of the extension folder. This message can be ignored, since the extension is only indended for development anyway.
25 changes: 25 additions & 0 deletions grammars/vscode-grammar-dev/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"name": "kotlin-grammar-dev",
"displayName": "Kotlin Grammar Development",
"description": "Grammar development extension for Kotlin",
"version": "0.0.1",
"engines": {
"vscode": "^1.67.0"
},
"categories": [
"Programming Languages"
],
"contributes": {
"languages": [{
"id": "kotlin",
"aliases": ["Kotlin", "kotlin"],
"extensions": [".kt",".kts"],
"configuration": "../kotlin.configuration.json"
}],
"grammars": [{
"language": "kotlin",
"scopeName": "source.kotlin",
"path": "../Kotlin.tmLanguage.json"
}]
}
}
3 changes: 2 additions & 1 deletion server/src/main/kotlin/org/javacs/kt/CompilerClassPath.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ import java.nio.file.Path
* and the compiler. Note that Kotlin sources are stored in SourcePath.
*/
class CompilerClassPath(private val config: CompilerConfiguration) : Closeable {
private val workspaceRoots = mutableSetOf<Path>()
val workspaceRoots = mutableSetOf<Path>()

private val javaSourcePath = mutableSetOf<Path>()
private val buildScriptClassPath = mutableSetOf<Path>()
val classPath = mutableSetOf<ClassPathEntry>()
Expand Down
9 changes: 8 additions & 1 deletion server/src/main/kotlin/org/javacs/kt/KotlinLanguageServer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import org.eclipse.lsp4j.jsonrpc.services.JsonDelegate
import org.eclipse.lsp4j.services.LanguageClient
import org.eclipse.lsp4j.services.LanguageClientAware
import org.eclipse.lsp4j.services.LanguageServer
import org.eclipse.lsp4j.services.NotebookDocumentService
import org.javacs.kt.command.ALL_COMMANDS
import org.javacs.kt.externalsources.*
import org.javacs.kt.util.AsyncExecutor
Expand All @@ -30,7 +31,7 @@ class KotlinLanguageServer : LanguageServer, LanguageClientAware, Closeable {

private val textDocuments = KotlinTextDocumentService(sourceFiles, sourcePath, config, tempDirectory, uriContentProvider, classPath)
private val workspaces = KotlinWorkspaceService(sourceFiles, sourcePath, classPath, textDocuments, config)
private val protocolExtensions = KotlinProtocolExtensionService(uriContentProvider, classPath)
private val protocolExtensions = KotlinProtocolExtensionService(uriContentProvider, classPath, sourcePath)

private lateinit var client: LanguageClient

Expand Down Expand Up @@ -163,4 +164,10 @@ class KotlinLanguageServer : LanguageServer, LanguageClientAware, Closeable {
}

override fun exit() {}

// Fixed in https://github.com/eclipse/lsp4j/commit/04b0c6112f0a94140e22b8b15bb5a90d5a0ed851
// Causes issue in lsp 0.15
override fun getNotebookDocumentService(): NotebookDocumentService? {
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,16 @@ package org.javacs.kt
import org.eclipse.lsp4j.*
import org.javacs.kt.util.AsyncExecutor
import org.javacs.kt.util.parseURI
import org.javacs.kt.resolve.resolveMain
import org.javacs.kt.position.offset
import org.javacs.kt.overridemembers.listOverridableMembers
import java.util.concurrent.CompletableFuture
import java.nio.file.Paths

class KotlinProtocolExtensionService(
private val uriContentProvider: URIContentProvider,
private val cp: CompilerClassPath
private val cp: CompilerClassPath,
private val sp: SourcePath
) : KotlinProtocolExtensions {
private val async = AsyncExecutor()

Expand All @@ -18,4 +23,30 @@ class KotlinProtocolExtensionService(
override fun buildOutputLocation(): CompletableFuture<String?> = async.compute {
cp.outputDirectory.absolutePath
}

override fun mainClass(textDocument: TextDocumentIdentifier): CompletableFuture<Map<String, Any?>> = async.compute {
val fileUri = parseURI(textDocument.uri)
val filePath = Paths.get(fileUri)

// we find the longest one in case both the root and submodule are included
val workspacePath = cp.workspaceRoots.filter {
filePath.startsWith(it)
}.map {
it.toString()
}.maxByOrNull(String::length) ?: ""

val compiledFile = sp.currentVersion(fileUri)

resolveMain(compiledFile) + mapOf(
"projectRoot" to workspacePath
)
}

override fun overrideMember(position: TextDocumentPositionParams): CompletableFuture<List<CodeAction>> = async.compute {
val fileUri = parseURI(position.textDocument.uri)
val compiledFile = sp.currentVersion(fileUri)
val cursorOffset = offset(compiledFile.content, position.position)

listOverridableMembers(compiledFile, cursorOffset)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,10 @@ interface KotlinProtocolExtensions {

@JsonRequest
fun buildOutputLocation(): CompletableFuture<String?>

@JsonRequest
fun mainClass(textDocument: TextDocumentIdentifier): CompletableFuture<Map<String, Any?>>

@JsonRequest
fun overrideMember(position: TextDocumentPositionParams): CompletableFuture<List<CodeAction>>
}
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ class KotlinTextDocumentService(
TODO("not implemented")
}

@Suppress("DEPRECATION")
override fun documentSymbol(params: DocumentSymbolParams): CompletableFuture<List<Either<SymbolInformation, DocumentSymbol>>> = async.compute {
LOG.info("Find symbols in {}", describeURI(params.textDocument.uri))

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.javacs.kt

import com.intellij.openapi.project.Project
import org.eclipse.lsp4j.*
import org.eclipse.lsp4j.services.WorkspaceService
import org.eclipse.lsp4j.services.LanguageClient
Expand All @@ -9,11 +8,9 @@ import org.eclipse.lsp4j.jsonrpc.messages.Either
import org.javacs.kt.symbols.workspaceSymbols
import org.javacs.kt.command.JAVA_TO_KOTLIN_COMMAND
import org.javacs.kt.j2k.convertJavaToKotlin
import org.javacs.kt.KotlinTextDocumentService
import org.javacs.kt.position.extractRange
import org.javacs.kt.util.filePath
import org.javacs.kt.util.parseURI
import java.net.URI
import java.nio.file.Paths
import java.util.concurrent.CompletableFuture
import com.google.gson.JsonElement
Expand Down Expand Up @@ -142,10 +139,11 @@ class KotlinWorkspaceService(
LOG.info("Updated configuration: {}", settings)
}

override fun symbol(params: WorkspaceSymbolParams): CompletableFuture<List<SymbolInformation>> {
@Suppress("DEPRECATION")
override fun symbol(params: WorkspaceSymbolParams): CompletableFuture<Either<List<SymbolInformation>, List<WorkspaceSymbol>>> {
val result = workspaceSymbols(params.query, sp)

return CompletableFuture.completedFuture(result)
return CompletableFuture.completedFuture(Either.forRight(result))
}

override fun didChangeWorkspaceFolders(params: DidChangeWorkspaceFoldersParams) {
Expand Down
4 changes: 2 additions & 2 deletions server/src/main/kotlin/org/javacs/kt/codeaction/CodeAction.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ package org.javacs.kt.codeaction
import org.eclipse.lsp4j.*
import org.eclipse.lsp4j.jsonrpc.messages.Either
import org.javacs.kt.CompiledFile
import org.javacs.kt.codeaction.quickfix.ImplementAbstractFunctionsQuickFix
import org.javacs.kt.codeaction.quickfix.ImplementAbstractMembersQuickFix
import org.javacs.kt.codeaction.quickfix.AddMissingImportsQuickFix
import org.javacs.kt.command.JAVA_TO_KOTLIN_COMMAND
import org.javacs.kt.util.toPath
import org.javacs.kt.index.SymbolIndex

val QUICK_FIXES = listOf(
ImplementAbstractFunctionsQuickFix(),
ImplementAbstractMembersQuickFix(),
AddMissingImportsQuickFix()
)

Expand Down
Loading

0 comments on commit daccc1b

Please sign in to comment.