com.intellij.psi.FileViewProvider Java Examples
The following examples show how to use
com.intellij.psi.FileViewProvider.
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: TemplateDataElementType.java From consulo with Apache License 2.0 | 6 votes |
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 #2
Source File: Utils.java From idea-gitignore with MIT License | 6 votes |
/** * Finds {@link PsiFile} for the given {@link VirtualFile} instance. If file is outside current project, * it's required to create new {@link PsiFile} manually. * * @param project current project * @param virtualFile to handle * @return {@link PsiFile} instance */ @Nullable public static PsiFile getPsiFile(@NotNull Project project, @NotNull VirtualFile virtualFile) { PsiFile psiFile = PsiManager.getInstance(project).findFile(virtualFile); if (psiFile == null) { FileViewProvider viewProvider = PsiManager.getInstance(project).findViewProvider(virtualFile); if (viewProvider != null) { IgnoreLanguage language = IgnoreBundle.obtainLanguage(virtualFile); if (language != null) { psiFile = language.createFile(viewProvider); } } } return psiFile; }
Example #3
Source File: GotoCustomRegionDialog.java From consulo with Apache License 2.0 | 6 votes |
@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 #4
Source File: IgnoreFile.java From idea-gitignore with MIT License | 6 votes |
/** Builds a new instance of {@link IgnoreFile}. */ public IgnoreFile(@NotNull FileViewProvider viewProvider, @NotNull IgnoreFileType fileType) { super(viewProvider); this.fileType = fileType; this.language = findLanguage(fileType.getLanguage(), viewProvider); final ParserDefinition parserDefinition = LanguageParserDefinitions.INSTANCE.forLanguage(this.language); if (parserDefinition == null) { throw new RuntimeException( "PsiFileBase: language.getParserDefinition() returned null for: " + this.language ); } this.parserDefinition = parserDefinition; final IFileElementType nodeType = parserDefinition.getFileNodeType(); init(nodeType, nodeType); }
Example #5
Source File: ParsingTestCase.java From consulo with Apache License 2.0 | 6 votes |
protected void doTest(boolean checkResult) { String name = getTestName(false); try { String text = loadFile(name + "." + myExtension); myFile = createPsiFile(name, text); ensureParsed(myFile); FileViewProvider viewProvider = myFile.getViewProvider(); VirtualFile virtualFile = viewProvider.getVirtualFile(); if (virtualFile instanceof LightVirtualFile) { assertEquals("light virtual file text mismatch", text, ((LightVirtualFile)virtualFile).getContent()); } assertEquals("virtual file text mismatch", text, LoadTextUtil.loadText(virtualFile)); assertEquals("doc text mismatch", text, viewProvider.getDocument().getText()); assertEquals("psi text mismatch", text, myFile.getText()); if (checkResult) { checkResult(name, myFile); } else { toParseTreeText(myFile, skipSpaces(), includeRanges()); } } catch (IOException e) { throw new RuntimeException(e); } }
Example #6
Source File: LossyEncodingInspection.java From consulo with Apache License 2.0 | 6 votes |
@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 #7
Source File: TemplateLanguageFormattingModelBuilder.java From consulo with Apache License 2.0 | 6 votes |
protected Block getRootBlock(PsiElement element, FileViewProvider viewProvider, CodeStyleSettings settings) { ASTNode node = element.getNode(); if (node == null) { return createDummyBlock(node); } if (viewProvider instanceof TemplateLanguageFileViewProvider) { final Language dataLanguage = ((TemplateLanguageFileViewProvider)viewProvider).getTemplateDataLanguage(); final FormattingModelBuilder builder = LanguageFormatting.INSTANCE.forLanguage(dataLanguage); if (builder instanceof DelegatingFormattingModelBuilder && ((DelegatingFormattingModelBuilder)builder).dontFormatMyModel()) { return createDummyBlock(node); } if (builder != null) { final FormattingModel model = builder.createModel(viewProvider.getPsi(dataLanguage), settings); List<DataLanguageBlockWrapper> childWrappers = buildChildWrappers(model.getRootBlock()); if (childWrappers.size() == 1) { childWrappers = buildChildWrappers(childWrappers.get(0).getOriginal()); } return createTemplateLanguageBlock(node, Wrap.createWrap(WrapType.NONE, false), null, filterBlocksByRange(childWrappers, node.getTextRange()), settings); } } return createTemplateLanguageBlock(node, Wrap.createWrap(WrapType.NONE, false), null, Collections.<DataLanguageBlockWrapper>emptyList(), settings); }
Example #8
Source File: ChangeTemplateDataLanguageAction.java From consulo with Apache License 2.0 | 6 votes |
@Override public void update(final AnActionEvent e) { e.getPresentation().setVisible(false); VirtualFile virtualFile = e.getData(PlatformDataKeys.VIRTUAL_FILE); VirtualFile[] files = e.getData(PlatformDataKeys.VIRTUAL_FILE_ARRAY); if (files != null && files.length > 1) { virtualFile = null; } if (virtualFile == null || virtualFile.isDirectory()) return; Project project = e.getData(CommonDataKeys.PROJECT); if (project == null) return; final FileViewProvider provider = PsiManager.getInstance(project).findViewProvider(virtualFile); if (provider instanceof ConfigurableTemplateLanguageFileViewProvider) { final TemplateLanguageFileViewProvider viewProvider = (TemplateLanguageFileViewProvider)provider; e.getPresentation().setText(LangBundle.message("quickfix.change.template.data.language.text", viewProvider.getTemplateDataLanguage().getDisplayName())); e.getPresentation().setEnabled(true); e.getPresentation().setVisible(true); } }
Example #9
Source File: FluidUtil.java From idea-php-typo3-plugin with MIT License | 6 votes |
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 #10
Source File: LineMarkersPass.java From consulo with Apache License 2.0 | 5 votes |
@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 #11
Source File: CSharpFileStubElementType.java From consulo-csharp with Apache License 2.0 | 5 votes |
@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 #12
Source File: MockPsiFile.java From needsmoredojo with Apache License 2.0 | 5 votes |
public MockPsiFile(String fileName, String path) { super(mock(FileViewProvider.class)); this.name = fileName; this.path = path; directory = new MockPsiDirectory(path); }
Example #13
Source File: StubTextInconsistencyException.java From consulo with Apache License 2.0 | 5 votes |
public static void checkStubTextConsistency(@Nonnull PsiFile file) throws StubTextInconsistencyException { PsiUtilCore.ensureValid(file); FileViewProvider viewProvider = file.getViewProvider(); if (viewProvider instanceof FreeThreadedFileViewProvider || viewProvider.getVirtualFile() instanceof LightVirtualFile) return; PsiFile bindingRoot = viewProvider.getStubBindingRoot(); if (!(bindingRoot instanceof PsiFileImpl)) return; IStubFileElementType fileElementType = ((PsiFileImpl)bindingRoot).getElementTypeForStubBuilder(); if (fileElementType == null || !fileElementType.shouldBuildStubFor(viewProvider.getVirtualFile())) return; List<PsiFileStub> fromText = restoreStubsFromText(viewProvider); List<PsiFileStub> fromPsi = ContainerUtil.map(StubTreeBuilder.getStubbedRoots(viewProvider), p -> ((PsiFileImpl)p.getSecond()).calcStubTree().getRoot()); if (fromPsi.size() != fromText.size()) { throw new StubTextInconsistencyException( "Inconsistent stub roots: " + "PSI says it's " + ContainerUtil.map(fromPsi, s -> s.getType()) + " but re-parsing the text gives " + ContainerUtil.map(fromText, s -> s.getType()), file, fromText, fromPsi); } for (int i = 0; i < fromPsi.size(); i++) { PsiFileStub psiStub = fromPsi.get(i); if (!DebugUtil.stubTreeToString(psiStub).equals(DebugUtil.stubTreeToString(fromText.get(i)))) { throw new StubTextInconsistencyException("Stub is inconsistent with text in " + file.getLanguage(), file, fromText, fromPsi); } } }
Example #14
Source File: StubTextInconsistencyException.java From consulo with Apache License 2.0 | 5 votes |
@Nonnull private static List<PsiFileStub> restoreStubsFromText(FileViewProvider viewProvider) { FileContentImpl fc = new FileContentImpl(viewProvider.getVirtualFile(), viewProvider.getContents(), 0); fc.putUserData(IndexingDataKeys.PROJECT, viewProvider.getManager().getProject()); PsiFileStubImpl copyTree = (PsiFileStubImpl)StubTreeBuilder.buildStubTree(fc); return copyTree == null ? Collections.emptyList() : Arrays.asList(copyTree.getStubRoots()); }
Example #15
Source File: MockFileManager.java From consulo with Apache License 2.0 | 5 votes |
@RequiredReadAction @Override @Nullable public PsiFile getCachedPsiFile(@Nonnull VirtualFile vFile) { final FileViewProvider provider = findCachedViewProvider(vFile); return provider.getPsi(provider.getBaseLanguage()); }
Example #16
Source File: BreadcrumbsUtilEx.java From consulo with Apache License 2.0 | 5 votes |
@Nullable public static BreadcrumbsProvider findProvider(@Nullable FileViewProvider viewProvider, @Nullable Boolean forceShown) { if (viewProvider == null) return null; if (forceShown == null) { return findProvider(true, viewProvider); } return forceShown ? findProvider(false, viewProvider) : null; }
Example #17
Source File: TemplateLanguageStructureViewBuilder.java From consulo with Apache License 2.0 | 5 votes |
@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 #18
Source File: DustTypedHandler.java From Intellij-Dust with MIT License | 5 votes |
/** * When appropriate, auto-inserts closing tags. i.e. When "{#tagName}" or "{^tagName} is typed, * {/tagName} is automatically inserted */ private void autoInsertCloseTag(Project project, int offset, Editor editor, FileViewProvider provider) { PsiDocumentManager.getInstance(project).commitAllDocuments(); PsiElement elementAtCaret = provider.findElementAt(offset - 1, DustLanguage.class); PsiElement openTag = DustPsiUtil.findParentOpenTagElement(elementAtCaret); String tagName = getTagName(openTag); if (!tagName.trim().equals("")) { boolean addCloseTag = true; PsiElement sibling = openTag.getNextSibling(); DustCloseTag closeTag; while (sibling != null && addCloseTag) { if (sibling instanceof DustCloseTag) { closeTag = (DustCloseTag) sibling; if (getTagName(closeTag).equals(tagName)) { addCloseTag = false; } } sibling = sibling.getNextSibling(); } if (addCloseTag) { // insert the corresponding close tag editor.getDocument().insertString(offset, "{/" + tagName + "}"); } } }
Example #19
Source File: XQueryXmlSlashTypedHandler.java From intellij-xquery with Apache License 2.0 | 5 votes |
@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 #20
Source File: LanguageVersionUtil.java From consulo with Apache License 2.0 | 5 votes |
@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 #21
Source File: SoyFileViewProviderFactory.java From bamboo-soy with Apache License 2.0 | 5 votes |
@NotNull @Override public FileViewProvider createFileViewProvider( @NotNull VirtualFile virtualFile, Language language, @NotNull PsiManager psiManager, boolean eventSystemEnabled) { assert language.isKindOf(SoyLanguage.INSTANCE); return new SoyFileViewProvider(psiManager, virtualFile, eventSystemEnabled); }
Example #22
Source File: FluidUtil.java From idea-php-typo3-plugin with MIT License | 5 votes |
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 #23
Source File: InTemplateDeclarationVariableProvider.java From idea-php-typo3-plugin with MIT License | 5 votes |
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 #24
Source File: Divider.java From consulo with Apache License 2.0 | 5 votes |
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 #25
Source File: LightPsiFileBase.java From consulo with Apache License 2.0 | 4 votes |
public LightPsiFileBase(final FileViewProvider provider, final Language language) { super(provider, language); }
Example #26
Source File: SQFParserDefinition.java From arma-intellij-plugin with MIT License | 4 votes |
@Override public PsiFile createFile(FileViewProvider viewProvider) { return new SQFFile(viewProvider); }
Example #27
Source File: CSharpDocParserDefinition.java From consulo-csharp with Apache License 2.0 | 4 votes |
@Override public PsiFile createFile(@Nonnull FileViewProvider viewProvider) { throw new UnsupportedOperationException(); }
Example #28
Source File: LatteFileViewProviderFactory.java From intellij-latte with MIT License | 4 votes |
@NotNull @Override public FileViewProvider createFileViewProvider(@NotNull VirtualFile file, Language language, @NotNull PsiManager manager, boolean eventSystemEnabled) { return new LatteFileViewProvider(manager, file, eventSystemEnabled); }
Example #29
Source File: PlainTextParserDefinition.java From consulo with Apache License 2.0 | 4 votes |
@Override public PsiFile createFile(FileViewProvider viewProvider) { return new PsiPlainTextFileImpl(viewProvider); }
Example #30
Source File: ProtoParserDefinition.java From protobuf-jetbrains-plugin with Apache License 2.0 | 4 votes |
@Override public PsiFile createFile(FileViewProvider viewProvider) { return new ProtoPsiFileRoot(viewProvider); }