diff --git a/java/src/processing/mode/java/lsp/App.java b/java/src/processing/mode/java/lsp/App.java deleted file mode 100644 index 2ac9adbe7..000000000 --- a/java/src/processing/mode/java/lsp/App.java +++ /dev/null @@ -1,22 +0,0 @@ -package processing.mode.java.lsp; - -import org.eclipse.lsp4j.launch.LSPLauncher; - -public class App { - public static void main(String[] args) { - var input = System.in; - var output = System.out; - System.setOut(System.err); - - var server = new PdeLanguageServer(); - var launcher = - LSPLauncher.createServerLauncher( - server, - input, - output - ); - var client = launcher.getRemoteProxy(); - server.connect(client); - launcher.startListening(); - } -} diff --git a/java/src/processing/mode/java/lsp/PdeAdapter.java b/java/src/processing/mode/java/lsp/PdeAdapter.java index 912e6ff46..24a3a6109 100644 --- a/java/src/processing/mode/java/lsp/PdeAdapter.java +++ b/java/src/processing/mode/java/lsp/PdeAdapter.java @@ -52,7 +52,7 @@ class Offset { } } -class ProcessingAdapter { +class PdeAdapter { File rootPath; LanguageClient client; JavaMode javaMode; @@ -66,7 +66,7 @@ class ProcessingAdapter { Set prevDiagnosticReportUris = new HashSet(); - ProcessingAdapter(File rootPath, LanguageClient client) { + PdeAdapter(File rootPath, LanguageClient client) { this.rootPath = rootPath; this.client = client; this.javaMode = (JavaMode) ModeContribution @@ -128,7 +128,7 @@ class ProcessingAdapter { } Optional findCodeByUri(URI uri) { - return ProcessingAdapter.uriToPath(uri) + return PdeAdapter.uriToPath(uri) .flatMap(path -> Arrays.stream(sketch.getCode()) .filter(code -> code.getFile().equals(path)) .findFirst() @@ -143,13 +143,13 @@ class ProcessingAdapter { new Range( new Position( prob.getLineNumber(), - ProcessingAdapter + PdeAdapter .toLineCol(code.getProgram(), prob.getStartOffset()) .col - 1 ), new Position( prob.getLineNumber(), - ProcessingAdapter + PdeAdapter .toLineCol(code.getProgram(), prob.getStopOffset()) .col - 1 ) @@ -162,7 +162,7 @@ class ProcessingAdapter { : DiagnosticSeverity.Warning ); return new AbstractMap.SimpleEntry( - ProcessingAdapter.pathToUri(code.getFile()), + PdeAdapter.pathToUri(code.getFile()), dia ); }) @@ -317,7 +317,7 @@ class ProcessingAdapter { .map(SketchCode::getProgram) .map(code -> { String newCode = new AutoFormat().format(code); - Offset end = ProcessingAdapter.toLineCol(code, code.length()); + Offset end = PdeAdapter.toLineCol(code, code.length()); return new TextEdit( new Range( new Position(0, 0), diff --git a/java/src/processing/mode/java/lsp/PdeLanguageServer.java b/java/src/processing/mode/java/lsp/PdeLanguageServer.java index c2f69a7dc..e5be4d71f 100644 --- a/java/src/processing/mode/java/lsp/PdeLanguageServer.java +++ b/java/src/processing/mode/java/lsp/PdeLanguageServer.java @@ -1,27 +1,51 @@ package processing.mode.java.lsp; +import java.io.File; +import java.net.URI; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.HashMap; +import java.util.concurrent.CompletableFuture; +import org.eclipse.lsp4j.launch.LSPLauncher; import org.eclipse.lsp4j.services.LanguageServer; import org.eclipse.lsp4j.services.TextDocumentService; import org.eclipse.lsp4j.services.WorkspaceService; import org.eclipse.lsp4j.InitializeResult; import org.eclipse.lsp4j.InitializeParams; -import java.util.concurrent.CompletableFuture; import org.eclipse.lsp4j.ServerCapabilities; import org.eclipse.lsp4j.TextDocumentSyncKind; import org.eclipse.lsp4j.CompletionOptions; - -import java.io.File; - import org.eclipse.lsp4j.services.LanguageClientAware; import org.eclipse.lsp4j.services.LanguageClient; -import java.net.URI; -import java.util.Optional; -import java.util.HashMap; -import java.util.Arrays; class PdeLanguageServer implements LanguageServer, LanguageClientAware { + Map adapters = new HashMap<>(); + LanguageClient client = null; + PdeTextDocumentService textDocumentService = new PdeTextDocumentService(this); + PdeWorkspaceService workspaceService = new PdeWorkspaceService(this); + + + @Override + public void exit() { + System.out.println("exit"); + } + + + @Override + public TextDocumentService getTextDocumentService() { + return textDocumentService; + } + + + @Override + public WorkspaceService getWorkspaceService() { + return workspaceService; + } + + static Optional lowerExtension(File file) { String s = file.toString(); int dot = s.lastIndexOf('.'); @@ -29,64 +53,62 @@ class PdeLanguageServer implements LanguageServer, LanguageClientAware { else return Optional.of(s.substring(dot + 1).toLowerCase()); } - HashMap adapters = new HashMap<>(); - LanguageClient client = null; - PdeTextDocumentService textDocumentService = new PdeTextDocumentService(this); - PdeWorkspaceService workspaceService = new PdeWorkspaceService(this); - @Override - public void exit() { - System.out.println("exit"); - } - - @Override - public TextDocumentService getTextDocumentService() { - return textDocumentService; - } - - @Override - public WorkspaceService getWorkspaceService() { - return workspaceService; - } - - Optional getAdapter(URI uri) { - return ProcessingAdapter.uriToPath(uri).filter(file -> { + Optional getAdapter(URI uri) { + return PdeAdapter.uriToPath(uri).filter(file -> { String ext = lowerExtension(file).orElse(""); return ext.equals("pde") || ext.equals("java"); }).map(file -> { File rootDir = file.getParentFile(); - return adapters.computeIfAbsent(rootDir, _k -> new ProcessingAdapter(rootDir, client)); + return adapters.computeIfAbsent(rootDir, _k -> new PdeAdapter(rootDir, client)); }); } + @Override public CompletableFuture initialize(InitializeParams params) { - ProcessingAdapter.init(); + PdeAdapter.init(); System.out.println("initialize"); var capabilities = new ServerCapabilities(); capabilities.setTextDocumentSync(TextDocumentSyncKind.Full); var completionOptions = new CompletionOptions(); completionOptions.setResolveProvider(true); - completionOptions.setTriggerCharacters( - Arrays.asList( - "." - ) - ); + completionOptions.setTriggerCharacters(List.of(".")); capabilities.setCompletionProvider(completionOptions); capabilities.setDocumentFormattingProvider(true); var result = new InitializeResult(capabilities); return CompletableFuture.completedFuture(result); } + @Override public CompletableFuture shutdown() { System.out.println("shutdown"); return CompletableFuture.completedFuture(null); } + @Override public void connect(LanguageClient client) { this.client = client; } + + + static public void main(String[] args) { + var input = System.in; + var output = System.out; + System.setOut(System.err); + + var server = new PdeLanguageServer(); + var launcher = + LSPLauncher.createServerLauncher( + server, + input, + output + ); + var client = launcher.getRemoteProxy(); + server.connect(client); + launcher.startListening(); + } } diff --git a/java/src/processing/mode/java/lsp/PdeWorkspaceService.java b/java/src/processing/mode/java/lsp/PdeWorkspaceService.java index e06019208..95ddf379f 100644 --- a/java/src/processing/mode/java/lsp/PdeWorkspaceService.java +++ b/java/src/processing/mode/java/lsp/PdeWorkspaceService.java @@ -25,7 +25,7 @@ class PdeWorkspaceService implements WorkspaceService { pls.getAdapter(uri).ifPresent(adapter -> { switch (change.getType()) { case Created: - ProcessingAdapter.uriToPath(uri).ifPresent(path -> { + PdeAdapter.uriToPath(uri).ifPresent(path -> { adapter.sketch.loadNewTab(path.getName().toString(), "pde", true); adapter.notifySketchChanged(); }); diff --git a/todo.txt b/todo.txt index 22c8e9ccf..d4c55bd1f 100755 --- a/todo.txt +++ b/todo.txt @@ -18,6 +18,7 @@ _ https://github.com/processing/processing4/issues/524 X look into LSP code contribution X https://github.com/processing/processing4/pull/564 _ https://github.com/processing/processing4/issues/117 +_ App was merged into processing.mode.java.lsp.PdeLanguageServer _ "Show Sketch Folder" for libraries needs to treat the sketch as Untitled