Skip to content

Commit f5dda17

Browse files
committed
Fix issue where rename doesn't work on variable declaration sites
1 parent 82e28c0 commit f5dda17

File tree

3 files changed

+18
-15
lines changed

3 files changed

+18
-15
lines changed

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import org.javacs.kt.compiler.CompilationKind
66
import org.javacs.kt.position.changedRegion
77
import org.javacs.kt.position.location
88
import org.javacs.kt.position.position
9+
import org.javacs.kt.position.range
910
import org.javacs.kt.util.findParent
1011
import org.javacs.kt.util.nullResult
1112
import org.javacs.kt.util.toPath
@@ -193,6 +194,22 @@ class CompiledFile(
193194
}
194195
}
195196

197+
/**
198+
* Find the declaration of the element at the cursor.
199+
* Works even in cases where the element at the cursor might not be a reference, so works for declaration sites.
200+
*/
201+
fun findDeclarationCursorSite(cursor: Int): Pair<KtNamedDeclaration, Location>? {
202+
// current symbol might be a declaration. This function is used as a fallback when
203+
// findDeclaration fails
204+
val declaration = elementAtPoint(cursor)?.findParent<KtNamedDeclaration>()
205+
206+
return declaration?.let {
207+
Pair(it,
208+
Location(it.containingFile.name,
209+
range(content, it.nameIdentifier?.textRange ?: return null)))
210+
}
211+
}
212+
196213
/**
197214
* Find the lexical-scope surrounding `cursor`.
198215
* This may be out-of-date if the user is typing quickly.

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

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,4 @@ fun documentHighlightsAt(file: CompiledFile, cursor: Int): List<DocumentHighligh
2323
} + references.map { DocumentHighlight(it, DocumentHighlightKind.Text) }
2424
}
2525

26-
private fun CompiledFile.findDeclarationCursorSite(cursor: Int): Pair<KtNamedDeclaration, Location>? {
27-
// current symbol might be a declaration. This function is used as a fallback when
28-
// findDeclaration fails
29-
val declaration = elementAtPoint(cursor)?.findParent<KtNamedDeclaration>()
30-
31-
return declaration?.let {
32-
// in this scenario we know that the declaration will be at the cursor site, so uri is not
33-
// important
34-
Pair(it,
35-
Location("",
36-
range(content, it.nameIdentifier?.textRange ?: return null)))
37-
}
38-
}
39-
4026
private fun KtNamedDeclaration.isInFile(file: KtFile) = this.containingFile == file

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) ?: return null
10+
val (declaration, location) = file.findDeclaration(cursor) ?: file.findDeclarationCursorSite(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)