@Override public boolean arePackagesTheSame(@NotNull PsiElement element1, @NotNull PsiElement element2) { PsiFile file1 = FileContextUtil.getContextFile(element1); PsiFile file2 = FileContextUtil.getContextFile(element2); if (Comparing.equal(file1, file2)) return true; if (file1 instanceof JavaDummyHolder && file2 instanceof JavaDummyHolder) return true; if (file1 instanceof JavaDummyHolder || file2 instanceof JavaDummyHolder) { JavaDummyHolder dummyHolder = (JavaDummyHolder) (file1 instanceof JavaDummyHolder ? file1 : file2); PsiElement other = file1 instanceof JavaDummyHolder ? file2 : file1; return dummyHolder.isSamePackage(other); } if (!(file1 instanceof PsiClassOwner)) return false; if (!(file2 instanceof PsiClassOwner)) return false; String package1 = ((PsiClassOwner) file1).getPackageName(); String package2 = ((PsiClassOwner) file2).getPackageName(); return Comparing.equal(package1, package2); }
@NotNull public static PsiElement getContainer(PsiElement place) { PsiElement parent = place; while (true) { if (parent instanceof PsiMember && !(parent instanceof PsiTypeParameter)) { return parent; } if (parent instanceof PsiFile) { PsiElement host = FileContextUtil.getFileContext((PsiFile)parent); if (host == null) { return parent; } parent = host; } parent = parent.getParent(); } }
private static boolean cacheEverything(@NotNull Place place, @NotNull DocumentWindowImpl documentWindow, @NotNull InjectedFileViewProvider viewProvider, @NotNull PsiFile psiFile, @NotNull SmartPsiElementPointer<PsiLanguageInjectionHost> pointer) { FileDocumentManagerImpl.registerDocument(documentWindow, viewProvider.getVirtualFile()); DebugUtil.startPsiModification("MultiHostRegistrar cacheEverything"); try { viewProvider.forceCachedPsi(psiFile); } finally { DebugUtil.finishPsiModification(); } psiFile.putUserData(FileContextUtil.INJECTED_IN_ELEMENT, pointer); PsiDocumentManagerBase.cachePsi(documentWindow, psiFile); keepTreeFromChameleoningBack(psiFile); return viewProvider.setShreds(place, psiFile.getProject()); }
public DtdParsing(CharSequence chars, final IElementType type, final XmlEntityDecl.EntityContextType contextType, PsiFile contextFile ) { this( type, contextType, PsiBuilderFactory.getInstance().createBuilder( LanguageParserDefinitions.INSTANCE.forLanguage(DTDLanguage.INSTANCE), new DtdLexer(false) { final int myInitialState = getLexerInitialState(type, contextType); @Override public void start(@NotNull CharSequence buffer, int startOffset, int endOffset, int initialState) { super.start(buffer, startOffset, endOffset, myInitialState); } }, chars ) ); if (contextFile != null) myBuilder.putUserDataUnprotected(FileContextUtil.CONTAINING_FILE_KEY, contextFile); }
private void commitToOriginal() { if (!isValid()) return; final PsiFile origFile = (PsiFile)myNewFile.getUserData(FileContextUtil.INJECTED_IN_ELEMENT).getElement(); VirtualFile origFileVirtualFile = origFile != null? origFile.getVirtualFile() : null; myCommittingToOriginal = true; try { if (origFileVirtualFile == null || !ReadonlyStatusHandler.getInstance(myProject).ensureFilesWritable(origFileVirtualFile).hasReadonlyFiles()) { PostprocessReformattingAspect.getInstance(myProject).disablePostprocessFormattingInside(new Runnable() { @Override public void run() { if (myAltFullRange != null) { altCommitToOriginal(); return; } commitToOriginalInner(); } }); } } finally { myCommittingToOriginal = false; } }
private static boolean cacheEverything(@NotNull Place place, @NotNull DocumentWindowImpl documentWindow, @NotNull InjectedFileViewProvider viewProvider, @NotNull PsiFile psiFile, @NotNull SmartPsiElementPointer<PsiLanguageInjectionHost> pointer) { FileDocumentManagerImpl.registerDocument(documentWindow, viewProvider.getVirtualFile()); viewProvider.forceCachedPsi(psiFile); psiFile.putUserData(FileContextUtil.INJECTED_IN_ELEMENT, pointer); PsiDocumentManagerImpl.cachePsi(documentWindow, psiFile); keepTreeFromChameleoningBack(psiFile); return viewProvider.setShreds(place, psiFile.getProject()); }
public DtdParsing(CharSequence chars, final IElementType type, final XmlEntityDecl.EntityContextType contextType, PsiFile contextFile ) { this( type, contextType, PsiBuilderFactory.getInstance().createBuilder( LanguageParserDefinitions.INSTANCE.forLanguage(DTDLanguage.INSTANCE), new DtdLexer(false) { final int myInitialState = getLexerInitialState(type, contextType); @Override public void start(CharSequence buffer, int startOffset, int endOffset, int initialState) { super.start(buffer, startOffset, endOffset, myInitialState); } }, chars ) ); if (contextFile != null) myBuilder.putUserDataUnprotected(FileContextUtil.CONTAINING_FILE_KEY, contextFile); }
@Override public ASTNode parse(IElementType t, PsiBuilder b) { if (debugging) { HaxeDebugTimeLog timeLog = null; // The file is attached to the user data. :/ I suspect this was a hack, but it's what we've got available. PsiFile file = b.getUserDataUnprotected(FileContextUtil.CONTAINING_FILE_KEY); String description = null != file ? file.getName() : t.toString(); timeLog = HaxeDebugTimeLog.startNew("HaxeParser " + description, HaxeDebugTimeLog.Since.StartAndPrevious); ASTNode node = super.parse(t, b); timeLog.stamp("Finished parsing."); timeLog.printIfTimeExceeds(20 /* milliseconds */); return node; } return super.parse(t,b); }
public DtdParsing(CharSequence chars, final IElementType type, final XmlEntityDecl.EntityContextType contextType, PsiFile contextFile ) { this( type, contextType, PsiBuilderFactory.getInstance().createBuilder( LanguageParserDefinitions.INSTANCE.forLanguage(DTDLanguage.INSTANCE), new DtdLexer(false) { final int myInitialState = getLexerInitialState(type, contextType); @Override public void start(CharSequence buffer, int startOffset, int endOffset, int initialState) { super.start(buffer, startOffset, endOffset, myInitialState); } }, LanguageVersionUtil.findDefaultVersion(DTDLanguage.INSTANCE), chars ) ); if (contextFile != null) myBuilder.putUserDataUnprotected(FileContextUtil.CONTAINING_FILE_KEY, contextFile); }
public static VirtualFile toVirtualFile( PsiFile file ) { VirtualFile vfile = file.getUserData( IndexingDataKeys.VIRTUAL_FILE ); if( vfile == null ) { vfile = file.getVirtualFile(); if( vfile == null ) { vfile = file.getOriginalFile().getVirtualFile(); if( vfile == null ) { vfile = file.getViewProvider().getVirtualFile(); } } else if( vfile instanceof LightVirtualFile ) { PsiFile containingFile = file.getContainingFile(); if( containingFile != null && containingFile != file ) { PsiFile originalFile = containingFile.getOriginalFile(); SmartPsiElementPointer owningFile = originalFile.getUserData( FileContextUtil.INJECTED_IN_ELEMENT ); if( owningFile != null ) { vfile = owningFile.getVirtualFile(); } } } } return vfile; }
@Override public boolean isInPackage(@NotNull PsiElement element, @NotNull PsiPackage aPackage) { final PsiFile file = FileContextUtil.getContextFile(element); if (file instanceof JavaDummyHolder) { return ((JavaDummyHolder) file).isInPackage(aPackage); } if (file instanceof PsiJavaFile) { final String packageName = ((PsiJavaFile) file).getPackageName(); return packageName.equals(aPackage.getQualifiedName()); } return false; }
@Override public <T> void putUserDataUnprotected(@NotNull final Key<T> key, @Nullable final T value) { if (key == FileContextUtil.CONTAINING_FILE_KEY) { myFile = (PsiFile)value; return; } if (myUserData == null) myUserData = ContainerUtil.newHashMap(); myUserData.put(key, value); }
public static void initState(ErrorState state, PsiBuilder builder, IElementType root, TokenSet[] extendsSets) { state.extendsSets = extendsSets; PsiFile file = builder.getUserDataUnprotected(FileContextUtil.CONTAINING_FILE_KEY); state.completionState = file == null? null: file.getUserData(COMPLETION_STATE_KEY); Language language = file == null? root.getLanguage() : file.getLanguage(); state.caseSensitive = language.isCaseSensitive(); PairedBraceMatcher matcher = LanguageBraceMatching.INSTANCE.forLanguage(language); state.braces = matcher == null ? null : matcher.getPairs(); if (state.braces != null && state.braces.length == 0) state.braces = null; }
@Override public PsiLanguageInjectionHost getInjectionHost(@NotNull PsiElement element) { final PsiFile file = element.getContainingFile(); final VirtualFile virtualFile = file == null ? null : file.getVirtualFile(); if (virtualFile instanceof VirtualFileWindow) { PsiElement host = FileContextUtil.getFileContext(file); // use utility method in case the file's overridden getContext() if (host instanceof PsiLanguageInjectionHost) { return (PsiLanguageInjectionHost)host; } } return null; }
@Nullable public static PsiDirectory getDirectory(@Nullable PsiElement element) { if (element == null) return null; // handle injection and fragment editor PsiFile file = FileContextUtil.getContextFile(element); return file == null ? null : file.getContainingDirectory(); }
private void getBindings() { final PsiClass containingClass = PsiTreeUtil.getParentOfType(myRefExpr, PsiClass.class); if (containingClass != null) return; final PsiFile file = FileContextUtil.getContextFile(myRefExpr); if (file instanceof GroovyFile) { ((GroovyFile)file).accept(new GroovyRecursiveElementVisitor() { @Override public void visitAssignmentExpression(GrAssignmentExpression expression) { super.visitAssignmentExpression(expression); final GrExpression value = expression.getLValue(); if (value instanceof GrReferenceExpression && !((GrReferenceExpression)value).isQualified()) { final PsiElement resolved = ((GrReferenceExpression)value).resolve(); if (resolved instanceof GrBindingVariable) { myProcessor.execute(resolved, ResolveState.initial()); } else if (resolved == null) { myProcessor.execute(new GrBindingVariable((GroovyFile)file, ((GrReferenceExpression)value).getReferenceName(), true), ResolveState.initial()); } } } @Override public void visitTypeDefinition(GrTypeDefinition typeDefinition) { //don't go into classes } }); } }
@Override public ASTNode parse(IElementType root, PsiBuilder builder) { if (debug.getValue()) { System.err.println(builder.getUserDataUnprotected(FileContextUtil.CONTAINING_FILE_KEY)); } PsiBuilder.Marker rootMarker = builder.mark(); ParsingContext context = new ParsingContext(builder); context.statementParser.parseFileInput(); rootMarker.done(root); return builder.getTreeBuilt(); }
private static void initState(ErrorState state, PsiBuilder builder, IElementType root, TokenSet[] extendsSets) { state.extendsSets = extendsSets; PsiFile file = builder.getUserDataUnprotected(FileContextUtil.CONTAINING_FILE_KEY); state.completionState = file == null? null: file.getUserData(COMPLETION_STATE_KEY); Language language = file == null? root.getLanguage() : file.getLanguage(); state.caseSensitive = language.isCaseSensitive(); PairedBraceMatcher matcher = LanguageBraceMatching.INSTANCE.forLanguage(language); state.braces = matcher == null ? null : matcher.getPairs(); if (state.braces != null && state.braces.length == 0) state.braces = null; }
@Override @NotNull public GlobalSearchScope getResolveScope(@NotNull PsiElement element) { ProgressIndicatorProvider.checkCanceled(); VirtualFile vFile; final PsiFile contextFile; if (element instanceof PsiDirectory) { vFile = ((PsiDirectory)element).getVirtualFile(); contextFile = null; } else { final PsiFile containingFile = element.getContainingFile(); if (containingFile instanceof PsiCodeFragment) { final GlobalSearchScope forcedScope = ((PsiCodeFragment)containingFile).getForcedResolveScope(); if (forcedScope != null) { return forcedScope; } final PsiElement context = containingFile.getContext(); if (context == null) { return GlobalSearchScope.allScope(myProject); } return getResolveScope(context); } contextFile = containingFile != null ? FileContextUtil.getContextFile(containingFile) : null; if (contextFile == null) { return GlobalSearchScope.allScope(myProject); } else if (contextFile instanceof FileResolveScopeProvider) { return ((FileResolveScopeProvider) contextFile).getFileResolveScope(); } vFile = contextFile.getOriginalFile().getVirtualFile(); } if (vFile == null || contextFile == null) { return GlobalSearchScope.allScope(myProject); } return getDefaultResolveScope(contextFile, vFile); }
private static void initState(IElementType root, PsiBuilder builder, ErrorState state) { PsiFile file = builder.getUserDataUnprotected(FileContextUtil.CONTAINING_FILE_KEY); state.completionState = file == null? null: file.getUserData(COMPLETION_STATE_KEY); Language language = file == null? root.getLanguage() : file.getLanguage(); state.caseSensitive = language.isCaseSensitive(); ParserDefinition parserDefinition = LanguageParserDefinitions.INSTANCE.forLanguage(language); if (parserDefinition != null) { state.commentTokens = parserDefinition.getCommentTokens(); state.whitespaceTokens = parserDefinition.getWhitespaceTokens(); } PairedBraceMatcher matcher = LanguageBraceMatching.INSTANCE.forLanguage(language); state.braces = matcher == null ? null : matcher.getPairs(); if (state.braces != null && state.braces.length == 0) state.braces = null; }
@Override @Nonnull public GlobalSearchScope getResolveScope(@Nonnull PsiElement element) { ProgressIndicatorProvider.checkCanceled(); VirtualFile vFile; final PsiFile contextFile; if (element instanceof PsiDirectory) { vFile = ((PsiDirectory)element).getVirtualFile(); contextFile = null; } else { final PsiFile containingFile = element.getContainingFile(); if (containingFile instanceof PsiCodeFragment) { final GlobalSearchScope forcedScope = ((PsiCodeFragment)containingFile).getForcedResolveScope(); if (forcedScope != null) { return forcedScope; } final PsiElement context = containingFile.getContext(); if (context == null) { return GlobalSearchScope.allScope(myProject); } return getResolveScope(context); } contextFile = containingFile != null ? FileContextUtil.getContextFile(containingFile) : null; if (contextFile == null) { return GlobalSearchScope.allScope(myProject); } else if (contextFile instanceof FileResolveScopeProvider) { return ((FileResolveScopeProvider)contextFile).getFileResolveScope(); } vFile = contextFile.getOriginalFile().getVirtualFile(); } if (vFile == null || contextFile == null) { return GlobalSearchScope.allScope(myProject); } return myDefaultResolveScopesCache.get(vFile); }
@Override public <T> void putUserDataUnprotected(@Nonnull final Key<T> key, @Nullable final T value) { if (key == FileContextUtil.CONTAINING_FILE_KEY) { myFile = (PsiFile)value; return; } if (myUserData == null) myUserData = ContainerUtil.newHashMap(); myUserData.put(key, value); }
@Override public PsiLanguageInjectionHost getInjectionHost(@Nonnull PsiElement element) { final PsiFile file = element.getContainingFile(); final VirtualFile virtualFile = file == null ? null : file.getVirtualFile(); if (virtualFile instanceof VirtualFileWindow) { PsiElement host = FileContextUtil.getFileContext(file); // use utility method in case the file's overridden getContext() if (host instanceof PsiLanguageInjectionHost) { return (PsiLanguageInjectionHost)host; } } return null; }
@Override public boolean isInPackage(@NotNull PsiElement element, @NotNull PsiJavaPackage aPackage) { final PsiFile file = FileContextUtil.getContextFile(element); if(file instanceof JavaDummyHolder) { return ((JavaDummyHolder) file).isInPackage(aPackage); } if(file instanceof PsiJavaFile) { final String packageName = ((PsiJavaFile) file).getPackageName(); return packageName.equals(aPackage.getQualifiedName()); } return false; }
@Override public boolean arePackagesTheSame(@NotNull PsiElement element1, @NotNull PsiElement element2) { PsiFile file1 = FileContextUtil.getContextFile(element1); PsiFile file2 = FileContextUtil.getContextFile(element2); if(Comparing.equal(file1, file2)) { return true; } if(file1 instanceof JavaDummyHolder && file2 instanceof JavaDummyHolder) { return true; } if(file1 instanceof JavaDummyHolder || file2 instanceof JavaDummyHolder) { JavaDummyHolder dummyHolder = (JavaDummyHolder) (file1 instanceof JavaDummyHolder ? file1 : file2); PsiElement other = file1 instanceof JavaDummyHolder ? file2 : file1; return dummyHolder.isSamePackage(other); } if(!(file1 instanceof PsiClassOwner)) { return false; } if(!(file2 instanceof PsiClassOwner)) { return false; } String package1 = ((PsiClassOwner) file1).getPackageName(); String package2 = ((PsiClassOwner) file2).getPackageName(); return Comparing.equal(package1, package2); }