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; }
@Test public void withNull() { final List<? extends DocumentHighlight> input = this.sort(CollectionLiterals.<DocumentHighlight>newArrayList(null, this.newHighlight(DocumentHighlightKind.Text, this.newRange(1, 1, 1, 1)), this.newHighlight(DocumentHighlightKind.Write, this.newRange(1, 1, 1, 1)), this.newHighlight(DocumentHighlightKind.Read, this.newRange(1, 1, 1, 1)))); Assert.assertEquals(1, input.get(0).getRange().getStart().getLine()); Assert.assertEquals(1, input.get(0).getRange().getStart().getCharacter()); Assert.assertEquals(1, input.get(0).getRange().getEnd().getLine()); Assert.assertEquals(1, input.get(0).getRange().getEnd().getCharacter()); Assert.assertEquals(DocumentHighlightKind.Text, input.get(0).getKind()); Assert.assertEquals(1, input.get(1).getRange().getStart().getLine()); Assert.assertEquals(1, input.get(1).getRange().getStart().getCharacter()); Assert.assertEquals(1, input.get(1).getRange().getEnd().getLine()); Assert.assertEquals(1, input.get(1).getRange().getEnd().getCharacter()); Assert.assertEquals(DocumentHighlightKind.Read, input.get(1).getKind()); Assert.assertEquals(1, input.get(2).getRange().getStart().getLine()); Assert.assertEquals(1, input.get(2).getRange().getStart().getCharacter()); Assert.assertEquals(1, input.get(2).getRange().getEnd().getLine()); Assert.assertEquals(1, input.get(2).getRange().getEnd().getCharacter()); Assert.assertEquals(DocumentHighlightKind.Write, input.get(2).getKind()); Assert.assertNull(IterableExtensions.last(input)); }
@Test public void testDocumentHighlightHandler() throws Exception { String uri = ClassFileUtil.getURI(project, "org.sample.Highlight"); TextDocumentIdentifier identifier = new TextDocumentIdentifier(uri); TextDocumentPositionParams params = new TextDocumentPositionParams(identifier, new Position(5, 10)); List<? extends DocumentHighlight> highlights = handler.documentHighlight(params, monitor); assertEquals(4, highlights.size()); assertHighlight(highlights.get(0), 5, 9, 15, DocumentHighlightKind.Write); assertHighlight(highlights.get(1), 6, 2, 8, DocumentHighlightKind.Read); assertHighlight(highlights.get(2), 7, 2, 8, DocumentHighlightKind.Write); assertHighlight(highlights.get(3), 8, 2, 8, DocumentHighlightKind.Read); }
@Test public void withoutNull() { final List<? extends DocumentHighlight> input = this.sort(CollectionLiterals.<DocumentHighlight>newArrayList(this.newHighlight(DocumentHighlightKind.Text, this.newRange(2, 2, 2, 2)), this.newHighlight(DocumentHighlightKind.Text, this.newRange(1, 1, 1, 1)), this.newHighlight(DocumentHighlightKind.Write, this.newRange(2, 2, 2, 2)), this.newHighlight(DocumentHighlightKind.Write, this.newRange(1, 1, 1, 1)), this.newHighlight(DocumentHighlightKind.Read, this.newRange(2, 2, 2, 2)), this.newHighlight(DocumentHighlightKind.Read, this.newRange(1, 1, 1, 1)))); Assert.assertEquals(1, input.get(0).getRange().getStart().getLine()); Assert.assertEquals(1, input.get(0).getRange().getStart().getCharacter()); Assert.assertEquals(1, input.get(0).getRange().getEnd().getLine()); Assert.assertEquals(1, input.get(0).getRange().getEnd().getCharacter()); Assert.assertEquals(DocumentHighlightKind.Text, input.get(0).getKind()); Assert.assertEquals(1, input.get(1).getRange().getStart().getLine()); Assert.assertEquals(1, input.get(1).getRange().getStart().getCharacter()); Assert.assertEquals(1, input.get(1).getRange().getEnd().getLine()); Assert.assertEquals(1, input.get(1).getRange().getEnd().getCharacter()); Assert.assertEquals(DocumentHighlightKind.Read, input.get(1).getKind()); Assert.assertEquals(1, input.get(2).getRange().getStart().getLine()); Assert.assertEquals(1, input.get(2).getRange().getStart().getCharacter()); Assert.assertEquals(1, input.get(2).getRange().getEnd().getLine()); Assert.assertEquals(1, input.get(2).getRange().getEnd().getCharacter()); Assert.assertEquals(DocumentHighlightKind.Write, input.get(2).getKind()); Assert.assertEquals(2, input.get(3).getRange().getStart().getLine()); Assert.assertEquals(2, input.get(3).getRange().getStart().getCharacter()); Assert.assertEquals(2, input.get(3).getRange().getEnd().getLine()); Assert.assertEquals(2, input.get(3).getRange().getEnd().getCharacter()); Assert.assertEquals(DocumentHighlightKind.Text, input.get(3).getKind()); Assert.assertEquals(2, input.get(4).getRange().getStart().getLine()); Assert.assertEquals(2, input.get(4).getRange().getStart().getCharacter()); Assert.assertEquals(2, input.get(4).getRange().getEnd().getLine()); Assert.assertEquals(2, input.get(4).getRange().getEnd().getCharacter()); Assert.assertEquals(DocumentHighlightKind.Read, input.get(4).getKind()); Assert.assertEquals(2, input.get(5).getRange().getStart().getLine()); Assert.assertEquals(2, input.get(5).getRange().getStart().getCharacter()); Assert.assertEquals(2, input.get(5).getRange().getEnd().getLine()); Assert.assertEquals(2, input.get(5).getRange().getEnd().getCharacter()); Assert.assertEquals(DocumentHighlightKind.Write, input.get(5).getKind()); }
protected String _toExpectation(final DocumentHighlight it) { String _xblockexpression = null; { StringConcatenation _builder = new StringConcatenation(); { Range _range = it.getRange(); boolean _tripleEquals = (_range == null); if (_tripleEquals) { _builder.append("[NaN, NaN]:[NaN, NaN]"); } else { String _expectation = this.toExpectation(it.getRange()); _builder.append(_expectation); } } final String rangeString = _builder.toString(); StringConcatenation _builder_1 = new StringConcatenation(); { DocumentHighlightKind _kind = it.getKind(); boolean _tripleEquals_1 = (_kind == null); if (_tripleEquals_1) { _builder_1.append("NaN"); } else { String _expectation_1 = this.toExpectation(it.getKind()); _builder_1.append(_expectation_1); } } _builder_1.append(" "); _builder_1.append(rangeString); _xblockexpression = _builder_1.toString(); } return _xblockexpression; }
@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 DocumentHighlight(@NonNull final Range range, final DocumentHighlightKind kind) { this(range); this.kind = kind; }
/** * The highlight kind, default is {@link DocumentHighlightKind#Text}. */ @Pure public DocumentHighlightKind getKind() { return this.kind; }
/** * The highlight kind, default is {@link DocumentHighlightKind#Text}. */ public void setKind(final DocumentHighlightKind kind) { this.kind = kind; }
private void assertHighlight(DocumentHighlight highlight, int expectedLine, int expectedStart, int expectedEnd, DocumentHighlightKind expectedKind) { Lsp4jAssertions.assertRange(expectedLine, expectedStart, expectedEnd, highlight.getRange()); assertEquals(expectedKind, highlight.getKind()); }
private DocumentHighlight newHighlight(final DocumentHighlightKind kind, final Range range) { return new DocumentHighlight(range, kind); }
protected String _toExpectation(final DocumentHighlightKind kind) { return kind.toString().substring(0, 1).toUpperCase(); }
protected String toExpectation(final Object it) { if (it instanceof Integer) { return _toExpectation((Integer)it); } else if (it instanceof List) { return _toExpectation((List<?>)it); } else if (it instanceof DocumentHighlightKind) { return _toExpectation((DocumentHighlightKind)it); } else if (it instanceof String) { return _toExpectation((String)it); } else if (it == null) { return _toExpectation((Void)null); } else if (it instanceof Map) { return _toExpectation((Map<Object, Object>)it); } else if (it instanceof CodeLens) { return _toExpectation((CodeLens)it); } else if (it instanceof ColoringInformation) { return _toExpectation((ColoringInformation)it); } else if (it instanceof Command) { return _toExpectation((Command)it); } else if (it instanceof CompletionItem) { return _toExpectation((CompletionItem)it); } else if (it instanceof DocumentHighlight) { return _toExpectation((DocumentHighlight)it); } else if (it instanceof Hover) { return _toExpectation((Hover)it); } else if (it instanceof Location) { return _toExpectation((Location)it); } else if (it instanceof Position) { return _toExpectation((Position)it); } else if (it instanceof Range) { return _toExpectation((Range)it); } else if (it instanceof SignatureHelp) { return _toExpectation((SignatureHelp)it); } else if (it instanceof SymbolInformation) { return _toExpectation((SymbolInformation)it); } else if (it instanceof TextEdit) { return _toExpectation((TextEdit)it); } else if (it instanceof WorkspaceEdit) { return _toExpectation((WorkspaceEdit)it); } else if (it instanceof Either) { return _toExpectation((Either<?, ?>)it); } else { throw new IllegalArgumentException("Unhandled parameter types: " + Arrays.<Object>asList(it).toString()); } }
/** * Transforms the {@link ITextRegion text region} argument into a * {@link DocumentHighlight document highlight} instance by calculating the * proper position from the document. The given kind will be used to * distinguish between {@link DocumentHighlightKind#Read read}, * {@link DocumentHighlightKind#Write write} and ordinary * {@link DocumentHighlightKind#Text text} occurrences. * * <p> * This conversion is required to transform the Xtext specific document * relative offsets into language server specific line relative offsets. * * @param document * the document that contains the text content. Cannot be * {@code null}. * @param region * the text region that has to be converted. Cannot be * {@code null}. * @param kind * the document highlight kind. Cannot be {@code null}. * * @return * with a new transformed {@link DocumentHighlight document * highlight} instance. */ DocumentHighlight apply(final Document document, final ITextRegion region, final DocumentHighlightKind kind);