com.intellij.injected.editor.VirtualFileWindow Java Examples

The following examples show how to use com.intellij.injected.editor.VirtualFileWindow. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: InjectedLanguageUtil.java    From consulo with Apache License 2.0 6 votes vote down vote up
public static Editor openEditorFor(@Nonnull PsiFile file, @Nonnull Project project) {
  Document document = PsiDocumentManager.getInstance(project).getDocument(file);
  // may return editor injected in current selection in the host editor, not for the file passed as argument
  VirtualFile virtualFile = file.getVirtualFile();
  if (virtualFile == null) {
    return null;
  }
  if (virtualFile instanceof VirtualFileWindow) {
    virtualFile = ((VirtualFileWindow)virtualFile).getDelegate();
  }
  Editor editor = FileEditorManager.getInstance(project).openTextEditor(new OpenFileDescriptor(project, virtualFile, -1), false);
  if (editor == null || editor instanceof EditorWindow || editor.isDisposed()) return editor;
  if (document instanceof DocumentWindowImpl) {
    return EditorWindowImpl.create((DocumentWindowImpl)document, (DesktopEditorImpl)editor, file);
  }
  return editor;
}
 
Example #2
Source File: InjectedLanguageUtil.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Nullable
public static PsiLanguageInjectionHost findInjectionHost(@Nullable PsiElement psi) {
  if (psi == null) return null;
  PsiFile containingFile = psi.getContainingFile().getOriginalFile();              // * formatting
  PsiElement fileContext = containingFile.getContext();                            // * quick-edit-handler
  if (fileContext instanceof PsiLanguageInjectionHost) return (PsiLanguageInjectionHost)fileContext;
  Place shreds = getShreds(containingFile.getViewProvider()); // * injection-registrar
  if (shreds == null) {
    VirtualFile virtualFile = PsiUtilCore.getVirtualFile(containingFile);
    if (virtualFile instanceof LightVirtualFile) {
      virtualFile = ((LightVirtualFile)virtualFile).getOriginalFile();             // * dynamic files-from-text
    }
    if (virtualFile instanceof VirtualFileWindow) {
      shreds = getShreds(((VirtualFileWindow)virtualFile).getDocumentWindow());
    }
  }
  return shreds != null ? shreds.getHostPointer().getElement() : null;
}
 
Example #3
Source File: InjectedLanguageUtil.java    From consulo with Apache License 2.0 6 votes vote down vote up
public static BooleanRunnable reparse(@Nonnull PsiFile injectedPsiFile,
                                      @Nonnull DocumentWindow injectedDocument,
                                      @Nonnull PsiFile hostPsiFile,
                                      @Nonnull Document hostDocument,
                                      @Nonnull FileViewProvider hostViewProvider,
                                      @Nonnull ProgressIndicator indicator,
                                      @Nonnull ASTNode oldRoot,
                                      @Nonnull ASTNode newRoot,
                                      @Nonnull PsiDocumentManagerBase documentManager) {
  LanguageVersion languageVersion = injectedPsiFile.getLanguageVersion();
  InjectedFileViewProvider provider = (InjectedFileViewProvider)injectedPsiFile.getViewProvider();
  VirtualFile oldInjectedVFile = provider.getVirtualFile();
  VirtualFile hostVirtualFile = hostViewProvider.getVirtualFile();
  BooleanRunnable runnable = InjectionRegistrarImpl
          .reparse(languageVersion, (DocumentWindowImpl)injectedDocument, injectedPsiFile, (VirtualFileWindow)oldInjectedVFile, hostVirtualFile, hostPsiFile, (DocumentEx)hostDocument, indicator, oldRoot,
                   newRoot, documentManager);
  if (runnable == null) {
    EditorWindowImpl.disposeEditorFor(injectedDocument);
  }
  return runnable;
}
 
Example #4
Source File: DirectoryGroupingRule.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Override
@javax.annotation.Nullable
public UsageGroup groupUsage(@Nonnull Usage usage) {
  if (usage instanceof UsageInFile) {
    UsageInFile usageInFile = (UsageInFile)usage;
    VirtualFile file = usageInFile.getFile();
    if (file != null) {
      if (file instanceof VirtualFileWindow) {
        file = ((VirtualFileWindow)file).getDelegate();
      }
      VirtualFile dir = file.getParent();
      if (dir == null) return null;
      return getGroupForFile(dir);
    }
  }
  return null;
}
 
