Skip to content

Commit 0d1ac46

Browse files
committed
feat(refactoring): extract method for readable #129
1 parent 5538584 commit 0d1ac46

File tree

2 files changed

+30
-15
lines changed

2 files changed

+30
-15
lines changed

docs/features/refactoring.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,9 @@ In [#132](https://github.com/unit-mesh/auto-dev/issues/132), we provide basic fu
5252
2. select the variable you want to rename use `Shift` + `F6`
5353

5454
<img src="https://unitmesh.cc/auto-dev/autodev-rename.png" alt="Rename Functions" width="600px"/>
55+
56+
## Resource
57+
58+
In [How to Refactor this Code? An Exploratory Study on Developer-ChatGPT Refactoring Conversations](https://arxiv.org/abs/2402.06013) ,
59+
talking about how to refactoring to better understand how developers identify areas for improvement in code
60+
and how ChatGPT addresses developers' needs.

src/main/kotlin/cc/unitmesh/devti/practise/RenameLookupManagerListener.kt

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import com.intellij.psi.PsiNameIdentifierOwner
1818
import com.intellij.psi.PsiWhiteSpace
1919
import com.intellij.psi.impl.source.tree.LeafPsiElement
2020
import com.intellij.psi.util.PsiEditorUtil
21+
import kotlinx.coroutines.cancel
2122
import kotlinx.coroutines.flow.*
2223
import kotlinx.coroutines.launch
2324

@@ -31,21 +32,7 @@ class RenameLookupManagerListener(val project: Project) : LookupManagerListener
3132
val lookupImpl = newLookup as? LookupImpl ?: return
3233
val editor = lookupImpl.editor as? EditorEx ?: return
3334

34-
val startOffset = lookupImpl.lookupOriginalStart
35-
val psiFile = PsiEditorUtil.getPsiFile(editor)
36-
37-
var targetElement: PsiElement? = null
38-
if (startOffset >= 0) {
39-
targetElement = psiFile.findElementAt(startOffset)
40-
}
41-
42-
if (targetElement == null) {
43-
targetElement = psiFile.findElementAt(editor.caretModel.offset)
44-
}
45-
46-
if (targetElement is LeafPsiElement || targetElement is PsiWhiteSpace) {
47-
targetElement = targetElement.parent
48-
}
35+
val targetElement: PsiElement? = lookupElement(lookupImpl, editor)
4936

5037
// maybe user just typing, we should handle for this
5138
val element = targetElement ?: return
@@ -98,6 +85,28 @@ class RenameLookupManagerListener(val project: Project) : LookupManagerListener
9885
stringJob.start()
9986
}
10087

88+
private fun lookupElement(
89+
lookupImpl: LookupImpl,
90+
editor: EditorEx
91+
): PsiElement? {
92+
val startOffset = lookupImpl.lookupOriginalStart
93+
val psiFile = PsiEditorUtil.getPsiFile(editor)
94+
95+
var targetElement: PsiElement? = null
96+
if (startOffset >= 0) {
97+
targetElement = psiFile.findElementAt(startOffset)
98+
}
99+
100+
if (targetElement == null) {
101+
targetElement = psiFile.findElementAt(editor.caretModel.offset)
102+
}
103+
104+
if (targetElement is LeafPsiElement || targetElement is PsiWhiteSpace) {
105+
targetElement = targetElement.parent
106+
}
107+
return targetElement
108+
}
109+
101110
private fun extractSuggestionsFromString(result: String) = result.split("\n").map {
102111
it.replace(Regex("^\\d+\\."), "")
103112
.trim()

0 commit comments

Comments
 (0)