Java Code Examples for com.intellij.psi.PsiFile#getOriginalFile()

The following examples show how to use com.intellij.psi.PsiFile#getOriginalFile() . 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: BlazeResourcesDomFileDescription.java    From intellij with Apache License 2.0 6 votes vote down vote up
/** Only check that the file is under res/values or res/values-*. */
private static boolean isBlazeResourcesFile(PsiFile file) {
  if (!Blaze.isBlazeProject(file.getProject())) {
    return false;
  }
  file = file.getOriginalFile();
  PsiDirectory parent = file.getContainingDirectory();
  if (parent == null) {
    return false;
  }
  String parentName = parent.getName();
  if (!parentName.equals(FD_RES_VALUES) && !parentName.startsWith(FD_RES_VALUES + '-')) {
    return false;
  }
  PsiDirectory grandParent = parent.getParentDirectory();
  return grandParent != null && grandParent.getName().equals(FD_RES);
}
 
Example 2
Source File: PropertySuppressableInspectionBase.java    From eslint-plugin with MIT License 6 votes vote down vote up
protected void createSuppression(@NotNull Project project, @NotNull PsiElement element, @NotNull PsiElement container) throws IncorrectOperationException {
    if (element.isValid()) {
        PsiFile psiFile = element.getContainingFile();
        if (psiFile != null) {
            psiFile = psiFile.getOriginalFile();
        }

        if (psiFile != null && psiFile.isValid()) {
            final Document document = PsiDocumentManager.getInstance(project).getDocument(psiFile);
            if (document != null) {
                int lineNo = document.getLineNumber(element.getTextOffset());
                final int lineEndOffset = document.getLineEndOffset(lineNo);
                CommandProcessor.getInstance().executeCommand(project, new Runnable() {
                    public void run() {
                        document.insertString(lineEndOffset, " //eslint-disable-line");
                    }
                }, null, null);
            }
        }
    }
}
 
Example 3
Source File: FileIncludeManagerImpl.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Override
public void processIncludingFiles(PsiFile context, Processor<? super Pair<VirtualFile, FileIncludeInfo>> processor) {
  context = context.getOriginalFile();
  VirtualFile contextFile = context.getVirtualFile();
  if (contextFile == null) return;

  String originalName = context.getName();
  Collection<String> names = getPossibleIncludeNames(context, originalName);

  GlobalSearchScope scope = GlobalSearchScope.allScope(myProject);
  for (String name : names) {
    MultiMap<VirtualFile, FileIncludeInfoImpl> infoList = FileIncludeIndex.getIncludingFileCandidates(name, scope);
    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;
          }
        }
      }
    }
  }
}
 
Example 4
Source File: VirtualFileUtil.java    From attic-polygene-java with Apache License 2.0 5 votes vote down vote up
/**
 * @param element element to process.
 * @return The containing virtual file of the element.
 * @since 0.1
 */
@Nullable
public static VirtualFile getVirtualFile( @NotNull PsiElement element )
{
    if( element instanceof PsiFileSystemItem )
    {
        PsiFileSystemItem fileSystemItem = (PsiFileSystemItem) element;
        return fileSystemItem.getVirtualFile();
    }

    // If it's not a file system, assume that this is an element within a file
    PsiFile containingFile = element.getContainingFile();
    if( containingFile == null )
    {
        return null;
    }

    VirtualFile virtualFile = containingFile.getVirtualFile();
    if( virtualFile != null )
    {
        return virtualFile;
    }

    PsiFile originalFile = containingFile.getOriginalFile();
    if( originalFile == null )
    {
        return null;
    }

    return originalFile.getVirtualFile();
}
 
