Skip to content

Commit a57d8a8

Browse files
committed
Unify the findDeclaration implementations to make it easier to use
1 parent f5dda17 commit a57d8a8

File tree

3 files changed

+8
-4
lines changed

3 files changed

+8
-4
lines changed

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,10 +176,15 @@ class CompiledFile(
176176
}
177177

178178

179+
/**
180+
* Find the declaration of the element at the cursor.
181+
*/
182+
fun findDeclaration(cursor: Int): Pair<KtNamedDeclaration, Location>? = findDeclarationReference(cursor) ?: findDeclarationCursorSite(cursor)
183+
179184
/**
180185
* Find the declaration of the element at the cursor. Only works if the element at the cursor is a reference.
181186
*/
182-
fun findDeclaration(cursor: Int): Pair<KtNamedDeclaration, Location>? {
187+
private fun findDeclarationReference(cursor: Int): Pair<KtNamedDeclaration, Location>? {
183188
val (_, target) = referenceAtPoint(cursor) ?: return null
184189
val psi = target.findPsi()
185190

@@ -198,7 +203,7 @@ class CompiledFile(
198203
* Find the declaration of the element at the cursor.
199204
* Works even in cases where the element at the cursor might not be a reference, so works for declaration sites.
200205
*/
201-
fun findDeclarationCursorSite(cursor: Int): Pair<KtNamedDeclaration, Location>? {
206+
private fun findDeclarationCursorSite(cursor: Int): Pair<KtNamedDeclaration, Location>? {
202207
// current symbol might be a declaration. This function is used as a fallback when
203208
// findDeclaration fails
204209
val declaration = elementAtPoint(cursor)?.findParent<KtNamedDeclaration>()

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import org.jetbrains.kotlin.psi.KtNamedDeclaration
1212

1313
fun documentHighlightsAt(file: CompiledFile, cursor: Int): List<DocumentHighlight> {
1414
val (declaration, declarationLocation) = file.findDeclaration(cursor)
15-
?: file.findDeclarationCursorSite(cursor)
1615
?: return emptyList()
1716
val references = findReferencesToDeclarationInFile(declaration, file)
1817

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import org.javacs.kt.SourcePath
77
import org.javacs.kt.references.findReferences
88

99
fun renameSymbol(file: CompiledFile, cursor: Int, sp: SourcePath, newName: String): WorkspaceEdit? {
10-
val (declaration, location) = file.findDeclaration(cursor) ?: file.findDeclarationCursorSite(cursor) ?: return null
10+
val (declaration, location) = file.findDeclaration(cursor) ?: return null
1111
return declaration.let {
1212
val declarationEdit = Either.forLeft<TextDocumentEdit, ResourceOperation>(TextDocumentEdit(
1313
VersionedTextDocumentIdentifier().apply { uri = location.uri },

0 commit comments

Comments
 (0)