Skip to content

Commit 0480a5b

Browse files
authored
refact: add LSPEclipseUtils.toRange() (#1253)
1 parent 4a429af commit 0480a5b

6 files changed

Lines changed: 15 additions & 23 deletions

File tree

org.eclipse.lsp4e/src/org/eclipse/lsp4e/DocumentContentSynchronizer.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@
5555
import org.eclipse.lsp4j.FormattingOptions;
5656
import org.eclipse.lsp4j.MessageParams;
5757
import org.eclipse.lsp4j.MessageType;
58-
import org.eclipse.lsp4j.Range;
5958
import org.eclipse.lsp4j.ServerCapabilities;
6059
import org.eclipse.lsp4j.TextDocumentContentChangeEvent;
6160
import org.eclipse.lsp4j.TextDocumentIdentifier;
@@ -198,9 +197,7 @@ private boolean createChangeEvent(DocumentEvent event) {
198197
int length = event.getLength();
199198
try {
200199
// try to convert the Eclipse start/end offset to LS range.
201-
final var range = new Range(LSPEclipseUtils.toPosition(offset, document),
202-
LSPEclipseUtils.toPosition(offset + length, document));
203-
changeEvent.setRange(range);
200+
changeEvent.setRange(LSPEclipseUtils.toRange(offset, offset + length, document));
204201
changeEvent.setText(newText);
205202
changeEvent.setRangeLength(length);
206203
} catch (BadLocationException e) {

org.eclipse.lsp4e/src/org/eclipse/lsp4e/LSPEclipseUtils.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,11 @@ private LSPEclipseUtils() {
191191
// this class shouldn't be instantiated
192192
}
193193

194+
public static Range toRange(final int startOffset, final int endOffset, final IDocument document)
195+
throws BadLocationException {
196+
return new Range(toPosition(startOffset, document), toPosition(endOffset, document));
197+
}
198+
194199
public static Position toPosition(int offset, IDocument document) throws BadLocationException {
195200
final var res = new Position();
196201
res.setLine(document.getLineOfOffset(offset));

org.eclipse.lsp4e/src/org/eclipse/lsp4e/operations/codeactions/LSPCodeActionQuickAssistProcessor.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838
import org.eclipse.lsp4e.ui.Messages;
3939
import org.eclipse.lsp4j.CodeActionContext;
4040
import org.eclipse.lsp4j.CodeActionParams;
41-
import org.eclipse.lsp4j.Range;
4241
import org.eclipse.lsp4j.ServerCapabilities;
4342
import org.eclipse.swt.graphics.Image;
4443
import org.eclipse.swt.graphics.Point;
@@ -197,8 +196,7 @@ private static CodeActionParams prepareCodeActionParams(final IDocument doc, int
197196
final var params = new CodeActionParams();
198197
params.setTextDocument(castNonNull(LSPEclipseUtils.toTextDocumentIdentifier(doc)));
199198
try {
200-
params.setRange(new Range(LSPEclipseUtils.toPosition(offset, doc), LSPEclipseUtils
201-
.toPosition(offset + (length > 0 ? length : 0), doc)));
199+
params.setRange(LSPEclipseUtils.toRange(offset, offset + (length > 0 ? length : 0), doc));
202200
} catch (BadLocationException e) {
203201
LanguageServerPlugin.logError(e);
204202
}

org.eclipse.lsp4e/src/org/eclipse/lsp4e/operations/codeactions/LSPCodeActionsMenu.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ public void initialize(IServiceLocator serviceLocator) {
6969
this.document = document;
7070
final var selection = (ITextSelection) textEditor.getSelectionProvider().getSelection();
7171
try {
72-
this.range = new Range(LSPEclipseUtils.toPosition(selection.getOffset(), document),
73-
LSPEclipseUtils.toPosition(selection.getOffset() + selection.getLength(), document));
72+
this.range = LSPEclipseUtils.toRange(selection.getOffset(),
73+
selection.getOffset() + selection.getLength(), document);
7474
} catch (BadLocationException e) {
7575
LanguageServerPlugin.logError(e);
7676
}

org.eclipse.lsp4e/src/org/eclipse/lsp4e/operations/completion/LSCompletionProposal.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -485,9 +485,7 @@ protected void apply(IDocument document, char trigger, int stateMask, int offset
485485
try {
486486
if (textEdit == null) {
487487
insertText = getInsertText();
488-
Position start = LSPEclipseUtils.toPosition(bestOffset, document);
489-
Position end = LSPEclipseUtils.toPosition(offset, document); // need 2 distinct objects
490-
textEdit = new TextEdit(new Range(start, end), insertText);
488+
textEdit = new TextEdit(LSPEclipseUtils.toRange(bestOffset, offset, document), insertText);
491489
} else if (offset > initialOffset) {
492490
// characters were added after completion was activated
493491
int shift = offset - initialOffset;
@@ -729,9 +727,7 @@ private Range getTextEditRange() throws BadLocationException {
729727
if (textEdit != null) {
730728
return textEdit.map(TextEdit::getRange, InsertReplaceEdit::getInsert);
731729
} else {
732-
Position start = LSPEclipseUtils.toPosition(bestOffset, document);
733-
Position end = LSPEclipseUtils.toPosition(initialOffset, document);
734-
return new Range(start, end);
730+
return LSPEclipseUtils.toRange(bestOffset, initialOffset, document);
735731
}
736732
}
737733

org.eclipse.lsp4e/src/org/eclipse/lsp4e/operations/format/LSPFormatter.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@
2929
import org.eclipse.lsp4j.DocumentFormattingParams;
3030
import org.eclipse.lsp4j.DocumentRangeFormattingParams;
3131
import org.eclipse.lsp4j.FormattingOptions;
32-
import org.eclipse.lsp4j.Position;
33-
import org.eclipse.lsp4j.Range;
3432
import org.eclipse.lsp4j.ServerCapabilities;
3533
import org.eclipse.lsp4j.TextDocumentIdentifier;
3634
import org.eclipse.ui.editors.text.EditorsUI;
@@ -80,12 +78,10 @@ public static DocumentRangeFormattingParams getRangeFormattingParams(IDocument d
8078
final var rangeParams = new DocumentRangeFormattingParams();
8179
rangeParams.setTextDocument(docId);
8280
rangeParams.setOptions(formatOptions);
83-
boolean fullFormat = textSelection.getLength() == 0;
84-
Position start = LSPEclipseUtils.toPosition(fullFormat ? 0 : textSelection.getOffset(), document);
85-
Position end = LSPEclipseUtils.toPosition(
86-
fullFormat ? document.getLength() : textSelection.getOffset() + textSelection.getLength(),
87-
document);
88-
rangeParams.setRange(new Range(start, end));
81+
final boolean isFullFormat = textSelection.isEmpty();
82+
final int startAt = isFullFormat ? 0 : textSelection.getOffset();
83+
final int endAt = isFullFormat ? document.getLength() : startAt + textSelection.getLength();
84+
rangeParams.setRange(LSPEclipseUtils.toRange(startAt, endAt, document));
8985
return rangeParams;
9086
}
9187

0 commit comments

Comments
 (0)