Skip to content
This repository was archived by the owner on Nov 14, 2023. It is now read-only.

Commit f0da479

Browse files
committed
Fix all warnings
1 parent 62b10c5 commit f0da479

File tree

11 files changed

+112
-172
lines changed

11 files changed

+112
-172
lines changed
Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import com.google.common.base.Strings;
2-
import java.io.UnsupportedEncodingException;
32
import java.net.URI;
43
import java.net.URLEncoder;
4+
import java.nio.charset.StandardCharsets;
55

66
public class CommitViewUriBuilder {
77

@@ -17,25 +17,20 @@ public URI build(String sourcegraphBase, String revisionNumber, RepoInfo repoInf
1717
// this is pretty hacky but to try to build the repo string we will just try to naively parse the git remote uri. Worst case scenario this 404s
1818
String remoteURL = repoInfo.remoteURL;
1919
if(remoteURL.startsWith("git")){
20-
remoteURL = repoInfo.remoteURL.replace(".git", "").replaceFirst(":", "/").replace("git@", "https://");;
20+
remoteURL = repoInfo.remoteURL.replace(".git", "").replaceFirst(":", "/").replace("git@", "https://");
2121
}
2222
URI remote = URI.create(remoteURL);
2323
String path = remote.getPath();
2424

25-
StringBuilder builder = new StringBuilder();
26-
try {
27-
builder.append(sourcegraphBase);
28-
builder.append(String.format("/%s%s", remote.getHost(), path));
29-
builder.append(String.format("/-/commit/%s", revisionNumber));
30-
builder.append(String.format("?editor=%s", URLEncoder.encode("JetBrains", "UTF-8")));
31-
builder.append(String.format("&version=%s", URLEncoder.encode(Util.VERSION, "UTF-8")));
32-
builder.append(String.format("&utm_product_name=%s", URLEncoder.encode(productName, "UTF-8")));
33-
builder.append(String.format("&utm_product_version=%s", URLEncoder.encode(productVersion, "UTF-8")));
34-
} catch (UnsupportedEncodingException e) {
35-
e.printStackTrace();
36-
}
25+
String url = sourcegraphBase +
26+
String.format("/%s%s", remote.getHost(), path) +
27+
String.format("/-/commit/%s", revisionNumber) +
28+
String.format("?editor=%s", URLEncoder.encode("JetBrains", StandardCharsets.UTF_8)) +
29+
String.format("&version=%s", URLEncoder.encode(Util.VERSION, StandardCharsets.UTF_8)) +
30+
String.format("&utm_product_name=%s", URLEncoder.encode(productName, StandardCharsets.UTF_8)) +
31+
String.format("&utm_product_version=%s", URLEncoder.encode(productVersion, StandardCharsets.UTF_8));
3732

38-
return URI.create(builder.toString());
33+
return URI.create(url);
3934
}
4035

4136
}

src/main/java/Config.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import com.intellij.openapi.components.PersistentStateComponent;
2-
import com.intellij.openapi.components.ServiceManager;
32
import com.intellij.openapi.components.State;
43
import com.intellij.openapi.components.Storage;
54
import com.intellij.openapi.project.Project;
@@ -42,7 +41,7 @@ public void loadState(@NotNull Config config) {
4241
this.remoteUrlReplacements = config.remoteUrlReplacements;
4342
}
4443

45-
@Nullable static Config getInstance(Project project) {
46-
return ServiceManager.getService(project, Config.class);
44+
static Config getInstance(Project project) {
45+
return project.getService(Config.class);
4746
}
4847
}

src/main/java/Copy.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ void handleFileUri(String uri) {
1717
// Display bubble
1818
Notification notification = new Notification("Sourcegraph", "Sourcegraph",
1919
"File URL copied to clipboard."+shortenURI, NotificationType.INFORMATION);
20+
// Editor.getProject
21+
// NotificationGroupManager.getInstance().getNotificationGroup("Sourcegraph")
22+
// .createNotification("File URL copied to clipboard."+shortenURI, NotificationType.INFORMATION)
23+
// .notify(this.);
2024
Notifications.Bus.notify(notification);
2125
}
22-
}
26+
}

src/main/java/FileAction.java

