Skip to content

Commit 29fe7e9

Browse files
refactor: general code improvements and fixes
- Remove redundant safe operators - Remove unused code - Remove unsused imports - Fix use of deprecated URI constructor - Reduce cognitive load for goToDefinition - Merge definition with when statement - Rename for consistency - Remove redundant comments - Remove wildcard imports - Run createDetektBaseline task to refresh the baseline
1 parent 8418fb5 commit 29fe7e9

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+1238
-1471
lines changed

detekt_baseline.xml

+10-395
Large diffs are not rendered by default.

server/src/main/kotlin/org/javacs/kt/CompiledFile.kt

+11-11
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package org.javacs.kt
22

33
import com.intellij.openapi.util.TextRange
44
import com.intellij.psi.PsiElement
5-
import org.javacs.kt.compiler.CompilationKind
5+
import org.javacs.kt.compiler.CompilationType
66
import org.javacs.kt.position.changedRegion
77
import org.javacs.kt.position.location
88
import org.javacs.kt.position.position
@@ -29,10 +29,10 @@ class CompiledFile(
2929
val parse: KtFile,
3030
val compile: BindingContext,
3131
val module: ModuleDescriptor,
32-
val sourcePath: Collection<KtFile>,
33-
val classPath: CompilerClassPath,
34-
val isScript: Boolean = false,
35-
val kind: CompilationKind = CompilationKind.DEFAULT
32+
private val sourcePath: Collection<KtFile>,
33+
private val classPath: CompilerClassPath,
34+
private val isScript: Boolean = false,
35+
val kind: CompilationType = CompilationType.DEFAULT
3636
) {
3737
/**
3838
* Find the type of the expression at `cursor`
@@ -99,9 +99,9 @@ class CompiledFile(
9999

100100
private fun expandForReference(cursor: Int, surroundingExpr: KtExpression): KtExpression {
101101
val parent: KtExpression? =
102-
surroundingExpr.parent as? KtDotQualifiedExpression // foo.bar
103-
?: surroundingExpr.parent as? KtSafeQualifiedExpression // foo?.bar
104-
?: surroundingExpr.parent as? KtCallExpression // foo()
102+
surroundingExpr.parent as? KtDotQualifiedExpression
103+
?: surroundingExpr.parent as? KtSafeQualifiedExpression
104+
?: surroundingExpr.parent as? KtCallExpression
105105
return parent?.let { expandForReference(cursor, it) } ?: surroundingExpr
106106
}
107107

@@ -155,12 +155,12 @@ class CompiledFile(
155155

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

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

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

267-
private fun describeRange(range: TextRange, oldContent: Boolean = false): String {
267+
private fun describeRange(range: TextRange, oldContent: Boolean = true): String {
268268
val c = if (oldContent) parse.text else content
269269
val start = position(c, range.startOffset)
270270
val end = position(c, range.endOffset)

server/src/main/kotlin/org/javacs/kt/CompilerClassPath.kt

+9-3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import org.javacs.kt.classpath.defaultClassPathResolver
55
import org.javacs.kt.compiler.Compiler
66
import org.javacs.kt.database.DatabaseService
77
import org.javacs.kt.util.AsyncExecutor
8+
import org.javacs.kt.util.KotlinLSException
89
import java.io.Closeable
910
import java.io.File
1011
import java.nio.file.FileSystems
@@ -39,7 +40,7 @@ class CompilerClassPath(
3940
)
4041
private set
4142

42-
private val async = AsyncExecutor()
43+
private val asyncExecutor = AsyncExecutor(name = "CompilerClassPath")
4344

4445
init {
4546
compiler.updateConfiguration(config)
@@ -64,7 +65,7 @@ class CompilerClassPath(
6465
refreshCompiler = true
6566
}
6667

67-
async.compute {
68+
asyncExecutor.compute {
6869
val newClassPathWithSources = resolver.classpathWithSources
6970
synchronized(classPath) {
7071
syncPaths(classPath, newClassPathWithSources, "class path with sources") { it.compiledJar }
@@ -170,7 +171,12 @@ class CompilerClassPath(
170171

171172
override fun close() {
172173
compiler.close()
173-
outputDirectory.delete()
174+
outputDirectory.delete().also { deleted ->
175+
if (!deleted) {
176+
throw KotlinLSException("Failed to delete output directory: $outputDirectory")
177+
}
178+
LOG.info("Deleted output directory: $outputDirectory")
179+
}
174180
}
175181
}
176182

server/src/main/kotlin/org/javacs/kt/Configuration.kt

+9-9
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,21 @@ import java.nio.file.InvalidPathException
1212
import java.nio.file.Path
1313
import java.nio.file.Paths
1414

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

20-
public data class CodegenConfiguration(
20+
data class CodegenConfiguration(
2121
/** Whether to enable code generation to a temporary build directory for Java interoperability. */
2222
var enabled: Boolean = false
2323
)
2424

