/** * Creates location to the given offset and length for the class file. * * @param unit * @param offset * @param length * @return location * @throws JavaModelException */ public static Location toLocation(IClassFile classFile, int offset, int length) throws JavaModelException { String uriString = toUri(classFile); if (uriString != null) { Range range = toRange(classFile, offset, length); return new Location(uriString, range); } return null; }
/** * Creates location to the given offset and length for the compilation unit * * @param unit * @param offset * @param length * @return location or null * @throws JavaModelException */ public static Location toLocation(ICompilationUnit unit, int offset, int length) throws JavaModelException { return new Location(ResourceUtils.toClientUri(toURI(unit)), toRange(unit, offset, length)); }
private static Range getRange(ICompilationUnit typeRoot, IJavaElement element) throws JavaModelException { ISourceRange r = ((ISourceReference) element).getNameRange(); return JDTUtils.toRange(typeRoot, r.getOffset(), r.getLength()); }
private static Range parseTestItemRange(IJavaElement element) throws JavaModelException { if (element instanceof ISourceReference) { final ISourceRange range = ((ISourceReference) element).getNameRange(); return JDTUtils.toRange(element.getOpenable(), range.getOffset(), range.getLength()); } return null; } }
private Range toReplacementRange(CompletionProposal proposal){ try { return JDTUtils.toRange(compilationUnit, proposal.getReplaceStart(), proposal.getReplaceEnd()-proposal.getReplaceStart()); } catch (JavaModelException e) { JavaLanguageServerPlugin.logException(e.getMessage(), e); } return null; }
@Override public boolean visit(DeleteEdit edit) { try { org.eclipse.lsp4j.TextEdit te = new org.eclipse.lsp4j.TextEdit(); te.setNewText(""); te.setRange(JDTUtils.toRange(compilationUnit,edit.getOffset(),edit.getLength())); converted.add(te); } catch (JavaModelException e) { JavaLanguageServerPlugin.logException("Error converting TextEdits", e); } return super.visit(edit); }
@Override public boolean visit(InsertEdit edit) { try { org.eclipse.lsp4j.TextEdit te = new org.eclipse.lsp4j.TextEdit(); te.setNewText(edit.getText()); te.setRange(JDTUtils.toRange(compilationUnit,edit.getOffset(),edit.getLength())); converted.add(te); } catch (JavaModelException e) { JavaLanguageServerPlugin.logException("Error converting TextEdits", e); } return super.visit(edit); }
@Override public boolean visit(ReplaceEdit edit) { try { org.eclipse.lsp4j.TextEdit te = new org.eclipse.lsp4j.TextEdit(); te.setNewText(edit.getText()); te.setRange(JDTUtils.toRange(compilationUnit,edit.getOffset(),edit.getLength())); converted.add(te); } catch (JavaModelException e) { JavaLanguageServerPlugin.logException("Error converting TextEdits", e); } return super.visit(edit); }
private CodeLens getCodeLens(String type, IJavaElement element, ITypeRoot typeRoot) throws JavaModelException { ISourceRange r = ((ISourceReference) element).getNameRange(); if (r == null) { return null; } CodeLens lens = new CodeLens(); final Range range = JDTUtils.toRange(typeRoot, r.getOffset(), r.getLength()); lens.setRange(range); String uri = ResourceUtils.toClientUri(JDTUtils.toUri(typeRoot)); lens.setData(Arrays.asList(uri, range.getStart(), type)); return lens; }
Range range = JDTUtils.toRange(unit, offset, 0); boolean isSnippetSupported = JavaLanguageServerPlugin.getPreferencesManager().getClientPreferences().isCompletionSnippetsSupported(); String replacement = prepareTemplate(buf.toString(), lineDelimiter, isSnippetSupported);
@Override public boolean visit(MultiTextEdit edit) { try { org.eclipse.lsp4j.TextEdit te = new org.eclipse.lsp4j.TextEdit(); te.setRange(JDTUtils.toRange(compilationUnit, edit.getOffset(), edit.getLength())); Document doc = new Document(compilationUnit.getSource()); edit.apply(doc, TextEdit.UPDATE_REGIONS); String content = doc.get(edit.getOffset(), edit.getLength()); te.setNewText(content); converted.add(te); return false; } catch (JavaModelException | MalformedTreeException | BadLocationException e) { JavaLanguageServerPlugin.logException("Error converting TextEdits", e); } return false; }
@Override public boolean visit(MoveSourceEdit edit) { try { // If MoveSourcedEdit & MoveTargetEdit are the same level, should delete the original contenxt. // See issue#https://github.com/redhat-developer/vscode-java/issues/253 if (edit.getParent() != null && edit.getTargetEdit() != null && edit.getParent().equals(edit.getTargetEdit().getParent())) { org.eclipse.lsp4j.TextEdit te = new org.eclipse.lsp4j.TextEdit(); te.setNewText(""); te.setRange(JDTUtils.toRange(compilationUnit, edit.getOffset(), edit.getLength())); converted.add(te); return false; } } catch (JavaModelException e) { JavaLanguageServerPlugin.logException("Error converting TextEdits", e); } return super.visit(edit); }
@Override public boolean visit(CopySourceEdit edit) { try { if (edit.getTargetEdit() != null) { org.eclipse.lsp4j.TextEdit te = new org.eclipse.lsp4j.TextEdit(); te.setRange(JDTUtils.toRange(compilationUnit, edit.getOffset(), edit.getLength())); Document doc = new Document(compilationUnit.getSource()); edit.apply(doc, TextEdit.UPDATE_REGIONS); String content = doc.get(edit.getOffset(), edit.getLength()); if (edit.getSourceModifier() != null) { content = applySourceModifier(content, edit.getSourceModifier()); } te.setNewText(content); converted.add(te); } return false; } catch (JavaModelException | MalformedTreeException | BadLocationException e) { JavaLanguageServerPlugin.logException("Error converting TextEdits", e); } return super.visit(edit); }
range = JDTUtils.toRange(unit, 0, length);
@Override public boolean visit(CopyTargetEdit edit) { try { if (edit.getSourceEdit() != null) { org.eclipse.lsp4j.TextEdit te = new org.eclipse.lsp4j.TextEdit(); te.setRange(JDTUtils.toRange(compilationUnit, edit.getOffset(), edit.getLength())); Document doc = new Document(compilationUnit.getSource()); edit.apply(doc, TextEdit.UPDATE_REGIONS); String content = doc.get(edit.getSourceEdit().getOffset(), edit.getSourceEdit().getLength()); if (edit.getSourceEdit().getSourceModifier() != null) { content = applySourceModifier(content, edit.getSourceEdit().getSourceModifier()); } te.setNewText(content); converted.add(te); } return false; // do not visit children } catch (MalformedTreeException | BadLocationException | CoreException e) { JavaLanguageServerPlugin.logException("Error converting TextEdits", e); } return super.visit(edit); }
@Override public boolean visit(MoveTargetEdit edit) { try { if (edit.getSourceEdit() != null) { org.eclipse.lsp4j.TextEdit te = new org.eclipse.lsp4j.TextEdit(); te.setRange(JDTUtils.toRange(compilationUnit, edit.getOffset(), edit.getLength())); Document doc = new Document(compilationUnit.getSource()); edit.apply(doc, TextEdit.UPDATE_REGIONS); String content = doc.get(edit.getSourceEdit().getOffset(), edit.getSourceEdit().getLength()); if (edit.getSourceEdit().getSourceModifier() != null) { content = applySourceModifier(content, edit.getSourceEdit().getSourceModifier()); } te.setNewText(content); converted.add(te); return false; // do not visit children } } catch (MalformedTreeException | BadLocationException | CoreException e) { JavaLanguageServerPlugin.logException("Error converting TextEdits", e); } return super.visit(edit); }
@SuppressWarnings("restriction") private static Range convertRange(IOpenable openable, IProblem problem) { try { return JDTUtils.toRange(openable, problem.getSourceStart(), problem.getSourceEnd() - problem.getSourceStart() + 1); } catch (CoreException e) { // In case failed to open the IOpenable's buffer, use the IProblem's information to calculate the range. Position start = new Position(); Position end = new Position(); start.setLine(problem.getSourceLineNumber() - 1);// The protocol is 0-based. end.setLine(problem.getSourceLineNumber() - 1); if (problem instanceof DefaultProblem) { DefaultProblem dProblem = (DefaultProblem) problem; start.setCharacter(dProblem.getSourceColumnNumber() - 1); int offset = 0; if (dProblem.getSourceStart() != -1 && dProblem.getSourceEnd() != -1) { offset = dProblem.getSourceEnd() - dProblem.getSourceStart() + 1; } end.setCharacter(dProblem.getSourceColumnNumber() - 1 + offset); } return new Range(start, end); } }
return Either.forLeft(JDTUtils.toRange(unit, loc.getOffset(), loc.getLength()));