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

The following examples show how to use com.intellij.psi.PsiFile#getViewProvider() . 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: XQueryXmlSlashTypedHandler.java    From intellij-xquery with Apache License 2.0 6 votes vote down vote up
@Override
public Result beforeCharTyped(final char c, final Project project, final Editor editor, final PsiFile editedFile, final FileType fileType) {
    if ((editedFile.getLanguage() instanceof XQueryLanguage) && c == '/') {
        PsiDocumentManager.getInstance(project).commitAllDocuments();
        PsiFile file = PsiDocumentManager.getInstance(project).getPsiFile(editor.getDocument());
        if (file == null) return Result.CONTINUE;
        final int offset = editor.getCaretModel().getOffset();
        FileViewProvider provider = file.getViewProvider();
        PsiElement element = provider.findElementAt(offset, XQueryLanguage.class);
        if (isAtTheSlashOfClosingOfEmptyTag(offset, element)) {
            moveCaretByOne(editor, offset);
            return Result.STOP;
        }
    }
    return Result.CONTINUE;
}
 
Example 2
Source File: XQueryXmlGtTypedHandler.java    From intellij-xquery with Apache License 2.0 6 votes vote down vote up
@Override
public Result beforeCharTyped(final char c, final Project project, final Editor editor, final PsiFile editedFile, final FileType fileType) {
    if ((editedFile.getLanguage() instanceof XQueryLanguage) && c == '>') {
        PsiDocumentManager.getInstance(project).commitAllDocuments();
        PsiFile file = PsiDocumentManager.getInstance(project).getPsiFile(editor.getDocument());
        if (file == null) return Result.CONTINUE;
        final int offset = editor.getCaretModel().getOffset();
        FileViewProvider provider = file.getViewProvider();
        PsiElement element = provider.findElementAt(offset, XQueryLanguage.class);

        if (element != null && element.getNode() != null && (
                element.getNode().getElementType() == XQueryTypes.XMLTAGEND ||
                        element.getNode().getElementType() == XQueryTypes.XMLEMPTYELEMENTEND)) {
            EditorModificationUtil.moveCaretRelatively(editor, 1);
            editor.getScrollingModel().scrollToCaret(ScrollType.RELATIVE);
            return Result.STOP;
        }
    }
    return Result.CONTINUE;
}
 
Example 3
Source File: LossyEncodingInspection.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Override
@Nullable
public ProblemDescriptor[] checkFile(@Nonnull PsiFile file, @Nonnull InspectionManager manager, boolean isOnTheFly) {
  if (InjectedLanguageManager.getInstance(file.getProject()).isInjectedFragment(file)) return null;
  if (!file.isPhysical()) return null;
  FileViewProvider viewProvider = file.getViewProvider();
  if (viewProvider.getBaseLanguage() != file.getLanguage()) return null;
  VirtualFile virtualFile = file.getVirtualFile();
  if (virtualFile == null) return null;
  if (!virtualFile.isInLocalFileSystem()) return null;
  CharSequence text = viewProvider.getContents();
  Charset charset = LoadTextUtil.extractCharsetFromFileContent(file.getProject(), virtualFile, text);

  // no sense in checking transparently decoded file: all characters there are already safely encoded
  if (charset instanceof Native2AsciiCharset) return null;

  List<ProblemDescriptor> descriptors = new SmartList<ProblemDescriptor>();
  boolean ok = checkFileLoadedInWrongEncoding(file, manager, isOnTheFly, virtualFile, charset, descriptors);
  if (ok) {
    checkIfCharactersWillBeLostAfterSave(file, manager, isOnTheFly, text, charset, descriptors);
  }

  return descriptors.toArray(new ProblemDescriptor[descriptors.size()]);
}
 
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: CSharpFileStubElementType.java    From consulo-csharp with Apache License 2.0 5 votes vote down vote up
@Nonnull
@RequiredReadAction
public static Set<String> getStableDefines(@Nonnull PsiFile psi)
{
	FileViewProvider viewProvider = psi.getViewProvider();
	VirtualFile virtualFile = viewProvider.getVirtualFile();
	if(virtualFile instanceof LightVirtualFile)
	{
		virtualFile = ((LightVirtualFile) virtualFile).getOriginalFile();
		// we need call second time, due second original file will be light
		if(virtualFile instanceof LightVirtualFile)
		{
			virtualFile = ((LightVirtualFile) virtualFile).getOriginalFile();
		}
	}
	if(virtualFile == null)
	{
		virtualFile = psi.getUserData(IndexingDataKeys.VIRTUAL_FILE);
	}

	Set<String> defVariables = Collections.emptySet();
	if(virtualFile != null)
	{
		List<String> variables = findVariables(virtualFile, psi.getProject());

		if(variables != null)
		{
			defVariables = new HashSet<>(variables);
		}
	}
	return defVariables;
}
 
