Search in sources :

Example 26 with FileViewProvider

use of com.intellij.psi.FileViewProvider in project intellij-plugins by JetBrains.

the class HbTypedHandler method charTyped.

@Override
public Result charTyped(char c, Project project, @NotNull Editor editor, @NotNull PsiFile file) {
    int offset = editor.getCaretModel().getOffset();
    FileViewProvider provider = file.getViewProvider();
    if (!provider.getBaseLanguage().isKindOf(HbLanguage.INSTANCE)) {
        return Result.CONTINUE;
    }
    if (offset < 2 || offset > editor.getDocument().getTextLength()) {
        return Result.CONTINUE;
    }
    String previousChar = editor.getDocument().getText(new TextRange(offset - 2, offset - 1));
    boolean closeBraceCompleted = false;
    if (file.getLanguage() instanceof HbLanguage) {
        if (HbConfig.isAutocompleteMustachesEnabled() && c == '}' && !previousChar.equals("}")) {
            // we may be able to complete the second brace
            PsiDocumentManager.getInstance(project).commitDocument(editor.getDocument());
            PsiElement elementAt = provider.findElementAt(offset - 1, provider.getBaseLanguage());
            ASTNode node = elementAt != null ? elementAt.getNode() : null;
            if (node != null && node.getElementType() == HbTokenTypes.INVALID) {
                // we should be looking at the beginning of a close brace.  Find its matching open brace and auto-complete based on its type
                PsiElement mustache = PsiTreeUtil.findFirstParent(elementAt, psiElement -> psiElement instanceof HbMustache);
                if (mustache != null) {
                    String braceCompleter;
                    if (mustache.getFirstChild().getNode().getElementType() == HbTokenTypes.OPEN_UNESCAPED) {
                        // add "}}" to complete the CLOSE_UNESCAPED
                        braceCompleter = "}}";
                    } else {
                        // add "}" to complete the CLOSE
                        braceCompleter = "}";
                    }
                    editor.getDocument().insertString(offset, braceCompleter);
                    offset = offset + braceCompleter.length();
                    editor.getCaretModel().moveToOffset(offset);
                    closeBraceCompleted = true;
                }
            }
        }
    }
    // if we just completed a close brace or the user just typed one, we may have some business to attend to
    if (closeBraceCompleted || (c == '}' && previousChar.equals("}"))) {
        autoInsertCloseTag(project, offset, editor, provider);
        adjustMustacheFormatting(project, offset, editor, file, provider);
    } else if (c == '/' && previousChar.equals("{")) {
        finishClosingTag(offset, editor, provider);
    }
    return Result.CONTINUE;
}
Also used : FileViewProvider(com.intellij.psi.FileViewProvider) HbLanguage(com.dmarcotte.handlebars.HbLanguage) ASTNode(com.intellij.lang.ASTNode) TextRange(com.intellij.openapi.util.TextRange) PsiElement(com.intellij.psi.PsiElement)

Example 27 with FileViewProvider

use of com.intellij.psi.FileViewProvider in project intellij-community by JetBrains.

the class LineMarkersPass method doCollectInformation.

@Override
public void doCollectInformation(@NotNull ProgressIndicator progress) {
    final List<LineMarkerInfo> 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(() -> 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);
    }
}
Also used : HighlightingLevelManager(com.intellij.codeInsight.daemon.impl.analysis.HighlightingLevelManager) InjectedFileViewProvider(com.intellij.psi.impl.source.tree.injected.InjectedFileViewProvider) FileViewProvider(com.intellij.psi.FileViewProvider) Language(com.intellij.lang.Language) PsiFile(com.intellij.psi.PsiFile)

Example 28 with FileViewProvider

use of com.intellij.psi.FileViewProvider in project intellij-community by JetBrains.

the class ExternalToolPass method collectInformationWithProgress.

