Skip to content

Commit 97995e0

Browse files
committed
Always use ISourceLocation for Rascal locations.
1 parent d8da3e8 commit 97995e0

File tree

7 files changed

+49
-65
lines changed

7 files changed

+49
-65
lines changed

rascal-lsp/src/main/java/org/rascalmpl/vscode/lsp/parametric/ParametricTextDocumentService.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -449,14 +449,13 @@ private CompletableFuture<ISourceLocation> computeRenameRange(final ILanguageCon
449449
@Override
450450
public CompletableFuture<WorkspaceEdit> rename(RenameParams params) {
451451
logger.trace("rename for: {}, new name: {}", params.getTextDocument().getUri(), params.getNewName());
452-
ISourceLocation loc = Locations.toLoc(params.getTextDocument());
452+
ISourceLocation loc = Locations.setPosition(Locations.toLoc(params.getTextDocument()), params.getPosition(), columns);
453453
ILanguageContributions contribs = contributions(loc);
454-
Position rascalPos = Locations.toRascalPosition(loc, params.getPosition(), columns);
455454
return getFile(loc)
456455
.getCurrentTreeAsync(true)
457456
.thenApply(Versioned::get)
458457
.thenCompose(tree -> computeRename(contribs,
459-
rascalPos.getLine(), rascalPos.getCharacter(), params.getNewName(), tree));
458+
loc.getBeginLine(), loc.getBeginColumn(), params.getNewName(), tree));
460459
}
461460