Example #5
Source File: FileEditorManagerImpl.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Override
@Nonnull
public FileEditor[] getEditors(@Nonnull VirtualFile file) {
  assertReadAccess();
  if (file instanceof VirtualFileWindow) file = ((VirtualFileWindow)file).getDelegate();

  final EditorWithProviderComposite composite = getCurrentEditorWithProviderComposite(file);
  if (composite != null) {
    return composite.getEditors();
  }

  final List<EditorWithProviderComposite> composites = getEditorComposites(file);
  if (!composites.isEmpty()) {
    return composites.get(0).getEditors();
  }
  return EMPTY_EDITOR_ARRAY;
}
 
Example #6
Source File: VirtualFileWindowImpl.java    From consulo with Apache License 2.0 5 votes vote down vote up
VirtualFileWindowImpl(@Nonnull String name, @Nonnull VirtualFile delegate, @Nonnull DocumentWindowImpl window, @Nonnull Language language, @Nonnull CharSequence text) {
  super(name, language, text);
  setCharset(delegate.getCharset());
  setFileType(language.getAssociatedFileType());
  if (delegate instanceof VirtualFileWindow) throw new IllegalArgumentException(delegate + " must not be injected");
  myDelegate = delegate;
  myDocumentWindow = window;
}
 
Example #7
Source File: InjectedLanguageUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nullable
public static DocumentWindow getDocumentWindow(@Nonnull PsiElement element) {
  PsiFile file = element.getContainingFile();
  if (file == null) return null;
  VirtualFile virtualFile = file.getVirtualFile();
  if (virtualFile instanceof VirtualFileWindow) return ((VirtualFileWindow)virtualFile).getDocumentWindow();
  return null;
}
 
Example #8
Source File: InjectedLanguageManagerImpl.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public PsiLanguageInjectionHost getInjectionHost(@Nonnull PsiElement injectedElement) {
  final PsiFile file = injectedElement.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;
}
 
Example #9
Source File: MultipleRootsInjectedFileViewProvider.java    From consulo with Apache License 2.0 5 votes vote down vote up
MultipleRootsInjectedFileViewProvider(@Nonnull PsiManager psiManager,
                                      @Nonnull VirtualFileWindow virtualFile,
                                      @Nonnull DocumentWindowImpl documentWindow,
                                      @Nonnull Language language,
                                      @Nonnull AbstractFileViewProvider original) {
  super(psiManager, (VirtualFile)virtualFile, true);
  myDocumentWindow = documentWindow;
  myLanguage = language;
  myOriginalProvider = original;
}
 
Example #10
Source File: IndexTodoCacheManagerImpl.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public int getTodoCount(@Nonnull final VirtualFile file, @Nonnull final IndexPattern pattern) {
  if (myProject.isDefault()) {
    return 0;
  }
  if (file instanceof VirtualFileWindow) return -1;
  return fetchCount(FileBasedIndex.getInstance(), file, pattern);
}
 
Example #11
Source File: IndexTodoCacheManagerImpl.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public int getTodoCount(@Nonnull final VirtualFile file, @Nonnull final IndexPatternProvider patternProvider) {
  if (myProject.isDefault()) {
    return 0;
  }
  if (file instanceof VirtualFileWindow) return -1;
  final FileBasedIndex fileBasedIndex = FileBasedIndex.getInstance();
  return Arrays.stream(patternProvider.getIndexPatterns()).mapToInt(indexPattern -> fetchCount(fileBasedIndex, file, indexPattern)).sum();
}
 
Example #12
Source File: PsiEventWrapperAspect.java    From consulo with Apache License 2.0 5 votes vote down vote up
private static void promoteNonPhysicalChangesToDocument(ASTNode rootElement, PsiFile file) {
  if (file instanceof DummyHolder) return;
  if (((PsiDocumentManagerImpl)PsiDocumentManager.getInstance(file.getProject())).isCommitInProgress()) return;

  VirtualFile vFile = file.getViewProvider().getVirtualFile();
  if (vFile instanceof LightVirtualFile && !(vFile instanceof VirtualFileWindow)) {
    Document document = FileDocumentManager.getInstance().getCachedDocument(vFile);
    if (document != null) {
      CharSequence text = rootElement.getChars();
      PsiToDocumentSynchronizer.performAtomically(file, () -> document.replaceString(0, document.getTextLength(), text));
    }
  }
}
 
