public static void enumerate( @NotNull PsiElement host, @NotNull PsiFile containingFile, boolean probeUp, @NotNull PsiLanguageInjectionHost.InjectedPsiVisitor visitor) { // do not inject into nonphysical files except during completion if (!containingFile.isPhysical() && containingFile.getOriginalFile() == containingFile) { final PsiElement context = InjectedLanguageManager.getInstance(containingFile.getProject()) .getInjectionHost(containingFile); if (context == null) return; final PsiFile file = context.getContainingFile(); if (file == null || !file.isPhysical() && file.getOriginalFile() == file) return; } if (containingFile.getViewProvider() instanceof InjectedFileViewProvider) return; // no injection inside injection PsiElement inTree = loadTree(host, containingFile); if (inTree != host) { host = inTree; containingFile = host.getContainingFile(); } MultiHostRegistrarImpl registrar = probeElementsUp(host, containingFile, probeUp); if (registrar == null) { return; } List<Pair<Place, PsiFile>> places = registrar.getResult(); for (Pair<Place, PsiFile> pair : places) { PsiFile injectedPsi = pair.second; visitor.visit(injectedPsi, pair.first); } }
public static Map<String, SpockVariableDescriptor> getVariableMap(@NotNull GrMethod method) { GrMethod originalMethod; PsiFile containingFile = method.getContainingFile(); if (containingFile != containingFile.getOriginalFile()) { int methodOffset = method.getTextOffset(); PsiElement originalPlace = containingFile.getOriginalFile().findElementAt(methodOffset); originalMethod = PsiTreeUtil.getParentOfType(originalPlace, GrMethod.class); assert originalMethod != null : containingFile .getOriginalFile() .getText() .substring( Math.max(0, methodOffset - 50), Math.min(methodOffset + 50, containingFile.getOriginalFile().getText().length())); } else { originalMethod = method; } Map<String, SpockVariableDescriptor> cachedValue = KEY.getCachedValue(originalMethod); if (cachedValue == null) { cachedValue = createVariableMap(originalMethod); cachedValue = KEY.putCachedValue(originalMethod, cachedValue); } return cachedValue; }
public static <T> T getOrCreateCache( Key<T> cacheKey, PsiElement element, CacheProvider<T> provider) { final PsiFile containingFile; if (element instanceof PsiFile) { containingFile = (PsiFile) element; } else { final PsiClass topLevelClass = PsiUtil.getTopLevelClass(element); if (topLevelClass == null) { return provider.provide(); } containingFile = topLevelClass.getContainingFile(); } if (containingFile == null) { return provider.provide(); } final PsiFile originalFile = containingFile.getOriginalFile(); T copyableUserData = originalFile.getCopyableUserData(cacheKey); if (copyableUserData != null) { if (!provider.isCacheValid(copyableUserData)) { copyableUserData = null; } } if (copyableUserData == null) { originalFile.putCopyableUserData(cacheKey, copyableUserData = provider.provide()); } return copyableUserData; }
@Override @Nullable public GlobalSearchScope getSearchScope() { GlobalSearchScope scope = null; Module[] modules = getConvertContextModules(); if (modules.length != 0) { PsiFile file = getFile(); file = file.getOriginalFile(); VirtualFile virtualFile = file.getVirtualFile(); if (virtualFile != null) { ProjectFileIndex fileIndex = ProjectRootManager.getInstance(file.getProject()).getFileIndex(); boolean tests = fileIndex.isInTestSourceContent(virtualFile); for (Module module : modules) { if (scope == null) { scope = module.getModuleRuntimeScope(tests); } else { scope = scope.union(module.getModuleRuntimeScope(tests)); } } } } return scope; // ??? scope == null ? GlobalSearchScope.allScope(getProject()) : scope; ??? }
@Nullable protected PsiFile getContainingFile() { final PsiFile file = InjectedLanguageUtil.getTopLevelFile(myElement.getContainingFile()); if (file == null) { LOG.error("Invalid element: " + myElement); } return file.getOriginalFile(); }
private CompletionParameters createCompletionParameters( int invocationCount, final CompletionContext newContext, Editor editor) { final int offset = newContext.getStartOffset(); final PsiFile fileCopy = newContext.file; PsiFile originalFile = fileCopy.getOriginalFile(); final PsiElement insertedElement = findCompletionPositionLeaf(newContext, offset, fileCopy, originalFile); insertedElement.putUserData(CompletionContext.COMPLETION_CONTEXT_KEY, newContext); return new CompletionParameters( insertedElement, originalFile, myCompletionType, offset, invocationCount, editor); }
public static long getLastUpdate(PsiElement element) { try { final PsiClass topLevelClass = PsiUtil.getTopLevelClass(element); if (topLevelClass == null) { return -1; } final PsiFile containingFile = topLevelClass.getContainingFile(); return containingFile.getOriginalFile().getModificationStamp(); } catch (PsiInvalidElementAccessException e) { return -1; } }
@Nullable public static <T extends PsiElement> T getOriginalElement(@NotNull T psi) { final PsiFile file = psi.getContainingFile(); if (file != null && file != file.getOriginalFile() && psi.getTextRange() != null) { TextRange range = psi.getTextRange(); Integer start = range.getStartOffset(); Integer end = range.getEndOffset(); final Document document = file.getViewProvider().getDocument(); if (document != null) { Document hostDocument = document instanceof DocumentWindowImpl ? ((DocumentWindowImpl) document).getDelegate() : document; OffsetTranslator translator = hostDocument.getUserData(RANGE_TRANSLATION); if (translator != null) { if (document instanceof DocumentWindowImpl) { start = ((DocumentWindowImpl) document).injectedToHost(start); end = ((DocumentWindowImpl) document).injectedToHost(end); } start = translator.translateOffset(start); end = translator.translateOffset(end); if (start == null || end == null) { return null; } if (document instanceof DocumentWindowImpl) { start = ((DocumentWindowImpl) document).hostToInjected(start); end = ((DocumentWindowImpl) document).hostToInjected(end); } } } //noinspection unchecked return (T) PsiTreeUtil.findElementOfClassAtRange(file.getOriginalFile(), start, end, psi.getClass()); } return psi; }
protected boolean isTestScope(@Nullable final PsiFile context) { if ((context == null)) { return false; } PsiFile _originalFile = context.getOriginalFile(); VirtualFile virtualFile = _originalFile.getVirtualFile(); if ((virtualFile != null)) { Project _project = context.getProject(); ProjectRootManager _instance = ProjectRootManager.getInstance(_project); ProjectFileIndex _fileIndex = _instance.getFileIndex(); return _fileIndex.isInTestSourceContent(virtualFile); } return false; }
@Override public ASTNode parseContents(ASTNode chameleon) { final CharTable table = SharedImplUtil.findCharTableByTree(chameleon); final FileElement treeElement = new DummyHolder(((TreeElement) chameleon).getManager(), null, table).getTreeElement(); final PsiFile file = (PsiFile) TreeUtil.getFileElement((TreeElement) chameleon).getPsi(); PsiFile originalFile = file.getOriginalFile(); final TemplateLanguageFileViewProvider viewProvider = (TemplateLanguageFileViewProvider) originalFile.getViewProvider(); final Language language = getTemplateFileLanguage(viewProvider); final CharSequence chars = chameleon.getChars(); final PsiFile templateFile = createTemplateFile(file, language, chars, viewProvider); final TreeElement parsed = ((PsiFileImpl) templateFile).calcTreeElement(); Lexer langLexer = LanguageParserDefinitions.INSTANCE.forLanguage(language).createLexer(file.getProject()); final Lexer lexer = new MergingLexerAdapter( new TemplateBlackAndWhiteLexer( createBaseLexer(viewProvider), langLexer, myTemplateElementType, myOuterElementType), TokenSet.create(myTemplateElementType, myOuterElementType)); lexer.start(chars); insertOuters(parsed, lexer, table); if (parsed != null) { final TreeElement element = parsed.getFirstChildNode(); if (element != null) { ((CompositeElement) parsed).rawRemoveAllChildren(); treeElement.rawAddChildren(element); } } treeElement.subtreeChanged(); TreeElement childNode = treeElement.getFirstChildNode(); DebugUtil.checkTreeStructure(parsed); DebugUtil.checkTreeStructure(treeElement); DebugUtil.checkTreeStructure(chameleon); DebugUtil.checkTreeStructure(file.getNode()); DebugUtil.checkTreeStructure(originalFile.getNode()); return childNode; }
@NotNull @Override public String getMissingElementNameSpace(@NotNull String prefix, boolean addLinkRef) { MultiMarkdownProjectComponent projectComponent = MultiMarkdownPlugin.getProjectComponent(getProject()); PsiFile psiFile = getContainingFile(); VirtualFile virtualFile = psiFile.getOriginalFile() != null ? psiFile.getOriginalFile().getVirtualFile() : psiFile.getVirtualFile(); PathInfo filePathInfo = new PathInfo(virtualFile); GitHubVcsRoot gitHubVcsRoot = projectComponent != null ? projectComponent.getGitHubRepo(filePathInfo.getPath()) : null; String vcsHome = gitHubVcsRoot != null ? gitHubVcsRoot.getBasePath() + "::" : ""; if (addLinkRef) { String pageRef = MultiMarkdownPsiImplUtil.getLinkRefTextWithAnchor(this); if (pageRef.isEmpty()) pageRef = WikiLinkRef.fileAsLink(filePathInfo.getFileName()); return prefix + (vcsHome.isEmpty() ? vcsHome : vcsHome + "::") + (pageRef.isEmpty() ? pageRef : pageRef + "::"); } return prefix + (vcsHome.isEmpty() ? vcsHome : vcsHome + "::"); }
/** * Finds a directory that many levels above a given file, making sure that every level has an * __init__.py. * * @param base file that works as a reference. * @param depth must be positive, 1 means the dir that contains base, 2 is one dir above, etc. * @return found directory, or null. */ @Nullable public static PsiDirectory stepBackFrom(PsiFile base, int depth) { if (depth == 0) { return base.getContainingDirectory(); } PsiDirectory result; if (base != null) { base = base.getOriginalFile(); // just to make sure result = base.getContainingDirectory(); int count = 1; while (result != null && PyUtil.isPackage(result, base)) { if (count >= depth) return result; result = result.getParentDirectory(); count += 1; } } return null; }
@NotNull private Collection<PsiFileSystemItem> getContextByFile(@NotNull PsiFile file) { final PsiElement context = file.getContext(); if (context != null) file = context.getContainingFile(); if (useIncludingFileAsContext()) { final FileContextProvider contextProvider = FileContextProvider.getProvider(file); if (contextProvider != null) { final Collection<PsiFileSystemItem> folders = contextProvider.getContextFolders(file); if (!folders.isEmpty()) { return folders; } final PsiFile contextFile = contextProvider.getContextFile(file); if (contextFile != null) { return Collections.<PsiFileSystemItem>singleton(contextFile.getParent()); } } } VirtualFile virtualFile = file.getOriginalFile().getVirtualFile(); if (virtualFile != null) { final FileReferenceHelper[] helpers = FileReferenceHelperRegistrar.getHelpers(); final ArrayList<PsiFileSystemItem> list = new ArrayList<PsiFileSystemItem>(); final Project project = file.getProject(); for (FileReferenceHelper helper : helpers) { if (helper.isMine(project, virtualFile)) { list.addAll(helper.getContexts(project, virtualFile)); } } if (list.size() > 0) { return list; } final VirtualFile parent = virtualFile.getParent(); if (parent != null) { final PsiDirectory directory = file.getManager().findDirectory(parent); if (directory != null) { return Collections.<PsiFileSystemItem>singleton(directory); } } } return Collections.emptyList(); }
public void processIncludingFiles( PsiFile context, Processor<Pair<VirtualFile, FileIncludeInfo>> processor) { context = context.getOriginalFile(); VirtualFile contextFile = context.getVirtualFile(); if (contextFile == null) return; MultiMap<VirtualFile, FileIncludeInfoImpl> infoList = FileIncludeIndex.getIncludingFileCandidates( context.getName(), GlobalSearchScope.allScope(myProject)); for (VirtualFile candidate : infoList.keySet()) { PsiFile psiFile = myPsiManager.findFile(candidate); if (psiFile == null || context.equals(psiFile)) continue; for (FileIncludeInfo info : infoList.get(candidate)) { PsiFileSystemItem item = resolveFileInclude(info, psiFile); if (item != null && contextFile.equals(item.getVirtualFile())) { if (!processor.process(Pair.create(candidate, info))) { return; } } } } }
private static PsiFile getContainingFile(final PsiElement elt) { PsiFile psiFile = elt.getContainingFile(); if (psiFile == null) return null; return psiFile.getOriginalFile(); }
@Nullable private static VirtualFile getVirtualFile(PsiFile psiFile) { if (psiFile == null) return null; psiFile = psiFile.getOriginalFile(); return psiFile.getVirtualFile(); }
public void setOriginalFile(@NotNull final PsiFile originalFile) { myOriginalFile = originalFile.getOriginalFile(); }