Java Code Examples for com.intellij.psi.FileViewProvider#getPsi()

The following examples show how to use com.intellij.psi.FileViewProvider#getPsi() . 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: HtmlNSNamespaceProvider.java    From idea-php-typo3-plugin with MIT License 6 votes vote down vote up
private synchronized Collection<FluidNamespace> doProvide(@NotNull PsiElement element) {
    FileViewProvider viewProvider = element.getContainingFile().getViewProvider();
    if (!viewProvider.getLanguages().contains(HTMLLanguage.INSTANCE)) {
        return ContainerUtil.emptyList();
    }

    PsiFile htmlFile = viewProvider.getPsi(HTMLLanguage.INSTANCE);
    CachedValue userData = htmlFile.getUserData(HTML_NS_KEY);
    if (userData != null) {
        return (Collection<FluidNamespace>) userData.getValue();
    }

    CachedValue<Collection<FluidNamespace>> cachedValue = CachedValuesManager.getManager(element.getProject()).createCachedValue(() -> {
        HtmlNSVisitor visitor = new HtmlNSVisitor();
        htmlFile.accept(visitor);

        return CachedValueProvider.Result.createSingleDependency(visitor.namespaces, htmlFile);
    }, false);

    htmlFile.putUserData(HTML_NS_KEY, cachedValue);

    return cachedValue.getValue();
}
 
Example 2
Source File: TemplateDataElementType.java    From consulo with Apache License 2.0 6 votes vote down vote up
protected PsiFile createPsiFileFromSource(final Language language, CharSequence sourceCode, PsiManager manager) {
  @NonNls final LightVirtualFile virtualFile = new LightVirtualFile("foo", createTemplateFakeFileType(language), sourceCode, LocalTimeCounter.currentTime());

  FileViewProvider viewProvider = new SingleRootFileViewProvider(manager, virtualFile, false) {
    @Override
    @Nonnull
    public Language getBaseLanguage() {
      return language;
    }
  };

  // Since we're already inside a template language PSI that was built regardless of the file size (for whatever reason),
  // there should also be no file size checks for template data files.
  SingleRootFileViewProvider.doNotCheckFileSizeLimit(virtualFile);

  return viewProvider.getPsi(language);
}
 
Example 3
Source File: GotoCustomRegionAction.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Nonnull
@RequiredReadAction
private static Collection<FoldingDescriptor> getCustomFoldingDescriptors(@Nonnull Editor editor, @Nonnull Project project) {
  Set<FoldingDescriptor> foldingDescriptors = new HashSet<>();
  final Document document = editor.getDocument();
  PsiDocumentManager documentManager = PsiDocumentManager.getInstance(project);
  PsiFile file = documentManager != null ? documentManager.getPsiFile(document) : null;
  if (file != null) {
    final FileViewProvider viewProvider = file.getViewProvider();
    for (final Language language : viewProvider.getLanguages()) {
      final PsiFile psi = viewProvider.getPsi(language);
      final FoldingBuilder foldingBuilder = LanguageFolding.INSTANCE.forLanguage(language);
      if (psi != null) {
        for (FoldingDescriptor descriptor : LanguageFolding.buildFoldingDescriptors(foldingBuilder, psi, document, false)) {
          CustomFoldingBuilder customFoldingBuilder = getCustomFoldingBuilder(foldingBuilder, descriptor);
          if (customFoldingBuilder != null) {
            if (customFoldingBuilder.isCustomRegionStart(descriptor.getElement())) {
              foldingDescriptors.add(descriptor);
            }
          }
        }
      }
    }
  }
  return foldingDescriptors;
}
 
Example 4
Source File: GotoCustomRegionDialog.java    From consulo with Apache License 2.0 6 votes vote down vote up
@RequiredReadAction
private Collection<FoldingDescriptor> getCustomFoldingDescriptors() {
  Set<FoldingDescriptor> foldingDescriptors = new HashSet<FoldingDescriptor>();
  final Document document = myEditor.getDocument();
  PsiDocumentManager documentManager = PsiDocumentManager.getInstance(myProject);
  PsiFile file = documentManager != null ? documentManager.getPsiFile(document) : null;
  if (file != null) {
    final FileViewProvider viewProvider = file.getViewProvider();
    for (final Language language : viewProvider.getLanguages()) {
      final PsiFile psi = viewProvider.getPsi(language);
      final FoldingBuilder foldingBuilder = LanguageFolding.INSTANCE.forLanguage(language);
      if (psi != null) {
        for (FoldingDescriptor descriptor : LanguageFolding.buildFoldingDescriptors(foldingBuilder, psi, document, false)) {
          CustomFoldingBuilder customFoldingBuilder = getCustomFoldingBuilder(foldingBuilder, descriptor);
          if (customFoldingBuilder != null) {
            if (customFoldingBuilder.isCustomRegionStart(descriptor.getElement())) {
              foldingDescriptors.add(descriptor);
            }
          }
        }
      }
    }
  }
  return foldingDescriptors;
}
 
