Skip to content

Commit 96b8f8e

Browse files
committed
Move findDeclaration into a more logical place and minor restructuring
1 parent 2ecaf44 commit 96b8f8e

File tree

3 files changed

+28
-28
lines changed

3 files changed

+28
-28
lines changed

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,21 @@ import com.intellij.openapi.util.TextRange
44
import com.intellij.psi.PsiElement
55
import org.javacs.kt.compiler.CompilationKind
66
import org.javacs.kt.position.changedRegion
7+
import org.javacs.kt.position.location
78
import org.javacs.kt.position.position
89
import org.javacs.kt.util.findParent
910
import org.javacs.kt.util.nullResult
1011
import org.javacs.kt.util.toPath
1112
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
1213
import org.jetbrains.kotlin.descriptors.ModuleDescriptor
1314
import org.jetbrains.kotlin.lexer.KtTokens
15+
import org.jetbrains.kotlin.js.resolve.diagnostics.findPsi
1416
import org.jetbrains.kotlin.psi.*
1517
import org.jetbrains.kotlin.psi.psiUtil.parentsWithSelf
1618
import org.jetbrains.kotlin.resolve.BindingContext
1719
import org.jetbrains.kotlin.resolve.scopes.LexicalScope
1820
import org.jetbrains.kotlin.types.KotlinType
21+
import org.eclipse.lsp4j.Location
1922
import java.nio.file.Paths
2023

2124
class CompiledFile(
@@ -171,6 +174,25 @@ class CompiledFile(
171174
return psi.findParent<KtElement>()
172175
}
173176

177+
178+
/**
179+
* Find the declaration of the element at the cursor. Only works if the element at the cursor is a reference.
180+
*/
181+
fun findDeclaration(cursor: Int): Pair<KtNamedDeclaration, Location>? {
182+
val (_, target) = referenceAtPoint(cursor) ?: return null
183+
val psi = target.findPsi()
184+
185+
return if (psi is KtNamedDeclaration) {
186+
psi.nameIdentifier?.let {
187+
location(it)?.let { location ->
188+
Pair(psi, location)
189+
}
190+
}
191+
} else {
192+
null
193+
}
194+
}
195+
174196
/**
175197
* Find the lexical-scope surrounding `cursor`.
176198
* This may be out-of-date if the user is typing quickly.

server/src/main/kotlin/org/javacs/kt/highlight/DocumentHighlight.kt

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,13 @@ import org.eclipse.lsp4j.Location
66
import org.javacs.kt.CompiledFile
77
import org.javacs.kt.position.range
88
import org.javacs.kt.references.findReferencesToDeclarationInFile
9-
import org.javacs.kt.rename.findDeclaration
109
import org.javacs.kt.util.findParent
1110
import org.jetbrains.kotlin.psi.KtFile
1211
import org.jetbrains.kotlin.psi.KtNamedDeclaration
1312

1413
fun documentHighlightsAt(file: CompiledFile, cursor: Int): List<DocumentHighlight> {
15-
val (declaration, declarationLocation) = findDeclaration(file, cursor)
16-
?: findDeclarationCursorSite(file, cursor)
14+
val (declaration, declarationLocation) = file.findDeclaration(cursor)
15+
?: file.findDeclarationCursorSite(cursor)
1716
?: return emptyList()
1817
val references = findReferencesToDeclarationInFile(declaration, file)
1918

@@ -24,20 +23,17 @@ fun documentHighlightsAt(file: CompiledFile, cursor: Int): List<DocumentHighligh
2423
} + references.map { DocumentHighlight(it, DocumentHighlightKind.Text) }
2524
}
2625

27-
private fun findDeclarationCursorSite(
28-
file: CompiledFile,
29-
cursor: Int
30-
): Pair<KtNamedDeclaration, Location>? {
26+
private fun CompiledFile.findDeclarationCursorSite(cursor: Int): Pair<KtNamedDeclaration, Location>? {
3127
// current symbol might be a declaration. This function is used as a fallback when
3228
// findDeclaration fails
33-
val declaration = file.elementAtPoint(cursor)?.findParent<KtNamedDeclaration>()
29+
val declaration = elementAtPoint(cursor)?.findParent<KtNamedDeclaration>()
3430

3531
return declaration?.let {
3632
// in this scenario we know that the declaration will be at the cursor site, so uri is not
3733
// important
3834
Pair(it,
3935
Location("",
40-
range(file.content, it.nameIdentifier?.textRange ?: return null)))
36+
range(content, it.nameIdentifier?.textRange ?: return null)))
4137
}
4238
}
4339

server/src/main/kotlin/org/javacs/kt/rename/Rename.kt

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,10 @@ import org.eclipse.lsp4j.*
44
import org.eclipse.lsp4j.jsonrpc.messages.Either
55
import org.javacs.kt.CompiledFile
66
import org.javacs.kt.SourcePath
7-
import org.javacs.kt.position.location
87
import org.javacs.kt.references.findReferences
9-
import org.jetbrains.kotlin.js.resolve.diagnostics.findPsi
10-
import org.jetbrains.kotlin.psi.KtNamedDeclaration
118

129
fun renameSymbol(file: CompiledFile, cursor: Int, sp: SourcePath, newName: String): WorkspaceEdit? {
13-
val (declaration, location) = findDeclaration(file, cursor) ?: return null
10+
val (declaration, location) = file.findDeclaration(cursor) ?: return null
1411
return declaration.let {
1512
val declarationEdit = Either.forLeft<TextDocumentEdit, ResourceOperation>(TextDocumentEdit(
1613
VersionedTextDocumentIdentifier().apply { uri = location.uri },
@@ -27,18 +24,3 @@ fun renameSymbol(file: CompiledFile, cursor: Int, sp: SourcePath, newName: Strin
2724
WorkspaceEdit(listOf(declarationEdit) + referenceEdits)
2825
}
2926
}
30-
31-
fun findDeclaration(file: CompiledFile, cursor: Int): Pair<KtNamedDeclaration, Location>? {
32-
val (_, target) = file.referenceAtPoint(cursor) ?: return null
33-
val psi = target.findPsi()
34-
35-
return if (psi is KtNamedDeclaration) {
36-
psi.nameIdentifier?.let {
37-
location(it)?.let { location ->
38-
Pair(psi, location)
39-
}
40-
}
41-
} else {
42-
null
43-
}
44-
}

0 commit comments

Comments
 (0)