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

Supporting source file launcher in VS Code extension #6262

Merged
merged 1 commit into from
Jul 31, 2023
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
9 changes: 9 additions & 0 deletions java/java.api.common/nbproject/project.xml
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,15 @@
<specification-version>1.61</specification-version>
</run-dependency>
</dependency>
<dependency>
<code-name-base>org.netbeans.modules.extexecution.base</code-name-base>
<build-prerequisite/>
<compile-dependency/>
<run-dependency>
<release-version>2</release-version>
<specification-version>1.27</specification-version>
</run-dependency>
</dependency>
<dependency>
<code-name-base>org.netbeans.modules.java.platform</code-name-base>
<build-prerequisite/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,22 @@
import java.util.List;
import java.util.concurrent.Callable;
import java.util.logging.Level;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.netbeans.api.extexecution.base.ExplicitProcessParameters;
import org.netbeans.api.java.platform.JavaPlatformManager;
import org.openide.filesystems.FileObject;
import org.openide.filesystems.FileUtil;
import org.openide.util.BaseUtilities;

final class LaunchProcess implements Callable<Process> {

private final FileObject fileObject;
private final JPDAStart start;
private final ExplicitProcessParameters params;

LaunchProcess(FileObject fileObject, JPDAStart start) {
LaunchProcess(FileObject fileObject, JPDAStart start, ExplicitProcessParameters params) {
this.fileObject = fileObject;
this.start = start;
this.params = params;
}

@Override
Expand Down Expand Up @@ -67,19 +69,28 @@ private Process setupProcess(String port) throws InterruptedException {
File javaFile = FileUtil.toFile(java);
String javaPath = javaFile.getAbsolutePath();

Object argumentsObject = fileObject.getAttribute(SingleSourceFileUtil.FILE_ARGUMENTS);
String arguments = argumentsObject != null ? ((String) argumentsObject).trim() : ""; // NOI18N

Object vmOptionsObj = fileObject.getAttribute(SingleSourceFileUtil.FILE_VM_OPTIONS);
String vmOptions = vmOptionsObj != null ? ((String) vmOptionsObj) : ""; // NOI18N
ExplicitProcessParameters paramsFromAttributes =
ExplicitProcessParameters.builder()
.args(readArgumentsFromAttribute(fileObject, SingleSourceFileUtil.FILE_ARGUMENTS))
.launcherArgs(readArgumentsFromAttribute(fileObject, SingleSourceFileUtil.FILE_VM_OPTIONS))
.workingDirectory(FileUtil.toFile(fileObject.getParent()))
.build();

ExplicitProcessParameters realParameters =
ExplicitProcessParameters.builder()
.combine(params)
.combine(paramsFromAttributes)
.build();
commandsList.add(javaPath);
if (!vmOptions.isEmpty()) {
commandsList.addAll(Arrays.asList(vmOptions.split(" "))); //NOI18N

if (realParameters.getLauncherArguments()!= null) {
commandsList.addAll(realParameters.getLauncherArguments());
}

if (port != null) {
commandsList.add("-agentlib:jdwp=transport=dt_socket,address=" + port + ",server=n"); //NOI18N
}

if (compile) {
commandsList.add("-cp");
commandsList.add(FileUtil.toFile(fileObject.getParent()).toString());
Expand All @@ -88,12 +99,13 @@ private Process setupProcess(String port) throws InterruptedException {
commandsList.add(fileObject.getNameExt());
}

if (!arguments.isEmpty()) {
commandsList.addAll(Arrays.asList(arguments.split(" "))); //NOI18N
if (realParameters.getArguments() != null) {
commandsList.addAll(realParameters.getArguments());
}

ProcessBuilder runFileProcessBuilder = new ProcessBuilder(commandsList);
runFileProcessBuilder.directory(FileUtil.toFile(fileObject.getParent())); //NOI18N
runFileProcessBuilder.environment().putAll(realParameters.getEnvironmentVariables());
runFileProcessBuilder.directory(realParameters.getWorkingDirectory());
runFileProcessBuilder.redirectErrorStream(true);
runFileProcessBuilder.redirectOutput();

Expand All @@ -105,4 +117,12 @@ private Process setupProcess(String port) throws InterruptedException {
}
return null;
}

private static List<String> readArgumentsFromAttribute(FileObject fileObject, String attributeName) {
Object argumentsObject = fileObject.getAttribute(attributeName);
if (!(argumentsObject instanceof String)) {
return null;
}
return Arrays.asList(BaseUtilities.parseParameters(((String) argumentsObject).trim()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.concurrent.Future;
import org.netbeans.api.extexecution.ExecutionDescriptor;
import org.netbeans.api.extexecution.ExecutionService;
import org.netbeans.api.extexecution.base.ExplicitProcessParameters;
import org.netbeans.spi.project.ActionProgress;
import org.netbeans.spi.project.ActionProvider;
import org.openide.filesystems.FileObject;
Expand Down Expand Up @@ -55,6 +56,7 @@ public void invokeAction(String command, Lookup context) throws IllegalArgumentE
if (fileObject == null)
return;

ExplicitProcessParameters params = ExplicitProcessParameters.buildExplicitParameters(context);
InputOutput io = IOProvider.getDefault().getIO(Bundle.CTL_SingleJavaFile(), false);
ActionProgress progress = ActionProgress.start(context);
ExecutionDescriptor descriptor = new ExecutionDescriptor().
Expand All @@ -65,7 +67,7 @@ public void invokeAction(String command, Lookup context) throws IllegalArgumentE
postExecution((exitCode) -> {
progress.finished(exitCode == 0);
});
LaunchProcess process = invokeActionHelper(io, command, fileObject);
LaunchProcess process = invokeActionHelper(io, command, fileObject, params);
ExecutionService exeService = ExecutionService.newService(
process,
descriptor, "Running Single Java File");
Expand All @@ -78,10 +80,10 @@ public boolean isActionEnabled(String command, Lookup context) throws IllegalArg
return fileObject != null;
}

final LaunchProcess invokeActionHelper (InputOutput io, String command, FileObject fo) {
final LaunchProcess invokeActionHelper (InputOutput io, String command, FileObject fo, ExplicitProcessParameters params) {
JPDAStart start = ActionProvider.COMMAND_DEBUG_SINGLE.equals(command) ?
new JPDAStart(io, fo) : null;
return new LaunchProcess(fo, start);
return new LaunchProcess(fo, start, params);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.io.InputStreamReader;
import java.util.logging.Logger;
import static junit.framework.TestCase.assertEquals;
import org.netbeans.api.extexecution.base.ExplicitProcessParameters;
import org.netbeans.junit.NbTestCase;
import org.openide.filesystems.FileObject;
import org.openide.filesystems.FileUtil;
Expand Down Expand Up @@ -55,7 +56,7 @@ public void testSingleJavaSourceRun() throws Exception {
FileObject javaFO = FileUtil.toFileObject(f1);
assertNotNull("FileObject found: " + f1, javaFO);
SingleJavaSourceRunActionProvider runActionProvider = new SingleJavaSourceRunActionProvider();
LaunchProcess process = runActionProvider.invokeActionHelper(null, "run.single", javaFO);
LaunchProcess process = runActionProvider.invokeActionHelper(null, "run.single", javaFO, ExplicitProcessParameters.empty());
BufferedReader reader
= new BufferedReader(new InputStreamReader(process.call().getInputStream()));
StringBuilder builder = new StringBuilder();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public void enablePreview(String newSourceLevel) throws Exception {
if (compilerArgs.contains(SOURCE_FLAG)) {
compilerArgs = m.replaceAll("--enable-preview " + SOURCE_FLAG + " " + newSourceLevel);
} else {
compilerArgs = (compilerArgs.isEmpty() ? "" : " ") + ENABLE_PREVIEW_FLAG + " " + SOURCE_FLAG + " " + newSourceLevel;
compilerArgs += (compilerArgs.isEmpty() ? "" : " ") + ENABLE_PREVIEW_FLAG + " " + SOURCE_FLAG + " " + newSourceLevel;
}
file.setAttribute(FILE_VM_OPTIONS, compilerArgs);
storeEditableProperties(ep, file);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,14 +194,14 @@ private static List<CodeAction> convertFixes(ErrorDescription err, Consumer<Exce
for (Fix f : fixes) {
if (f instanceof IncompleteClassPath.ResolveFix) {
// We know that this is a project problem and that the problems reported by ProjectProblemsProvider should be resolved
CodeAction action = new CodeAction(f.getText(), new Command(f.getText(), "nbls.java.project.resolveProjectProblems"));
CodeAction action = new CodeAction(f.getText(), new Command(f.getText(), "java.project.resolveProjectProblems"));
result.add(action);
}
if (f instanceof org.netbeans.modules.java.hints.errors.EnablePreview.ResolveFix) {
org.netbeans.modules.java.hints.errors.EnablePreview.ResolveFix rf = (org.netbeans.modules.java.hints.errors.EnablePreview.ResolveFix) f;
List<Object> params = rf.getNewSourceLevel() != null ? Arrays.asList(rf.getNewSourceLevel())
: Collections.emptyList();
CodeAction action = new CodeAction(f.getText(), new Command(f.getText(), "nbls.java.project.enable.preview", params));
CodeAction action = new CodeAction(f.getText(), new Command(f.getText(), "java.project.enable.preview", params));
result.add(action);
}
if (f instanceof ImportClass.FixImport) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ public class TextDocumentServiceImpl implements TextDocumentService, LanguageCli
Lookup.getDefault().lookup(RefreshDocument.class).register(this);
}

private void reRunDiagnostics() {
void reRunDiagnostics() {
for (String doc : server.getOpenedDocuments().getUris()) {
runDiagnosticTasks(doc, true);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@
import org.netbeans.modules.java.lsp.server.debugging.attach.AttachConfigurations;
import org.netbeans.modules.java.lsp.server.debugging.attach.AttachNativeConfigurations;
import org.netbeans.modules.java.lsp.server.project.LspProjectInfo;
import org.netbeans.modules.java.lsp.server.singlesourcefile.CompilerOptionsQueryImpl;
import org.netbeans.modules.java.source.ElementHandleAccessor;
import org.netbeans.modules.java.source.ui.JavaSymbolProvider;
import org.netbeans.modules.java.source.ui.JavaTypeProvider;
Expand Down Expand Up @@ -1216,6 +1217,18 @@ public void didChangeConfiguration(DidChangeConfigurationParams params) {
updateJavaImportPreferences(projects[0].getProjectDirectory(), ((JsonObject) params.getSettings()).getAsJsonObject("netbeans").getAsJsonObject("java").getAsJsonObject("imports"));
}
});
boolean modified = false;
String newVMOptions = "";
JsonObject javaPlus = ((JsonObject) params.getSettings()).getAsJsonObject("java+");
if (javaPlus != null) {
newVMOptions = javaPlus.getAsJsonObject("runConfig").getAsJsonPrimitive("vmOptions").getAsString();
}
for (CompilerOptionsQueryImpl query : Lookup.getDefault().lookupAll(CompilerOptionsQueryImpl.class)) {
modified |= query.setConfiguration(client, newVMOptions);
}
if (modified) {
((TextDocumentServiceImpl)server.getTextDocumentService()).reRunDiagnostics();
}
}

void updateJavaFormatPreferences(FileObject fo, JsonObject configuration) {
Expand Down
Loading