Skip to content

Refactor/general code improvements and fixes #636

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
405 changes: 10 additions & 395 deletions detekt_baseline.xml

Large diffs are not rendered by default.

22 changes: 11 additions & 11 deletions server/src/main/kotlin/org/javacs/kt/CompiledFile.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package org.javacs.kt

import com.intellij.openapi.util.TextRange
import com.intellij.psi.PsiElement
import org.javacs.kt.compiler.CompilationKind
import org.javacs.kt.compiler.CompilationType
import org.javacs.kt.position.changedRegion
import org.javacs.kt.position.location
import org.javacs.kt.position.position
Expand All @@ -29,10 +29,10 @@ class CompiledFile(
val parse: KtFile,
val compile: BindingContext,
val module: ModuleDescriptor,
val sourcePath: Collection<KtFile>,
val classPath: CompilerClassPath,
val isScript: Boolean = false,
val kind: CompilationKind = CompilationKind.DEFAULT
private val sourcePath: Collection<KtFile>,
private val classPath: CompilerClassPath,
private val isScript: Boolean = false,
val kind: CompilationType = CompilationType.DEFAULT
) {
/**
* Find the type of the expression at `cursor`
Expand Down Expand Up @@ -99,9 +99,9 @@ class CompiledFile(

private fun expandForReference(cursor: Int, surroundingExpr: KtExpression): KtExpression {
val parent: KtExpression? =
surroundingExpr.parent as? KtDotQualifiedExpression // foo.bar
?: surroundingExpr.parent as? KtSafeQualifiedExpression // foo?.bar
?: surroundingExpr.parent as? KtCallExpression // foo()
surroundingExpr.parent as? KtDotQualifiedExpression
?: surroundingExpr.parent as? KtSafeQualifiedExpression
?: surroundingExpr.parent as? KtCallExpression
return parent?.let { expandForReference(cursor, it) } ?: surroundingExpr
}

Expand Down Expand Up @@ -155,12 +155,12 @@ class CompiledFile(

// Otherwise just use the expression
val recoveryRange = parent.textRange
LOG.info("Re-parsing {}", describeRange(recoveryRange, true))
LOG.info("Re-parsing {}", describeRange(recoveryRange))

surroundingContent = content.substring(recoveryRange.startOffset, content.length - (parse.text.length - recoveryRange.endOffset))
offset = recoveryRange.startOffset

if (asReference && !((parent as? KtParameter)?.hasValOrVar() ?: true)) {
if (asReference && (parent as? KtParameter)?.hasValOrVar() == false) {
// Prepend 'val' to (e.g. function) parameters
val prefix = "val "
surroundingContent = prefix + surroundingContent
Expand Down Expand Up @@ -264,7 +264,7 @@ class CompiledFile(
return "$file ${pos.line + 1}:${pos.character + 1}"
}

private fun describeRange(range: TextRange, oldContent: Boolean = false): String {
private fun describeRange(range: TextRange, oldContent: Boolean = true): String {
val c = if (oldContent) parse.text else content
val start = position(c, range.startOffset)
val end = position(c, range.endOffset)
Expand Down
12 changes: 9 additions & 3 deletions server/src/main/kotlin/org/javacs/kt/CompilerClassPath.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import org.javacs.kt.classpath.defaultClassPathResolver
import org.javacs.kt.compiler.Compiler
import org.javacs.kt.database.DatabaseService
import org.javacs.kt.util.AsyncExecutor
import org.javacs.kt.util.KotlinLSException
import java.io.Closeable
import java.io.File
import java.nio.file.FileSystems
Expand Down Expand Up @@ -39,7 +40,7 @@ class CompilerClassPath(
)
private set

private val async = AsyncExecutor()
private val asyncExecutor = AsyncExecutor(name = "CompilerClassPath")

init {
compiler.updateConfiguration(config)
Expand All @@ -64,7 +65,7 @@ class CompilerClassPath(
refreshCompiler = true
}

async.compute {
asyncExecutor.compute {
val newClassPathWithSources = resolver.classpathWithSources
synchronized(classPath) {
syncPaths(classPath, newClassPathWithSources, "class path with sources") { it.compiledJar }
Expand Down Expand Up @@ -170,7 +171,12 @@ class CompilerClassPath(

override fun close() {
compiler.close()
outputDirectory.delete()
outputDirectory.delete().also { deleted ->
if (!deleted) {
throw KotlinLSException("Failed to delete output directory: $outputDirectory")
}
LOG.info("Deleted output directory: $outputDirectory")
}
}
}

Expand Down
18 changes: 9 additions & 9 deletions server/src/main/kotlin/org/javacs/kt/Configuration.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,21 @@ import java.nio.file.InvalidPathException
import java.nio.file.Path
import java.nio.file.Paths

public data class SnippetsConfiguration(
data class SnippetsConfiguration(
/** Whether code completion should return VSCode-style snippets. */
var enabled: Boolean = true
)

