Search in sources :

Example 1 with FileViewProvider

use of com.intellij.psi.FileViewProvider in project idea-handlebars by dmarcotte.

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 (provider instanceof HbFileViewProvider) {
        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, new Condition<PsiElement>() {

                    @Override
                    public boolean value(PsiElement psiElement) {
                        return 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 : HbFileViewProvider(com.dmarcotte.handlebars.file.HbFileViewProvider) FileViewProvider(com.intellij.psi.FileViewProvider) HbFileViewProvider(com.dmarcotte.handlebars.file.HbFileViewProvider) ASTNode(com.intellij.lang.ASTNode) TextRange(com.intellij.openapi.util.TextRange) PsiElement(com.intellij.psi.PsiElement)

Example 2 with FileViewProvider

use of com.intellij.psi.FileViewProvider in project intellij-elixir by KronicDeth.

the class Logger method excerpt.

@NotNull
private static String excerpt(@NotNull PsiFile containingFile, @NotNull PsiElement element) {
    StringBuilder excerptBuilder = new StringBuilder();
    excerptBuilder.append('\n');
    excerptBuilder.append("### Excerpt\n");
    excerptBuilder.append('\n');
    excerptBuilder.append("```\n");
    excerptBuilder.append(element.getText());
    excerptBuilder.append('\n');
    excerptBuilder.append("```\n");
    FileViewProvider fileViewProvider = containingFile.getViewProvider();
    Document document = fileViewProvider.getDocument();
    if (document != null) {
        TextRange textRange = element.getTextRange();
        int startingLine = document.getLineNumber(textRange.getStartOffset());
        int endingLine = document.getLineNumber(textRange.getEndOffset());
        excerptBuilder.append(" Line(s) ");
        excerptBuilder.append(startingLine);
        excerptBuilder.append('-');
        excerptBuilder.append(endingLine);
        VirtualFile virtualFile = containingFile.getVirtualFile();
        if (virtualFile != null) {
            excerptBuilder.append(" in ");
            excerptBuilder.append(virtualFile.getPath());
        }
        excerptBuilder.append("\n");
    }
    return excerptBuilder.toString();
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) FileViewProvider(com.intellij.psi.FileViewProvider) TextRange(com.intellij.openapi.util.TextRange) Document(com.intellij.openapi.editor.Document) NotNull(org.jetbrains.annotations.NotNull)

Example 3 with FileViewProvider

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

the class XPathEvalAction method execute.

private void execute(Editor editor) {
    final Project project = editor.getProject();
    final PsiFile psiFile = PsiDocumentManager.getInstance(project).getPsiFile(editor.getDocument());
    if (psiFile == null) {
        return;
    }
    InputExpressionDialog.Context input;
    XmlElement contextNode = null;
    final Config cfg = XPathAppComponent.getInstance().getConfig();
    do {
        RangeHighlighter contextHighlighter = null;
        if (cfg.isUseContextAtCursor()) {
            // find out current context node
            contextNode = MyPsiUtil.findContextNode(psiFile, editor);
            if (contextNode != null) {
                contextHighlighter = HighlighterUtil.highlightNode(editor, contextNode, cfg.getContextAttributes(), cfg);
            }
        }
        if (contextNode == null) {
            // in XPath data model, / is the document itself, including comments, PIs and the root element
            contextNode = ((XmlFile) psiFile).getDocument();
            if (contextNode == null) {
                FileViewProvider fileViewProvider = psiFile.getViewProvider();
                if (fileViewProvider instanceof TemplateLanguageFileViewProvider) {
                    Language dataLanguage = ((TemplateLanguageFileViewProvider) fileViewProvider).getTemplateDataLanguage();
                    PsiFile templateDataFile = fileViewProvider.getPsi(dataLanguage);
                    if (templateDataFile instanceof XmlFile)
                        contextNode = ((XmlFile) templateDataFile).getDocument();
                }
            }
        }
        input = inputXPathExpression(project, contextNode);
        if (contextHighlighter != null) {
            contextHighlighter.dispose();
        }
        if (input == null) {
            return;
        }
        HighlighterUtil.clearHighlighters(editor);
    } while (contextNode != null && evaluateExpression(input, contextNode, editor, cfg));
}
Also used : Project(com.intellij.openapi.project.Project) RangeHighlighter(com.intellij.openapi.editor.markup.RangeHighlighter) TemplateLanguageFileViewProvider(com.intellij.psi.templateLanguages.TemplateLanguageFileViewProvider) FileViewProvider(com.intellij.psi.FileViewProvider) Language(com.intellij.lang.Language) XmlFile(com.intellij.psi.xml.XmlFile) XmlElement(com.intellij.psi.xml.XmlElement) PsiFile(com.intellij.psi.PsiFile) TemplateLanguageFileViewProvider(com.intellij.psi.templateLanguages.TemplateLanguageFileViewProvider) InputExpressionDialog(org.intellij.plugins.xpathView.ui.InputExpressionDialog)

Example 4 with FileViewProvider

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

the class XmlUnwrapDescriptor method collectUnwrappers.

@Override
public List<Pair<PsiElement, Unwrapper>> collectUnwrappers(Project project, Editor editor, PsiFile file) {
    int offset = editor.getCaretModel().getOffset();
    PsiElement e1 = file.findElementAt(offset);
    if (e1 != null) {
        Language language = e1.getParent().getLanguage();
        if (language != file.getLanguage()) {
            UnwrapDescriptor unwrapDescriptor = LanguageUnwrappers.INSTANCE.forLanguage(language);
            if (unwrapDescriptor != null && !(unwrapDescriptor instanceof XmlUnwrapDescriptor)) {
                return unwrapDescriptor.collectUnwrappers(project, editor, file);
            }
        }
    }
    List<Pair<PsiElement, Unwrapper>> result = new ArrayList<>();
    FileViewProvider viewProvider = file.getViewProvider();
    for (Language language : viewProvider.getLanguages()) {
        UnwrapDescriptor unwrapDescriptor = LanguageUnwrappers.INSTANCE.forLanguage(language);
        if (unwrapDescriptor instanceof XmlUnwrapDescriptor) {
            PsiElement e = viewProvider.findElementAt(offset, language);
            PsiElement tag = PsiTreeUtil.getParentOfType(e, XmlTag.class);
            while (tag != null) {
                if (XmlChildRole.START_TAG_NAME_FINDER.findChild(tag.getNode()) != null) {
                    // Exclude implicit tags suck as 'jsp:root'
                    result.add(new Pair<>(tag, new XmlEnclosingTagUnwrapper()));
                }
                tag = PsiTreeUtil.getParentOfType(tag, XmlTag.class);
            }
        }
    }
    Collections.sort(result, (o1, o2) -> o2.first.getTextOffset() - o1.first.getTextOffset());
    return result;
}
Also used : Language(com.intellij.lang.Language) FileViewProvider(com.intellij.psi.FileViewProvider) ArrayList(java.util.ArrayList) PsiElement(com.intellij.psi.PsiElement) UnwrapDescriptor(com.intellij.codeInsight.unwrap.UnwrapDescriptor) Pair(com.intellij.openapi.util.Pair) XmlTag(com.intellij.psi.xml.XmlTag)

Example 5 with FileViewProvider

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

the class XmlAutoPopupHandler method autoPopupXmlLookup.

public static void autoPopupXmlLookup(final Project project, final Editor editor) {
    AutoPopupController.getInstance(project).autoPopupMemberLookup(editor, file -> {
        int offset = editor.getCaretModel().getOffset();
        PsiElement lastElement = InjectedLanguageUtil.findElementAtNoCommit(file, offset - 1);
        if (lastElement instanceof PsiFile) {
            lastElement = file.findElementAt(offset - 1);
        }
        if (lastElement == null || !lastElement.isValid())
            return false;
        if (doCompleteIfNeeded(offset, file, lastElement)) {
            return true;
        }
        FileViewProvider fileViewProvider = file.getViewProvider();
        Language templateDataLanguage;
        final PsiElement parent = lastElement.getParent();
        if (fileViewProvider instanceof TemplateLanguageFileViewProvider && (templateDataLanguage = ((TemplateLanguageFileViewProvider) fileViewProvider).getTemplateDataLanguage()) != parent.getLanguage()) {
            lastElement = fileViewProvider.findElementAt(offset - 1, templateDataLanguage);
            if (lastElement == null || !lastElement.isValid())
                return false;
            return doCompleteIfNeeded(offset, file, lastElement);
        }
        return false;
    });
}
Also used : TemplateLanguageFileViewProvider(com.intellij.psi.templateLanguages.TemplateLanguageFileViewProvider) FileViewProvider(com.intellij.psi.FileViewProvider) Language(com.intellij.lang.Language) XMLLanguage(com.intellij.lang.xml.XMLLanguage) PsiFile(com.intellij.psi.PsiFile) TemplateLanguageFileViewProvider(com.intellij.psi.templateLanguages.TemplateLanguageFileViewProvider) PsiElement(com.intellij.psi.PsiElement)

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