Example 6
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 7
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 8
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 9
Source File: TemplateLanguageErrorQuickFixProvider.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public void registerErrorQuickFix(final PsiErrorElement errorElement, final HighlightInfo highlightInfo) {
  final PsiFile psiFile = errorElement.getContainingFile();
  final FileViewProvider provider = psiFile.getViewProvider();
  if (!(provider instanceof TemplateLanguageFileViewProvider)) return;
  if (psiFile.getLanguage() != ((TemplateLanguageFileViewProvider) provider).getTemplateDataLanguage()) return;

  QuickFixAction.registerQuickFixAction(highlightInfo, createChangeTemplateDataLanguageFix(errorElement));

}
 
Example 10
Source File: XQueryXmlSlashTypedHandler.java    From intellij-xquery with Apache License 2.0 5 votes vote down vote up
@Override
public Result charTyped(char c, final Project project, final @NotNull Editor editor, @NotNull final PsiFile editedFile) {
    if ((editedFile.getLanguage() instanceof XQueryLanguage) && c == '/') {
        PsiDocumentManager.getInstance(project).commitAllDocuments();
        PsiFile file = PsiDocumentManager.getInstance(project).getPsiFile(editor.getDocument());
        if (file == null) return Result.CONTINUE;
        FileViewProvider provider = file.getViewProvider();
        final int offset = editor.getCaretModel().getOffset();
        PsiElement element = provider.findElementAt(offset - 1, XQueryLanguage.class);
        if (element == null) return Result.CONTINUE;
        if (!(element.getLanguage() instanceof XQueryLanguage)) return Result.CONTINUE;
        ASTNode prevLeaf = element.getNode();
        if (prevLeaf == null) return Result.CONTINUE;
        final String prevLeafText = prevLeaf.getText();
        if (isStartOfEndOfTag(prevLeaf, prevLeafText)) {
            XQueryXmlFullTag tag = PsiTreeUtil.getParentOfType(element, XQueryXmlFullTag.class);
            if (tag != null) {
                XQueryXmlTagName tagName = tag.getXmlTagNameList().get(0);
                if (hasNoClosingTagName(prevLeaf, tag, tagName)) {
                    finishClosingTag(editor, tagName);
                    return Result.STOP;
                }
            }
        }
        if (!"/".equals(prevLeafText.trim())) return Result.CONTINUE;
        prevLeaf = getPreviousNonWhiteSpaceLeaf(prevLeaf);
        if (prevLeaf == null) return Result.CONTINUE;
        if (PsiTreeUtil.getParentOfType(element, XQueryDirAttributeValue.class) != null) return Result.CONTINUE;
        if (prevLeaf.getElementType() == XQueryTypes.ELEMENTCONTENTCHAR) return Result.CONTINUE;
        XQueryEnclosedExpression parentEnclosedExpression = PsiTreeUtil.getParentOfType(element, XQueryEnclosedExpression.class, true, XQueryXmlFullTag.class);
        XQueryXmlFullTag fullTag = getParentFullTag(prevLeaf);
        if (isInEnclosedExpressionNestedInXmlTag(parentEnclosedExpression, fullTag)) return Result.CONTINUE;
        if (isInUnclosedXmlTag(fullTag)) {
            closeEmptyTag(editor);
            return Result.STOP;
        }
    }
    return Result.CONTINUE;
}
 
Example 11
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);
}
 
