Skip to content

Commit 72e661e

Browse files
committed
Add positioning function
1 parent ed32e36 commit 72e661e

File tree

11 files changed

+109
-6
lines changed

11 files changed

+109
-6
lines changed

src/main/java/com/shuzijun/leetcode/plugin/actions/editor/AbstractEditAction.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
package com.shuzijun.leetcode.plugin.actions.editor;
22

33
import com.intellij.openapi.actionSystem.AnActionEvent;
4-
import com.intellij.openapi.actionSystem.PlatformDataKeys;
4+
import com.intellij.openapi.fileEditor.FileEditorManager;
55
import com.intellij.openapi.vfs.VirtualFile;
6+
import com.intellij.util.ArrayUtil;
67
import com.shuzijun.leetcode.plugin.actions.AbstractAction;
78
import com.shuzijun.leetcode.plugin.manager.ViewManager;
89
import com.shuzijun.leetcode.plugin.model.Config;
@@ -19,7 +20,10 @@ abstract class AbstractEditAction extends AbstractAction {
1920

2021
@Override
2122
public void actionPerformed(AnActionEvent anActionEvent, Config config) {
22-
VirtualFile vf = anActionEvent.getData(PlatformDataKeys.VIRTUAL_FILE);
23+
VirtualFile vf = ArrayUtil.getFirstElement(FileEditorManager.getInstance(anActionEvent.getProject()).getSelectedFiles());
24+
if(vf == null){
25+
return;
26+
}
2327
LeetcodeEditor leetcodeEditor = ProjectConfig.getInstance(anActionEvent.getProject()).getEditor(vf.getPath());
2428
if (leetcodeEditor == null) {
2529
return;
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.shuzijun.leetcode.plugin.actions.editor;
2+
3+
4+
import com.intellij.openapi.actionSystem.AnActionEvent;
5+
import com.intellij.openapi.application.ApplicationManager;
6+
import com.intellij.openapi.fileEditor.FileEditorManager;
7+
import com.intellij.openapi.vfs.VirtualFile;
8+
import com.intellij.ui.components.JBScrollPane;
9+
import com.intellij.util.ArrayUtil;
10+
import com.shuzijun.leetcode.plugin.manager.ViewManager;
11+
import com.shuzijun.leetcode.plugin.model.Config;
12+
import com.shuzijun.leetcode.plugin.model.LeetcodeEditor;
13+
import com.shuzijun.leetcode.plugin.model.Question;
14+
import com.shuzijun.leetcode.plugin.setting.ProjectConfig;
15+
import com.shuzijun.leetcode.plugin.utils.DataKeys;
16+
import com.shuzijun.leetcode.plugin.window.WindowFactory;
17+
import org.jetbrains.annotations.NotNull;
18+
19+
import javax.swing.*;
20+
21+
/**
22+
* @author shuzijun
23+
*/
24+
public class PositionAction extends AbstractEditAction {
25+
26+
@Override
27+
public void update(@NotNull AnActionEvent e) {
28+
VirtualFile vf = ArrayUtil.getFirstElement(FileEditorManager.getInstance(e.getProject()).getSelectedFiles());
29+
if (vf == null) {
30+
e.getPresentation().setEnabled(false);
31+
return;
32+
}
33+
LeetcodeEditor leetcodeEditor = ProjectConfig.getInstance(e.getProject()).getEditor(vf.getPath());
34+
if (leetcodeEditor == null) {
35+
e.getPresentation().setEnabled(false);
36+
return;
37+
}
38+
e.getPresentation().setEnabled(true);
39+
}
40+
41+
@Override
42+
public void actionPerformed(AnActionEvent anActionEvent, Config config, Question question) {
43+
JTree tree = WindowFactory.getDataContext(anActionEvent.getProject()).getData(DataKeys.LEETCODE_PROJECTS_TREE);
44+
if (tree == null) {
45+
return;
46+
}
47+
JBScrollPane scrollPane = WindowFactory.getDataContext(anActionEvent.getProject()).getData(DataKeys.LEETCODE_PROJECTS_SCROLL);
48+
ApplicationManager.getApplication().invokeAndWait(() -> {
49+
ViewManager.position(tree, scrollPane, question);
50+
});
51+
}
52+
}

src/main/java/com/shuzijun/leetcode/plugin/manager/ViewManager.java

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ public static void pick(JTree tree, JBScrollPane scrollPane) {
194194
return;
195195
}
196196

197-
public static Question getTreeQuestion(JTree tree,Project project) {
197+
public static Question getTreeQuestion(JTree tree, Project project) {
198198
Question question = null;
199199
if (tree != null) {
200200
DefaultMutableTreeNode note = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
@@ -216,8 +216,8 @@ public static Question getTreeQuestion(JTree tree,Project project) {
216216
return question;
217217
}
218218

219-
public static Question getQuestionById(String id,Project project) {
220-
if(question.isEmpty()){
219+
public static Question getQuestionById(String id, Project project) {
220+
if (question.isEmpty()) {
221221
MessageUtils.getInstance(project).showInfoMsg("info", PropertiesUtils.getInfo("tree.load"));
222222
ApplicationManager.getApplication().invokeAndWait(new Runnable() {
223223
@Override
@@ -248,4 +248,32 @@ private static void addChild(DefaultMutableTreeNode rootNode, List<Tag> Lists, M
248248
}
249249
}
250250

251+
public static void position(JTree tree, JBScrollPane scrollPane, Question question) {
252+
253+
DefaultTreeModel treeMode = (DefaultTreeModel) tree.getModel();
254+
DefaultMutableTreeNode root = (DefaultMutableTreeNode) treeMode.getRoot();
255+
if (root.isLeaf()) {
256+
return;
257+
}
258+
DefaultMutableTreeNode node = (DefaultMutableTreeNode) root.getChildAt(0);
259+
if (node.isLeaf()) {
260+
return;
261+
}
262+
263+
for (int i = 0, j = node.getChildCount(); i < j; i++) {
264+
DefaultMutableTreeNode childNode = (DefaultMutableTreeNode) node.getChildAt(i);
265+
Question nodeData = (Question) childNode.getUserObject();
266+
if(nodeData.getQuestionId().equals(question.getQuestionId())){
267+
TreePath toShowPath = new TreePath(childNode.getPath());
268+
tree.setSelectionPath(toShowPath);
269+
Rectangle bounds = tree.getPathBounds(toShowPath);
270+
Point point = new Point(0, (int) bounds.getY());
271+
JViewport viewport = scrollPane.getViewport();
272+
viewport.setViewPosition(point);
273+
return;
274+
}
275+
276+
}
277+
}
278+
251279
}

src/main/java/icons/LeetCodeEditorIcons.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public interface LeetCodeEditorIcons {
2525
Icon LOGIN = IconLoader.getIcon("/icons/login.png");
2626
Icon LOGOUT = IconLoader.getIcon("/icons/logout.png");
2727
Icon POPUP = IconLoader.getIcon("/icons/popup.png");
28+
Icon POSITION = IconLoader.getIcon("/icons/position.png");
2829
Icon PROGRESS = IconLoader.getIcon("/icons/progress.png");
2930
Icon QUESTION = IconLoader.getIcon("/icons/question.png");
3031
Icon RANDOM = IconLoader.getIcon("/icons/random.png");

src/main/resources/META-INF/plugin.xml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@
347347
<!-- <postStartupActivity implementation = "com.shuzijun.leetcode.plugin.listener.RegisterPluginInstallerStateListener"></postStartupActivity>-->
348348
<errorHandler implementation="com.shuzijun.leetcode.plugin.listener.ErrorReportHandler"/>
349349
<toolWindow id="leetcode" secondary="true" icon="LeetCodeEditorIcons.LEETCODE_TOOL_WINDOW" anchor="right"
350-
factoryClass="com.shuzijun.leetcode.plugin.window.WindowFactory"/>
350+
factoryClass="com.shuzijun.leetcode.plugin.window.WindowFactory" />
351351
<applicationService serviceInterface="com.shuzijun.leetcode.plugin.setting.PersistentConfig"
352352
serviceImplementation="com.shuzijun.leetcode.plugin.setting.PersistentConfig"/>
353353
<projectService serviceInterface="com.shuzijun.leetcode.plugin.setting.ProjectConfig"
@@ -424,6 +424,8 @@
424424
<action id="leetcode.PickAction" class="com.shuzijun.leetcode.plugin.actions.toolbar.PickAction"
425425
text="Pick one" description="Pick one" icon="LeetCodeEditorIcons.RANDOM">
426426
</action>
427+
<action id="leetcode.positionAction" class="com.shuzijun.leetcode.plugin.actions.editor.PositionAction"
428+
text="position" description="position" icon="LeetCodeEditorIcons.POSITION"/>
427429

428430
<group id="leetcode.NavigatorActionsToolbar">
429431
<reference id="leetcode.LoginAction"/>
@@ -432,6 +434,7 @@
432434
<reference id="leetcode.RefreshAction"/>
433435
<reference id="leetcode.PickAction"/>
434436
<reference id="leetcode.FindAction"/>
437+
<reference id="leetcode.positionAction"/>
435438
<reference id="leetcode.CollapseAction"/>
436439
<reference id="leetcode.ProgressAction"/>
437440
<separator/>
@@ -526,6 +529,7 @@
526529
text="open in web" description="open in web(editor)" icon="LeetCodeEditorIcons.POPUP">
527530
</action>
528531
<separator/>
532+
<reference id="leetcode.positionAction"/>
529533
<group id="leetcode.editor.timer" popup="true" text="Timer" description="timer(editor)" icon="LeetCodeEditorIcons.TIME">
530534
<action id="leetcode.editor.StartTimeAction"
531535
class="com.shuzijun.leetcode.plugin.actions.editor.StartTimeAction"

src/main/resources/icons/position.png

321 Bytes
Loading

src/main/resources/icons/position.svg

Lines changed: 7 additions & 0 deletions
Loading
869 Bytes
Loading
866 Bytes
Loading
525 Bytes
Loading

0 commit comments

Comments
 (0)