Example 5
Source File: HectorComponent.java    From consulo with Apache License 2.0 6 votes vote down vote up
private boolean isModified() {
  final FileViewProvider viewProvider = myFile.getViewProvider();
  for (Language language : mySliders.keySet()) {
    JSlider slider = mySliders.get(language);
    final PsiFile root = viewProvider.getPsi(language);
    HighlightingLevelManager highlightingLevelManager = HighlightingLevelManager.getInstance(myFile.getProject());
    if (root != null && getValue(highlightingLevelManager.shouldHighlight(root), highlightingLevelManager.shouldInspect(root)) != slider.getValue()) {
      return true;
    }
  }
  for (HectorComponentPanel panel : myAdditionalPanels) {
    if (panel.isModified()) {
      return true;
    }
  }

  return false;
}
 
Example 6
Source File: ExternalToolPass.java    From consulo with Apache License 2.0 6 votes vote down vote up
@RequiredReadAction
@Override
public void doCollectInformation(@Nonnull ProgressIndicator progress) {
  myDocumentChanged = false;

  final FileViewProvider viewProvider = myFile.getViewProvider();
  final Set<Language> relevantLanguages = viewProvider.getLanguages();
  for (Language language : relevantLanguages) {
    PsiFile psiRoot = viewProvider.getPsi(language);
    if (!HighlightingLevelManager.getInstance(myProject).shouldInspect(psiRoot)) continue;
    final List<ExternalAnnotator> externalAnnotators = ExternalLanguageAnnotators.allForFile(language, psiRoot);

    if (!externalAnnotators.isEmpty()) {
      DaemonCodeAnalyzerEx daemonCodeAnalyzer = DaemonCodeAnalyzerEx.getInstanceEx(myProject);
      boolean errorFound = daemonCodeAnalyzer.getFileStatusMap().wasErrorFound(myDocument);

      for(ExternalAnnotator externalAnnotator: externalAnnotators) {
        final Object collectedInfo = externalAnnotator.collectInformation(psiRoot, myEditor, errorFound);
        if (collectedInfo != null) {
          myAnnotator2DataMap.put(externalAnnotator, new MyData(psiRoot, collectedInfo));
        }
      }
    }
  }
}
 
Example 7
Source File: StubTreeBuilder.java    From consulo with Apache License 2.0 6 votes vote down vote up
/**
 * Order is deterministic. First element matches {@link FileViewProvider#getStubBindingRoot()}
 */
@Nonnull
public static List<Pair<IStubFileElementType, PsiFile>> getStubbedRoots(@Nonnull FileViewProvider viewProvider) {
  final List<Trinity<Language, IStubFileElementType, PsiFile>> roots = new SmartList<>();
  final PsiFile stubBindingRoot = viewProvider.getStubBindingRoot();
  for (Language language : viewProvider.getLanguages()) {
    final PsiFile file = viewProvider.getPsi(language);
    if (file instanceof PsiFileImpl) {
      final IElementType type = ((PsiFileImpl)file).getElementTypeForStubBuilder();
      if (type != null) {
        roots.add(Trinity.create(language, (IStubFileElementType)type, file));
      }
    }
  }

  ContainerUtil.sort(roots, (o1, o2) -> {
    if (o1.third == stubBindingRoot) return o2.third == stubBindingRoot ? 0 : -1;
    else if (o2.third == stubBindingRoot) return 1;
    else return StringUtil.compare(o1.first.getID(), o2.first.getID(), false);
  });

  return ContainerUtil.map(roots, trinity -> Pair.create(trinity.second, trinity.third));
}
 
Example 8
Source File: ParsingTestCase.java    From consulo with Apache License 2.0 6 votes vote down vote up
public static void doCheckResult(String myFullDataPath,
                                 PsiFile file,
                                 boolean checkAllPsiRoots,
                                 String targetDataName,
                                 boolean skipSpaces,
                                 boolean printRanges) throws IOException {
  FileViewProvider provider = file.getViewProvider();
  Set<Language> languages = provider.getLanguages();

  if (!checkAllPsiRoots || languages.size() == 1) {
    doCheckResult(myFullDataPath, targetDataName + ".txt", toParseTreeText(file, skipSpaces, printRanges).trim());
    return;
  }

  for (Language language : languages) {
    PsiFile root = provider.getPsi(language);
    String expectedName = targetDataName + "." + language.getID() + ".txt";
    doCheckResult(myFullDataPath, expectedName, toParseTreeText(root, skipSpaces, printRanges).trim());
  }
}
 