Example #13
Source File: InspectionResultsView.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nullable
private Navigatable getSelectedNavigatable(final CommonProblemDescriptor descriptor, final PsiElement psiElement) {
  if (descriptor instanceof ProblemDescriptorBase) {
    Navigatable navigatable = ((ProblemDescriptorBase)descriptor).getNavigatable();
    if (navigatable != null) {
      return navigatable;
    }
  }
  if (psiElement == null || !psiElement.isValid()) return null;
  PsiFile containingFile = psiElement.getContainingFile();
  VirtualFile virtualFile = containingFile == null ? null : containingFile.getVirtualFile();

  if (virtualFile != null) {
    int startOffset = psiElement.getTextOffset();
    if (descriptor instanceof ProblemDescriptorBase) {
      final TextRange textRange = ((ProblemDescriptorBase)descriptor).getTextRangeForNavigation();
      if (textRange != null) {
        if (virtualFile instanceof VirtualFileWindow) {
          virtualFile = ((VirtualFileWindow)virtualFile).getDelegate();
        }
        startOffset = textRange.getStartOffset();
      }
    }
    return new OpenFileDescriptor(myProject, virtualFile, startOffset);
  }
  return null;
}
 
Example #14
Source File: EmptyFileManager.java    From consulo with Apache License 2.0 5 votes vote down vote up
@RequiredReadAction
@Override
public void setViewProvider(@Nonnull final VirtualFile virtualFile, final FileViewProvider singleRootFileViewProvider) {
  if (!(virtualFile instanceof VirtualFileWindow)) {
    if (singleRootFileViewProvider == null) {
      myVFileToViewProviderMap.remove(virtualFile);
    }
    else {
      myVFileToViewProviderMap.put(virtualFile, singleRootFileViewProvider);
    }
  }
}
 
Example #15
Source File: ProjectScopeImpl.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public boolean contains(@Nonnull VirtualFile file) {
  if (file instanceof VirtualFileWindow) return true;

  if (myFileIndex.isInLibraryClasses(file) && !myFileIndex.isInSourceContent(file)) return false;

  return myFileIndex.isInContent(file);
}
 
Example #16
Source File: AbstractFileViewProvider.java    From consulo with Apache License 2.0 5 votes vote down vote up
protected AbstractFileViewProvider(@Nonnull PsiManager manager, @Nonnull VirtualFile virtualFile, boolean eventSystemEnabled) {
  myManager = (PsiManagerEx)manager;
  myVirtualFile = virtualFile;
  myEventSystemEnabled = eventSystemEnabled;
  setContent(new VirtualFileContent());
  myPhysical = isEventSystemEnabled() && !(virtualFile instanceof LightVirtualFile) && !(virtualFile.getFileSystem() instanceof NonPhysicalFileSystem);
  virtualFile.putUserData(FREE_THREADED, isFreeThreaded(this));
  if (virtualFile instanceof VirtualFileWindow && !(this instanceof FreeThreadedFileViewProvider) && !isFreeThreaded(this)) {
    throw new IllegalArgumentException("Must not create " + getClass() + " for injected file " + virtualFile + "; InjectedFileViewProvider must be used instead");
  }
}
 
Example #17
Source File: FileEditorManagerImpl.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
@Nullable
public FileEditorWithProvider getSelectedEditorWithProvider(@Nonnull VirtualFile file) {
  if (file instanceof VirtualFileWindow) file = ((VirtualFileWindow)file).getDelegate();
  final EditorWithProviderComposite composite = getCurrentEditorWithProviderComposite(file);
  if (composite != null) {
    return composite.getSelectedEditorWithProvider();
  }

  final List<EditorWithProviderComposite> composites = getEditorComposites(file);
  return composites.isEmpty() ? null : composites.get(0).getSelectedEditorWithProvider();
}
 
Example #18
Source File: AstLoadingFilter.java    From consulo with Apache License 2.0 5 votes vote down vote up
public static void assertTreeLoadingAllowed(@Nonnull VirtualFile file) {
  if (!Registry.is("ast.loading.filter")) return;
  if (file instanceof VirtualFileWindow) return;
  Supplier<String> disallowedInfo = myDisallowedInfo.get();
  if (disallowedInfo == null) {
    // loading was not disabled in current thread
  }
  else if (myForcedAllowedFiles.get().contains(file)) {
    // loading was disabled but then re-enabled for file
  }
  else {
    AstLoadingException throwable = new AstLoadingException();
    ourErrorLogger.error("Tree access disabled", throwable, AttachmentFactory.get().create("debugInfo", buildDebugInfo(file, disallowedInfo)));
  }
}
 
