Skip to content

Commit 8bc8fef

Browse files
committed
compatible with latest IDE, fix Github accounts issue, support deleting gists
1 parent 9f61c19 commit 8bc8fef

File tree

9 files changed

+255
-36
lines changed

9 files changed

+255
-36
lines changed

changeNotes.txt

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<ul>
2+
<li>Compatible with latest IDE</li>
3+
<li>Fixed multiple Github accounts issue</li>
4+
<li>Supported deleting gists</li>
5+
</ul>

gradle.properties

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
version = 1.0.0
1+
version = 1.0.1
22
ideaVersion = IC-2018.3
3-
customUtilBuild = 194.*
3+
customUtilBuild = 299.*
44
isEAP = false
55
pluginChannels = nightly
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.chuntung.plugin.gistsnippet.action;
2+
3+
import com.intellij.openapi.actionSystem.AnAction;
4+
import com.intellij.openapi.actionSystem.AnActionEvent;
5+
import org.jetbrains.annotations.NotNull;
6+
7+
8+
/**
9+
* Open add dialog to create gist.
10+
*/
11+
public class AddAction extends AnAction {
12+
@Override
13+
public void actionPerformed(@NotNull AnActionEvent e) {
14+
// TODO check selected file or path or text, pass them to dialog
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
package com.chuntung.plugin.gistsnippet.action;
2+
3+
import com.chuntung.plugin.gistsnippet.dto.ScopeEnum;
4+
import com.chuntung.plugin.gistsnippet.dto.SnippetNodeDTO;
5+
import com.chuntung.plugin.gistsnippet.dto.SnippetRootNode;
6+
import com.chuntung.plugin.gistsnippet.service.GistSnippetService;
7+
import com.chuntung.plugin.gistsnippet.service.GithubAccountHolder;
8+
import com.intellij.openapi.actionSystem.AnAction;
9+
import com.intellij.openapi.actionSystem.AnActionEvent;
10+
import com.intellij.openapi.progress.ProgressIndicator;
11+
import com.intellij.openapi.progress.Task;
12+
import com.intellij.openapi.project.DumbAware;
13+
import com.intellij.openapi.project.Project;
14+
import com.intellij.openapi.ui.MessageDialogBuilder;
15+
import com.intellij.ui.tree.StructureTreeModel;
16+
import org.jetbrains.annotations.NotNull;
17+
import org.jetbrains.plugins.github.authentication.accounts.GithubAccount;
18+
19+
import javax.swing.*;
20+
import javax.swing.tree.DefaultMutableTreeNode;
21+
import javax.swing.tree.TreePath;
22+
import java.util.ArrayList;
23+
import java.util.HashSet;
24+
import java.util.List;
25+
import java.util.Set;
26+
27+
/**
28+
* Delete selected own gists.
29+
*/
30+
public class DeleteAction extends AnAction implements DumbAware {
31+
private JTree tree;
32+
private StructureTreeModel structure;
33+
private SnippetRootNode root;
34+
private Project project;
35+
36+
public DeleteAction(JTree tree, StructureTreeModel structure, SnippetRootNode root, Project project) {
37+
super("Delete", "Delete selected gists", null);
38+
this.tree = tree;
39+
this.structure = structure;
40+
this.root = root;
41+
this.project = project;
42+
}
43+
44+
@Override
45+
public void actionPerformed(@NotNull AnActionEvent e) {
46+
List<String> gistIds = new ArrayList<>();
47+
Set<SnippetNodeDTO> gists = new HashSet<>();
48+
StringBuilder msgs = new StringBuilder();
49+
TreePath[] paths = tree.getSelectionPaths();
50+
for (TreePath path : paths) {
51+
Object userObject = ((DefaultMutableTreeNode) path.getLastPathComponent()).getUserObject();
52+
if (userObject instanceof SnippetNodeDTO) {
53+
SnippetNodeDTO node = (SnippetNodeDTO) userObject;
54+
if (node.getScope().equals(ScopeEnum.OWN)) {
55+
gists.add(node);
56+
gistIds.add(node.getId());
57+
msgs.append('\n').append(node.getDescription() == null ? node.getId() : node.getDescription());
58+
}
59+
}
60+
}
61+
62+
boolean yes = MessageDialogBuilder.yesNo("Delete", "Delete selected gists?" + msgs).isYes();
63+
if (yes) {
64+
new Task.Backgroundable(project, "Deleting gists...") {
65+
private boolean deleted = false;
66+
67+
@Override
68+
public void run(@NotNull ProgressIndicator indicator) {
69+
GithubAccount account = GithubAccountHolder.getInstance(project).getAccount();
70+
GistSnippetService service = GistSnippetService.getInstance();
71+
service.deleteGist(account, gistIds);
72+
deleted = true;
73+
}
74+
75+
@Override
76+
public void onSuccess() {
77+
// refresh tree
78+
if (deleted) {
79+
root.children().removeAll(gists);
80+
structure.invalidate();
81+
}
82+
}
83+
}.queue();
84+
}
85+
}
86+
87+
@Override
88+
public void update(@NotNull AnActionEvent event) {
89+
TreePath[] paths = tree.getSelectionPaths();
90+
if (paths != null) {
91+
for (TreePath path : paths) {
92+
Object userObject = ((DefaultMutableTreeNode) path.getLastPathComponent()).getUserObject();
93+
if (userObject instanceof SnippetNodeDTO) {
94+
SnippetNodeDTO node = (SnippetNodeDTO) userObject;
95+
if (node.getScope().equals(ScopeEnum.OWN)) {
96+
return;
97+
}
98+
}
99+
}
100+
}
101+
102+
event.getPresentation().setVisible(false);
103+
}
104+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.chuntung.plugin.gistsnippet.action;
2+
3+
import com.intellij.openapi.actionSystem.AnAction;
4+
import com.intellij.openapi.actionSystem.AnActionEvent;
5+
import org.jetbrains.annotations.NotNull;
6+
7+
/**
8+
* Edit the first gist in the selection.
9+
*/
10+
public class EditAction extends AnAction {
11+
@Override
12+
public void actionPerformed(@NotNull AnActionEvent e) {
13+
14+
}
15+
}

src/main/java/com/chuntung/plugin/gistsnippet/service/GistSnippetService.java

+32-9
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
package com.chuntung.plugin.gistsnippet.service;
22

33
import com.chuntung.plugin.gistsnippet.dto.api.GistDTO;
4-
import com.intellij.notification.*;
54
import com.intellij.openapi.components.ServiceManager;
65
import com.intellij.openapi.diagnostic.Logger;
7-
import com.intellij.openapi.project.Project;
86
import com.intellij.util.containers.ContainerUtil;
97
import org.jetbrains.plugins.github.api.GithubApiRequest;
108
import org.jetbrains.plugins.github.api.GithubApiRequestExecutor;
@@ -17,6 +15,9 @@
1715
import java.util.Map;
1816
import java.util.concurrent.atomic.AtomicReference;
1917

18+
/**
19+
* API Doc: https://developer.github.com/v3/gists/
20+
*/
2021
public class GistSnippetService {
2122
public static final Logger logger = Logger.getInstance(GistSnippetService.class);
2223

@@ -78,11 +79,12 @@ private List<String> putIntoCache(List<GistDTO> gistList) {
7879
return null;
7980
}
8081

81-
private List<GistDTO> decideResult(GithubAccount account, AtomicReference<List<GistDTO>> result, List<String> idList) {
82-
if (result.get() == null && idList != null) {
82+
private List<GistDTO> decideResult(GithubAccount account, AtomicReference<List<GistDTO>> result, List<String> cacheList) {
83+
// load from cache
84+
if (result.get() == null && cacheList != null) {
8385
// N + 1
84-
List<GistDTO> gistList = new ArrayList<>(idList.size());
85-
for (String gistId : idList) {
86+
List<GistDTO> gistList = new ArrayList<>(cacheList.size());
87+
for (String gistId : cacheList) {
8688
gistList.add(getGistDetail(account, gistId, false));
8789
}
8890
result.set(gistList);
@@ -100,7 +102,7 @@ public List<GistDTO> queryStarredGist(GithubAccount account, boolean forced) {
100102
}
101103

102104
AtomicReference<List<GistDTO>> result = new AtomicReference<>();
103-
List<String> list = scopeCache.computeIfAbsent(key, (k) -> {
105+
List<String> cacheList = scopeCache.computeIfAbsent(key, (k) -> {
104106
try {
105107
GithubApiRequest.Get.JsonList<GistDTO> request = new GithubApiRequest.Get.JsonList<>(STARRED_GISTS_URL, GistDTO.class, MIME_TYPE);
106108
GithubApiRequestExecutor executor = GithubApiRequestExecutorManager.getInstance().getExecutor(account);
@@ -113,7 +115,7 @@ public List<GistDTO> queryStarredGist(GithubAccount account, boolean forced) {
113115
}
114116
});
115117

116-
return decideResult(account, result, list);
118+
return decideResult(account, result, cacheList);
117119
}
118120

119121
// queryPublicGist
@@ -122,15 +124,21 @@ public List<GistDTO> getPublicGist(GithubAccount account) {
122124
return null;
123125
}
124126

127+
/**
128+
* @param account
129+
* @param gistId
130+
* @param forced true to load file content from remote server
131+
* @return
132+
*/
125133
public GistDTO getGistDetail(GithubAccount account, String gistId, boolean forced) {
126134
if (forced) {
127135
gistCache.computeIfPresent(gistId, (k, v) -> gistCache.remove(k));
128136
}
129137

130138
return gistCache.computeIfAbsent(gistId, (k) -> {
131139
String url = String.format(GIST_DETAIL_URL, gistId);
140+
GithubApiRequest.Get.Json<GistDTO> request = new GithubApiRequest.Get.Json(url, GistDTO.class, MIME_TYPE);
132141
try {
133-
GithubApiRequest.Get.Json<GistDTO> request = new GithubApiRequest.Get.Json(url, GistDTO.class, MIME_TYPE);
134142
GithubApiRequestExecutor executor = GithubApiRequestExecutorManager.getInstance().getExecutor(account);
135143
return executor.execute(request);
136144
} catch (IOException e) {
@@ -139,4 +147,19 @@ public GistDTO getGistDetail(GithubAccount account, String gistId, boolean force
139147
}
140148
});
141149
}
150+
151+
public void deleteGist(GithubAccount account, List<String> gistIds) {
152+
try {
153+
GithubApiRequestExecutor executor = GithubApiRequestExecutorManager.getInstance().getExecutor(account);
154+
for (String gistId : gistIds) {
155+
String url = String.format(GIST_DETAIL_URL, gistId);
156+
GithubApiRequest.Delete delete = new GithubApiRequest.Delete(url);
157+
executor.execute(delete);
158+
gistCache.remove(gistId);
159+
}
160+
} catch (IOException e) {
161+
logger.info("Failed to delete gist, error: " + e.getMessage());
162+
throw new GistException(e);
163+
}
164+
}
142165
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.chuntung.plugin.gistsnippet.service;
2+
3+
4+
import com.intellij.openapi.components.ServiceManager;
5+
import com.intellij.openapi.project.Project;
6+
import org.jetbrains.plugins.github.authentication.accounts.GithubAccount;
7+
8+
public class GithubAccountHolder {
9+
private GithubAccount account;
10+
11+
public static GithubAccountHolder getInstance(Project project) {
12+
return ServiceManager.getService(project, GithubAccountHolder.class);
13+
}
14+
15+
public GithubAccount getAccount() {
16+
return account;
17+
}
18+
19+
public void setAccount(GithubAccount account) {
20+
this.account = account;
21+
}
22+
}

0 commit comments

Comments
 (0)