-
Notifications
You must be signed in to change notification settings - Fork 380
/
AutoImportFix.kt
196 lines (164 loc) · 7.49 KB
/
AutoImportFix.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*
* Use of this source code is governed by the MIT license that can be
* found in the LICENSE file.
*/
package org.rust.ide.inspections.import
import com.intellij.codeInsight.daemon.impl.ShowAutoImportPass
import com.intellij.codeInsight.hint.HintManager
import com.intellij.codeInsight.intention.PriorityAction
import com.intellij.codeInspection.HintAction
import com.intellij.codeInspection.LocalQuickFixOnPsiElement
import com.intellij.ide.DataManager
import com.intellij.openapi.actionSystem.DataContext
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.project.Project
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiFile
import org.rust.ide.inspections.import.AutoImportFix.Type.*
import org.rust.ide.settings.RsCodeInsightSettings
import org.rust.ide.utils.import.*
import org.rust.lang.core.psi.*
import org.rust.lang.core.psi.ext.*
import org.rust.lang.core.resolve.TYPES_N_VALUES
import org.rust.lang.core.types.infer.ResolvedPath
import org.rust.lang.core.types.inference
import org.rust.openapiext.Testmark
import org.rust.openapiext.runWriteCommandAction
class AutoImportFix(element: RsElement, private val context: Context) :
LocalQuickFixOnPsiElement(element), PriorityAction, HintAction {
private var isConsumed: Boolean = false
override fun getFamilyName(): String = NAME
override fun getText(): String = familyName
public override fun isAvailable(): Boolean = super.isAvailable() && !isConsumed
override fun getPriority(): PriorityAction.Priority = PriorityAction.Priority.TOP
override fun invoke(project: Project, file: PsiFile, startElement: PsiElement, endElement: PsiElement) {
invoke(project)
}
private fun invoke(project: Project) {
val element = startElement as? RsElement ?: return
val candidates = context.candidates
if (candidates.size == 1) {
project.runWriteCommandAction {
candidates.first().import(element)
}
} else {
DataManager.getInstance().dataContextFromFocusAsync.onSuccess {
chooseItemAndImport(project, it, candidates, element)
}
}
isConsumed = true
}
private fun chooseItemAndImport(
project: Project,
dataContext: DataContext,
items: List<ImportCandidateBase>,
context: RsElement
) {
showItemsToImportChooser(project, dataContext, items) { selectedValue ->
project.runWriteCommandAction {
selectedValue.import(context)
}
}
}
override fun isAvailable(project: Project, editor: Editor?, file: PsiFile?): Boolean = isAvailable
override fun invoke(project: Project, editor: Editor?, file: PsiFile?) = invoke(project)
override fun startInWriteAction(): Boolean = true
override fun showHint(editor: Editor): Boolean {
if (!RsCodeInsightSettings.getInstance().showImportPopup) return false
if (HintManager.getInstance().hasShownHintsThatWillHideByOtherHint(true)) return false
val candidates = context.candidates
val hint = candidates[0].info.usePath
val multiple = candidates.size > 1
val message = ShowAutoImportPass.getMessage(multiple, hint)
val element = startElement
HintManager.getInstance().showQuestionHint(editor, message, element.textOffset, element.endOffset) {
invoke(element.project)
true
}
return true
}
override fun fixSilently(editor: Editor): Boolean {
if (!RsCodeInsightSettings.getInstance().addUnambiguousImportsOnTheFly) return false
val candidates = context.candidates
if (candidates.size != 1) return false
val project = editor.project ?: return false
invoke(project)
return true
}
companion object {
const val NAME = "Import"
fun findApplicableContext(project: Project, path: RsPath): Context? {
if (path.reference == null) return null
val basePath = path.basePath()
if (basePath.resolveStatus != PathResolveStatus.UNRESOLVED) return null
if (path.ancestorStrict<RsUseSpeck>() != null) {
// Don't try to import path in use item
Testmarks.pathInUseItem.hit()
return null
}
val referenceName = basePath.referenceName ?: return null
val isNameInScope = path.hasInScope(referenceName, TYPES_N_VALUES) && path.parent !is RsMacroCall
if (isNameInScope) {
// Don't import names that are already in scope but cannot be resolved
// because namespace of psi element prevents correct name resolution.
// It's possible for incorrect or incomplete code like "let map = HashMap"
Testmarks.nameInScope.hit()
return null
}
val superPath = path.rootPath()
val candidates = if (path.useAutoImportWithNewResolve) run {
val importContext = ImportContext2.from(path, ImportContext2.Type.AUTO_IMPORT) ?: return@run emptyList()
ImportCandidatesCollector2.getImportCandidates(importContext, referenceName)
} else {
ImportCandidatesCollector.getImportCandidates(
ImportContext.from(project, path, false),
referenceName,
superPath.text
) {
superPath != basePath || !(it.item is RsMod || it.item is RsModDeclItem || it.item.parent is RsMembers)
}.toList()
}
return Context(GENERAL_PATH, candidates)
}
fun findApplicableContext(project: Project, methodCall: RsMethodCall): Context? {
val results = methodCall.inference?.getResolvedMethod(methodCall) ?: emptyList()
if (results.isEmpty()) return Context(METHOD, emptyList())
val candidates = if (methodCall.useAutoImportWithNewResolve) {
ImportCandidatesCollector2.getImportCandidates(methodCall, results)
} else {
ImportCandidatesCollector.getImportCandidates(project, methodCall, results)?.toList()
} ?: return null
return Context(METHOD, candidates)
}
/** Import traits for type-related UFCS method calls and assoc items */
fun findApplicableContextForAssocItemPath(project: Project, path: RsPath): Context? {
val parent = path.parent as? RsPathExpr ?: return null
val qualifierElement = path.qualifier?.reference?.resolve()
if (qualifierElement is RsTraitItem) return null
val resolved = path.inference?.getResolvedPath(parent) ?: return null
val sources = resolved.map {
if (it !is ResolvedPath.AssocItem) return null
it.source
}
val candidates = if (path.useAutoImportWithNewResolve) {
ImportCandidatesCollector2.getTraitImportCandidates(path, sources)
} else {
ImportCandidatesCollector.getTraitImportCandidates(project, path, sources)?.toList()
} ?: return null
return Context(ASSOC_ITEM_PATH, candidates)
}
}
data class Context(
val type: Type,
val candidates: List<ImportCandidateBase>
)
enum class Type {
GENERAL_PATH,
ASSOC_ITEM_PATH,
METHOD
}
object Testmarks {
val pathInUseItem = Testmark("pathInUseItem")
val nameInScope = Testmark("nameInScope")
}
}