Lines changed: 20 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,22 @@
11
import com.intellij.openapi.actionSystem.AnAction;
22
import com.intellij.openapi.actionSystem.AnActionEvent;
33
import com.intellij.openapi.application.ApplicationInfo;
4-
import com.intellij.openapi.diagnostic.Logger;
5-
import com.intellij.openapi.editor.Document;
6-
import com.intellij.openapi.editor.Editor;
7-
import com.intellij.openapi.editor.LogicalPosition;
8-
import com.intellij.openapi.editor.SelectionModel;
4+
import com.intellij.openapi.editor.*;
95
import com.intellij.openapi.fileEditor.FileDocumentManager;
106
import com.intellij.openapi.fileEditor.FileEditorManager;
117
import com.intellij.openapi.project.Project;
128
import com.intellij.openapi.vfs.VirtualFile;
139

14-
import java.awt.*;
15-
import java.io.IOException;
16-
import java.io.UnsupportedEncodingException;
17-
import java.net.URI;
1810
import java.net.URLEncoder;
19-
import java.util.Arrays;
20-
import java.util.List;
11+
import java.nio.charset.StandardCharsets;
12+
import java.util.Objects;
2113

2214
public abstract class FileAction extends AnAction {
2315

2416
abstract void handleFileUri(String uri);
2517

2618
@Override
2719
public void actionPerformed(AnActionEvent e) {
28-
Logger logger = Logger.getInstance(this.getClass());
29-
3020
// Get project, editor, document, file, and position information.
3121
final Project project = e.getProject();
3222
if (project == null) {
@@ -37,9 +27,6 @@ public void actionPerformed(AnActionEvent e) {
3727
return;
3828
}
3929
Document currentDoc = editor.getDocument();
40-
if (currentDoc == null) {
41-
return;
42-
}
4330
VirtualFile currentFile = FileDocumentManager.getInstance().getFile(currentDoc);
4431
if (currentFile == null) {
4532
return;
@@ -48,7 +35,7 @@ public void actionPerformed(AnActionEvent e) {
4835

4936
// Get repo information.
5037
RepoInfo repoInfo = Util.repoInfo(currentFile.getPath(), project);
51-
if (repoInfo.remoteURL == "") {
38+
if (Objects.equals(repoInfo.remoteURL, "")) {
5239
return;
5340
}
5441

@@ -57,26 +44,22 @@ public void actionPerformed(AnActionEvent e) {
5744
String productVersion = ApplicationInfo.getInstance().getFullVersion();
5845
String uri;
5946

60-
try {
61-
LogicalPosition start = editor.visualToLogicalPosition(sel.getSelectionStartPosition());
62-
LogicalPosition end = editor.visualToLogicalPosition(sel.getSelectionEndPosition());
63-
uri = Util.sourcegraphURL(project)+"-/editor"
64-
+ "?remote_url=" + URLEncoder.encode(repoInfo.remoteURL, "UTF-8")
65-
+ "&branch=" + URLEncoder.encode(repoInfo.branch, "UTF-8")
66-
+ "&file=" + URLEncoder.encode(repoInfo.fileRel, "UTF-8")
67-
+ "&editor=" + URLEncoder.encode("JetBrains", "UTF-8")
68-
+ "&version=" + URLEncoder.encode(Util.VERSION, "UTF-8")
69-
+ "&start_row=" + URLEncoder.encode(Integer.toString(start.line), "UTF-8")
70-
+ "&start_col=" + URLEncoder.encode(Integer.toString(start.column), "UTF-8")
71-
+ "&end_row=" + URLEncoder.encode(Integer.toString(end.line), "UTF-8")
72-
+ "&end_col=" + URLEncoder.encode(Integer.toString(end.column), "UTF-8")
73-
+ "&utm_product_name=" + URLEncoder.encode(productName, "UTF-8")
74-
+ "&utm_product_version=" + URLEncoder.encode(productVersion, "UTF-8");
75-
} catch (UnsupportedEncodingException err) {
76-
logger.debug("failed to build URL");
77-
err.printStackTrace();
78-
return;
79-
}
47+
VisualPosition selectionStartPosition = sel.getSelectionStartPosition();
48+
VisualPosition selectionEndPosition = sel.getSelectionEndPosition();
49+
LogicalPosition start = selectionStartPosition != null ? editor.visualToLogicalPosition(selectionStartPosition) : null;
50+
LogicalPosition end = selectionEndPosition != null ? editor.visualToLogicalPosition(selectionEndPosition) : null;
51+
uri = Util.sourcegraphURL(project)+"-/editor"
52+
+ "?remote_url=" + URLEncoder.encode(repoInfo.remoteURL, StandardCharsets.UTF_8)
53+
+ "&branch=" + URLEncoder.encode(repoInfo.branch, StandardCharsets.UTF_8)
54+
+ "&file=" + URLEncoder.encode(repoInfo.fileRel, StandardCharsets.UTF_8)
55+
+ "&editor=" + URLEncoder.encode("JetBrains", StandardCharsets.UTF_8)
56+
+ "&version=" + URLEncoder.encode(Util.VERSION, StandardCharsets.UTF_8)
57+
+ (start != null ? ("&start_row=" + URLEncoder.encode(Integer.toString(start.line), StandardCharsets.UTF_8)
58+
+ "&start_col=" + URLEncoder.encode(Integer.toString(start.column), StandardCharsets.UTF_8)) : "")
59+
+ (end != null ? ("&end_row=" + URLEncoder.encode(Integer.toString(end.line), StandardCharsets.UTF_8)
60+
+ "&end_col=" + URLEncoder.encode(Integer.toString(end.column), StandardCharsets.UTF_8)) : "")
61+
+ "&utm_product_name=" + URLEncoder.encode(productName, StandardCharsets.UTF_8)
62+
+ "&utm_product_version=" + URLEncoder.encode(productVersion, StandardCharsets.UTF_8);
8063

8164
handleFileUri(uri);
8265
}

src/main/java/Open.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,5 @@ void handleFileUri(String uri) {
1616
logger.debug("failed to open browser");
1717
err.printStackTrace();
1818
}
19-
return;
2019
}
21-
}
20+
}

src/main/java/OpenRevisionAction.java

Lines changed: 49 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -7,75 +7,76 @@
77
import com.intellij.openapi.vcs.history.VcsFileRevision;
88
import com.intellij.vcs.log.VcsLog;
99
import com.intellij.vcs.log.VcsLogDataKeys;
10+
1011
import java.awt.Desktop;
1112
import java.io.IOException;
1213
import java.net.URI;
1314
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;
1618
import org.jetbrains.annotations.NotNull;
1719

1820
/**
1921
* Jetbrains IDE action to open a selected revision in Sourcegraph.
2022
*/
2123
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());
2325

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();
2729

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));
3339
}
3440

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();
3844

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+
}
4251

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));
4554
}
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-
}
5355

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."));
6061

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());
6566

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);
6869

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+
}
7476
}
75-
}
7677

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+
}
8182
}