Example #19
Source File: LanguageSubstitutors.java    From consulo with Apache License 2.0 5 votes vote down vote up
private static void processLanguageSubstitution(@Nonnull final VirtualFile file,
                                                @Nonnull Language originalLang,
                                                @Nonnull final Language substitutedLang) {
  if (file instanceof VirtualFileWindow) {
    // Injected files are created with substituted language, no need to reparse:
    //   com.intellij.psi.impl.source.tree.injected.MultiHostRegistrarImpl#doneInjecting
    return;
  }
  Language prevSubstitutedLang = SUBSTITUTED_LANG_KEY.get(file);
  final Language prevLang = ObjectUtil.notNull(prevSubstitutedLang, originalLang);
  if (!prevLang.is(substitutedLang)) {
    if (file.replace(SUBSTITUTED_LANG_KEY, prevSubstitutedLang, substitutedLang)) {
      if (prevSubstitutedLang == null) {
        return; // no need to reparse for the first language substitution
      }
      if (ApplicationManager.getApplication().isUnitTestMode()) {
        return;
      }
      file.putUserData(REPARSING_SCHEDULED, true);
      ApplicationManager.getApplication().invokeLater(new Runnable() {
        @Override
        public void run() {
          if (file.replace(REPARSING_SCHEDULED, true, null)) {
            LOG.info("Reparsing " + file.getPath() + " because of language substitution " +
                     prevLang.getID() + "->" + substitutedLang.getID());
            FileContentUtilCore.reparseFiles(file);
          }
        }
      }, ModalityState.defaultModalityState());
    }
  }
}
 
Example #20
Source File: UsageInfo.java    From consulo with Apache License 2.0 5 votes vote down vote up
private Pair<VirtualFile, Integer> offset() {
  VirtualFile containingFile0 = getVirtualFile();
  int shift0 = 0;
  if (containingFile0 instanceof VirtualFileWindow) {
    shift0 = ((VirtualFileWindow)containingFile0).getDocumentWindow().injectedToHost(0);
    containingFile0 = ((VirtualFileWindow)containingFile0).getDelegate();
  }
  Segment range = myPsiFileRange == null ? mySmartPointer.getPsiRange() : myPsiFileRange.getPsiRange();
  if (range == null) return null;
  return Pair.create(containingFile0, range.getStartOffset() + shift0);
}
 
Example #21
Source File: UsageInfo2UsageAdapter.java    From consulo with Apache License 2.0 5 votes vote down vote up
private OpenFileDescriptor getDescriptor() {
  VirtualFile file = getFile();
  if (file == null) return null;
  Segment range = getNavigationRange();
  if (range != null && file instanceof VirtualFileWindow && range.getStartOffset() >= 0) {
    // have to use injectedToHost(TextRange) to calculate right offset in case of multiple shreds
    range = ((VirtualFileWindow)file).getDocumentWindow().injectedToHost(TextRange.create(range));
    file = ((VirtualFileWindow)file).getDelegate();
  }
  return new OpenFileDescriptor(getProject(), file, range == null ? getNavigationOffset() : range.getStartOffset());
}
 
Example #22
Source File: FileTreeAccessFilter.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public boolean accept(VirtualFile file) {
  if (file instanceof VirtualFileWindow) return false;

  if (myAddedClasses.contains(file) || myTreeAccessAllowed) return false;

  FileType fileType = file.getFileType();
  return (fileType == InternalStdFileTypes.JAVA || fileType == InternalStdFileTypes.CLASS) && !file.getName().equals("package-info.java");
}
 
Example #23
Source File: ProjectFileIndexImpl.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nullable
@Override
public Module getModuleForFile(@Nonnull VirtualFile file, boolean honorExclusion) {
  if (file instanceof VirtualFileWindow) file = ((VirtualFileWindow)file).getDelegate();
  DirectoryInfo info = getInfoForFileOrDirectory(file);
  if (info.isInProject(file) || !honorExclusion && info.isExcluded(file)) {
    return info.getModule();
  }
  return null;
}
 
Example #24
Source File: FileIndexBase.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nonnull
public DirectoryInfo getInfoForFileOrDirectory(@Nonnull VirtualFile file) {
  if (file instanceof VirtualFileWindow) {
    file = ((VirtualFileWindow)file).getDelegate();
  }
  return myDirectoryIndex.getInfoForFile(file);
}
 
Example #25
Source File: HaxeImportOptimizer.java    From intellij-haxe with Apache License 2.0 5 votes vote down vote up
@NotNull
@Override
public Runnable processFile(final PsiFile file) {
  VirtualFile vFile = file.getVirtualFile();
  if (vFile instanceof VirtualFileWindow) vFile = ((VirtualFileWindow)vFile).getDelegate();
  if (vFile == null || !ProjectRootManager.getInstance(file.getProject()).getFileIndex().isInSourceContent(vFile)) {
    return EmptyRunnable.INSTANCE;
  }

  return () -> optimizeImports(file);
}
 
