/** * 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); }
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(); }
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(); }
/** * @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); } }
/** * @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 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; }
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); }
document = JsonRpcHelpers.toDocument(cu.getBuffer()); } catch (JavaModelException e) { JavaLanguageServerPlugin.logException("Failed to publish diagnostics for " + uri, e); document = JsonRpcHelpers.toDocument(file);
IDocument document = JsonRpcHelpers.toDocument(this.compilationUnit.getBuffer()); IRegion region= document.getLineInformationOfOffset(proposal.getReplaceEnd()); String line= document.get(region.getOffset(),region.getLength());
private List<org.eclipse.lsp4j.TextEdit> format(String uri, FormattingOptions options, Range range, IProgressMonitor monitor) { if (!preferenceManager.getPreferences().isJavaFormatEnabled()) { 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) { region = (range == null ? new Region(0, document.getLength()) : getRegion(range, document)); } } catch (JavaModelException e) { JavaLanguageServerPlugin.logException(e.getMessage(), e); } if (region == null) { return Collections.emptyList(); } return format(cu, document, region, options, preferenceManager.getPreferences().isJavaFormatComments(), monitor); }
if (element instanceof IType) { try { IDocument document = JsonRpcHelpers.toDocument(typeRoot.getBuffer()); int offset = document.getLineOffset(position.getLine()) + position.getCharacter(); locations = findImplementations((IType) element, offset, monitor);
private void publishMarkers(IProject project, IMarker[] markers) throws CoreException { Range range = new Range(new Position(0, 0), new Position(0, 0)); List<IMarker> projectMarkers = new ArrayList<>(markers.length); String uri = JDTUtils.getFileURI(project); IFile pom = project.getFile("pom.xml"); List<IMarker> pomMarkers = new ArrayList<>(); for (IMarker marker : markers) { if (!marker.exists() || CheckMissingNaturesListener.MARKER_TYPE.equals(marker.getType())) { continue; } if (IMavenConstants.MARKER_CONFIGURATION_ID.equals(marker.getType())) { pomMarkers.add(marker); } else { projectMarkers.add(marker); } } List<Diagnostic> diagnostics = toDiagnosticArray(range, projectMarkers); String clientUri = ResourceUtils.toClientUri(uri); connection.publishDiagnostics(new PublishDiagnosticsParams(clientUri, diagnostics)); if (pom.exists()) { IDocument document = JsonRpcHelpers.toDocument(pom); diagnostics = toDiagnosticsArray(document, pom.findMarkers(null, true, IResource.DEPTH_ZERO)); List<Diagnostic> diagnosicts2 = toDiagnosticArray(range, pomMarkers); diagnostics.addAll(diagnosicts2); connection.publishDiagnostics(new PublishDiagnosticsParams(ResourceUtils.toClientUri(clientUri + "/pom.xml"), diagnostics)); } }
IDocument d = JsonRpcHelpers.toDocument(cu.getBuffer()); if (offset < 0 || d.getLength() == 0) { return result;
IDocument document = JsonRpcHelpers.toDocument(this.compilationUnit.getBuffer()); IRegion region= document.getLineInformationOfOffset(proposal.getReplaceEnd()); prefix = document.get(region.getOffset(), proposal.getReplaceEnd() -region.getOffset()).trim();
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);
markers = Arrays.copyOf(javaMarkers, javaMarkers.length + taskMarkers.length); System.arraycopy(taskMarkers, 0, markers, javaMarkers.length, taskMarkers.length); document = JsonRpcHelpers.toDocument(cu.getBuffer()); document = JsonRpcHelpers.toDocument(file);
try { IBuffer buffer = this.compilationUnit.getBuffer(); document = JsonRpcHelpers.toDocument(buffer); char[] declarationKey = proposal.getDeclarationKey(); if (declarationKey == null) {