@Override
protected void collectInformationWithProgress(@NotNull ProgressIndicator progress) {
    final FileViewProvider viewProvider = myFile.getViewProvider();
    final Set<Language> relevantLanguages = viewProvider.getLanguages();
    int externalAnnotatorsInRoots = 0;
    for (Language language : relevantLanguages) {
        PsiFile psiRoot = viewProvider.getPsi(language);
        if (!HighlightingLevelManager.getInstance(myProject).shouldInspect(psiRoot))
            continue;
        final List<ExternalAnnotator> externalAnnotators = ExternalLanguageAnnotators.allForFile(language, psiRoot);
        externalAnnotatorsInRoots += externalAnnotators.size();
    }
    setProgressLimit(externalAnnotatorsInRoots);
    InspectionProfileImpl profile = InspectionProjectProfileManager.getInstance(myProject).getCurrentProfile();
    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) {
                String shortName = externalAnnotator.getPairedBatchInspectionShortName();
                if (shortName != null) {
                    HighlightDisplayKey key = HighlightDisplayKey.find(shortName);
                    LOG.assertTrue(key != null, "Paired tool '" + shortName + "' not found for external annotator: " + externalAnnotator);
                    if (!profile.isToolEnabled(key, myFile))
                        continue;
                }
                final Object collectedInfo;
                Editor editor = getEditor();
                if (editor != null) {
                    collectedInfo = externalAnnotator.collectInformation(psiRoot, editor, errorFound);
                } else {
                    collectedInfo = externalAnnotator.collectInformation(psiRoot);
                }
                advanceProgress(1);
                if (collectedInfo != null) {
                    myAnnotator2DataMap.put(externalAnnotator, new MyData(psiRoot, collectedInfo));
                }
            }
        }
    }
}
Also used : ExternalAnnotator(com.intellij.lang.annotation.ExternalAnnotator) InspectionProfileImpl(com.intellij.codeInspection.ex.InspectionProfileImpl) HighlightDisplayKey(com.intellij.codeInsight.daemon.HighlightDisplayKey) FileViewProvider(com.intellij.psi.FileViewProvider) Language(com.intellij.lang.Language) PsiFile(com.intellij.psi.PsiFile) Editor(com.intellij.openapi.editor.Editor)

Example 29 with FileViewProvider

use of com.intellij.psi.FileViewProvider in project intellij-community by JetBrains.

the class OffsetsElementSignatureProvider method restoreBySignatureTokens.

@Override
protected PsiElement restoreBySignatureTokens(@NotNull PsiFile file, @NotNull PsiElement parent, @NotNull String type, @NotNull StringTokenizer tokenizer, @Nullable StringBuilder processingInfoStorage) {
    if (!TYPE_MARKER.equals(type)) {
        if (processingInfoStorage != null) {
            processingInfoStorage.append(String.format("Stopping '%s' provider because given signature doesn't have expected type - can work with '%s' but got '%s'%n", getClass().getName(), TYPE_MARKER, type));
        }
        return null;
    }
    int start;
    int end;
    try {
        start = Integer.parseInt(tokenizer.nextToken());
        end = Integer.parseInt(tokenizer.nextToken());
    } catch (NumberFormatException e) {
        return null;
    }
    if (processingInfoStorage != null) {
        processingInfoStorage.append(String.format("Parsed target offsets - [%d; %d)%n", start, end));
    }
    int index = 0;
    if (tokenizer.hasMoreTokens()) {
        try {
            index = Integer.parseInt(tokenizer.nextToken());
        } catch (NumberFormatException e) {
        // Do nothing
        }
    }
    Language language = file.getLanguage();
    if (tokenizer.hasMoreTokens()) {
        String languageId = tokenizer.nextToken();
        Language languageFromSignature = Language.findLanguageByID(languageId);
        if (languageFromSignature == null) {
            if (processingInfoStorage != null) {
                processingInfoStorage.append(String.format("Couldn't find language for id %s", languageId));
            }
        } else {
            language = languageFromSignature;
        }
    }
    FileViewProvider viewProvider = file.getViewProvider();
    PsiElement element = viewProvider.findElementAt(start, language);
    if (element == null) {
        return null;
    }
    PsiElement result = findElement(start, end, index, element, processingInfoStorage);
    if (result != null) {
        return result;
    } else if (processingInfoStorage != null) {
        processingInfoStorage.append(String.format("Failed to find an element by the given offsets for language %s. Started by the element '%s' (%s)", language, element, element.getText()));
    }
    PsiFile psiFile = viewProvider.getPsi(language);
    if (psiFile == null) {
        if (processingInfoStorage != null) {
            processingInfoStorage.append(String.format("Couldn't find PSI for language %s", language.toString()));
        }
        return null;
    }
    final PsiElement injectedStartElement = InjectedLanguageUtil.findElementAtNoCommit(psiFile, start);
    if (processingInfoStorage != null) {
        processingInfoStorage.append(String.format("Trying to find injected element starting from the '%s'%s%n", injectedStartElement, injectedStartElement == null ? "" : String.format("(%s)", injectedStartElement.getText())));
    }
    if (injectedStartElement != null && injectedStartElement != element) {
        return findElement(start, end, index, injectedStartElement, processingInfoStorage);
    }
    return null;
}
Also used : Language(com.intellij.lang.Language) FileViewProvider(com.intellij.psi.FileViewProvider) PsiFile(com.intellij.psi.PsiFile) PsiElement(com.intellij.psi.PsiElement)

