Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use proper syntax to open file at line for Visual Studio Code, Intellij and Eclipse #22575

Merged
merged 5 commits into from
Jan 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ public void run() {
List<String> command = new ArrayList<>();
command.add(effectiveCommand);
command.addAll(args);
log.debugf("Opening IDE with %s", command);
new ProcessBuilder(command).redirectOutput(ProcessBuilder.Redirect.DISCARD)
.redirectError(ProcessBuilder.Redirect.DISCARD).start().waitFor(10,
TimeUnit.SECONDS);
Expand Down
38 changes: 23 additions & 15 deletions core/deployment/src/main/java/io/quarkus/deployment/ide/Ide.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,36 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

import org.jboss.logging.Logger;

import io.quarkus.dev.console.DevConsoleManager;

public enum Ide {

// see for cli syntax of idea https://www.jetbrains.com/help/idea/opening-files-from-command-line.html
IDEA("idea", null, "--help"),
ECLIPSE("eclipse", null, (String[]) null),
VSCODE("code", null, "--version"),
NETBEANS("netbeans", null, "--help");
IDEA("idea", List.of("--line", "{lineNumber}", "{fileName}"), List.of("--help")),
ECLIPSE("eclipse", List.of("--launcher.openFile", "{fileName}:{lineNumber}"), Collections.emptyList()),
VSCODE("code", List.of("--goto", "{fileName}:{lineNumber}"), List.of("--version")),
NETBEANS("netbeans", Collections.emptyList(), List.of("--help"));

private static final Logger log = Logger.getLogger(Ide.class);

private final String defaultCommand;
private final List<String> markerArgs;
private final String lineNumberArg;
private final List<String> lineNumberArgs;
private String machineSpecificCommand;

private String effectiveCommand;

Ide(String defaultCommand, String lineNumberArg, String... markerArgs) {
Ide(String defaultCommand, List<String> lineNumberArgs, List<String> markerArgs) {
this.defaultCommand = defaultCommand;
this.lineNumberArg = lineNumberArg;
this.markerArgs = markerArgs != null ? Arrays.asList(markerArgs) : Collections.emptyList();
this.lineNumberArgs = lineNumberArgs;
this.markerArgs = markerArgs;
}

/**
Expand All @@ -46,14 +50,15 @@ public String getEffectiveCommand() {

private String doGetEffectiveCommand() {
if (defaultCommand != null) {
if (markerArgs == null) {
if (markerArgs.isEmpty()) {
// in this case there is nothing much we can do but hope that the default command will work
return defaultCommand;
} else {
try {
List<String> command = new ArrayList<>(1 + markerArgs.size());
command.add(defaultCommand);
command.addAll(markerArgs);
log.debugf("Checking if IDE available with %s", command);
new ProcessBuilder(command).redirectError(ProcessBuilder.Redirect.DISCARD.file())
.redirectOutput(ProcessBuilder.Redirect.DISCARD.file()).start()
.waitFor(10,
Expand All @@ -75,13 +80,16 @@ public List<String> createFileOpeningArgs(String fileName, String line) {
return Collections.singletonList(fileName);
}

if (lineNumberArg == null) {
return Collections.singletonList(fileName + ":" + line);
// we don't know the syntax for opening a file at a given line
// so we just open the file
if (lineNumberArgs.isEmpty()) {
log.debug("No syntax provided for opening the file at a given line for this IDE so we will just open the file");
return Collections.singletonList(fileName);
}

String formattedLineArg = String.format(lineNumberArg, line);

return List.of(formattedLineArg, fileName);
return lineNumberArgs.stream()
.map(arg -> arg.replace("{fileName}", fileName).replace("{lineNumber}", line))
.collect(Collectors.toList());
}

public void setMachineSpecificCommand(String machineSpecificCommand) {
Expand Down