5555import org .eclipse .lsp4j .InsertReplaceEdit ;
5656import org .eclipse .lsp4j .MessageActionItem ;
5757import org .eclipse .lsp4j .MessageParams ;
58- import org .eclipse .lsp4j .Position ;
5958import org .eclipse .lsp4j .PublishDiagnosticsParams ;
6059import org .eclipse .lsp4j .Range ;
6160import org .eclipse .lsp4j .ShowMessageRequestParams ;
7170import org .eclipse .lsp4j .services .LanguageServer ;
7271import org .eclipse .lsp4j .services .TextDocumentService ;
7372import org .eclipse .lsp4j .services .WorkspaceService ;
74- import org .scijava .code .lsp .Positions ;
7573
7674/**
7775 * Shows a language server SciJava scripts as plain code of its language (see
@@ -192,7 +190,7 @@ public void didSave(final DidSaveTextDocumentParams params) {
192190 final ScriptDocument doc = docs .get (params .getTextDocument ().getUri ());
193191 if (doc == null ) return inner .getTextDocumentService ().completion (params );
194192 final CompletionParams p = new CompletionParams (params .getTextDocument (),
195- toTransformed (doc , params .getPosition ()), params .getContext ());
193+ doc . toTransformed (params .getPosition ()), params .getContext ());
196194 final CompletableFuture <Either <List <CompletionItem >, CompletionList >> raw =
197195 inner .getTextDocumentService ().completion (p );
198196 final CompletableFuture <Either <List <CompletionItem >, CompletionList >> out =
@@ -221,7 +219,7 @@ public CompletableFuture<SignatureHelp> signatureHelp(
221219 if (doc == null ) return inner .getTextDocumentService ().signatureHelp (
222220 params );
223221 return inner .getTextDocumentService ().signatureHelp (new SignatureHelpParams (
224- params .getTextDocument (), toTransformed (doc , params .getPosition ()),
222+ params .getTextDocument (), doc . toTransformed (params .getPosition ()),
225223 params .getContext ()));
226224 }
227225
@@ -230,12 +228,12 @@ public CompletableFuture<Hover> hover(final HoverParams params) {
230228 final ScriptDocument doc = docs .get (params .getTextDocument ().getUri ());
231229 if (doc == null ) return inner .getTextDocumentService ().hover (params );
232230 return inner .getTextDocumentService ().hover (new HoverParams (params
233- .getTextDocument (), toTransformed (doc , params .getPosition ()))).thenApply (
231+ .getTextDocument (), doc . toTransformed (params .getPosition ()))).thenApply (
234232 h -> {
235233 if (h == null || h .getRange () == null ) return h ;
236234 final Hover out = new Hover ();
237235 out .setContents (h .getContents ());
238- out .setRange (toOriginal (doc , h .getRange ()));
236+ out .setRange (doc . toOriginal (h .getRange ()));
239237 return out ;
240238 });
241239 }
@@ -252,31 +250,6 @@ public void didChangeWatchedFiles(final DidChangeWatchedFilesParams p) {
252250 inner .getWorkspaceService ().didChangeWatchedFiles (p );
253251 }
254252
255- // -- Position mapping --
256-
257- /** Maps a position in a script to one in its transformed text. */
258- static Position toTransformed (final ScriptDocument doc , final Position p ) {
259- final int offset = Positions .offset (doc .original (), p );
260- return Positions .position (doc .text (), doc .map ().toTransformed (offset ));
261- }
262-
263- /**
264- * Maps a position in the transformed text to one in the script; or returns
265- * null if it lies in the lines the transformation added.
266- */
267- static Position toOriginal (final ScriptDocument doc , final Position p ) {
268- final PositionMap map = doc .map ();
269- final int offset = map .toOriginal (Positions .offset (doc .text (), p ));
270- return offset < 0 ? null : Positions .position (doc .original (), offset );
271- }
272-
273- /** Maps a range back; or returns null if it starts in the added lines. */
274- static Range toOriginal (final ScriptDocument doc , final Range r ) {
275- final Position start = toOriginal (doc , r .getStart ());
276- final Position end = toOriginal (doc , r .getEnd ());
277- return start == null || end == null ? null : new Range (start , end );
278- }
279-
280253 /** Maps completions' edits back, leaving out those in the added lines. */
281254 private static CompletionList back (final ScriptDocument doc ,
282255 final CompletionList list )
@@ -287,14 +260,14 @@ private static CompletionList back(final ScriptDocument doc,
287260 if (item .getTextEdit () != null ) {
288261 if (item .getTextEdit ().isLeft ()) {
289262 final TextEdit e = item .getTextEdit ().getLeft ();
290- final Range r = toOriginal (doc , e .getRange ());
263+ final Range r = doc . toOriginal (e .getRange ());
291264 if (r == null ) continue ;
292265 c .setTextEdit (Either .forLeft (new TextEdit (r , e .getNewText ())));
293266 }
294267 else {
295268 final InsertReplaceEdit e = item .getTextEdit ().getRight ();
296- final Range insert = toOriginal (doc , e .getInsert ());
297- final Range replace = toOriginal (doc , e .getReplace ());
269+ final Range insert = doc . toOriginal (e .getInsert ());
270+ final Range replace = doc . toOriginal (e .getReplace ());
298271 if (insert == null || replace == null ) continue ;
299272 c .setTextEdit (Either .forRight (new InsertReplaceEdit (e .getNewText (),
300273 insert , replace )));
@@ -303,7 +276,7 @@ private static CompletionList back(final ScriptDocument doc,
303276 if (item .getAdditionalTextEdits () != null ) {
304277 final List <TextEdit > edits = new ArrayList <>();
305278 for (final TextEdit e : item .getAdditionalTextEdits ()) {
306- final Range r = toOriginal (doc , e .getRange ());
279+ final Range r = doc . toOriginal (e .getRange ());
307280 if (r != null ) edits .add (new TextEdit (r , e .getNewText ()));
308281 }
309282 c .setAdditionalTextEdits (edits );
@@ -327,7 +300,7 @@ public void publishDiagnostics(final PublishDiagnosticsParams p) {
327300 }
328301 final List <Diagnostic > mapped = new ArrayList <>();
329302 for (final Diagnostic d : p .getDiagnostics ()) {
330- final Range r = toOriginal (doc , d .getRange ());
303+ final Range r = doc . toOriginal (d .getRange ());
331304 if (r == null ) continue ; // NB: In the added lines.
332305 final Diagnostic m = new Diagnostic ();
333306 m .setRange (r );
0 commit comments