|
7 | 7 | import com.intellij.openapi.vcs.history.VcsFileRevision; |
8 | 8 | import com.intellij.vcs.log.VcsLog; |
9 | 9 | import com.intellij.vcs.log.VcsLogDataKeys; |
| 10 | + |
10 | 11 | import java.awt.Desktop; |
11 | 12 | import java.io.IOException; |
12 | 13 | import java.net.URI; |
13 | 14 | import java.util.Optional; |
14 | | -import org.apache.log4j.LogManager; |
15 | | -import org.apache.log4j.Logger; |
| 15 | + |
| 16 | +import org.slf4j.LoggerFactory; |
| 17 | +import org.slf4j.Logger; |
16 | 18 | import org.jetbrains.annotations.NotNull; |
17 | 19 |
|
18 | 20 | /** |
19 | 21 | * Jetbrains IDE action to open a selected revision in Sourcegraph. |
20 | 22 | */ |
21 | 23 | public class OpenRevisionAction extends AnAction implements DumbAware { |
22 | | - private final Logger logger = LogManager.getLogger(this.getClass()); |
| 24 | + private final Logger logger = LoggerFactory.getLogger(this.getClass()); |
23 | 25 |
|
24 | | - private Optional<RevisionContext> getHistoryRevision(AnActionEvent e) { |
25 | | - VcsFileRevision revision = e.getDataContext().getData(VcsDataKeys.VCS_FILE_REVISION); |
26 | | - Project project = e.getProject(); |
| 26 | + private Optional<RevisionContext> getHistoryRevision(AnActionEvent e) { |
| 27 | + VcsFileRevision revision = e.getDataContext().getData(VcsDataKeys.VCS_FILE_REVISION); |
| 28 | + Project project = e.getProject(); |
27 | 29 |
|
28 | | - if (project == null) { |
29 | | - return Optional.empty(); |
30 | | - } |
31 | | - if (revision == null) { |
32 | | - return Optional.empty(); |
| 30 | + if (project == null) { |
| 31 | + return Optional.empty(); |
| 32 | + } |
| 33 | + if (revision == null) { |
| 34 | + return Optional.empty(); |
| 35 | + } |
| 36 | + |
| 37 | + String rev = revision.getRevisionNumber().toString(); |
| 38 | + return Optional.of(new RevisionContext(project, rev)); |
33 | 39 | } |
34 | 40 |
|
35 | | - String rev = revision.getRevisionNumber().toString(); |
36 | | - return Optional.of(new RevisionContext(project, rev)); |
37 | | - } |
| 41 | + private Optional<RevisionContext> getLogRevision(AnActionEvent e) { |
| 42 | + VcsLog log = e.getDataContext().getData(VcsLogDataKeys.VCS_LOG); |
| 43 | + Project project = e.getProject(); |
38 | 44 |
|
39 | | - private Optional<RevisionContext> getLogRevision(AnActionEvent e) { |
40 | | - VcsLog log = e.getDataContext().getData(VcsLogDataKeys.VCS_LOG); |
41 | | - Project project = e.getProject(); |
| 45 | + if (project == null) { |
| 46 | + return Optional.empty(); |
| 47 | + } |
| 48 | + if (log == null || log.getSelectedCommits().isEmpty()) { |
| 49 | + return Optional.empty(); |
| 50 | + } |
42 | 51 |
|
43 | | - if (project == null) { |
44 | | - return Optional.empty(); |
| 52 | + String rev = log.getSelectedCommits().get(0).getHash().asString(); |
| 53 | + return Optional.of(new RevisionContext(project, rev)); |
45 | 54 | } |
46 | | - if (log == null || log.getSelectedCommits().isEmpty()) { |
47 | | - return Optional.empty(); |
48 | | - } |
49 | | - |
50 | | - String rev = log.getSelectedCommits().get(0).getHash().asString(); |
51 | | - return Optional.of(new RevisionContext(project, rev)); |
52 | | - } |
53 | 55 |
|
54 | | - @Override |
55 | | - public void actionPerformed(@NotNull AnActionEvent e) { |
56 | | - // This action handles events for both log and history views, so attempt to load from any possible option. |
57 | | - RevisionContext context = getHistoryRevision(e).map(Optional::of) |
58 | | - .orElseGet(() -> getLogRevision(e)) |
59 | | - .orElseThrow(() -> new RuntimeException("Unable to determine revision from history or log.")); |
| 56 | + @Override |
| 57 | + public void actionPerformed(@NotNull AnActionEvent e) { |
| 58 | + // This action handles events for both log and history views, so attempt to load from any possible option. |
| 59 | + RevisionContext context = getHistoryRevision(e).or(() -> getLogRevision(e)) |
| 60 | + .orElseThrow(() -> new RuntimeException("Unable to determine revision from history or log.")); |
60 | 61 |
|
61 | | - try { |
62 | | - String productName = ApplicationInfo.getInstance().getVersionName(); |
63 | | - String productVersion = ApplicationInfo.getInstance().getFullVersion(); |
64 | | - RepoInfo repoInfo = Util.repoInfo(context.getProject().getProjectFilePath(), context.getProject()); |
| 62 | + try { |
| 63 | + String productName = ApplicationInfo.getInstance().getVersionName(); |
| 64 | + String productVersion = ApplicationInfo.getInstance().getFullVersion(); |
| 65 | + RepoInfo repoInfo = Util.repoInfo(context.getProject().getProjectFilePath(), context.getProject()); |
65 | 66 |
|
66 | | - CommitViewUriBuilder builder = new CommitViewUriBuilder(); |
67 | | - URI uri = builder.build(Util.sourcegraphURL(context.getProject()), context.getRevisionNumber(), repoInfo, productName, productVersion); |
| 67 | + CommitViewUriBuilder builder = new CommitViewUriBuilder(); |
| 68 | + URI uri = builder.build(Util.sourcegraphURL(context.getProject()), context.getRevisionNumber(), repoInfo, productName, productVersion); |
68 | 69 |
|
69 | | - // Open the URL in the browser. |
70 | | - Desktop.getDesktop().browse(uri); |
71 | | - } catch (IOException err) { |
72 | | - logger.debug("failed to open browser"); |
73 | | - err.printStackTrace(); |
| 70 | + // Open the URL in the browser. |
| 71 | + Desktop.getDesktop().browse(uri); |
| 72 | + } catch (IOException err) { |
| 73 | + logger.debug("failed to open browser"); |
| 74 | + err.printStackTrace(); |
| 75 | + } |
74 | 76 | } |
75 | | - } |
76 | 77 |
|
77 | | - @Override |
78 | | - public void update(@NotNull AnActionEvent e) { |
79 | | - e.getPresentation().setEnabledAndVisible(true); |
80 | | - } |
| 78 | + @Override |
| 79 | + public void update(@NotNull AnActionEvent e) { |
| 80 | + e.getPresentation().setEnabledAndVisible(true); |
| 81 | + } |
81 | 82 | } |
0 commit comments