Skip to content

Commit 13df211

Browse files
authored
Merge pull request #359 from shuzijun/gradle
add note
2 parents daa0bf2 + e2fbcde commit 13df211

File tree

17 files changed

+309
-41
lines changed

17 files changed

+309
-41
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.shuzijun.leetcode.plugin.actions.editor;
2+
3+
import com.intellij.openapi.actionSystem.AnActionEvent;
4+
import com.shuzijun.leetcode.plugin.manager.NoteManager;
5+
import com.shuzijun.leetcode.plugin.model.Config;
6+
import com.shuzijun.leetcode.plugin.model.Question;
7+
8+
/**
9+
* @author shuzijun
10+
*/
11+
public class PullNoteAction extends AbstractEditAction {
12+
13+
@Override
14+
public void actionPerformed(AnActionEvent anActionEvent, Config config, Question question){
15+
NoteManager.pull(question,anActionEvent.getProject());
16+
NoteManager.show(question,anActionEvent.getProject());
17+
}
18+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.shuzijun.leetcode.plugin.actions.editor;
2+
3+
import com.intellij.openapi.actionSystem.AnActionEvent;
4+
import com.shuzijun.leetcode.plugin.manager.NoteManager;
5+
import com.shuzijun.leetcode.plugin.model.Config;
6+
import com.shuzijun.leetcode.plugin.model.Question;
7+
8+
/**
9+
* @author shuzijun
10+
*/
11+
public class PushNoteAction extends AbstractEditAction {
12+
13+
@Override
14+
public void actionPerformed(AnActionEvent anActionEvent, Config config, Question question){
15+
NoteManager.push(question,anActionEvent.getProject());
16+
}
17+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.shuzijun.leetcode.plugin.actions.editor;
2+
3+
import com.intellij.openapi.actionSystem.AnActionEvent;
4+
import com.shuzijun.leetcode.plugin.manager.NoteManager;
5+
import com.shuzijun.leetcode.plugin.model.Config;
6+
import com.shuzijun.leetcode.plugin.model.Question;
7+
8+
/**
9+
* @author shuzijun
10+
*/
11+
public class ShowNoteAction extends AbstractEditAction {
12+
13+
@Override
14+
public void actionPerformed(AnActionEvent anActionEvent, Config config, Question question){
15+
NoteManager.show(question,anActionEvent.getProject());
16+
}
17+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.shuzijun.leetcode.plugin.actions.tree;
2+
3+
import com.intellij.openapi.actionSystem.AnActionEvent;
4+
import com.shuzijun.leetcode.plugin.manager.NoteManager;
5+
import com.shuzijun.leetcode.plugin.model.Config;
6+
import com.shuzijun.leetcode.plugin.model.Question;
7+
8+
import javax.swing.*;
9+
10+
/**
11+
* @author shuzijun
12+
*/
13+
public class PullNoteAction extends AbstractTreeAction {
14+
15+
@Override
16+
public void actionPerformed(AnActionEvent anActionEvent, Config config, JTree tree, Question question) {
17+
NoteManager.pull(question,anActionEvent.getProject());
18+
NoteManager.show(question,anActionEvent.getProject());
19+
}
20+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.shuzijun.leetcode.plugin.actions.tree;
2+
3+
import com.intellij.openapi.actionSystem.AnActionEvent;
4+
import com.shuzijun.leetcode.plugin.manager.NoteManager;
5+
import com.shuzijun.leetcode.plugin.model.Config;
6+
import com.shuzijun.leetcode.plugin.model.Question;
7+
8+
import javax.swing.*;
9+
10+
/**
11+
* @author shuzijun
12+
*/
13+
public class PushNoteAction extends AbstractTreeAction {
14+
15+
@Override
16+
public void actionPerformed(AnActionEvent anActionEvent, Config config, JTree tree, Question question) {
17+
NoteManager.push(question,anActionEvent.getProject());
18+
}
19+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.shuzijun.leetcode.plugin.actions.tree;
2+
3+
import com.intellij.openapi.actionSystem.AnActionEvent;
4+
import com.shuzijun.leetcode.plugin.manager.NoteManager;
5+
import com.shuzijun.leetcode.plugin.model.Config;
6+
import com.shuzijun.leetcode.plugin.model.Question;
7+
8+
import javax.swing.*;
9+
10+
/**
11+
* @author shuzijun
12+
*/
13+
public class ShowNoteAction extends AbstractTreeAction {
14+
15+
@Override
16+
public void actionPerformed(AnActionEvent anActionEvent, Config config, JTree tree, Question question) {
17+
NoteManager.show(question,anActionEvent.getProject());
18+
}
19+
}
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
package com.shuzijun.leetcode.plugin.manager;
2+
3+
import com.alibaba.fastjson.JSONObject;
4+
import com.intellij.openapi.project.Project;
5+
import com.intellij.openapi.vfs.LocalFileSystem;
6+
import com.intellij.openapi.vfs.VfsUtil;
7+
import com.intellij.openapi.vfs.VirtualFile;
8+
import com.shuzijun.leetcode.plugin.model.Config;
9+
import com.shuzijun.leetcode.plugin.model.Constant;
10+
import com.shuzijun.leetcode.plugin.model.Question;
11+
import com.shuzijun.leetcode.plugin.setting.PersistentConfig;
12+
import com.shuzijun.leetcode.plugin.utils.*;
13+
14+
import java.io.File;
15+
16+
/**
17+
* @author shuzijun
18+
*/
19+
public class NoteManager {
20+
21+
22+
public static void show(Question question, Project project) {
23+
Config config = PersistentConfig.getInstance().getInitConfig();
24+
String filePath = PersistentConfig.getInstance().getTempFilePath() + Constant.DOC_NOTE + VelocityUtils.convert(config.getCustomFileName(), question) + ".md";
25+
File file = new File(filePath);
26+
if (file.exists()) {
27+
FileUtils.openFileEditor(file,project);
28+
}else {
29+
if(pull( question, project)){
30+
FileUtils.openFileEditor(file,project);
31+
}
32+
}
33+
}
34+
public static boolean pull(Question question, Project project) {
35+
try {
36+
if (!HttpRequestUtils.isLogin()) {
37+
MessageUtils.getInstance(project).showWarnMsg("info", PropertiesUtils.getInfo("login.not"));
38+
return false;
39+
}
40+
41+
Config config = PersistentConfig.getInstance().getInitConfig();
42+
String filePath = PersistentConfig.getInstance().getTempFilePath() + Constant.DOC_NOTE + VelocityUtils.convert(config.getCustomFileName(), question) + ".md";
43+
44+
HttpRequest httpRequest = HttpRequest.post(URLUtils.getLeetcodeGraphql(),"application/json");
45+
httpRequest.setBody("{\"operationName\":\"QuestionNote\",\"variables\":{\"titleSlug\":\""+question.getTitleSlug()+"\"},\"query\":\"query QuestionNote($titleSlug: String!) {\\n question(titleSlug: $titleSlug) {\\n questionId\\n note\\n __typename\\n }\\n}\\n\"}");
46+
httpRequest.addHeader("Accept", "application/json");
47+
HttpResponse response = HttpRequestUtils.executePost(httpRequest);
48+
if (response != null && response.getStatusCode() == 200) {
49+
50+
String body = response.getBody();
51+
52+
JSONObject jsonObject = JSONObject.parseObject(body).getJSONObject("data").getJSONObject("question");
53+
FileUtils.saveFile(filePath,jsonObject.getString("note"));
54+
return Boolean.TRUE;
55+
} else {
56+
MessageUtils.getInstance(project).showWarnMsg("error", PropertiesUtils.getInfo("request.failed"));
57+
}
58+
59+
} catch (Exception e) {
60+
LogUtils.LOG.error("pull node error", e);
61+
MessageUtils.getInstance(project).showWarnMsg("error", PropertiesUtils.getInfo("request.failed"));
62+
}
63+
return Boolean.FALSE;
64+
}
65+
66+
public static void push(Question question, Project project) {
67+
try {
68+
if (!HttpRequestUtils.isLogin()) {
69+
MessageUtils.getInstance(project).showWarnMsg("info", PropertiesUtils.getInfo("login.not"));
70+
return;
71+
}
72+
73+
Config config = PersistentConfig.getInstance().getInitConfig();
74+
String filePath = PersistentConfig.getInstance().getTempFilePath() + Constant.DOC_NOTE + VelocityUtils.convert(config.getCustomFileName(), question) + ".md";
75+
File file = new File(filePath);
76+
if (!file.exists()) {
77+
MessageUtils.getInstance(project).showWarnMsg("error", PropertiesUtils.getInfo("request.code"));
78+
return;
79+
}
80+
VirtualFile vf = LocalFileSystem.getInstance().refreshAndFindFileByIoFile(file);
81+
FileUtils.saveEditDocument(vf);
82+
String note = VfsUtil.loadText(vf);
83+
HttpRequest httpRequest = HttpRequest.post(URLUtils.getLeetcodeGraphql(),"application/json");
84+
JSONObject variables = new JSONObject();
85+
variables.put("titleSlug",question.getTitleSlug());
86+
variables.put("content",note);
87+
httpRequest.setBody("{\"operationName\":\"updateNote\",\"variables\":"+variables.toJSONString()+",\"query\":\"mutation updateNote($titleSlug: String!, $content: String!) {\\n updateNote(titleSlug: $titleSlug, content: $content) {\\n ok\\n error\\n question {\\n questionId\\n note\\n __typename\\n }\\n __typename\\n }\\n}\\n\"}");
88+
httpRequest.addHeader("Accept", "application/json");
89+
HttpResponse response = HttpRequestUtils.executePost(httpRequest);
90+
if (response != null && response.getStatusCode() == 200) {
91+
String body = response.getBody();
92+
JSONObject jsonObject = JSONObject.parseObject(body).getJSONObject("data").getJSONObject("updateNote");
93+
if(!jsonObject.getBoolean("ok")){
94+
MessageUtils.getInstance(project).showWarnMsg("error", jsonObject.getString("error"));
95+
}else {
96+
MessageUtils.getInstance(project).showInfoMsg("info", "success");
97+
}
98+
} else {
99+
MessageUtils.getInstance(project).showWarnMsg("error", PropertiesUtils.getInfo("request.failed"));
100+
}
101+
} catch (Exception e) {
102+
LogUtils.LOG.error("pull node error", e);
103+
MessageUtils.getInstance(project).showWarnMsg("error", PropertiesUtils.getInfo("request.failed"));
104+
}
105+
}
106+
107+
}

src/main/java/com/shuzijun/leetcode/plugin/model/Constant.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ public class Constant {
108108
public static final String DOC_SOLUTION = "doc"+ File.separator + "solution" + File.separator;
109109
public static final String DOC_CONTENT = "doc"+ File.separator + "content" + File.separator;
110110
public static final String DOC_SUBMISSION = "doc"+ File.separator + "submission" + File.separator;
111+
public static final String DOC_NOTE = "doc"+ File.separator + "note" + File.separator;
111112

112113

113114
}

src/main/java/com/shuzijun/leetcode/plugin/utils/FileUtils.java

Lines changed: 47 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import com.intellij.openapi.vfs.LocalFileSystem;
1313
import com.intellij.openapi.vfs.VfsUtil;
1414
import com.intellij.openapi.vfs.VirtualFile;
15+
import com.intellij.openapi.vfs.newvfs.RefreshQueue;
1516
import com.intellij.util.ExceptionUtil;
1617
import com.shuzijun.leetcode.plugin.model.CodeTypeEnum;
1718
import com.shuzijun.leetcode.plugin.model.Constant;
@@ -78,47 +79,7 @@ public static String getFileBody(File file) {
7879
public static String getClearCommentFileBody(File file, CodeTypeEnum codeTypeEnum) {
7980

8081
VirtualFile vf = LocalFileSystem.getInstance().refreshAndFindFileByIoFile(file);
81-
if (FileDocumentManager.getInstance().isFileModified(vf)) {
82-
try {
83-
ThrowableComputable<Boolean, Throwable> action = new ThrowableComputable<Boolean, Throwable>() {
84-
@Override
85-
public Boolean compute() throws Throwable {
86-
FileDocumentManager.getInstance().saveDocument(FileDocumentManager.getInstance().getDocument(vf));
87-
return true;
88-
}
89-
};
90-
91-
92-
Application application = ApplicationManager.getApplication();
93-
if (application.isDispatchThread()) {
94-
ApplicationManager.getApplication().runWriteAction(action);
95-
} else {
96-
if (application.isReadAccessAllowed()) {
97-
LogUtils.LOG.error("Must not start write action from within read action in the other thread - deadlock is coming");
98-
}
99-
100-
AtomicReference<Boolean> result = new AtomicReference();
101-
AtomicReference<Throwable> exception = new AtomicReference();
102-
TransactionGuard.getInstance().submitTransactionAndWait(() -> {
103-
try {
104-
result.set(WriteAction.compute(action));
105-
} catch (Throwable var4) {
106-
exception.set(var4);
107-
}
108-
109-
});
110-
Throwable t = (Throwable) exception.get();
111-
if (t != null) {
112-
t.addSuppressed(new RuntimeException());
113-
ExceptionUtil.rethrowUnchecked(t);
114-
throw t;
115-
}
116-
}
117-
} catch (Throwable ignore) {
118-
LogUtils.LOG.error("自动保存文件错误", ignore);
119-
}
120-
121-
}
82+
saveEditDocument(vf);
12283
StringBuffer code = new StringBuffer();
12384
try {
12485
String body = VfsUtil.loadText(vf);
@@ -261,6 +222,7 @@ public static void openFileEditor(File file, Project project) {
261222
VirtualFile vf = LocalFileSystem.getInstance().refreshAndFindFileByIoFile(file);
262223
OpenFileDescriptor descriptor = new OpenFileDescriptor(project, vf);
263224
FileEditorManager.getInstance(project).openTextEditor(descriptor, false);
225+
RefreshQueue.getInstance().refresh(false, false, null, vf);
264226
});
265227
}
266228

@@ -279,4 +241,48 @@ public static void openFileEditorAndSaveState(File file, Project project, Questi
279241
});
280242
}
281243

244+
public static void saveEditDocument(VirtualFile file){
245+
if (FileDocumentManager.getInstance().isFileModified(file)) {
246+
try {
247+
ThrowableComputable<Boolean, Throwable> action = new ThrowableComputable<Boolean, Throwable>() {
248+
@Override
249+
public Boolean compute() throws Throwable {
250+
FileDocumentManager.getInstance().saveDocument(FileDocumentManager.getInstance().getDocument(file));
251+
return true;
252+
}
253+
};
254+
255+
256+
Application application = ApplicationManager.getApplication();
257+
if (application.isDispatchThread()) {
258+
ApplicationManager.getApplication().runWriteAction(action);
259+
} else {
260+
if (application.isReadAccessAllowed()) {
261+
LogUtils.LOG.error("Must not start write action from within read action in the other thread - deadlock is coming");
262+
}
263+
264+
AtomicReference<Boolean> result = new AtomicReference();
265+
AtomicReference<Throwable> exception = new AtomicReference();
266+
TransactionGuard.getInstance().submitTransactionAndWait(() -> {
267+
try {
268+
result.set(WriteAction.compute(action));
269+
} catch (Throwable var4) {
270+
exception.set(var4);
271+
}
272+
273+
});
274+
Throwable t = (Throwable) exception.get();
275+
if (t != null) {
276+
t.addSuppressed(new RuntimeException());
277+
ExceptionUtil.rethrowUnchecked(t);
278+
throw t;
279+
}
280+
}
281+
} catch (Throwable ignore) {
282+
LogUtils.LOG.error("自动保存文件错误", ignore);
283+
}
284+
285+
}
286+
}
287+
282288
}

src/main/java/icons/LeetCodeEditorIcons.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,5 @@ public interface LeetCodeEditorIcons {
3636
Icon TIME = IconLoader.getIcon("/icons/time.png");
3737
Icon SORT_ASC = IconLoader.getIcon("/icons/sortAsc.png");
3838
Icon SORT_DESC = IconLoader.getIcon("/icons/sortDesc.png");
39+
Icon NOTE = IconLoader.getIcon("/icons/note.png");
3940
}

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -529,6 +529,18 @@
529529
<group id="leetcode.FavoriteGroup" class="com.shuzijun.leetcode.plugin.actions.tree.FavoriteActionGroup"
530530
popup="true" text="Favorite" description="Favorite" icon="LeetCodeEditorIcons.FAVORITE">
531531
</group>
532+
<group id="leetcode.note" popup="true" text="note" description="note" icon="LeetCodeEditorIcons.NOTE">
533+
<action id="leetcode.ShowNote"
534+
class="com.shuzijun.leetcode.plugin.actions.tree.ShowNoteAction"
535+
text="ShowNote" description="Show Note">
536+
</action>
537+
<action id="leetcode.PullNote" class="com.shuzijun.leetcode.plugin.actions.tree.PullNoteAction"
538+
text="PullNote" description="Pull Note">
539+
</action>
540+
<action id="leetcode.PushNote" class="com.shuzijun.leetcode.plugin.actions.tree.PushNoteAction"
541+
text="PushNote" description="Push Note">
542+
</action>
543+
</group>
532544
<separator/>
533545
<group id="leetcode.timer" popup="true" text="Timer" description="timer" icon="LeetCodeEditorIcons.TIME">
534546
<action id="leetcode.StartTimeAction"
@@ -601,6 +613,21 @@
601613
</action>
602614
<separator/>
603615
<reference id="leetcode.positionAction"/>
616+
<group id="leetcode.editor.note" popup="true" text="note" description="note"
617+
icon="LeetCodeEditorIcons.NOTE">
618+
<action id="leetcode.editor.ShowNote"
619+
class="com.shuzijun.leetcode.plugin.actions.editor.ShowNoteAction"
620+
text="ShowNote" description="Show Note">
621+
</action>
622+
<action id="leetcode.editor.PullNote"
623+
class="com.shuzijun.leetcode.plugin.actions.editor.PullNoteAction"
624+
text="PullNote" description="Pull Note">
625+
</action>
626+
<action id="leetcode.editor.PushNote"
627+
class="com.shuzijun.leetcode.plugin.actions.editor.PushNoteAction"
628+
text="PushNote" description="Push Note">
629+
</action>
630+
</group>
604631
<group id="leetcode.editor.timer" popup="true" text="Timer" description="timer(editor)" icon="LeetCodeEditorIcons.TIME">
605632
<action id="leetcode.editor.StartTimeAction"
606633
class="com.shuzijun.leetcode.plugin.actions.editor.StartTimeAction"

src/main/resources/icons/note.png

446 Bytes
Loading

0 commit comments

Comments
 (0)