/** * Returns rendered text-edits. * * @return rendered edits */ @Override public List<TextEdit> getRenderedTextEdits() { List<TextEdit> edits = new ArrayList<>(); //Calculate text-edits positions.forEach( (pos, value) -> value.forEach((placeHolder, content) -> { Position position = new Position(pos.eLine, pos.eCol); Range range = new Range(position, position); TextEdit textEdit = new TextEdit(range, content); edits.add(textEdit); }) ); return edits; }
@Override public DocumentHighlight apply(final Document document, final ITextRegion region, final DocumentHighlightKind kind) { Preconditions.checkNotNull(document, "document"); Preconditions.checkNotNull(region, "region"); Preconditions.checkNotNull(kind, "kind"); final int offset = region.getOffset(); final Position start = document.getPosition(offset); final Position end = document.getPosition(offset + region.getLength()); return new DocumentHighlight(new Range(start, end), kind); }
public Range newRange(final Resource resource, final int startOffset, final int endOffset) { final Position startPosition = this.newPosition(resource, startOffset); if ((startPosition == null)) { return null; } final Position endPosition = this.newPosition(resource, endOffset); if ((endPosition == null)) { return null; } return new Range(startPosition, endPosition); }
/** * Renders the template. * * @return rendered string */ public List<TextEdit> getRenderedTextEdits() { Position position = new Position(0, 0); Range range = new Range(position, position); TextEdit textEdit = new TextEdit(range, getRenderedContent()); List<TextEdit> edits = new ArrayList<>(); edits.add(textEdit); return edits; }
/** * Determines the range of a snippet of text in the document. * * @param contextSnippet A larger snippet containing the actual snippet to look for. * This larger snippet is used to narrow the section of the document * where we look for the actual snippet. This is useful when the snippet * occurs multiple times in the document. * @param focusSnippet The snippet to look for */ public Range rangeOf(String longSnippet, String focusSnippet) throws Exception { int relativeOffset = longSnippet.indexOf(focusSnippet); int contextStart = getRawText().indexOf(longSnippet); Assert.assertTrue("'"+longSnippet+"' not found in editor", contextStart>=0); int start = contextStart+relativeOffset; return new Range(doc.toPosition(start), doc.toPosition(start+focusSnippet.length())); }
/** * Creates a new {@link Range} with its start and end {@link Position}s set to line=0, character=0 * * @return a new {@link Range}; */ public static Range newRange() { return new Range(new Position(), new Position()); }
/** * Creates a new {@link Range} with its start and end {@link Position}s set to * the given line * * @return a new {@link Range}; */ public static Range newLineRange(int line, int start, int end) { return new Range(new Position(line, start), new Position(line, end)); }
/** * Get TextEdit from doc attachment info. * * @param attachmentInfo Doc attachment info * @return {@link TextEdit} Text edit for attachment info */ private static TextEdit getTextEdit(DocAttachmentInfo attachmentInfo) { Range range = new Range(attachmentInfo.getDocStartPos(), attachmentInfo.getDocStartPos()); return new TextEdit(range, attachmentInfo.getDocAttachment()); } }
/** * Returns a newly created range. */ public static Range createRange(int startLine, int startColumn, int endLine, int endColumn) { return new Range(new Position(startLine, startColumn), new Position(endLine, endColumn)); }
Range clone(Range range) { if (range == null) { return null; } Position start = range.getStart(); Position end = range.getEnd(); Range cloned = new Range(); cloned.setStart(clone(start)); cloned.setEnd(clone(end)); return cloned; }
private TextEdit addPackage(String pkgName, BLangPackage srcOwnerPkg, LSContext context) { DiagnosticPos pos = null; // Filter the imports except the runtime import List<BLangImportPackage> imports = CommonUtil.getCurrentFileImports(srcOwnerPkg, context); if (!imports.isEmpty()) { BLangImportPackage lastImport = CommonUtil.getLastItem(imports); pos = lastImport.getPosition(); } int endCol = 0; int endLine = pos == null ? 0 : pos.getEndLine(); String editText = "import " + pkgName + ";\n"; Range range = new Range(new Position(endLine, endCol), new Position(endLine, endCol)); return new TextEdit(range, editText); }
private TextEdit addPackage(String pkgName, BLangPackage srcOwnerPkg, LSContext context) { DiagnosticPos pos = null; // Filter the imports except the runtime import List<BLangImportPackage> imports = CommonUtil.getCurrentFileImports(srcOwnerPkg, context); if (!imports.isEmpty()) { BLangImportPackage lastImport = CommonUtil.getLastItem(imports); pos = lastImport.getPosition(); } int endCol = 0; int endLine = pos == null ? 0 : pos.getEndLine(); String editText = "import " + pkgName + ";\n"; Range range = new Range(new Position(endLine, endCol), new Position(endLine, endCol)); return new TextEdit(range, editText); }
private static org.eclipse.lsp4j.TextEdit convertEdit(TextEdit edit, IDocument document) { org.eclipse.lsp4j.TextEdit textEdit = new org.eclipse.lsp4j.TextEdit(); if (edit instanceof ReplaceEdit) { ReplaceEdit replaceEdit = (ReplaceEdit) edit; textEdit.setNewText(replaceEdit.getText()); int offset = edit.getOffset(); textEdit.setRange(new Range(createPosition(document, offset), createPosition(document, offset + edit.getLength()))); } return textEdit; }
private static Range newRange(int startLine, int startCharacter, int endLine, int endCharacter) { return new Range(new Position(startLine, startCharacter), new Position(endLine, endCharacter)); }
protected TextEdit toTextEdit(final Document document, final String formattedText, final int startOffset, final int length) { TextEdit _textEdit = new TextEdit(); final Procedure1<TextEdit> _function = (TextEdit it) -> { it.setNewText(formattedText); Range _range = new Range(); final Procedure1<Range> _function_1 = (Range it_1) -> { it_1.setStart(document.getPosition(startOffset)); it_1.setEnd(document.getPosition((startOffset + length))); }; Range _doubleArrow = ObjectExtensions.<Range>operator_doubleArrow(_range, _function_1); it.setRange(_doubleArrow); }; return ObjectExtensions.<TextEdit>operator_doubleArrow(_textEdit, _function); }
@Test public void testDocumentResolve() throws Exception, ExecutionException { LanguageServer languageServer = wrap(LanguageServer.class, new MockLanguageServer() { @Override public CompletableFuture<DocumentLink> documentLinkResolve(DocumentLink params) { return CompletableFutures.computeAsync(canceler -> { params.setTarget("resolved"); return params; }); } }); CompletableFuture<DocumentLink> future = languageServer.getTextDocumentService().documentLinkResolve( new DocumentLink(new Range(new Position(0, 0), new Position(0, 0)), "unresolved") ); DocumentLink resolved = future.get(TIMEOUT, TimeUnit.MILLISECONDS); Assert.assertEquals("resolved", resolved.getTarget()); }
@Test public void testInvalidCodeLens() { ResponseMessage message = new ResponseMessage(); message.setId("1"); CodeLens codeLens = new CodeLens(new Range(new Position(3, 32), new Position(3, 35)), null, null); // forbidden self reference! codeLens.setData(codeLens); message.setResult(codeLens); assertIssues(message, "An element of the message has a direct or indirect reference to itself."); }
@Test public void testEquals() { Assert.assertEquals(new TextDocumentIdentifier("foo"), new TextDocumentIdentifier("foo")); Assert.assertNotEquals(new TextDocumentIdentifier("foo"), new TextDocumentIdentifier("bar")); CompletionItem e1 = new CompletionItem(); e1.setAdditionalTextEdits(new ArrayList<>()); e1.getAdditionalTextEdits().add(new TextEdit(new Range(new Position(1,1), new Position(1,1)), "foo")); CompletionItem e2 = new CompletionItem(); e2.setAdditionalTextEdits(new ArrayList<>()); e2.getAdditionalTextEdits().add(new TextEdit(new Range(new Position(1,1), new Position(1,1)), "foo")); CompletionItem e3 = new CompletionItem(); e3.setAdditionalTextEdits(new ArrayList<>()); e3.getAdditionalTextEdits().add(new TextEdit(new Range(new Position(1,1), new Position(1,2)), "foo")); Assert.assertEquals(e1, e2); Assert.assertNotEquals(e1, e3); Assert.assertEquals(e1.hashCode(), e2.hashCode()); Assert.assertNotEquals(e1.hashCode(), e3.hashCode()); }
private Range getRange(BLangNode node) { Range r = new Range(); int startLine = node.getPosition().getStartLine() - 1; // LSP range is 0 based int startChar = node.getPosition().getStartColumn() - 1; int endLine = node.getPosition().getEndLine() - 1; int endChar = node.getPosition().getEndColumn() - 1; if (endLine <= 0) { endLine = startLine; } if (endChar <= 0) { endChar = startChar + 1; } r.setStart(new Position(startLine, startChar)); r.setEnd(new Position(endLine, endChar)); return r; } }
private DocumentHighlight convertToHighlight(ITypeRoot unit, OccurrenceLocation occurrence) throws JavaModelException { DocumentHighlight h = new DocumentHighlight(); if ((occurrence.getFlags() | IOccurrencesFinder.F_WRITE_OCCURRENCE) == IOccurrencesFinder.F_WRITE_OCCURRENCE) { h.setKind(DocumentHighlightKind.Write); } else if ((occurrence.getFlags() | IOccurrencesFinder.F_READ_OCCURRENCE) == IOccurrencesFinder.F_READ_OCCURRENCE) { h.setKind(DocumentHighlightKind.Read); } int[] loc = JsonRpcHelpers.toLine(unit.getBuffer(), occurrence.getOffset()); int[] endLoc = JsonRpcHelpers.toLine(unit.getBuffer(), occurrence.getOffset() + occurrence.getLength()); h.setRange(new Range( new Position(loc[0], loc[1]), new Position(endLoc[0],endLoc[1]) )); return h; }