Example 30 with FileViewProvider

use of com.intellij.psi.FileViewProvider in project intellij-community by JetBrains.

the class JavaParsingTestCase method createPsiFile.

private PsiFile createPsiFile(final String name, final String text, final TestParser parser) {
    if (TEST_FILE_ELEMENT_TYPE == null) {
        TEST_FILE_ELEMENT_TYPE = new MyIFileElementType();
    }
    TEST_PARSER = parser;
    final LightVirtualFile virtualFile = new LightVirtualFile(name + '.' + myFileExt, StdFileTypes.JAVA, text, -1);
    final FileViewProvider viewProvider = new SingleRootFileViewProvider(PsiManager.getInstance(getProject()), virtualFile, true);
    return new PsiJavaFileImpl(viewProvider) {

        @NotNull
        @Override
        protected FileElement createFileElement(@NotNull final CharSequence text) {
            return new FileElement(TEST_FILE_ELEMENT_TYPE, text);
        }
    };
}
Also used : FileViewProvider(com.intellij.psi.FileViewProvider) SingleRootFileViewProvider(com.intellij.psi.SingleRootFileViewProvider) LightVirtualFile(com.intellij.testFramework.LightVirtualFile) FileElement(com.intellij.psi.impl.source.tree.FileElement) SingleRootFileViewProvider(com.intellij.psi.SingleRootFileViewProvider) PsiJavaFileImpl(com.intellij.psi.impl.source.PsiJavaFileImpl) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

FileViewProvider (com.intellij.psi.FileViewProvider)41 PsiFile (com.intellij.psi.PsiFile)22 Language (com.intellij.lang.Language)21 PsiElement (com.intellij.psi.PsiElement)15 NotNull (org.jetbrains.annotations.NotNull)12 TextRange (com.intellij.openapi.util.TextRange)6 VirtualFile (com.intellij.openapi.vfs.VirtualFile)5 TemplateLanguageFileViewProvider (com.intellij.psi.templateLanguages.TemplateLanguageFileViewProvider)5 Project (com.intellij.openapi.project.Project)4 ASTNode (com.intellij.lang.ASTNode)3 Document (com.intellij.openapi.editor.Document)3 Pair (com.intellij.openapi.util.Pair)3 Nullable (org.jetbrains.annotations.Nullable)3 HighlightingLevelManager (com.intellij.codeInsight.daemon.impl.analysis.HighlightingLevelManager)2 FileASTNode (com.intellij.lang.FileASTNode)2 ExternalAnnotator (com.intellij.lang.annotation.ExternalAnnotator)2 XMLLanguage (com.intellij.lang.xml.XMLLanguage)2 FileType (com.intellij.openapi.fileTypes.FileType)2 SingleRootFileViewProvider (com.intellij.psi.SingleRootFileViewProvider)2 IStubFileElementType (com.intellij.psi.tree.IStubFileElementType)2