Java Code Examples for com.intellij.injected.editor.VirtualFileWindow

The following examples show how to use com.intellij.injected.editor.VirtualFileWindow. These examples are extracted from open source projects. 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 Project: consulo   Source File: DirectoryGroupingRule.java    License: 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 2
Source Project: consulo   Source File: FileEditorManagerImpl.java    License: 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 3
Source Project: consulo   Source File: InjectedLanguageUtil.java    License: 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 4
Source Project: consulo   Source File: InjectedLanguageUtil.java    License: 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 5
Source Project: consulo   Source File: InjectedLanguageUtil.java    License: 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 6
/**
 * 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 7
Source Project: intellij-haxe   Source File: HaxeImportOptimizer.java    License: 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 8
Source Project: consulo   Source File: FileIndexBase.java    License: 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 9
Source Project: consulo   Source File: ProjectFileIndexImpl.java    License: 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 10
Source Project: consulo   Source File: FileTreeAccessFilter.java    License: 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 11
Source Project: consulo   Source File: UsageInfo2UsageAdapter.java    License: 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 12
Source Project: consulo   Source File: UsageInfo.java    License: 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 13
Source Project: consulo   Source File: LanguageSubstitutors.java    License: 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 14
Source Project: consulo   Source File: AstLoadingFilter.java    License: 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 15
Source Project: consulo   Source File: FileEditorManagerImpl.java    License: 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 16
Source Project: consulo   Source File: AbstractFileViewProvider.java    License: 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 Project: consulo   Source File: ProjectScopeImpl.java    License: 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 18
Source Project: consulo   Source File: EmptyFileManager.java    License: 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 19
Source Project: consulo   Source File: InspectionResultsView.java    License: 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 20
Source Project: consulo   Source File: PsiEventWrapperAspect.java    License: 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 21
Source Project: consulo   Source File: IndexTodoCacheManagerImpl.java    License: 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 22
Source Project: consulo   Source File: IndexTodoCacheManagerImpl.java    License: 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 23
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 24
Source Project: consulo   Source File: InjectedLanguageManagerImpl.java    License: 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 25
Source Project: consulo   Source File: InjectedLanguageUtil.java    License: 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 26
Source Project: consulo   Source File: VirtualFileWindowImpl.java    License: 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 27
Source Project: BashSupport   Source File: BashResolveUtil.java    License: Apache License 2.0 4 votes vote down vote up
public static boolean isNotIndexedFile(@NonNls Project project, @Nullable VirtualFile virtualFile) {
    return virtualFile == null
            || virtualFile instanceof VirtualFileWindow
            || !FileIndexFacade.getInstance(project).isInContent(virtualFile);
}
 
Example 28
Source Project: consulo   Source File: FileGroupingRule.java    License: Apache License 2.0 4 votes vote down vote up
public FileUsageGroup(@Nonnull Project project, @Nonnull VirtualFile file) {
  myProject = project;
  myFile = file instanceof VirtualFileWindow ? ((VirtualFileWindow)file).getDelegate() : file;
  myPresentableName = myFile.getName();
  update();
}
 
Example 29
Source Project: consulo   Source File: UsageViewManagerImpl.java    License: Apache License 2.0 4 votes vote down vote up
private static boolean isFileInScope(@Nonnull VirtualFile file, @Nonnull SearchScope searchScope) {
  if (file instanceof VirtualFileWindow) {
    file = ((VirtualFileWindow)file).getDelegate();
  }
  return searchScope.contains(file);
}
 
Example 30
Source Project: consulo   Source File: ReadonlyStatusHandlerImpl.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public OperationStatus ensureFilesWritable(@Nonnull VirtualFile... files) {
  if (files.length == 0) {
    return new OperationStatusImpl(VirtualFile.EMPTY_ARRAY);
  }
  ApplicationManager.getApplication().assertIsDispatchThread();

  Set<VirtualFile> realFiles = new THashSet<VirtualFile>(files.length);
  for (VirtualFile file : files) {
    if (file instanceof VirtualFileWindow) file = ((VirtualFileWindow)file).getDelegate();
    if (file != null) {
      realFiles.add(file);
    }
  }
  files = VfsUtilCore.toVirtualFileArray(realFiles);

  for (final WritingAccessProvider accessProvider : myAccessProviders) {
    Collection<VirtualFile> denied = ContainerUtil.filter(files, virtualFile -> !accessProvider.isPotentiallyWritable(virtualFile));

    if (denied.isEmpty()) {
      denied = accessProvider.requestWriting(files);
    }
    if (!denied.isEmpty()) {
      return new OperationStatusImpl(VfsUtilCore.toVirtualFileArray(denied));
    }
  }

  final FileInfo[] fileInfos = createFileInfos(files);
  if (fileInfos.length == 0) { // if all files are already writable
    return createResultStatus(files);
  }

  if (ApplicationManager.getApplication().isUnitTestMode()) {
    return createResultStatus(files);
  }

  // This event count hack is necessary to allow actions that called this stuff could still get data from their data contexts.
  // Otherwise data manager stuff will fire up an assertion saying that event count has been changed (due to modal dialog show-up)
  // The hack itself is safe since we guarantee that focus will return to the same component had it before modal dialog have been shown.
  final int savedEventCount = IdeEventQueue.getInstance().getEventCount();
  if (myState.SHOW_DIALOG) {
    new ReadOnlyStatusDialog(myProject, fileInfos).show();
  }
  else {
    processFiles(new ArrayList<FileInfo>(Arrays.asList(fileInfos)), null); // the collection passed is modified
  }
  IdeEventQueue.getInstance().setEventCount(savedEventCount);
  return createResultStatus(files);
}