/** * 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)); }
/** * 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); }
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; }
private static void searchInNestedClass(List<TestItem> resultList, SearchTestItemParams params) throws JavaModelException { final ICompilationUnit compilationUnit = JDTUtils.resolveCompilationUnit(params.getUri()); for (final IType type : compilationUnit.getAllTypes()) { if (type.getFullyQualifiedName().equals(params.getFullName())) { resultList.addAll(searchTestMethodsOfType(type)); } } }
public List<CodeLens> getCodeLensSymbols(String uri, IProgressMonitor monitor) { if (!preferenceManager.getPreferences().isCodeLensEnabled()) { return Collections.emptyList(); } final ICompilationUnit unit = JDTUtils.resolveCompilationUnit(uri); IClassFile classFile = null; if (unit == null) { classFile = JDTUtils.resolveClassFile(uri); if (classFile == null) { return Collections.emptyList(); } } else { if (!unit.getResource().exists() || monitor.isCanceled()) { return Collections.emptyList(); } } try { ITypeRoot typeRoot = unit != null ? unit : classFile; IJavaElement[] elements = typeRoot.getChildren(); LinkedHashSet<CodeLens> lenses = new LinkedHashSet<>(elements.length); collectCodeLenses(typeRoot, elements, lenses, monitor); if (monitor.isCanceled()) { lenses.clear(); } return new ArrayList<>(lenses); } catch (JavaModelException e) { JavaLanguageServerPlugin.logException("Problem getting code lenses for" + unit.getElementName(), e); } return Collections.emptyList(); }
/** * Resolve the main methods from the current file. * @return an array of main methods. */ public static Object resolveMainMethods(List<Object> arguments) throws DebugException { if (arguments == null || arguments.isEmpty()) { return Collections.emptyList(); } // When the current document is changed, the language server will receive a didChange request about the changed text and then // trigger a background job to update the change to the CompilationUnit. Because of race condition, the resolveMainMethods may read // an old CompilationUnit. So add some waiting logic to wait the Document Update to finish first. try { Job.getJobManager().join(DocumentLifeCycleHandler.DOCUMENT_LIFE_CYCLE_JOBS, new NullProgressMonitor()); } catch (OperationCanceledException ignorable) { // Do nothing. } catch (InterruptedException e) { // Do nothing. } String uri = (String) arguments.get(0); final ICompilationUnit unit = JDTUtils.resolveCompilationUnit(uri); if (unit == null || unit.getResource() == null || !unit.getResource().exists()) { return Collections.emptyList(); } try { return resolveMainMethodCore(unit); } catch (Exception e) { throw new DebugException("Failed to resolve main method codeLens: " + e.getMessage(), e); } }
private static void searchInClass(List<TestItem> resultList, SearchTestItemParams params) throws JavaModelException { final ICompilationUnit unit = JDTUtils.resolveCompilationUnit(params.getUri()); for (final IType type : unit.getTypes()) { if (type.getFullyQualifiedName().equals(params.getFullName())) { for (final IType innerType : type.getTypes()) { resultList.add(TestItemUtils.constructTestItem(innerType, TestLevel.NESTED_CLASS)); } for (final IMethod method : type.getMethods()) { final TestItem item = TestFrameworkUtils.resoveTestItemForMethod(method); if (item != null) { resultList.add(item); } } } } }
final ICompilationUnit unit = JDTUtils.resolveCompilationUnit(uri); if (!isJavaElementExist(unit) || !isInTestScope(unit) || monitor.isCanceled()) { return resultList;
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); }
String uri = JDTUtils.getFileURI(file); if (JavaCore.isJavaLikeFileName(file.getName())) { ICompilationUnit cu = JDTUtils.resolveCompilationUnit(uri);
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); }
final ICompilationUnit unit = JDTUtils.resolveCompilationUnit(params.getTextDocument().getUri()); if (unit == null) { return Collections.emptyList();
public void handleSaved(DidSaveTextDocumentParams params) { String uri = params.getTextDocument().getUri(); ICompilationUnit unit = JDTUtils.resolveCompilationUnit(uri); if (unit == null) { JavaLanguageServerPlugin.logError(uri + " does not resolve to a ICompilationUnit"); return; } // see https://github.com/redhat-developer/vscode-java/issues/274 unit = checkPackageDeclaration(uri, unit); if (unit.isWorkingCopy()) { try { projectsManager.fileChanged(uri, CHANGE_TYPE.CHANGED); unit.discardWorkingCopy(); unit.becomeWorkingCopy(new NullProgressMonitor()); } catch (JavaModelException e) { JavaLanguageServerPlugin.logException("Error while handling document save. URI: " + uri, e); } } }
unit = JDTUtils.resolveCompilationUnit(uri); unit.becomeWorkingCopy(new NullProgressMonitor()); triggerValidation(unit);
public void handleClosed(DidCloseTextDocumentParams params) { String uri = params.getTextDocument().getUri(); ICompilationUnit unit = JDTUtils.resolveCompilationUnit(uri); if (unit == null) { return;
ICompilationUnit unit = JDTUtils.resolveCompilationUnit(position.getTextDocument().getUri()); final int offset = JsonRpcHelpers.toOffset(unit.getBuffer(), position.getPosition().getLine(), position.getPosition().getCharacter()); int[] contextInfomation = getContextInfomation(unit.getBuffer(), offset);
Either<List<CompletionItem>, CompletionList> completion(CompletionParams position, IProgressMonitor monitor) { List<CompletionItem> completionItems = null; try { ICompilationUnit unit = JDTUtils.resolveCompilationUnit(position.getTextDocument().getUri()); completionItems = this.computeContentAssist(unit, position.getPosition().getLine(), position.getPosition().getCharacter(), monitor); } catch (OperationCanceledException ignorable) { // No need to pollute logs when query is cancelled monitor.setCanceled(true); } catch (Exception e) { JavaLanguageServerPlugin.logException("Problem with codeComplete for " + position.getTextDocument().getUri(), e); monitor.setCanceled(true); } CompletionList $ = new CompletionList(); if (monitor.isCanceled()) { $.setIsIncomplete(true); completionItems = null; JavaLanguageServerPlugin.logInfo("Completion request cancelled"); } else { JavaLanguageServerPlugin.logInfo("Completion request completed"); } $.setItems(completionItems == null ? Collections.emptyList() : completionItems); return Either.forRight($); }
handler.didClose(new DidCloseTextDocumentParams(new TextDocumentIdentifier(fileEvent.getUri()))); ICompilationUnit unit = JDTUtils.resolveCompilationUnit(fileEvent.getUri()); if (unit != null && changeType == CHANGE_TYPE.CREATED && !unit.exists()) { final ICompilationUnit[] units = new ICompilationUnit[1];
final ICompilationUnit unit = JDTUtils.resolveCompilationUnit(params.getTextDocument().getUri());
public Either<Range, PrepareRenameResult> prepareRename(TextDocumentPositionParams params, IProgressMonitor monitor) { final ICompilationUnit unit = JDTUtils.resolveCompilationUnit(params.getTextDocument().getUri()); if (unit != null) { try {