/** * Returns uri for a compilation unit * @param cu * @return */ @Deprecated public static String getFileURI(ICompilationUnit cu) { return toURI(cu); }
/** * Given the uri returns a {@link ICompilationUnit}. * May return null if it can not associate the uri with a Java * file. * * @param uriString * @return compilation unit */ public static ICompilationUnit resolveCompilationUnit(String uriString) { return resolveCompilationUnit(toURI(uriString)); }
/** * Given the uri string returns a {@link IPackageFragement}. May return null if * it can not associate the uri with a package fragment. * * @param uriString * @return package fragment */ public static IPackageFragment resolvePackage(String uriString) { return resolvePackage(toURI(uriString)); }
public static String toUri(ITypeRoot typeRoot) { if (typeRoot instanceof ICompilationUnit) { return toURI((ICompilationUnit) typeRoot); } if (typeRoot instanceof IClassFile) { return toUri((IClassFile) typeRoot); } return null; }
/** * Given the uri returns a {@link IClassFile}. * May return null if it can not resolve the uri to a * library. * * @see #toLocation(IClassFile, int, int) * @param uri with 'jdt' scheme * @return class file */ public static IClassFile resolveClassFile(String uriString){ return resolveClassFile(toURI(uriString)); }
public static IFile findFile(String uriString) { return (IFile) findResource(toURI(uriString), ResourcesPlugin.getWorkspace().getRoot()::findFilesForLocationURI); }
public static IContainer findFolder(String uriString) { return (IContainer) findResource(toURI(uriString), ResourcesPlugin.getWorkspace().getRoot()::findContainersForLocationURI); }
private List<TextEdit> handleSaveActionOrganizeImports(String documentUri, IProgressMonitor monitor) { String uri = ResourceUtils.fixURI(JDTUtils.toURI(documentUri)); if (monitor.isCanceled()) { return Collections.emptyList(); } WorkspaceEdit organizedResult = organizeImportsCommand.organizeImportsInFile(uri); List<TextEdit> edit = organizedResult.getChanges().get(uri); edit = edit == null ? Collections.emptyList() : edit; return edit; }
public DiagnosticsHandler(JavaClientConnection conn, ICompilationUnit cu) { problems = new ArrayList<>(); this.cu = cu; this.uri = JDTUtils.toURI(cu); this.connection = conn; this.isDefaultProject = JDTUtils.isDefaultProject(cu); this.reportAllErrors = !isDefaultProject && JDTUtils.isOnClassPath(cu); }
/** * Convenience method that combines {@link #resolveClassFile(String)} and * {@link #resolveCompilationUnit(String)}. * * @param uri * @return either a class file or compilation unit */ public static ITypeRoot resolveTypeRoot(String uriString) { URI uri = toURI(uriString); if (uri == null) { return null; } if (JDT_SCHEME.equals(uri.getScheme())) { return resolveClassFile(uri); } return resolveCompilationUnit(uri); }
private static void setFields(CompletionItem ci, ICompilationUnit cu) { ci.setKind(CompletionItemKind.Snippet); ci.setInsertTextFormat(InsertTextFormat.Snippet); ci.setDocumentation(ci.getInsertText()); Map<String, String> data = new HashMap<>(3); data.put(CompletionResolveHandler.DATA_FIELD_URI, JDTUtils.toURI(cu)); data.put(CompletionResolveHandler.DATA_FIELD_REQUEST_ID, "0"); data.put(CompletionResolveHandler.DATA_FIELD_PROPOSAL_ID, "0"); ci.setData(data); }
public WorkspaceEdit organizeImportsInFile(String fileUri) { WorkspaceEdit rootEdit = new WorkspaceEdit(); ICompilationUnit unit = null; if (JDTUtils.toURI(fileUri) != null) { unit = JDTUtils.resolveCompilationUnit(fileUri); } if (unit == null) { return rootEdit; } organizeImportsInCompilationUnit(unit, rootEdit); return rootEdit; }
/** * 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)); }
public CompletionItem toCompletionItem(CompletionProposal proposal, int index) { final CompletionItem $ = new CompletionItem(); $.setKind(mapKind(proposal.getKind())); Map<String, String> data = new HashMap<>(); // append data field so that resolve request can use it. data.put(CompletionResolveHandler.DATA_FIELD_URI, JDTUtils.toURI(unit)); data.put(CompletionResolveHandler.DATA_FIELD_REQUEST_ID,String.valueOf(response.getId())); data.put(CompletionResolveHandler.DATA_FIELD_PROPOSAL_ID,String.valueOf(index)); $.setData(data); this.descriptionProvider.updateDescription(proposal, $); $.setSortText(SortTextHelper.computeSortText(proposal)); return $; }
public TextDocumentEdit convertToTextDocumentEdit(int version) { String uri = JDTUtils.toURI(compilationUnit); VersionedTextDocumentIdentifier identifier = new VersionedTextDocumentIdentifier(version); identifier.setUri(uri); return new TextDocumentEdit(identifier, this.convert()); }
private void addWorkspaceEdit(ICompilationUnit cu, CUCorrectionProposal proposal, WorkspaceEdit rootEdit) throws CoreException { TextChange textChange = proposal.getTextChange(); TextEdit edit = textChange.getEdit(); TextEditConverter converter = new TextEditConverter(cu, edit); List<org.eclipse.lsp4j.TextEdit> edits = converter.convert(); if (ChangeUtil.hasChanges(edits)) { rootEdit.getChanges().put(JDTUtils.toURI(cu), edits); } } }
@Override public CompletableFuture<String> classFileContents(TextDocumentIdentifier param) { logInfo(">> java/classFileContents"); ContentProviderManager handler = JavaLanguageServerPlugin.getContentProviderManager(); URI uri = JDTUtils.toURI(param.getUri()); return computeAsync((monitor) -> handler.getContent(uri, monitor)); }
private static WorkspaceEdit convertChangeToWorkspaceEdit(ICompilationUnit unit, Change change) throws CoreException { WorkspaceEdit $ = new WorkspaceEdit(); if (change instanceof TextChange) { TextEditConverter converter = new TextEditConverter(unit, ((TextChange) change).getEdit()); String uri = JDTUtils.toURI(unit); $.getChanges().put(uri, converter.convert()); } else if (change instanceof ResourceChange) { ChangeUtil.convertResourceChange((ResourceChange) change, $); } else if (change instanceof CompositeChange) { ChangeUtil.convertCompositeChange(change, $); } return $; }
private static void convertTextEdit(WorkspaceEdit root, ICompilationUnit unit, TextEdit textEdits) { TextEdit[] children = textEdits.getChildren(); if (children.length == 0) { return; } for (TextEdit textEdit : children) { TextEditConverter converter = new TextEditConverter(unit, textEdit); String uri = JDTUtils.toURI(unit); if (JavaLanguageServerPlugin.getPreferencesManager().getClientPreferences().isResourceOperationSupported()) { List<Either<TextDocumentEdit, ResourceOperation>> changes = root.getDocumentChanges(); if (changes == null) { changes = new LinkedList<>(); root.setDocumentChanges(changes); } changes.add(Either.forLeft(converter.convertToTextDocumentEdit(0))); } else { Map<String, List<org.eclipse.lsp4j.TextEdit>> changes = root.getChanges(); if (changes.containsKey(uri)) { changes.get(uri).addAll(converter.convert()); } else { changes.put(uri, converter.convert()); } } } }
private static Location fixLocation(IJavaElement element, Location location, IJavaProject javaProject) { if (!javaProject.equals(element.getJavaProject()) && element.getJavaProject().getProject().getName().equals(ProjectsManager.DEFAULT_PROJECT_NAME)) { // see issue at: https://github.com/eclipse/eclipse.jdt.ls/issues/842 and https://bugs.eclipse.org/bugs/show_bug.cgi?id=541573 // for jdk classes, jdt will reuse the java model by altering project to share the model between projects // so that sometimes the project for `element` is default project and the project is different from the project for `unit` // this fix is to replace the project name with non-default ones since default project should be transparent to users. if (location.getUri().contains(ProjectsManager.DEFAULT_PROJECT_NAME)) { String patched = StringUtils.replaceOnce(location.getUri(), ProjectsManager.DEFAULT_PROJECT_NAME, javaProject.getProject().getName()); try { IClassFile cf = (IClassFile) JavaCore.create(JDTUtils.toURI(patched).getQuery()); if (cf != null && cf.exists()) { location.setUri(patched); } } catch (Exception ex) { } } } return location; }