462461
private CompletableFuture<WorkspaceEdit> computeRename(final ILanguageContributions contribs, final int startLine,
@@ -767,8 +766,8 @@ public CompletableFuture<List<Either<Command, CodeAction>>> codeAction(CodeActio
767766
.getCurrentTreeAsync(true)
768767
.thenApply(Versioned::get)
769768
.thenCompose(tree -> {
770-
var range = Locations.toRascalRange(location, params.getRange(), columns);
771-
return computeCodeActions(contribs, range.getStart().getLine(), range.getStart().getCharacter(), tree);
769+
var start = Locations.setPosition(location, params.getRange().getStart(), columns);
770+
return computeCodeActions(contribs, start.getBeginLine(), start.getBeginColumn(), tree);
772771
})
773772
.thenApply(IList::stream)
774773
, Stream::empty)
@@ -866,9 +865,9 @@ public CompletableFuture<List<SelectionRange>> selectionRange(SelectionRangePara
866865
return recoverExceptions(file.getCurrentTreeAsync(true)
867866
.thenApply(Versioned::get)
868867
.thenCompose(t -> CompletableFutureUtils.reduce(params.getPositions().stream()
869-
.map(p -> Locations.toRascalPosition(loc, p, columns))
868+
.map(p -> Locations.setPosition(loc, p, columns))
870869
.map(p -> computeSelection
871-
.thenCompose(compute -> compute.apply(TreeSearch.computeFocusList(t, p.getLine(), p.getCharacter())))
870+
.thenCompose(compute -> compute.apply(TreeSearch.computeFocusList(t, p.getBeginLine(), p.getBeginColumn())))
872871
.thenApply(selection -> SelectionRanges.toSelectionRange(p, selection, columns)))
873872
.collect(Collectors.toUnmodifiableList()), exec)),
874873
Collections::emptyList);
@@ -883,8 +882,8 @@ public CompletableFuture<List<CallHierarchyItem>> prepareCallHierarchy(CallHiera
883882
return recoverExceptions(file.getCurrentTreeAsync(true)
884883
.thenApply(Versioned::get)
885884
.thenCompose(t -> {
886-
final var pos = Locations.toRascalPosition(loc, params.getPosition(), columns);
887-
return contrib.prepareCallHierarchy(TreeSearch.computeFocusList(t, pos.getLine(), pos.getCharacter()))
885+
final var pos = Locations.setPosition(loc, params.getPosition(), columns);
886+
return contrib.prepareCallHierarchy(TreeSearch.computeFocusList(t, pos.getBeginLine(), pos.getBeginColumn()))
888887
.get()
889888
.thenApply(items -> {
890889
var ch = new CallHierarchy(exec);

rascal-lsp/src/main/java/org/rascalmpl/vscode/lsp/parametric/model/ParametricSummary.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@
3434
import java.util.function.BiFunction;
3535
import java.util.function.Function;
3636
import java.util.stream.Collectors;
37-
import org.apache.logging.log4j.Logger;
3837
import org.apache.logging.log4j.LogManager;
38+
import org.apache.logging.log4j.Logger;
3939
import org.checkerframework.checker.initialization.qual.UnderInitialization;
4040
import org.checkerframework.checker.nullness.qual.Nullable;
4141
import org.eclipse.lsp4j.Diagnostic;
@@ -44,6 +44,7 @@
4444
import org.eclipse.lsp4j.Position;
4545
import org.eclipse.lsp4j.Range;
4646
import org.eclipse.lsp4j.jsonrpc.messages.Either;
47+
import org.rascalmpl.util.locations.ColumnMaps;
4748
import org.rascalmpl.values.IRascalValueFactory;
4849
import org.rascalmpl.values.parsetrees.ITree;
4950
import org.rascalmpl.vscode.lsp.parametric.ILanguageContributions;
@@ -56,7 +57,6 @@
5657
import org.rascalmpl.vscode.lsp.util.Lazy;
5758
import org.rascalmpl.vscode.lsp.util.Versioned;
5859
import org.rascalmpl.vscode.lsp.util.concurrent.InterruptibleFuture;
59-
import org.rascalmpl.util.locations.ColumnMaps;
6060
import org.rascalmpl.vscode.lsp.util.locations.IRangeMap;
6161
import org.rascalmpl.vscode.lsp.util.locations.Locations;
6262
import org.rascalmpl.vscode.lsp.util.locations.impl.TreeMapLookup;
@@ -498,13 +498,13 @@ public void invalidate() {
498498
return null;
499499
}
500500

501-
var pos = Locations.toRascalPosition(file, cursor, columns);
502-
var focus = TreeSearch.computeFocusList(tree.get(), pos.getLine(), pos.getCharacter());
501+
var pos = Locations.setRange(file, new Range(cursor, cursor), columns);
502+
var focus = TreeSearch.computeFocusList(tree.get(), pos.getBeginLine(), pos.getBeginColumn());
503503

504504
InterruptibleFuture<ISet> set = null;
505505

506506
if (focus.isEmpty()) {
507-
logger.trace("{}: could not find substree at line {} and offset {}", logName, pos.getLine(), pos.getCharacter());
507+
logger.trace("{}: could not find substree at line {} and offset {}", logName, pos.getBeginLine(), pos.getBeginColumn());
508508
set = InterruptibleFuture.completedFuture(IRascalValueFactory.getInstance().set(), exec);
509509
} else {
510510
logger.trace("{}: looked up focus with length: {}, now calling dedicated function", logName, focus.length());

rascal-lsp/src/main/java/org/rascalmpl/vscode/lsp/rascal/RascalTextDocumentService.java

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@
7575
import org.eclipse.lsp4j.MarkupContent;
7676
import org.eclipse.lsp4j.MessageParams;
7777
import org.eclipse.lsp4j.MessageType;
78-
import org.eclipse.lsp4j.Position;
7978
import org.eclipse.lsp4j.PrepareRenameDefaultBehavior;
8079
import org.eclipse.lsp4j.PrepareRenameParams;
8180
import org.eclipse.lsp4j.PrepareRenameResult;
@@ -408,8 +407,8 @@ public CompletableFuture<Either3<Range, PrepareRenameResult, PrepareRenameDefaul
408407
return recoverExceptions(file.getCurrentTreeAsync(false)
409408
.thenApply(Versioned::get)
410409
.thenApply(tr -> {
411-
Position rascalCursorPos = Locations.toRascalPosition(file.getLocation(), params.getPosition(), columns);
412-
IList focus = TreeSearch.computeFocusList(tr, rascalCursorPos.getLine(), rascalCursorPos.getCharacter());
410+
ISourceLocation rascalCursorPos = Locations.setPosition(file.getLocation(), params.getPosition(), columns);
411+
IList focus = TreeSearch.computeFocusList(tr, rascalCursorPos.getBeginLine(), rascalCursorPos.getBeginColumn());
413412
return findQualifiedNameUnderCursor(focus);
414413
})
415414
.thenApply(cur -> Locations.toRange(TreeAdapter.getLocation(cur), columns))
@@ -430,8 +429,8 @@ public CompletableFuture<WorkspaceEdit> rename(RenameParams params) {
430429
return t;
431430
})
432431
.thenCompose(tr -> {
433-
Position rascalCursorPos = Locations.toRascalPosition(file.getLocation(), params.getPosition(), columns);
434-
var focus = TreeSearch.computeFocusList(tr, rascalCursorPos.getLine(), rascalCursorPos.getCharacter());
432+
ISourceLocation rascalCursorPos = Locations.setPosition(file.getLocation(), params.getPosition(), columns);
433+
var focus = TreeSearch.computeFocusList(tr, rascalCursorPos.getBeginLine(), rascalCursorPos.getBeginColumn());
435434
var cursorTree = findQualifiedNameUnderCursor(focus);
436435
var workspaceFolders = availableWorkspaceServices().workspaceFolders()
437436
.stream()
@@ -611,9 +610,9 @@ public CompletableFuture<List<SelectionRange>> selectionRange(SelectionRangePara
611610
return recoverExceptions(file.getCurrentTreeAsync(true)
612611
.thenApply(Versioned::get)
613612
.thenApply(tr -> params.getPositions().stream()
614-
.map(p -> Locations.toRascalPosition(file.getLocation(), p, columns))
613+
.map(p -> Locations.setPosition(file.getLocation(), p, columns))
615614
.map(p -> {
616-
var focus = TreeSearch.computeFocusList(tr, p.getLine(), p.getCharacter());
615+
var focus = TreeSearch.computeFocusList(tr, p.getBeginLine(), p.getBeginColumn());
617616
var locs = SelectionRanges.uniqueTreeLocations(focus);
618617
return SelectionRanges.toSelectionRange(p, locs, columns);
619618
})
@@ -693,9 +692,9 @@ public CompletableFuture<List<Either<Command, CodeAction>>> codeAction(CodeActio
693692
.getCurrentTreeAsync(true)
694693
.thenApply(Versioned::get)
695694
.thenCompose((ITree tree) -> {
696-
var doc = params.getTextDocument();
697-
var range = Locations.toRascalRange(doc, params.getRange(), columns);
698-
return computeCodeActions(range.getStart().getLine(), range.getStart().getCharacter(), tree, availableFacts().getPathConfig(Locations.toLoc(doc)));
695+
var loc = Locations.toLoc(params.getTextDocument());
696+
var start = Locations.setPosition(loc, params.getRange().getStart(), columns);
697+
return computeCodeActions(start.getBeginLine(), start.getBeginColumn(), tree, availableFacts().getPathConfig(loc));
699698
})
700699
.thenApply(IList::stream)
701700
, Stream::empty)

rascal-lsp/src/main/java/org/rascalmpl/vscode/lsp/rascal/model/SummaryBridge.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@
3535
import org.eclipse.lsp4j.Location;
3636
import org.eclipse.lsp4j.Position;
3737
import org.eclipse.lsp4j.Range;
38+
import org.rascalmpl.util.locations.ColumnMaps;
3839
import org.rascalmpl.values.IRascalValueFactory;
3940
import org.rascalmpl.values.ValueFactoryFactory;
4041
import org.rascalmpl.vscode.lsp.util.Lazy;
41-
import org.rascalmpl.util.locations.ColumnMaps;
4242
import org.rascalmpl.vscode.lsp.util.locations.IRangeMap;
4343
import org.rascalmpl.vscode.lsp.util.locations.Locations;
4444
import org.rascalmpl.vscode.lsp.util.locations.impl.TreeMapLookup;
@@ -80,7 +80,7 @@ public SummaryBridge() {
8080

8181
public SummaryBridge(ISourceLocation self, IConstructor summary, ColumnMaps cm) {
8282
this.data = summary.asWithKeywordParameters();
83-
definitions = Lazy.defer(() -> translateRelation(getKWFieldSet(data, "useDef"), self, v -> Locations.toLSPLocation((ISourceLocation)v, cm), cm));
83+
definitions = Lazy.defer(() -> translateRelation(getKWFieldSet(data, "useDef"), self, v -> Locations.toLocation((ISourceLocation)v, cm), cm));
8484
typeNames = Lazy.defer(() -> translateMap(getKWFieldMap(data, "locationTypes"), self, v -> ((IString)v).getValue(), cm));
8585

8686
}

rascal-lsp/src/main/java/org/rascalmpl/vscode/lsp/util/Diagnostics.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,10 @@
4444
import org.rascalmpl.exceptions.RuntimeExceptionFactory;
4545
import org.rascalmpl.exceptions.Throw;
4646
import org.rascalmpl.parser.gtd.exception.ParseError;
47+
import org.rascalmpl.util.locations.ColumnMaps;
4748
import org.rascalmpl.values.ValueFactoryFactory;
4849
import org.rascalmpl.values.parsetrees.ITree;
4950
import org.rascalmpl.values.parsetrees.TreeAdapter;
50-
import org.rascalmpl.util.locations.ColumnMaps;
5151
import org.rascalmpl.vscode.lsp.util.locations.Locations;
5252
import io.usethesource.vallang.ICollection;
5353
import io.usethesource.vallang.IConstructor;
@@ -179,7 +179,7 @@ public static List<Template> generateParseErrorDiagnostics(ITree errorTree) {
179179
}
180180

181181
private static DiagnosticRelatedInformation related(ColumnMaps cm, ISourceLocation loc, String message) {
182-
return new DiagnosticRelatedInformation(Locations.toLSPLocation(loc, cm), message);
182+
return new DiagnosticRelatedInformation(Locations.toLocation(loc, cm), message);
183183
}
184184

185185
private static void storeFixCommands(IConstructor d, Diagnostic result) {
@@ -220,7 +220,7 @@ public static Diagnostic translateDiagnostic(IConstructor d, Range range, Column
220220
((IList) dKW.getParameter("causes")).stream()
221221
.map(IConstructor.class::cast)
222222
.map(c -> new DiagnosticRelatedInformation(
223-
Locations.toLSPLocation(getMessageLocation(c), otherFiles),
223+
Locations.toLocation(getMessageLocation(c), otherFiles),
224224
getMessageString(c)))
225225
.collect(Collectors.toList())
226226
);

rascal-lsp/src/main/java/org/rascalmpl/vscode/lsp/util/SelectionRanges.java

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,12 @@
3131
import java.util.NoSuchElementException;
3232
import java.util.Objects;
3333
import java.util.stream.Collectors;
34-
35-
import org.eclipse.lsp4j.Position;
3634
import org.eclipse.lsp4j.Range;
3735
import org.eclipse.lsp4j.SelectionRange;
36+
import org.rascalmpl.util.locations.ColumnMaps;
3837
import org.rascalmpl.values.IRascalValueFactory;
3938
import org.rascalmpl.values.parsetrees.ITree;
4039
import org.rascalmpl.values.parsetrees.TreeAdapter;
41-
import org.rascalmpl.util.locations.ColumnMaps;
4240
import org.rascalmpl.vscode.lsp.util.locations.Locations;
4341

4442
import io.usethesource.vallang.IList;
@@ -55,17 +53,17 @@ private SelectionRanges() { /* hide implicit constructor */ }
5553
* @return A range with optional parent ranges, or an empty range when {@link ranges} is empty.
5654
* @throws IllegalArgumentException when the list of ranges contains anything else than source locations
5755
*/
58-
public static SelectionRange toSelectionRange(Position origin, IList ranges, ColumnMaps columns) {
56+
public static SelectionRange toSelectionRange(ISourceLocation origin, IList ranges, ColumnMaps columns) {
5957
if (ranges.isEmpty()) {
60-
return empty(origin);
58+
return empty(origin, columns);
6159
}
6260

6361
try {
6462
var lspRanges = ranges.stream()
6563
.map(ISourceLocation.class::cast)
6664
.map(l -> Locations.toRange(l, columns))
6765
.collect(Collectors.toList());
68-
return toSelectionRange(origin, lspRanges);
66+
return toSelectionRange(origin, lspRanges, columns);
6967
} catch (ClassCastException e) {
7068
throw new IllegalArgumentException("List of selection ranges should only contain source locations", e);
7169
}
@@ -76,9 +74,9 @@ public static SelectionRange toSelectionRange(Position origin, IList ranges, Col
7674
* @param ranges The range hierarchy. Should be ordered child-before-parent, where any range is contained by the next.
7775
* @return A range with optional parent ranges
7876
*/
79-
public static SelectionRange toSelectionRange(Position origin, List<Range> ranges) {
77+
public static SelectionRange toSelectionRange(ISourceLocation origin, List<Range> ranges, ColumnMaps columns) {
8078
if (ranges.isEmpty()) {
81-
return empty(origin);
79+
return empty(origin, columns);
8280
}
8381

8482
// assumes child-before-parent ordering
@@ -126,7 +124,7 @@ public static IList uniqueTreeLocations(IList trees) {
126124
.collect(IRascalValueFactory.getInstance().listWriter());
127125
}
128126

129-
public static SelectionRange empty(Position p) {
130-
return new SelectionRange(new Range(p, p), null);
127+
public static SelectionRange empty(ISourceLocation loc, ColumnMaps columns) {
128+
return new SelectionRange(Locations.toRange(loc, columns), null);
131129
}
132130
}

rascal-lsp/src/main/java/org/rascalmpl/vscode/lsp/util/locations/Locations.java

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -95,35 +95,19 @@ public static ISourceLocation toLoc(TextDocumentIdentifier doc) {
9595
* This fixes line offset off-by-one and column offsets character widths.
9696
* Mapping them from the LSP standard to the Rascal standard.
9797
*/
98-
public static Range toRascalRange(TextDocumentIdentifier doc, Range range, ColumnMaps columns) {
99-
return toRascalRange(toLoc(doc), range, columns);
98+
public static ISourceLocation toLoc(TextDocumentIdentifier doc, Position pos, ColumnMaps columns) {
99+
return toLoc(doc, new Range(pos, pos), columns);
100100
}
101101

102-
/**
103-
* This fixes line offset off-by-one and column offsets character widths.
104-
* Mapping them from the LSP standard to the Rascal standard.
105-
*/
106-
public static Range toRascalRange(ISourceLocation loc, Range range, ColumnMaps columns) {
107-
return new Range(
108-
toRascalPosition(loc, range.getStart(), columns),
109-
toRascalPosition(loc, range.getEnd(), columns)
110-
);
102+
public static ISourceLocation toLoc(TextDocumentIdentifier doc, Range range, ColumnMaps columns) {
103+
return setRange(toLoc(doc.getUri()), range, columns);
111104
}
112105

113106
/**
114107
* This fixes line offset off-by-one and column offsets character widths.
115108
* Mapping them from the LSP standard to the Rascal standard.
116109
*/
117-
public static Position toRascalPosition(TextDocumentIdentifier doc, Position pos, ColumnMaps columns) {
118-
var loc = toLoc(doc.getUri());
119-
return toRascalPosition(loc, pos, columns);
120-
}
121-
122-
/**
123-
* This fixes line offset off-by-one and column offsets character widths.
124-
* Mapping them from the LSP standard to the Rascal standard.
125-
*/
126-
public static Position toRascalPosition(ISourceLocation doc, Position pos, ColumnMaps columns) {
110+
private static Position toPosition(ISourceLocation doc, Position pos, ColumnMaps columns) {
127111
return new Position(
128112
pos.getLine() + 1,
129113
columns.get(doc).translateInverseColumn(pos.getLine(), pos.getCharacter(), false)
@@ -193,16 +177,16 @@ public static URI toUri(ISourceLocation loc) {
193177

194178
public static Location mapValueToLocation(IValue v, ColumnMaps cm) {
195179
if (v instanceof ISourceLocation) {
196-
return Locations.toLSPLocation((ISourceLocation)v, cm);
180+
return Locations.toLocation((ISourceLocation)v, cm);
197181
}
198182
throw new RuntimeException(v + "is not a ISourceLocation");
199183
}
200184

201-
public static Location toLSPLocation(ISourceLocation sloc, ColumnMaps cm) {
185+
public static Location toLocation(ISourceLocation sloc, ColumnMaps cm) {
202186
return new Location(Locations.toUri(sloc).toString(), toRange(sloc, cm));
203187
}
204188

205-
public static Location toLSPLocation(ISourceLocation sloc, LineColumnOffsetMap map) {
189+
public static Location toLocation(ISourceLocation sloc, LineColumnOffsetMap map) {
206190
return new Location(Locations.toUri(sloc).toString(), toRange(sloc, map));
207191
}
208192

@@ -222,13 +206,17 @@ public static Range toRange(ISourceLocation sloc, LineColumnOffsetMap map) {
222206
}
223207
}
224208

209+
public static ISourceLocation setPosition(ISourceLocation loc, Position pos, ColumnMaps columns) {
210+
return setRange(loc, new Range(pos, pos), columns);
211+
}
212+
225213
public static ISourceLocation setRange(ISourceLocation loc, Range lspRange, ColumnMaps columns) {
226214
var map = columns.get(loc);
227215
final var lspStart = lspRange.getStart();
228216
final var lspEnd = lspRange.getEnd();
229217
final var offsetLength = map.calculateInverseOffsetLength(lspStart.getLine(), lspStart.getCharacter(), lspEnd.getLine(), lspEnd.getCharacter());
230-
final var rascalStart = toRascalPosition(loc, lspStart, columns);
231-
final var rascalEnd = toRascalPosition(loc, lspEnd, columns);
218+
final var rascalStart = toPosition(loc, lspStart, columns);
219+
final var rascalEnd = toPosition(loc, lspEnd, columns);
232220
return VF.sourceLocation(loc,
233221
offsetLength.getLeft(),
234222
offsetLength.getRight(),

0 commit comments

Comments
 (0)