|
| 1 | +package org.rhai.refactoring |
| 2 | + |
| 3 | +import com.intellij.openapi.actionSystem.CommonDataKeys |
| 4 | +import com.intellij.openapi.actionSystem.DataContext |
| 5 | +import com.intellij.openapi.editor.Editor |
| 6 | +import com.intellij.openapi.project.Project |
| 7 | +import com.intellij.psi.PsiElement |
| 8 | +import com.intellij.psi.PsiFile |
| 9 | +import com.intellij.psi.util.elementType |
| 10 | +import com.intellij.refactoring.rename.PsiElementRenameHandler |
| 11 | +import com.intellij.refactoring.rename.RenameHandler |
| 12 | +import org.rhai.RhaiTypes |
| 13 | +import org.rhai.lang.RhaiFile |
| 14 | + |
| 15 | +/** |
| 16 | + * Rename handler for Rhai identifiers. |
| 17 | + * Enables Shift+F6 rename refactoring for variables, functions, constants, etc. |
| 18 | + */ |
| 19 | +class RhaiRenameHandler : RenameHandler { |
| 20 | + |
| 21 | + override fun isAvailableOnDataContext(dataContext: DataContext): Boolean { |
| 22 | + val element = getTargetElement(dataContext) ?: return false |
| 23 | + return isRenamableElement(element) |
| 24 | + } |
| 25 | + |
| 26 | + override fun isRenaming(dataContext: DataContext): Boolean { |
| 27 | + return isAvailableOnDataContext(dataContext) |
| 28 | + } |
| 29 | + |
| 30 | + override fun invoke(project: Project, editor: Editor?, file: PsiFile?, dataContext: DataContext) { |
| 31 | + val element = getTargetElement(dataContext) ?: return |
| 32 | + if (editor == null || file == null) return |
| 33 | + |
| 34 | + if (isRenamableElement(element)) { |
| 35 | + // Use built-in rename dialog with our element |
| 36 | + PsiElementRenameHandler.invoke(element, project, element, editor) |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + override fun invoke(project: Project, elements: Array<out PsiElement>, dataContext: DataContext) { |
| 41 | + if (elements.isNotEmpty()) { |
| 42 | + val editor = CommonDataKeys.EDITOR.getData(dataContext) |
| 43 | + if (editor != null) { |
| 44 | + PsiElementRenameHandler.invoke(elements[0], project, elements[0], editor) |
| 45 | + } |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + private fun getTargetElement(dataContext: DataContext): PsiElement? { |
| 50 | + val editor = CommonDataKeys.EDITOR.getData(dataContext) ?: return null |
| 51 | + val file = CommonDataKeys.PSI_FILE.getData(dataContext) ?: return null |
| 52 | + |
| 53 | + if (file !is RhaiFile) return null |
| 54 | + |
| 55 | + val offset = editor.caretModel.offset |
| 56 | + return file.findElementAt(offset) |
| 57 | + } |
| 58 | + |
| 59 | + private fun isRenamableElement(element: PsiElement): Boolean { |
| 60 | + // Only allow renaming IDENTIFIER tokens |
| 61 | + if (element.elementType != RhaiTypes.IDENTIFIER) return false |
| 62 | + |
| 63 | + val name = element.text |
| 64 | + |
| 65 | + // Don't allow renaming keywords |
| 66 | + val keywords = setOf( |
| 67 | + "let", "const", "fn", "if", "else", "while", "for", "in", |
| 68 | + "loop", "break", "continue", "return", "throw", "try", "catch", |
| 69 | + "switch", "default", "import", "export", "module", "private", "pub", |
| 70 | + "true", "false", "null", "this", "global", "is", "as" |
| 71 | + ) |
| 72 | + if (keywords.contains(name)) return false |
| 73 | + |
| 74 | + // Don't allow renaming built-in functions |
| 75 | + val builtins = setOf( |
| 76 | + "print", "println", "debug", "type_of", "is_def", |
| 77 | + "to_string", "to_int", "to_float", "to_decimal", |
| 78 | + "abs", "sin", "cos", "tan", "sqrt", "exp", "ln", "log", |
| 79 | + "push", "pop", "shift", "insert", "remove", "reverse", "sort", |
| 80 | + "map", "filter", "reduce", "some", "all", "for_each", |
| 81 | + "keys", "values", "len", "contains", "trim", "replace", "split" |
| 82 | + ) |
| 83 | + if (builtins.contains(name)) return false |
| 84 | + |
| 85 | + return true |
| 86 | + } |
| 87 | +} |
0 commit comments