Example 9
Source File: CSharpPsiUtilImpl.java    From consulo-csharp with Apache License 2.0 6 votes vote down vote up
@Nullable
public static CSharpFile findCSharpFile(@Nullable PsiFile psiFile)
{
	if(psiFile == null)
	{
		return null;
	}
	FileViewProvider viewProvider = psiFile.getViewProvider();

	PsiFile psi = viewProvider.getPsi(CSharpLanguage.INSTANCE);
	if(psi == null)
	{
		return null;
	}
	return (CSharpFile) psi;
}
 
Example 10
Source File: FluidUtil.java    From idea-php-typo3-plugin with MIT License 6 votes vote down vote up
public static PsiElement retrieveFluidElementAtPosition(PsiElement psiElement) {
    FileViewProvider viewProvider = psiElement.getContainingFile().getViewProvider();
    if (!viewProvider.getLanguages().contains(FluidLanguage.INSTANCE)) {
        return null;
    }

    int textOffset = psiElement.getTextOffset();
    FluidFile psi = (FluidFile) viewProvider.getPsi(FluidLanguage.INSTANCE);

    if (psiElement instanceof XmlAttributeValue) {
        textOffset += 2;
    }

    PsiElement elementAt = psi.findElementAt(textOffset);
    if (elementAt == null) {
        return null;
    }

    if (elementAt.getNode().getElementType().equals(FluidTypes.IDENTIFIER)) {
        return elementAt.getParent();
    }

    return null;
}
 
Example 11
Source File: Divider.java    From consulo with Apache License 2.0 5 votes vote down vote up
public static void divideInsideAndOutsideAllRoots(@Nonnull PsiFile file,
                                                  @Nonnull TextRange restrictRange,
                                                  @Nonnull TextRange priorityRange,
                                                  @Nonnull Condition<PsiFile> rootFilter,
                                                  @Nonnull Processor<DividedElements> processor) {
  final FileViewProvider viewProvider = file.getViewProvider();
  for (Language language : viewProvider.getLanguages()) {
    final PsiFile root = viewProvider.getPsi(language);
    if (!rootFilter.value(root)) {
      continue;
    }
    divideInsideAndOutsideInOneRoot(root, restrictRange, priorityRange, processor);
  }
}
 
Example 12
Source File: LanguageVersionUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
@RequiredReadAction
public static LanguageVersion findLanguageVersion(@Nonnull Language language, @Nonnull PsiFile psiFile) {
  if (psiFile.getLanguage() == language) {
    return psiFile.getLanguageVersion();
  }

  FileViewProvider viewProvider = psiFile.getViewProvider();

  PsiFile psi = viewProvider.getPsi(language);
  if (psi == null) {
    return LanguageVersionResolvers.INSTANCE.forLanguage(language).getLanguageVersion(language, psiFile);
  }
  return psi.getLanguageVersion();
}
 
Example 13
Source File: TemplateLanguageUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nullable
public static PsiFile getTemplateFile(PsiFile file) {
  final FileViewProvider viewProvider = file.getViewProvider();
  if (viewProvider instanceof TemplateLanguageFileViewProvider) {
    return viewProvider.getPsi(((TemplateLanguageFileViewProvider)viewProvider).getTemplateDataLanguage());
  } else {
    return null;
  }
}
 
Example 14
Source File: InTemplateDeclarationVariableProvider.java    From idea-php-typo3-plugin with MIT License 5 votes vote down vote up
private static PsiFile extractLanguagePsiForElement(@NotNull Language language, @NotNull PsiElement psiElement) {
    FileViewProvider viewProvider = psiElement.getContainingFile().getViewProvider();

    int textOffset = psiElement.getTextOffset();
    PsiFile psi = viewProvider.getPsi(language);
    PsiElement elementAt = psi.findElementAt(textOffset);
    if (elementAt == null) {
        return null;
    }

    return psi;
}
 
