|
1 | 1 | package com.joutvhu.intellij.dartscripts; |
2 | 2 |
|
3 | 3 | import com.intellij.codeInsight.daemon.LineMarkerInfo; |
4 | | -import com.intellij.codeInsight.daemon.MergeableLineMarkerInfo; |
5 | | -import com.intellij.codeInsight.intention.PriorityAction; |
6 | | -import com.intellij.execution.ExecutorRegistryImpl; |
7 | | -import com.intellij.execution.actions.RunContextAction; |
8 | | -import com.intellij.execution.lineMarker.ExecutorAction; |
9 | 4 | import com.intellij.execution.lineMarker.RunLineMarkerProvider; |
10 | 5 | import com.intellij.icons.AllIcons; |
11 | | -import com.intellij.ide.DataManager; |
12 | | -import com.intellij.openapi.actionSystem.ActionGroup; |
13 | | -import com.intellij.openapi.actionSystem.ActionWithDelegate; |
14 | 6 | import com.intellij.openapi.actionSystem.AnAction; |
15 | | -import com.intellij.openapi.actionSystem.AnActionEvent; |
16 | | -import com.intellij.openapi.actionSystem.DataContext; |
17 | 7 | import com.intellij.openapi.actionSystem.DefaultActionGroup; |
18 | | -import com.intellij.openapi.diagnostic.Logger; |
19 | | -import com.intellij.openapi.editor.markup.GutterIconRenderer; |
20 | | -import com.intellij.openapi.editor.markup.MarkupEditorFilter; |
21 | | -import com.intellij.openapi.editor.markup.MarkupEditorFilterFactory; |
22 | | -import com.intellij.openapi.util.Key; |
23 | | -import com.intellij.openapi.util.Pair; |
24 | 8 | import com.intellij.openapi.vfs.VirtualFile; |
25 | 9 | import com.intellij.psi.PsiElement; |
26 | | -import com.intellij.psi.SmartPointerManager; |
27 | | -import com.intellij.psi.SmartPsiElementPointer; |
28 | 10 | import com.intellij.psi.impl.source.tree.LeafPsiElement; |
29 | | -import com.intellij.util.Function; |
30 | | -import com.intellij.util.containers.ContainerUtil; |
31 | 11 | import com.joutvhu.intellij.dartscripts.action.PubspecRunScriptAction; |
32 | 12 | import com.joutvhu.intellij.dartscripts.util.DartBundle; |
33 | | -import org.jetbrains.annotations.Nls; |
34 | 13 | import org.jetbrains.annotations.NotNull; |
35 | | -import org.jetbrains.annotations.Nullable; |
36 | 14 | import org.jetbrains.yaml.YAMLTokenTypes; |
37 | 15 | import org.jetbrains.yaml.psi.YAMLDocument; |
38 | 16 | import org.jetbrains.yaml.psi.YAMLFile; |
39 | 17 | import org.jetbrains.yaml.psi.YAMLKeyValue; |
40 | 18 | import org.jetbrains.yaml.psi.YAMLScalar; |
41 | 19 | import org.jetbrains.yaml.psi.impl.YAMLBlockMappingImpl; |
42 | 20 |
|
43 | | -import javax.swing.*; |
44 | | -import java.util.Arrays; |
45 | | -import java.util.List; |
46 | | - |
47 | 21 | public class PubspecLineMarkerProvider extends RunLineMarkerProvider { |
48 | 22 | public static final String SCRIPT_KEY = "scripts"; |
49 | 23 | public static final String PUBSPEC_FILE = "pubspec.yaml"; |
50 | | - public static final Key<Pair<PsiElement, DataContext>> LOCATION_WRAPPER = Key.create("LOCATION_WRAPPER"); |
51 | 24 |
|
52 | 25 | @Override |
53 | 26 | public LineMarkerInfo<?> getLineMarkerInfo(@NotNull PsiElement element) { |
@@ -81,159 +54,4 @@ private LineMarkerInfo<?> createMarkerInfo(YAMLKeyValue element) { |
81 | 54 | actionGroup.add(new LineMarkerActionWrapper(element, action)); |
82 | 55 | return new RunLineMarkerInfo(element, AllIcons.Actions.Execute, actionGroup); |
83 | 56 | } |
84 | | - |
85 | | - public static class RunLineMarkerInfo extends MergeableLineMarkerInfo<PsiElement> { |
86 | | - private final DefaultActionGroup myActionGroup; |
87 | | - private final AnAction mySingleAction; |
88 | | - |
89 | | - RunLineMarkerInfo(PsiElement element, Icon icon, DefaultActionGroup actionGroup) { |
90 | | - this(element, icon, psiElement -> "", actionGroup); |
91 | | - } |
92 | | - |
93 | | - RunLineMarkerInfo(PsiElement element, Icon icon, Function<? super PsiElement, @Nls String> tooltipProvider, DefaultActionGroup actionGroup) { |
94 | | - super(element, element.getTextRange(), icon, tooltipProvider, null, GutterIconRenderer.Alignment.CENTER, () -> tooltipProvider.fun(element)); |
95 | | - myActionGroup = actionGroup; |
96 | | - if (myActionGroup.getChildrenCount() == 1) { |
97 | | - mySingleAction = myActionGroup.getChildActionsOrStubs()[0]; |
98 | | - } else { |
99 | | - mySingleAction = null; |
100 | | - } |
101 | | - } |
102 | | - |
103 | | - @Override |
104 | | - public GutterIconRenderer createGutterRenderer() { |
105 | | - return new LineMarkerGutterIconRenderer<>(this) { |
106 | | - @Override |
107 | | - public AnAction getClickAction() { |
108 | | - return mySingleAction; |
109 | | - } |
110 | | - |
111 | | - @Override |
112 | | - public boolean isNavigateAction() { |
113 | | - return true; |
114 | | - } |
115 | | - |
116 | | - @Override |
117 | | - public ActionGroup getPopupMenuActions() { |
118 | | - return myActionGroup; |
119 | | - } |
120 | | - }; |
121 | | - } |
122 | | - |
123 | | - @NotNull |
124 | | - @Override |
125 | | - public MarkupEditorFilter getEditorFilter() { |
126 | | - return MarkupEditorFilterFactory.createIsNotDiffFilter(); |
127 | | - } |
128 | | - |
129 | | - @Override |
130 | | - public boolean canMergeWith(@NotNull MergeableLineMarkerInfo<?> info) { |
131 | | - return info instanceof RunLineMarkerInfo && info.getIcon() == getIcon(); |
132 | | - } |
133 | | - |
134 | | - @Override |
135 | | - public Icon getCommonIcon(@NotNull List<? extends MergeableLineMarkerInfo<?>> infos) { |
136 | | - return getIcon(); |
137 | | - } |
138 | | - } |
139 | | - |
140 | | - public static class LineMarkerActionWrapper extends ActionGroup implements PriorityAction, ActionWithDelegate<AnAction> { |
141 | | - private static final Logger logger = Logger.getInstance(LineMarkerActionWrapper.class); |
142 | | - protected final SmartPsiElementPointer<PsiElement> myElement; |
143 | | - protected final AnAction myOrigin; |
144 | | - |
145 | | - public LineMarkerActionWrapper(PsiElement element, @NotNull AnAction origin) { |
146 | | - myElement = SmartPointerManager.createPointer(element); |
147 | | - myOrigin = origin; |
148 | | - copyFrom(origin); |
149 | | - } |
150 | | - |
151 | | - @Override |
152 | | - public AnAction @NotNull [] getChildren(@Nullable AnActionEvent e) { |
153 | | - if (myOrigin instanceof ExecutorAction) { |
154 | | - if (((ExecutorAction)myOrigin).getOrigin() instanceof ExecutorRegistryImpl.ExecutorGroupActionGroup) { |
155 | | - final AnAction[] children = ((ExecutorRegistryImpl.ExecutorGroupActionGroup)((ExecutorAction)myOrigin).getOrigin()).getChildren(null); |
156 | | - logger.assertTrue(ContainerUtil.all(Arrays.asList(children), o -> o instanceof RunContextAction)); |
157 | | - return ContainerUtil.mapNotNull(children, o -> { |
158 | | - PsiElement element = myElement.getElement(); |
159 | | - return element != null ? new LineMarkerActionWrapper(element, o) : null; |
160 | | - }).toArray(AnAction.EMPTY_ARRAY); |
161 | | - } |
162 | | - } |
163 | | - if (myOrigin instanceof ActionGroup) { |
164 | | - return ((ActionGroup)myOrigin).getChildren(e == null ? null : wrapEvent(e)); |
165 | | - } |
166 | | - return AnAction.EMPTY_ARRAY; |
167 | | - } |
168 | | - |
169 | | - @Override |
170 | | - public boolean canBePerformed(@NotNull DataContext context) { |
171 | | - return !(myOrigin instanceof ActionGroup) || ((ActionGroup)myOrigin).canBePerformed(wrapContext(context)); |
172 | | - } |
173 | | - |
174 | | - @Override |
175 | | - public boolean isDumbAware() { |
176 | | - return myOrigin.isDumbAware(); |
177 | | - } |
178 | | - |
179 | | - @Override |
180 | | - public boolean isPopup() { |
181 | | - return !(myOrigin instanceof ActionGroup) || ((ActionGroup)myOrigin).isPopup(); |
182 | | - } |
183 | | - |
184 | | - @Override |
185 | | - public boolean hideIfNoVisibleChildren() { |
186 | | - return myOrigin instanceof ActionGroup && ((ActionGroup)myOrigin).hideIfNoVisibleChildren(); |
187 | | - } |
188 | | - |
189 | | - @Override |
190 | | - public boolean disableIfNoVisibleChildren() { |
191 | | - return !(myOrigin instanceof ActionGroup) || ((ActionGroup)myOrigin).disableIfNoVisibleChildren(); |
192 | | - } |
193 | | - |
194 | | - @Override |
195 | | - public void update(@NotNull AnActionEvent e) { |
196 | | - AnActionEvent wrapped = wrapEvent(e); |
197 | | - myOrigin.update(wrapped); |
198 | | - Icon icon = wrapped.getPresentation().getIcon(); |
199 | | - if (icon != null) { |
200 | | - getTemplatePresentation().setIcon(icon); |
201 | | - } |
202 | | - } |
203 | | - |
204 | | - @NotNull |
205 | | - protected AnActionEvent wrapEvent(@NotNull AnActionEvent e) { |
206 | | - DataContext dataContext = wrapContext(e.getDataContext()); |
207 | | - return new AnActionEvent(e.getInputEvent(), dataContext, e.getPlace(), e.getPresentation(), e.getActionManager(), e.getModifiers()); |
208 | | - } |
209 | | - |
210 | | - @NotNull |
211 | | - protected DataContext wrapContext(DataContext dataContext) { |
212 | | - Pair<PsiElement, DataContext> pair = DataManager.getInstance() |
213 | | - .loadFromDataContext(dataContext, PubspecLineMarkerProvider.LOCATION_WRAPPER); |
214 | | - PsiElement element = myElement.getElement(); |
215 | | - if (pair == null || pair.first != element) { |
216 | | - pair = Pair.pair(element, dataContext); |
217 | | - DataManager.getInstance().saveInDataContext(dataContext, PubspecLineMarkerProvider.LOCATION_WRAPPER, pair); |
218 | | - } |
219 | | - return dataContext; |
220 | | - } |
221 | | - |
222 | | - @Override |
223 | | - public void actionPerformed(@NotNull AnActionEvent e) { |
224 | | - myOrigin.actionPerformed(wrapEvent(e)); |
225 | | - } |
226 | | - |
227 | | - @NotNull |
228 | | - @Override |
229 | | - public Priority getPriority() { |
230 | | - return Priority.TOP; |
231 | | - } |
232 | | - |
233 | | - @NotNull |
234 | | - @Override |
235 | | - public AnAction getDelegate() { |
236 | | - return myOrigin; |
237 | | - } |
238 | | - } |
239 | 57 | } |
0 commit comments