Example 12
Source File: RTExternalAnnotator.java    From react-templates-plugin with MIT License 5 votes vote down vote up
@Nullable
    private static ExternalLintAnnotationInput collectInformation(@NotNull PsiFile psiFile, @Nullable Editor editor) {
        if (psiFile.getContext() != null || !RTFileUtil.isRTFile(psiFile)) {
            return null;
        }
        VirtualFile virtualFile = psiFile.getVirtualFile();
        if (virtualFile == null || !virtualFile.isInLocalFileSystem()) {
            return null;
        }
        if (psiFile.getViewProvider() instanceof MultiplePsiFilesPerDocumentFileViewProvider) {
            return null;
        }
        Project project = psiFile.getProject();
        RTProjectComponent component = project.getComponent(RTProjectComponent.class);
        if (component == null || !component.isValidAndEnabled()) {
            return null;
        }
        Document document = PsiDocumentManager.getInstance(project).getDocument(psiFile);
        if (document == null) {
            return null;
        }
        String fileContent = document.getText();
        if (StringUtil.isEmptyOrSpaces(fileContent)) {
            return null;
        }
        EditorColorsScheme colorsScheme = editor == null ? null : editor.getColorsScheme();
//        tabSize = getTabSize(editor);
//        tabSize = 4;
        return new ExternalLintAnnotationInput(project, psiFile, fileContent, colorsScheme);
    }
 
Example 13
Source File: SassLintExternalAnnotator.java    From sass-lint-plugin with MIT License 5 votes vote down vote up
@Nullable
    private static ExternalLintAnnotationInput collectInformation(@NotNull PsiFile psiFile, @Nullable Editor editor) {
        if (psiFile.getContext() != null || !SassLintConfigFileUtil.isSassFile(psiFile)) {
            return null;
        }
        VirtualFile virtualFile = psiFile.getVirtualFile();
        if (virtualFile == null || !virtualFile.isInLocalFileSystem()) {
            return null;
        }
        if (psiFile.getViewProvider() instanceof MultiplePsiFilesPerDocumentFileViewProvider) {
            return null;
        }
        Project project = psiFile.getProject();
        SassLintProjectComponent component = project.getComponent(SassLintProjectComponent.class);
        if (!component.isSettingsValid() || !component.isEnabled()) {
            return null;
        }
        Document document = PsiDocumentManager.getInstance(project).getDocument(psiFile);
        if (document == null) {
            return null;
        }
        String fileContent = document.getText();
        if (StringUtil.isEmptyOrSpaces(fileContent)) {
            return null;
        }
        EditorColorsScheme colorsScheme = editor != null ? editor.getColorsScheme() : null;
//        tabSize = getTabSize(editor);
//        tabSize = 4;
        return new ExternalLintAnnotationInput(project, psiFile, fileContent, colorsScheme);
    }
 
Example 14
Source File: InTemplateDeclarationVariableProvider.java    From idea-php-typo3-plugin with MIT License 5 votes vote down vote up
private static boolean containsLanguage(@NotNull Language language, @NotNull PsiElement psiElement) {
    PsiFile containingFile = psiElement.getContainingFile();
    if (containingFile == null) {
        return false;
    }

    FileViewProvider viewProvider = containingFile.getViewProvider();

    return viewProvider.getLanguages().contains(language);
}
 
Example 15
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 16
Source File: TemplateLanguageStructureViewBuilder.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nonnull
private static JBIterable<Language> getLanguages(@Nullable PsiFile psiFile) {
  if (psiFile == null) return JBIterable.empty();
  FileViewProvider provider = psiFile.getViewProvider();

  Language baseLanguage = provider.getBaseLanguage();
  Language dataLanguage = provider instanceof TemplateLanguageFileViewProvider ? ((TemplateLanguageFileViewProvider)provider).getTemplateDataLanguage() : null;
  return JBIterable.of(baseLanguage).append(dataLanguage).append(JBIterable.from(provider.getLanguages()).filter(o -> o != baseLanguage && o != dataLanguage));
}
 
Example 17
Source File: ToggleBreadcrumbsAction.java    From consulo with Apache License 2.0 4 votes vote down vote up
boolean isEnabled(AnActionEvent event) {
  PsiFile psiFile = event.getData(CommonDataKeys.PSI_FILE);
  if (psiFile == null) return true;
  FileViewProvider provider = psiFile.getViewProvider();
  return BreadcrumbsUtilEx.findProvider(false, provider) != null;
}
 
