|
1 | 1 | package processing.mode.java.lsp; |
2 | 2 |
|
| 3 | +import java.io.File; |
| 4 | +import java.net.URI; |
| 5 | +import java.util.List; |
| 6 | +import java.util.Map; |
| 7 | +import java.util.Optional; |
| 8 | +import java.util.HashMap; |
| 9 | +import java.util.concurrent.CompletableFuture; |
3 | 10 |
|
| 11 | +import org.eclipse.lsp4j.launch.LSPLauncher; |
4 | 12 | import org.eclipse.lsp4j.services.LanguageServer; |
5 | 13 | import org.eclipse.lsp4j.services.TextDocumentService; |
6 | 14 | import org.eclipse.lsp4j.services.WorkspaceService; |
7 | 15 | import org.eclipse.lsp4j.InitializeResult; |
8 | 16 | import org.eclipse.lsp4j.InitializeParams; |
9 | | -import java.util.concurrent.CompletableFuture; |
10 | 17 | import org.eclipse.lsp4j.ServerCapabilities; |
11 | 18 | import org.eclipse.lsp4j.TextDocumentSyncKind; |
12 | 19 | import org.eclipse.lsp4j.CompletionOptions; |
13 | | - |
14 | | -import java.io.File; |
15 | | - |
16 | 20 | import org.eclipse.lsp4j.services.LanguageClientAware; |
17 | 21 | import org.eclipse.lsp4j.services.LanguageClient; |
18 | 22 |
|
19 | | -import java.net.URI; |
20 | | -import java.util.Optional; |
21 | | -import java.util.HashMap; |
22 | | -import java.util.Arrays; |
23 | 23 |
|
24 | 24 | class PdeLanguageServer implements LanguageServer, LanguageClientAware { |
25 | | - static Optional<String> lowerExtension(File file) { |
26 | | - String s = file.toString(); |
27 | | - int dot = s.lastIndexOf('.'); |
28 | | - if (dot == -1) return Optional.empty(); |
29 | | - else return Optional.of(s.substring(dot + 1).toLowerCase()); |
30 | | - } |
31 | | - |
32 | | - HashMap<File, ProcessingAdapter> adapters = new HashMap<>(); |
| 25 | + Map<File, PdeAdapter> adapters = new HashMap<>(); |
33 | 26 | LanguageClient client = null; |
34 | 27 | PdeTextDocumentService textDocumentService = new PdeTextDocumentService(this); |
35 | 28 | PdeWorkspaceService workspaceService = new PdeWorkspaceService(this); |
36 | 29 |
|
| 30 | + |
37 | 31 | @Override |
38 | 32 | public void exit() { |
39 | 33 | System.out.println("exit"); |
40 | 34 | } |
41 | 35 |
|
| 36 | + |
42 | 37 | @Override |
43 | 38 | public TextDocumentService getTextDocumentService() { |
44 | 39 | return textDocumentService; |
45 | 40 | } |
46 | 41 |
|
| 42 | + |
47 | 43 | @Override |
48 | 44 | public WorkspaceService getWorkspaceService() { |
49 | 45 | return workspaceService; |
50 | 46 | } |
51 | 47 |
|
52 | | - Optional<ProcessingAdapter> getAdapter(URI uri) { |
53 | | - return ProcessingAdapter.uriToPath(uri).filter(file -> { |
| 48 | + |
| 49 | + static Optional<String> lowerExtension(File file) { |
| 50 | + String s = file.toString(); |
| 51 | + int dot = s.lastIndexOf('.'); |
| 52 | + if (dot == -1) return Optional.empty(); |
| 53 | + else return Optional.of(s.substring(dot + 1).toLowerCase()); |
| 54 | + } |
| 55 | + |
| 56 | + |
| 57 | + Optional<PdeAdapter> getAdapter(URI uri) { |
| 58 | + return PdeAdapter.uriToPath(uri).filter(file -> { |
54 | 59 | String ext = lowerExtension(file).orElse(""); |
55 | 60 | return ext.equals("pde") || ext.equals("java"); |
56 | 61 | }).map(file -> { |
57 | 62 | File rootDir = file.getParentFile(); |
58 | | - return adapters.computeIfAbsent(rootDir, _k -> new ProcessingAdapter(rootDir, client)); |
| 63 | + return adapters.computeIfAbsent(rootDir, _k -> new PdeAdapter(rootDir, client)); |
59 | 64 | }); |
60 | 65 | } |
61 | 66 |
|
| 67 | + |
62 | 68 | @Override |
63 | 69 | public CompletableFuture<InitializeResult> initialize(InitializeParams params) { |
64 | | - ProcessingAdapter.init(); |
| 70 | + PdeAdapter.init(); |
65 | 71 | System.out.println("initialize"); |
66 | 72 | var capabilities = new ServerCapabilities(); |
67 | 73 | capabilities.setTextDocumentSync(TextDocumentSyncKind.Full); |
68 | 74 |
|
69 | 75 | var completionOptions = new CompletionOptions(); |
70 | 76 | completionOptions.setResolveProvider(true); |
71 | | - completionOptions.setTriggerCharacters( |
72 | | - Arrays.asList( |
73 | | - "." |
74 | | - ) |
75 | | - ); |
| 77 | + completionOptions.setTriggerCharacters(List.of(".")); |
76 | 78 | capabilities.setCompletionProvider(completionOptions); |
77 | 79 | capabilities.setDocumentFormattingProvider(true); |
78 | 80 | var result = new InitializeResult(capabilities); |
79 | 81 | return CompletableFuture.completedFuture(result); |
80 | 82 | } |
81 | 83 |
|
| 84 | + |
82 | 85 | @Override |
83 | 86 | public CompletableFuture<Object> shutdown() { |
84 | 87 | System.out.println("shutdown"); |
85 | 88 | return CompletableFuture.completedFuture(null); |
86 | 89 | } |
87 | 90 |
|
| 91 | + |
88 | 92 | @Override |
89 | 93 | public void connect(LanguageClient client) { |
90 | 94 | this.client = client; |
91 | 95 | } |
| 96 | + |
| 97 | + |
| 98 | + static public void main(String[] args) { |
| 99 | + var input = System.in; |
| 100 | + var output = System.out; |
| 101 | + System.setOut(System.err); |
| 102 | + |
| 103 | + var server = new PdeLanguageServer(); |
| 104 | + var launcher = |
| 105 | + LSPLauncher.createServerLauncher( |
| 106 | + server, |
| 107 | + input, |
| 108 | + output |
| 109 | + ); |
| 110 | + var client = launcher.getRemoteProxy(); |
| 111 | + server.connect(client); |
| 112 | + launcher.startListening(); |
| 113 | + } |
92 | 114 | } |
0 commit comments