src/main/java/Search.java

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,9 @@
1-
import com.intellij.openapi.actionSystem.AnAction;
21
import com.intellij.openapi.actionSystem.AnActionEvent;
3-
import com.intellij.openapi.editor.Document;
4-
import com.intellij.openapi.editor.Editor;
5-
import com.intellij.openapi.editor.SelectionModel;
6-
import com.intellij.openapi.fileEditor.FileDocumentManager;
7-
import com.intellij.openapi.fileEditor.FileEditorManager;
8-
import com.intellij.openapi.project.Project;
9-
import com.intellij.openapi.vfs.VirtualFile;
10-
import com.intellij.openapi.diagnostic.Logger;
11-
import com.intellij.openapi.application.ApplicationInfo;
12-
13-
import javax.annotation.Nullable;
14-
import java.io.*;
15-
import java.awt.Desktop;
16-
import java.net.URI;
17-
import java.net.URLEncoder;
2+
import org.jetbrains.annotations.NotNull;
183

194
public class Search extends SearchActionBase {
205
@Override
21-
public void actionPerformed(AnActionEvent e) {
6+
public void actionPerformed(@NotNull AnActionEvent e) {
227
super.actionPerformedMode(e, "search");
238
}
24-
}
9+
}

0 commit comments

Comments
 (0)