/** * Convert offset to line number and column. * @param buffer * @param line * @param column * @return */ public static int[] toLine(IBuffer buffer, int offset){ return toLine(toDocument(buffer), offset); }
protected int[] getLineAndColumn(IDocument document, HighlightedPositionCore position) { //@formatter:off int[] lineAndColumn = JsonRpcHelpers.toLine(document, position.offset); Assert.isNotNull( lineAndColumn, "Cannot retrieve the line and column information for document. Position was: " + position + " Document was:>" + document.get() + "<." ); return lineAndColumn; //@formatter:off }
protected List<SemanticHighlightingInformation> toInfos(IDocument document, List<HighlightedPositionCore> positions) { Multimap<Integer, SemanticHighlightingTokens.Token> infos = HashMultimap.create(); for (HighlightedPositionCore position : positions) { int[] lineAndColumn = JsonRpcHelpers.toLine(document, position.offset); if (lineAndColumn == null) { JavaLanguageServerPlugin.logError("Cannot locate line and column information for the semantic highlighting position: " + position + ". Skipping it."); continue; } int line = lineAndColumn[0]; int character = lineAndColumn[1]; int length = position.length; int scope = LOOKUP_TABLE.inverse().get(position.getHighlighting()); infos.put(line, new SemanticHighlightingTokens.Token(character, length, scope)); } //@formatter:off return infos.asMap().entrySet().stream() .map(entry -> new SemanticHighlightingInformation(entry.getKey(), SemanticHighlightingTokens.encode(entry.getValue()))) .collect(Collectors.toList()); //@formatter:on }
/** * Creates a range for the given offset and length for an {@link IOpenable} * * @param openable * @param offset * @param length * @return * @throws JavaModelException */ public static Range toRange(IOpenable openable, int offset, int length) throws JavaModelException{ Range range = newRange(); if (offset > 0 || length > 0) { int[] loc = null; int[] endLoc = null; IBuffer buffer = openable.getBuffer(); if (buffer != null) { loc = JsonRpcHelpers.toLine(buffer, offset); endLoc = JsonRpcHelpers.toLine(buffer, offset + length); } if (loc == null) { loc = new int[2]; } if (endLoc == null) { endLoc = new int[2]; } setPosition(range.getStart(), loc); setPosition(range.getEnd(), endLoc); } return range; }
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; }