Example 15
Source File: LineMarkersPass.java    From consulo with Apache License 2.0 5 votes vote down vote up
@RequiredReadAction
@Override
public void doCollectInformation(@Nonnull ProgressIndicator progress) {
  final List<LineMarkerInfo<PsiElement>> lineMarkers = new ArrayList<>();
  FileViewProvider viewProvider = myFile.getViewProvider();
  for (Language language : viewProvider.getLanguages()) {
    final PsiFile root = viewProvider.getPsi(language);
    HighlightingLevelManager highlightingLevelManager = HighlightingLevelManager.getInstance(myProject);
    if (!highlightingLevelManager.shouldHighlight(root)) continue;
    Divider.divideInsideAndOutsideInOneRoot(root, myRestrictRange, myPriorityBounds, elements -> {
      Collection<LineMarkerProvider> providers = getMarkerProviders(language, myProject);
      List<LineMarkerProvider> providersList = new ArrayList<>(providers);

      queryProviders(elements.inside, root, providersList, (element, info) -> {
        lineMarkers.add(info);
        ApplicationManager.getApplication().invokeLater(() -> {
          if (isValid()) {
            LineMarkersUtil.addLineMarkerToEditorIncrementally(myProject, getDocument(), info);
          }
        }, myProject.getDisposed());
      });
      queryProviders(elements.outside, root, providersList, (element, info) -> lineMarkers.add(info));
      return true;
    });
  }

  myMarkers = mergeLineMarkers(lineMarkers, getDocument());
  if (LOG.isDebugEnabled()) {
    LOG.debug("LineMarkersPass.doCollectInformation. lineMarkers: " + lineMarkers + "; merged: " + myMarkers);
  }
}
 
Example 16
Source File: InTemplateDeclarationVariableProvider.java    From idea-php-typo3-plugin with MIT License 5 votes vote down vote up
private static PsiElement extractLanguagePsiElementForElementAtPosition(@NotNull Language language, @NotNull PsiElement psiElement, int offset) {
    FileViewProvider viewProvider = psiElement.getContainingFile().getViewProvider();

    PsiFile psi = viewProvider.getPsi(language);

    PsiElement elementAt = psi.findElementAt(offset);
    if (elementAt == null) {
        return null;
    }

    return elementAt.getParent();
}
 
Example 17
Source File: MoverWrapper.java    From consulo with Apache License 2.0 5 votes vote down vote up
private static void indentLinesIn(final Editor editor, final PsiFile file, final Document document, final Project project, RangeMarker range) {
  final CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(project);
  int line1 = editor.offsetToLogicalPosition(range.getStartOffset()).line;
  int line2 = editor.offsetToLogicalPosition(range.getEndOffset()).line;

  while (!lineContainsNonSpaces(document, line1) && line1 < line2) line1++;
  while (!lineContainsNonSpaces(document, line2) && line1 < line2) line2--;

  final FileViewProvider provider = file.getViewProvider();
  PsiFile rootToAdjustIndentIn = provider.getPsi(provider.getBaseLanguage());
  codeStyleManager.adjustLineIndent(rootToAdjustIndentIn, new TextRange(document.getLineStartOffset(line1), document.getLineStartOffset(line2)));
}
 
Example 18
Source File: MockFileManager.java    From consulo with Apache License 2.0 5 votes vote down vote up
@RequiredReadAction
@Override
@Nullable
public PsiFile getCachedPsiFile(@Nonnull VirtualFile vFile) {
  final FileViewProvider provider = findCachedViewProvider(vFile);
  return provider.getPsi(provider.getBaseLanguage());
}
 
Example 19
Source File: FluidUtil.java    From idea-php-typo3-plugin with MIT License 5 votes vote down vote up
public static FluidElement retrieveFluidElementAtPosition(PsiFile psiFile, int startOffset) {
    FileViewProvider viewProvider = psiFile.getViewProvider();
    if (!viewProvider.getLanguages().contains(FluidLanguage.INSTANCE)) {
        return null;
    }

    FluidFile psi = (FluidFile) viewProvider.getPsi(FluidLanguage.INSTANCE);

    PsiElement elementAt = psi.findElementAt(startOffset);
    if (elementAt == null) {
        return null;
    }

    return (FluidElement) elementAt;
}
 
Example 20
Source File: TemplateLanguageStructureViewBuilder.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nullable
private StructureViewBuilder getBuilder(@Nonnull PsiFile psiFile, @Nonnull Language language) {
  FileViewProvider viewProvider = psiFile.getViewProvider();
  Language baseLanguage = viewProvider.getBaseLanguage();
  PsiFile psi = viewProvider.getPsi(language);
  if (psi == null) return null;
  if (language == baseLanguage) return createMainBuilder(psi);
  if (!isAcceptableBaseLanguageFile(psi)) return null;
  PsiStructureViewFactory factory = LanguageStructureViewBuilder.INSTANCE.forLanguage(language);
  return factory == null ? null : factory.getStructureViewBuilder(psi);
}