/** * Convert line, column to a document offset. * @param buffer * @param line * @param column * @return */ public static int toOffset(IBuffer buffer, int line, int column){ if (buffer != null) { return toOffset(toDocument(buffer), line, column); } return -1; }
/** * 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); }
private List<DocumentHighlight> computeOccurrences(ITypeRoot unit, int line, int column, IProgressMonitor monitor) { if (unit != null) { try { int offset = JsonRpcHelpers.toOffset(unit.getBuffer(), line, column); OccurrencesFinder finder = new OccurrencesFinder(); CompilationUnit ast = CoreASTProvider.getInstance().getAST(unit, CoreASTProvider.WAIT_YES, monitor); if (ast != null) { String error = finder.initialize(ast, offset, 0); if (error == null){ List<DocumentHighlight> result = new ArrayList<>(); OccurrenceLocation[] occurrences = finder.getOccurrences(); if (occurrences != null) { for (OccurrenceLocation loc : occurrences) { if (monitor.isCanceled()) { return Collections.emptyList(); } result.add(convertToHighlight(unit, loc)); } } return result; } } } catch (JavaModelException e) { JavaLanguageServerPlugin.logException("Problem with compute occurrences for" + unit.getElementName(), e); } } return Collections.emptyList(); }
public List<HighlightedPositionCore> calculateHighlightedPositions(ICompilationUnit unit, boolean cache) throws JavaModelException, BadPositionCategoryException { if (enabled.get()) { IDocument document = JsonRpcHelpers.toDocument(unit.getBuffer()); ASTNode ast = getASTNode(unit); List<HighlightedPositionCore> positions = calculateHighlightedPositions(document, ast); if (cache) { String uri = JDTUtils.getFileURI(unit.getResource()); this.cache.put(uri, positions); } return ImmutableList.copyOf(positions); } return emptyList(); }
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 }
/** * Gets the end offset for the diagnostic. * * @param unit * @param range * @return starting offset or negative value if can not be determined */ public static int getEndOffset(ICompilationUnit unit, Range range){ try { return JsonRpcHelpers.toOffset(unit.getBuffer(), range.getEnd().getLine(), range.getEnd().getCharacter()); } catch (JavaModelException e) { return -1; } }
public List<Position> install(ICompilationUnit unit) throws JavaModelException, BadPositionCategoryException { if (enabled.get()) { List<HighlightedPositionCore> positions = calculateHighlightedPositions(unit, false); String uri = JDTUtils.getFileURI(unit.getResource()); this.cache.put(uri, positions); if (!positions.isEmpty()) { IDocument document = JsonRpcHelpers.toDocument(unit.getBuffer()); List<SemanticHighlightingInformation> infos = toInfos(document, positions); VersionedTextDocumentIdentifier textDocument = new VersionedTextDocumentIdentifier(uri, 1); notifyClient(textDocument, infos); } return ImmutableList.copyOf(positions); } return emptyList(); }
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 }
/** * Gets the start offset for the diagnostic. * * @param unit * @param range * @return starting offset or negative value if can not be determined */ public static int getStartOffset(ICompilationUnit unit, Range range){ try { return JsonRpcHelpers.toOffset(unit.getBuffer(), range.getStart().getLine(), range.getStart().getCharacter()); } catch (JavaModelException e) { return -1; } } /**
int startOffset = JsonRpcHelpers.toOffset(unit.getBuffer(), range.getStart().getLine(), range.getStart().getCharacter()); IDocument newState = JsonRpcHelpers.toDocument(unit.getBuffer()); diffContexts.add(new HighlightedPositionDiffContext(oldState, event, oldPositions, newPositions)); } else { IDocument document = JsonRpcHelpers.toDocument(unit.getBuffer()); edit.apply(document, TextEdit.NONE);
/** * @param completionBuffer * @param proposal */ private void appendMethodPotentialReplacement(StringBuilder completionBuffer, GetterSetterCompletionProposal proposal) { IDocument document; try { document = JsonRpcHelpers.toDocument(this.compilationUnit.getBuffer()); int offset = proposal.getReplaceStart(); String replacement = proposal.updateReplacementString(document, offset, importRewrite, client.isCompletionSnippetsSupported()); completionBuffer.append(replacement); } catch (BadLocationException | CoreException e) { JavaLanguageServerPlugin.logException("Failed to compute potential replacement", e); } }
/** * 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; }
IType type = resolveType(frame); if (type != null && type.getCompilationUnit() != null) { final int offset = JsonRpcHelpers.toOffset(type.getCompilationUnit().getBuffer(), frame.location().lineNumber(), 0); CompletionProposalRequestor collector = new CompletionProposalRequestor(type.getCompilationUnit(), offset);
/** * @param completionBuffer * @param proposal */ private void appendMethodOverrideReplacement(StringBuilder completionBuffer, CompletionProposal proposal) { IDocument document; try { document = JsonRpcHelpers.toDocument(this.compilationUnit.getBuffer()); String signature = String.valueOf(proposal.getSignature()); String[] types = Stream.of(Signature.getParameterTypes(signature)).map(t -> Signature.toString(t)) .toArray(String[]::new); String methodName = String.valueOf(proposal.getName()); int offset = proposal.getReplaceStart(); String completion = new String(proposal.getCompletion()); OverrideCompletionProposal overrider = new OverrideCompletionProposal(compilationUnit, methodName, types, completion); String replacement = overrider.updateReplacementString(document, offset, importRewrite, client.isCompletionSnippetsSupported()); completionBuffer.append(replacement); } catch (BadLocationException | CoreException e) { JavaLanguageServerPlugin.logException("Failed to compute override replacement", e); } }
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; }
return null; int offset = JsonRpcHelpers.toOffset(unit.getBuffer(), line, column); if (offset > -1) { return unit.codeSelect(offset, 0);
private int getOffset(TextDocumentPositionParams param, ITypeRoot typeRoot) { int offset = 0; try { IDocument document = JsonRpcHelpers.toDocument(typeRoot.getBuffer()); offset = document.getLineOffset(param.getPosition().getLine()) + param.getPosition().getCharacter(); } catch (JavaModelException | BadLocationException e) { JavaLanguageServerPlugin.logException(e.getMessage(), e); } return offset; }
final int offset = JsonRpcHelpers.toOffset(unit.getBuffer(), line, column); CompletionProposalRequestor collector = new CompletionProposalRequestor(unit, offset);
private List<? extends org.eclipse.lsp4j.TextEdit> format(String uri, FormattingOptions options, Position position, String triggerChar, IProgressMonitor monitor) { if (!preferenceManager.getPreferences().isJavaFormatOnTypeEnabled()) { return Collections.emptyList(); } ICompilationUnit cu = JDTUtils.resolveCompilationUnit(uri); if (cu == null) { return Collections.emptyList(); } IRegion region = null; IDocument document = null; try { document = JsonRpcHelpers.toDocument(cu.getBuffer()); if (document != null && position != null) { region = getRegion(cu, document, position, triggerChar); } } catch (JavaModelException e) { JavaLanguageServerPlugin.logException(e.getMessage(), e); } if (region == null) { return Collections.emptyList(); } return format(cu, document, region, options, false, monitor); }
final int offset = JsonRpcHelpers.toOffset(unit.getBuffer(), position.getPosition().getLine(), position.getPosition().getCharacter()); int[] contextInfomation = getContextInfomation(unit.getBuffer(), offset); if (contextInfomation[0] == -1) {