25-
public data class CompletionConfiguration(
25+
data class CompletionConfiguration(
2626
val snippets: SnippetsConfiguration = SnippetsConfiguration()
2727
)
2828

29-
public data class DiagnosticsConfiguration(
29+
data class DiagnosticsConfiguration(
3030
/** Whether diagnostics are enabled. */
3131
var enabled: Boolean = true,
3232
/** The minimum severity of enabled diagnostics. */
@@ -35,21 +35,21 @@ public data class DiagnosticsConfiguration(
3535
var debounceTime: Long = 250L
3636
)
3737

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

43-
public data class CompilerConfiguration(
43+
data class CompilerConfiguration(
4444
val jvm: JVMConfiguration = JVMConfiguration()
4545
)
4646

47-
public data class IndexingConfiguration(
47+
data class IndexingConfiguration(
4848
/** Whether an index of global symbols should be built in the background. */
4949
var enabled: Boolean = true
5050
)
5151

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

107-
public data class Configuration(
107+
data class Configuration(
108108
val codegen: CodegenConfiguration = CodegenConfiguration(),
109109
val compiler: CompilerConfiguration = CompilerConfiguration(),
110110
val completion: CompletionConfiguration = CompletionConfiguration(),

server/src/main/kotlin/org/javacs/kt/KotlinProtocolExtensionService.kt

+7-7
Original file line numberDiff line numberDiff line change
@@ -14,35 +14,35 @@ class KotlinProtocolExtensionService(
1414
private val cp: CompilerClassPath,
1515
private val sp: SourcePath
1616
) : KotlinProtocolExtensions {
17-
private val async = AsyncExecutor()
17+
private val asyncExecutor = AsyncExecutor(name = "KotlinProtocolExtensionService")
1818

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

23-
override fun buildOutputLocation(): CompletableFuture<String?> = async.compute {
23+
override fun buildOutputLocation(): CompletableFuture<String?> = asyncExecutor.compute {
2424
cp.outputDirectory.absolutePath
2525
}
2626

27-
override fun mainClass(textDocument: TextDocumentIdentifier): CompletableFuture<Map<String, Any?>> = async.compute {
27+
override fun mainClass(textDocument: TextDocumentIdentifier): CompletableFuture<Map<String, Any?>> = asyncExecutor.compute {
2828
val fileUri = parseURI(textDocument.uri)
2929
val filePath = Paths.get(fileUri)
30-
30+
3131
// we find the longest one in case both the root and submodule are included
3232
val workspacePath = cp.workspaceRoots.filter {
3333
filePath.startsWith(it)
3434
}.map {
3535
it.toString()
3636
}.maxByOrNull(String::length) ?: ""
37-
37+
3838
val compiledFile = sp.currentVersion(fileUri)
3939

4040
resolveMain(compiledFile) + mapOf(
4141
"projectRoot" to workspacePath
4242
)
4343
}
4444

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

0 commit comments

Comments
 (0)