3030package org .scijava .code .lsp ;
3131
3232import java .io .File ;
33+ import java .util .ArrayList ;
3334import java .util .Collections ;
3435import java .util .List ;
3536import java .util .concurrent .CompletableFuture ;
3637import java .util .concurrent .TimeUnit ;
3738import java .util .concurrent .atomic .AtomicInteger ;
3839import java .util .function .Consumer ;
40+ import java .util .function .Function ;
3941import java .util .function .Supplier ;
4042
4143import org .eclipse .lsp4j .CompletionItem ;
5052import org .eclipse .lsp4j .InitializeParams ;
5153import org .eclipse .lsp4j .InitializeResult ;
5254import org .eclipse .lsp4j .InitializedParams ;
55+ import org .eclipse .lsp4j .InsertReplaceEdit ;
5356import org .eclipse .lsp4j .MessageActionItem ;
5457import org .eclipse .lsp4j .MessageParams ;
58+ import org .eclipse .lsp4j .Position ;
5559import org .eclipse .lsp4j .PublishDiagnosticsParams ;
60+ import org .eclipse .lsp4j .Range ;
5661import org .eclipse .lsp4j .ShowMessageRequestParams ;
5762import org .eclipse .lsp4j .SignatureHelp ;
5863import org .eclipse .lsp4j .SignatureHelpParams ;
5964import org .eclipse .lsp4j .TextDocumentContentChangeEvent ;
6065import org .eclipse .lsp4j .TextDocumentIdentifier ;
6166import org .eclipse .lsp4j .TextDocumentItem ;
67+ import org .eclipse .lsp4j .TextEdit ;
6268import org .eclipse .lsp4j .VersionedTextDocumentIdentifier ;
69+ import org .eclipse .lsp4j .jsonrpc .messages .Either ;
6370import org .eclipse .lsp4j .services .LanguageClient ;
6471import org .eclipse .lsp4j .services .LanguageClientAware ;
6572import org .eclipse .lsp4j .services .LanguageServer ;
73+ import org .scijava .code .lsp .script .ScriptDocument ;
6674import org .scijava .log .Logger ;
6775import org .scijava .script .ScriptLanguage ;
6876
8088 * {@link Positions}). Answers are lsp4j's own types.
8189 * </p>
8290 * <p>
91+ * The server need not know SciJava scripts: given a transformation (e.g. by
92+ * the {@link LanguageServerPlugin#dialect() dialect} of the plugin offering
93+ * the server), the session shows the server each script as plain code of its
94+ * language (see {@link ScriptDocument}), and maps positions both ways.
95+ * Whatever the server says about the lines the transformation added is left
96+ * out.
97+ * </p>
98+ * <p>
8399 * A session owns its server: it is the server's client, initializes it, and
84100 * shuts it down when closed. Get one from the
85101 * {@link LanguageServerService#session service}, which makes it a server of
@@ -97,13 +113,16 @@ public class ScriptSession implements AutoCloseable {
97113
98114 private final LanguageServer server ;
99115 private final ScriptLanguage language ;
116+ private final Function <String , ScriptDocument > transform ;
100117 private final String untitled ;
101118 private volatile Supplier <File > file = () -> null ;
102119 private volatile Logger log ;
103120 private volatile Consumer <List <Diagnostic >> diagnosticsListener ;
104121
105122 private String uri ;
106123 private String text ;
124+ /** The text, as the server sees it. */
125+ private ScriptDocument doc ;
107126 private int version ;
108127 private List <Diagnostic > diagnostics = Collections .emptyList ();
109128 private CompletableFuture <List <Diagnostic >> nextDiagnostics =
@@ -112,12 +131,16 @@ public class ScriptSession implements AutoCloseable {
112131 /**
113132 * @param server The server, which the session owns.
114133 * @param language The script's language, or null if unknown.
134+ * @param transform Makes scripts what the server sees; or null, to show
135+ * them as they are.
115136 */
116137 private ScriptSession (final LanguageServer server ,
117- final ScriptLanguage language )
138+ final ScriptLanguage language ,
139+ final Function <String , ScriptDocument > transform )
118140 {
119141 this .server = server ;
120142 this .language = language ;
143+ this .transform = transform == null ? ScriptDocument ::of : transform ;
121144 final List <String > extensions = language == null ? Collections
122145 .emptyList () : language .getExtensions ();
123146 untitled = "untitled:/script-" + UNTITLED .incrementAndGet () + (extensions
@@ -136,7 +159,23 @@ private ScriptSession(final LanguageServer server,
136159 public static ScriptSession owning (final LanguageServer server ,
137160 final ScriptLanguage language )
138161 {
139- return new ScriptSession (server , language ).connect ();
162+ return owning (server , language , null );
163+ }
164+
165+ /**
166+ * As {@link #owning(LanguageServer, ScriptLanguage)}, showing the server
167+ * each script transformed (e.g. into plain code of its language).
168+ *
169+ * @param server The server, which the session owns.
170+ * @param language The script's language, or null if unknown.
171+ * @param transform Makes scripts what the server sees; or null, to show
172+ * them as they are.
173+ */
174+ public static ScriptSession owning (final LanguageServer server ,
175+ final ScriptLanguage language ,
176+ final Function <String , ScriptDocument > transform )
177+ {
178+ return new ScriptSession (server , language , transform ).connect ();
140179 }
141180
142181 /**
@@ -197,17 +236,19 @@ public synchronized ScriptSession update(final String newText) {
197236 uri = current ;
198237 version = 1 ;
199238 text = newText ;
239+ doc = transform .apply (text );
200240 newVersion ();
201241 server .getTextDocumentService ().didOpen (new DidOpenTextDocumentParams (
202- new TextDocumentItem (uri , languageId (), version , text )));
242+ new TextDocumentItem (uri , languageId (), version , doc . text () )));
203243 }
204244 else if (!newText .equals (text )) {
205245 text = newText ;
246+ doc = transform .apply (text );
206247 version ++;
207248 newVersion ();
208249 server .getTextDocumentService ().didChange (new DidChangeTextDocumentParams (
209250 new VersionedTextDocumentIdentifier (uri , version ), Collections
210- .singletonList (new TextDocumentContentChangeEvent (text ))));
251+ .singletonList (new TextDocumentContentChangeEvent (doc . text () ))));
211252 }
212253 return this ;
213254 }
@@ -219,10 +260,11 @@ public synchronized CompletableFuture<CompletionList> completion(
219260 final String script , final int offset )
220261 {
221262 update (script );
263+ final ScriptDocument d = doc ;
222264 return server .getTextDocumentService ().completion (new CompletionParams (
223- identifier (), Positions . position (script , offset ))).thenApply (
224- answer -> answer == null ? new CompletionList () : answer .isRight ()
225- ? answer . getRight () : new CompletionList (false , answer .getLeft ()));
265+ identifier (), position (offset ))).thenApply (answer -> answer == null
266+ ? new CompletionList () : back ( d , answer .isRight () ? answer . getRight ()
267+ : new CompletionList (false , answer .getLeft () )));
226268 }
227269
228270 /** A completion, with what it takes long to know (e.g. documentation). */
@@ -236,17 +278,23 @@ public synchronized CompletableFuture<SignatureHelp> signatureHelp(
236278 {
237279 update (script );
238280 return server .getTextDocumentService ().signatureHelp (
239- new SignatureHelpParams (identifier (), Positions .position (script ,
240- offset )));
281+ new SignatureHelpParams (identifier (), position (offset )));
241282 }
242283
243284 /** What is known about the code at an offset (null if nothing). */
244285 public synchronized CompletableFuture <Hover > hover (final String script ,
245286 final int offset )
246287 {
247288 update (script );
289+ final ScriptDocument d = doc ;
248290 return server .getTextDocumentService ().hover (new HoverParams (identifier (),
249- Positions .position (script , offset )));
291+ position (offset ))).thenApply (h -> {
292+ if (h == null || h .getRange () == null ) return h ;
293+ final Hover out = new Hover ();
294+ out .setContents (h .getContents ());
295+ out .setRange (d .toOriginal (h .getRange ()));
296+ return out ;
297+ });
250298 }
251299
252300 // -- Problems --
@@ -301,9 +349,15 @@ private void closeDocument() {
301349 new TextDocumentIdentifier (uri )));
302350 uri = null ;
303351 text = null ;
352+ doc = null ;
304353 diagnostics = Collections .emptyList ();
305354 }
306355
356+ /** Where an offset into the script is, in what the server sees. */
357+ private Position position (final int offset ) {
358+ return Positions .position (doc .text (), doc .toTransformed (offset ));
359+ }
360+
307361 private TextDocumentIdentifier identifier () {
308362 return new TextDocumentIdentifier (uri );
309363 }
@@ -322,18 +376,68 @@ private void newVersion() {
322376 if (nextDiagnostics .isDone ()) nextDiagnostics = new CompletableFuture <>();
323377 }
324378
379+ /** Maps completions' edits back, leaving out those in the added lines. */
380+ private static CompletionList back (final ScriptDocument doc ,
381+ final CompletionList list )
382+ {
383+ final List <CompletionItem > items = new ArrayList <>();
384+ for (final CompletionItem item : list .getItems ()) {
385+ if (item .getTextEdit () != null ) {
386+ if (item .getTextEdit ().isLeft ()) {
387+ final TextEdit e = item .getTextEdit ().getLeft ();
388+ final Range r = doc .toOriginal (e .getRange ());
389+ if (r == null ) continue ;
390+ item .setTextEdit (Either .forLeft (new TextEdit (r , e .getNewText ())));
391+ }
392+ else {
393+ final InsertReplaceEdit e = item .getTextEdit ().getRight ();
394+ final Range insert = doc .toOriginal (e .getInsert ());
395+ final Range replace = doc .toOriginal (e .getReplace ());
396+ if (insert == null || replace == null ) continue ;
397+ item .setTextEdit (Either .forRight (new InsertReplaceEdit (e
398+ .getNewText (), insert , replace )));
399+ }
400+ }
401+ if (item .getAdditionalTextEdits () != null ) {
402+ final List <TextEdit > edits = new ArrayList <>();
403+ for (final TextEdit e : item .getAdditionalTextEdits ()) {
404+ final Range r = doc .toOriginal (e .getRange ());
405+ if (r != null ) edits .add (new TextEdit (r , e .getNewText ()));
406+ }
407+ item .setAdditionalTextEdits (edits );
408+ }
409+ items .add (item );
410+ }
411+ return new CompletionList (list .isIncomplete (), items );
412+ }
413+
414+ /** Maps problems back, leaving out those in the added lines. */
415+ private static List <Diagnostic > back (final ScriptDocument doc ,
416+ final List <Diagnostic > diagnostics )
417+ {
418+ final List <Diagnostic > out = new ArrayList <>();
419+ for (final Diagnostic d : diagnostics ) {
420+ final Range r = doc .toOriginal (d .getRange ());
421+ if (r == null ) continue ;
422+ d .setRange (r );
423+ out .add (d );
424+ }
425+ return out ;
426+ }
427+
325428 /** Receives the server's messages about the script. */
326429 private final class Client implements LanguageClient {
327430
328431 @ Override
329432 public void publishDiagnostics (final PublishDiagnosticsParams p ) {
330- final List <Diagnostic > found = p .getDiagnostics () == null ? Collections
331- .emptyList () : p .getDiagnostics ();
433+ final List <Diagnostic > found ;
332434 final CompletableFuture <List <Diagnostic >> next ;
333435 synchronized (ScriptSession .this ) {
334436 if (!p .getUri ().equals (uri )) return ;
335437 // NB: Those of an earlier version are out of date.
336438 if (p .getVersion () != null && p .getVersion () < version ) return ;
439+ found = p .getDiagnostics () == null ? Collections .emptyList () : back (
440+ doc , p .getDiagnostics ());
337441 diagnostics = found ;
338442 next = nextDiagnostics ;
339443 }
0 commit comments