Example 5
Source File: CompletionUtilCoreImpl.java    From consulo with Apache License 2.0 5 votes vote down vote up
public static <T extends PsiElement> T getOriginalElement(@Nonnull T psi, PsiFile containingFile) {
  if (containingFile != null && containingFile != containingFile.getOriginalFile() && psi.getTextRange() != null) {
    TextRange range = psi.getTextRange();
    Integer start = range.getStartOffset();
    Integer end = range.getEndOffset();
    final Document document = containingFile.getViewProvider().getDocument();
    if (document != null) {
      Document hostDocument = document instanceof DocumentWindow ? ((DocumentWindow)document).getDelegate() : document;
      OffsetTranslator translator = hostDocument.getUserData(OffsetTranslator.RANGE_TRANSLATION);
      if (translator != null) {
        if (document instanceof DocumentWindow) {
          TextRange translated = ((DocumentWindow)document).injectedToHost(new TextRange(start, end));
          start = translated.getStartOffset();
          end = translated.getEndOffset();
        }

        start = translator.translateOffset(start);
        end = translator.translateOffset(end);
        if (start == null || end == null) {
          return null;
        }

        if (document instanceof DocumentWindow) {
          start = ((DocumentWindow)document).hostToInjected(start);
          end = ((DocumentWindow)document).hostToInjected(end);
        }
      }
    }
    //noinspection unchecked
    return (T)PsiTreeUtil.findElementOfClassAtRange(containingFile.getOriginalFile(), start, end, psi.getClass());
  }

  return psi;
}
 
Example 6
Source File: CompletionInitializationUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nonnull
static CompletionParameters createCompletionParameters(CompletionInitializationContext initContext, CompletionProcessEx indicator, OffsetsInFile finalOffsets) {
  int offset = finalOffsets.getOffsets().getOffset(CompletionInitializationContext.START_OFFSET);
  PsiFile fileCopy = finalOffsets.getFile();
  PsiFile originalFile = fileCopy.getOriginalFile();
  PsiElement insertedElement = findCompletionPositionLeaf(finalOffsets, offset, originalFile);
  insertedElement.putUserData(CompletionContext.COMPLETION_CONTEXT_KEY, new CompletionContext(fileCopy, finalOffsets.getOffsets()));
  return new CompletionParameters(insertedElement, originalFile, initContext.getCompletionType(), offset, initContext.getInvocationCount(), initContext.getEditor(), indicator);
}
 
Example 7
Source File: TemplateDataElementType.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public ASTNode parseContents(ASTNode chameleon) {
  final CharTable charTable = SharedImplUtil.findCharTableByTree(chameleon);
  final FileElement fileElement = TreeUtil.getFileElement((TreeElement)chameleon);
  final PsiFile psiFile = (PsiFile)fileElement.getPsi();
  PsiFile originalPsiFile = psiFile.getOriginalFile();

  final TemplateLanguageFileViewProvider viewProvider = (TemplateLanguageFileViewProvider)originalPsiFile.getViewProvider();

  final Language templateLanguage = getTemplateFileLanguage(viewProvider);
  final CharSequence sourceCode = chameleon.getChars();

  RangesCollector collector = new RangesCollector();
  final PsiFile templatePsiFile = createTemplateFile(psiFile, templateLanguage, sourceCode, viewProvider, collector);

  final FileElement templateFileElement = ((PsiFileImpl)templatePsiFile).calcTreeElement();

  DebugUtil.startPsiModification("template language parsing");
  try {
    prepareParsedTemplateFile(templateFileElement);
    insertOuters(templateFileElement, sourceCode, collector.myRanges, charTable);

    TreeElement childNode = templateFileElement.getFirstChildNode();

    DebugUtil.checkTreeStructure(templateFileElement);
    DebugUtil.checkTreeStructure(chameleon);
    if (fileElement != chameleon) {
      DebugUtil.checkTreeStructure(psiFile.getNode());
      DebugUtil.checkTreeStructure(originalPsiFile.getNode());
    }

    return childNode;
  }
  finally {
    DebugUtil.finishPsiModification();
  }
}
 
Example 8
Source File: NavigationCommand.java    From CppTools with Apache License 2.0 5 votes vote down vote up
private static String getPath(PsiFile file) {
  VirtualFile virtualFile = file.getVirtualFile();

  if (virtualFile == null && file.getOriginalFile() != null) {
    virtualFile = file.getOriginalFile().getVirtualFile();
  }
  return virtualFile != null ? virtualFile.getPath():"";
}
 
Example 9
Source File: CompletionAssertions.java    From consulo with Apache License 2.0 4 votes vote down vote up
static void assertCorrectOriginalFile(String prefix, PsiFile file, PsiFile copy) {
  if (copy.getOriginalFile() != file) {
    throw new AssertionError(prefix + " copied file doesn't have correct original: noOriginal=" + (copy.getOriginalFile() == copy) + "\n file " + fileInfo(file) + "\n copy " + fileInfo(copy));
  }
}