Example #26
Source File: GraphQLPsiSearchHelper.java    From js-graphql-intellij-plugin with MIT License 5 votes vote down vote up
/**
 * Gets the virtual file system path of a PSI file
 */
public static String getFileName(PsiFile psiFile) {
    VirtualFile virtualFile = getVirtualFile(psiFile);
    if (virtualFile != null) {
        while (virtualFile instanceof VirtualFileWindow) {
            // injected virtual files
            virtualFile = ((VirtualFileWindow) virtualFile).getDelegate();
        }
        return virtualFile.getPath();
    }
    return psiFile.getName();
}
 
Example #27
Source File: InplaceRefactoring.java    From consulo with Apache License 2.0 4 votes vote down vote up
protected static VirtualFile getTopLevelVirtualFile(final FileViewProvider fileViewProvider) {
  VirtualFile file = fileViewProvider.getVirtualFile();
  if (file instanceof VirtualFileWindow) file = ((VirtualFileWindow)file).getDelegate();
  return file;
}
 
Example #28
Source File: LanguagePerFileConfigurable.java    From consulo with Apache License 2.0 4 votes vote down vote up
public void selectFile(@Nonnull VirtualFile virtualFile) {
  myTreeView.select(virtualFile instanceof VirtualFileWindow? ((VirtualFileWindow)virtualFile).getDelegate() : virtualFile);
}
 
Example #29
Source File: AbstractProjectViewPane.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Nonnull
public PsiDirectory[] getSelectedDirectories() {
  List<PsiDirectory> directories = ContainerUtil.newArrayList();
  for (PsiDirectoryNode node : getSelectedNodes(PsiDirectoryNode.class)) {
    PsiDirectory directory = node.getValue();
    if (directory != null) {
      directories.add(directory);
      Object parentValue = node.getParent().getValue();
      if (parentValue instanceof PsiDirectory && Registry.is("projectView.choose.directory.on.compacted.middle.packages")) {
        while (true) {
          directory = directory.getParentDirectory();
          if (directory == null || directory.equals(parentValue)) {
            break;
          }
          directories.add(directory);
        }
      }
    }
  }
  if (!directories.isEmpty()) {
    return directories.toArray(PsiDirectory.EMPTY_ARRAY);
  }

  final PsiElement[] elements = getSelectedPSIElements();
  if (elements.length == 1) {
    final PsiElement element = elements[0];
    if (element instanceof PsiDirectory) {
      return new PsiDirectory[]{(PsiDirectory)element};
    }
    else if (element instanceof PsiDirectoryContainer) {
      return ((PsiDirectoryContainer)element).getDirectories();
    }
    else {
      final PsiFile containingFile = element.getContainingFile();
      if (containingFile != null) {
        final PsiDirectory psiDirectory = containingFile.getContainingDirectory();
        if (psiDirectory != null) {
          return new PsiDirectory[]{psiDirectory};
        }
        final VirtualFile file = containingFile.getVirtualFile();
        if (file instanceof VirtualFileWindow) {
          final VirtualFile delegate = ((VirtualFileWindow)file).getDelegate();
          final PsiFile delegatePsiFile = containingFile.getManager().findFile(delegate);
          if (delegatePsiFile != null && delegatePsiFile.getContainingDirectory() != null) {
            return new PsiDirectory[]{delegatePsiFile.getContainingDirectory()};
          }
        }
        return PsiDirectory.EMPTY_ARRAY;
      }
    }
  }
  else {
    TreePath path = getSelectedPath();
    if (path != null) {
      Object component = path.getLastPathComponent();
      if (component instanceof DefaultMutableTreeNode) {
        return getSelectedDirectoriesInAmbiguousCase(((DefaultMutableTreeNode)component).getUserObject());
      }
      return getSelectedDirectoriesInAmbiguousCase(component);
    }
  }
  return PsiDirectory.EMPTY_ARRAY;
}
 
Example #30
Source File: InjectedLanguageUtil.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Nullable
public static PsiLanguageInjectionHost findInjectionHost(@Nullable VirtualFile virtualFile) {
  return virtualFile instanceof VirtualFileWindow ? getShreds(((VirtualFileWindow)virtualFile).getDocumentWindow()).getHostPointer().getElement() : null;
}