Example 18
Source File: OSSPantsPythonRunConfigurationIntegrationTest.java    From intellij-pants-plugin with Apache License 2.0 4 votes vote down vote up
private PyTestFile(PsiFile pyFile, PyClass testClass) {
  super(pyFile.getViewProvider());
  myTestClass = testClass;
}
 
Example 19
Source File: StubTreeBuilder.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Nullable
public static Stub buildStubTree(final FileContent inputData) {
  Stub data = inputData.getUserData(stubElementKey);
  if (data != null) return data;

  //noinspection SynchronizationOnLocalVariableOrMethodParameter
  synchronized (inputData) {
    data = inputData.getUserData(stubElementKey);
    if (data != null) return data;

    final FileType fileType = inputData.getFileType();

    final BinaryFileStubBuilder builder = BinaryFileStubBuilders.INSTANCE.forFileType(fileType);
    if (builder != null) {
      data = builder.buildStubTree(inputData);
      if (data instanceof PsiFileStubImpl && !((PsiFileStubImpl)data).rootsAreSet()) {
        ((PsiFileStubImpl)data).setStubRoots(new PsiFileStub[]{(PsiFileStubImpl)data});
      }
    }
    else {
      CharSequence contentAsText = inputData.getContentAsText();
      PsiDependentFileContent fileContent = (PsiDependentFileContent)inputData;
      PsiFile psi = fileContent.getPsiFile();
      final FileViewProvider viewProvider = psi.getViewProvider();
      psi = viewProvider.getStubBindingRoot();
      psi.putUserData(IndexingDataKeys.FILE_TEXT_CONTENT_KEY, contentAsText);

      // if we load AST, it should be easily gc-able. See PsiFileImpl.createTreeElementPointer()
      psi.getManager().startBatchFilesProcessingMode();

      try {
        IStubFileElementType stubFileElementType = ((PsiFileImpl)psi).getElementTypeForStubBuilder();
        if (stubFileElementType != null) {
          final StubBuilder stubBuilder = stubFileElementType.getBuilder();
          if (stubBuilder instanceof LightStubBuilder) {
            LightStubBuilder.FORCED_AST.set(fileContent.getLighterAST());
          }
          data = stubBuilder.buildStubTree(psi);

          final List<Pair<IStubFileElementType, PsiFile>> stubbedRoots = getStubbedRoots(viewProvider);
          final List<PsiFileStub> stubs = new ArrayList<>(stubbedRoots.size());
          stubs.add((PsiFileStub)data);

          for (Pair<IStubFileElementType, PsiFile> stubbedRoot : stubbedRoots) {
            final PsiFile secondaryPsi = stubbedRoot.second;
            if (psi == secondaryPsi) continue;
            final StubBuilder stubbedRootBuilder = stubbedRoot.first.getBuilder();
            if (stubbedRootBuilder instanceof LightStubBuilder) {
              LightStubBuilder.FORCED_AST.set(new TreeBackedLighterAST(secondaryPsi.getNode()));
            }
            final StubElement element = stubbedRootBuilder.buildStubTree(secondaryPsi);
            if (element instanceof PsiFileStub) {
              stubs.add((PsiFileStub)element);
            }
            ensureNormalizedOrder(element);
          }
          final PsiFileStub[] stubsArray = stubs.toArray(PsiFileStub.EMPTY_ARRAY);
          for (PsiFileStub stub : stubsArray) {
            if (stub instanceof PsiFileStubImpl) {
              ((PsiFileStubImpl)stub).setStubRoots(stubsArray);
            }
          }
        }
      }
      finally {
        psi.putUserData(IndexingDataKeys.FILE_TEXT_CONTENT_KEY, null);
        psi.getManager().finishBatchFilesProcessingMode();
      }
    }

    ensureNormalizedOrder(data);
    inputData.putUserData(stubElementKey, data);
    return data;
  }
}
 
Example 20
Source File: CompositeElement.java    From consulo with Apache License 2.0 4 votes vote down vote up
private static String getThreadingDiagnostics(@Nonnull PsiFile psiFile) {
  return "psiFile: " + psiFile +
         "; psiFile.getViewProvider(): " + psiFile.getViewProvider() +
         "; psiFile.isPhysical(): " + psiFile.isPhysical() +
         "; nonPhysicalOrInjected: " + isNonPhysicalOrInjected(psiFile);
}