public data class CodegenConfiguration(
data class CodegenConfiguration(
/** Whether to enable code generation to a temporary build directory for Java interoperability. */
var enabled: Boolean = false
)

public data class CompletionConfiguration(
data class CompletionConfiguration(
val snippets: SnippetsConfiguration = SnippetsConfiguration()
)

public data class DiagnosticsConfiguration(
data class DiagnosticsConfiguration(
/** Whether diagnostics are enabled. */
var enabled: Boolean = true,
/** The minimum severity of enabled diagnostics. */
Expand All @@ -35,21 +35,21 @@ public data class DiagnosticsConfiguration(
var debounceTime: Long = 250L
)

public data class JVMConfiguration(
data class JVMConfiguration(
/** Which JVM target the Kotlin compiler uses. See Compiler.jvmTargetFrom for possible values. */
var target: String = "default"
)

public data class CompilerConfiguration(
data class CompilerConfiguration(
val jvm: JVMConfiguration = JVMConfiguration()
)

public data class IndexingConfiguration(
data class IndexingConfiguration(
/** Whether an index of global symbols should be built in the background. */
var enabled: Boolean = true
)

public data class ExternalSourcesConfiguration(
data class ExternalSourcesConfiguration(
/** Whether kls-URIs should be sent to the client to describe classes in JARs. */
var useKlsScheme: Boolean = false,
/** Whether external classes should be automatically converted to Kotlin. */
Expand Down Expand Up @@ -104,7 +104,7 @@ class GsonPathConverter : JsonDeserializer<Path?> {
}
}

public data class Configuration(
data class Configuration(
val codegen: CodegenConfiguration = CodegenConfiguration(),
val compiler: CompilerConfiguration = CompilerConfiguration(),
val completion: CompletionConfiguration = CompletionConfiguration(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,35 +14,35 @@ class KotlinProtocolExtensionService(
private val cp: CompilerClassPath,
private val sp: SourcePath
) : KotlinProtocolExtensions {
private val async = AsyncExecutor()
private val asyncExecutor = AsyncExecutor(name = "KotlinProtocolExtensionService")

override fun jarClassContents(textDocument: TextDocumentIdentifier): CompletableFuture<String?> = async.compute {
override fun jarClassContents(textDocument: TextDocumentIdentifier): CompletableFuture<String?> = asyncExecutor.compute {
uriContentProvider.contentOf(parseURI(textDocument.uri))
}

override fun buildOutputLocation(): CompletableFuture<String?> = async.compute {
override fun buildOutputLocation(): CompletableFuture<String?> = asyncExecutor.compute {
cp.outputDirectory.absolutePath
}

override fun mainClass(textDocument: TextDocumentIdentifier): CompletableFuture<Map<String, Any?>> = async.compute {
override fun mainClass(textDocument: TextDocumentIdentifier): CompletableFuture<Map<String, Any?>> = asyncExecutor.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 {
override fun overrideMember(position: TextDocumentPositionParams): CompletableFuture<List<CodeAction>> = asyncExecutor.compute {
val fileUri = parseURI(position.textDocument.uri)
val compiledFile = sp.currentVersion(fileUri)
val cursorOffset = offset(compiledFile.content, position.position)
Expand Down
Loading