Search in sources :

Example 71 with PsiElement

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

the class HbEnterHandler method isBetweenHbTags.

/**
   * Checks to see if {@code Enter} has been typed while the caret is between an open and close tag pair.
   *
   * @return true if between open and close tags, false otherwise
   */
private static boolean isBetweenHbTags(Editor editor, PsiFile file, int offset) {
    if (offset == 0)
        return false;
    CharSequence chars = editor.getDocument().getCharsSequence();
    if (chars.charAt(offset - 1) != '}')
        return false;
    EditorHighlighter highlighter = ((EditorEx) editor).getHighlighter();
    HighlighterIterator iterator = highlighter.createIterator(offset - 1);
    final PsiElement openerElement = file.findElementAt(iterator.getStart());
    PsiElement openTag = HbPsiUtil.findParentOpenTagElement(openerElement);
    if (openTag == null) {
        return false;
    }
    iterator.advance();
    if (iterator.atEnd()) {
        // no more tokens, so certainly no close tag
        return false;
    }
    final PsiElement closerElement = file.findElementAt(iterator.getStart());
    PsiElement closeTag = HbPsiUtil.findParentCloseTagElement(closerElement);
    // if we got this far, we're between open and close tags iff this is a close tag
    return closeTag != null;
}
Also used : EditorEx(com.intellij.openapi.editor.ex.EditorEx) HighlighterIterator(com.intellij.openapi.editor.highlighter.HighlighterIterator) PsiElement(com.intellij.psi.PsiElement) EditorHighlighter(com.intellij.openapi.editor.highlighter.EditorHighlighter)

Example 72 with PsiElement

use of com.intellij.psi.PsiElement 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 73 with PsiElement

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

the class HbTypedHandler method autoInsertCloseTag.

/**
   * When appropriate, auto-inserts Handlebars close tags.  i.e.  When "{{#tagId}}" or "{{^tagId}} is typed,
   * {{/tagId}} is automatically inserted
   */
private static void autoInsertCloseTag(Project project, int offset, Editor editor, FileViewProvider provider) {
    if (!HbConfig.isAutoGenerateCloseTagEnabled()) {
        return;
    }
    PsiDocumentManager.getInstance(project).commitDocument(editor.getDocument());
    PsiElement elementAtCaret = provider.findElementAt(offset - 1, HbLanguage.class);
    if (elementAtCaret == null || elementAtCaret.getNode().getElementType() != HbTokenTypes.CLOSE) {
        return;
    }
    HbOpenBlockMustache openTag = HbPsiUtil.findParentOpenTagElement(elementAtCaret);
    if (openTag != null && openTag.getChildren().length > 1) {
        HbMustacheName mustacheName = PsiTreeUtil.findChildOfType(openTag, HbMustacheName.class);
        if (mustacheName != null) {
            // insert the corresponding close tag
            editor.getDocument().insertString(offset, "{{/" + mustacheName.getText() + "}}");
        }
    }
}
Also used : PsiElement(com.intellij.psi.PsiElement)

Example 74 with PsiElement

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

the class HbFoldingBuilder method appendDescriptors.

private void appendDescriptors(PsiElement psi, List<FoldingDescriptor> descriptors, Document document) {
    if (isSingleLine(psi, document)) {
        return;
    }
    if (HbTokenTypes.COMMENT == psi.getNode().getElementType()) {
        ASTNode commentNode = psi.getNode();
        String commentText = commentNode.getText();
        // tags before we allow folding
        if (commentText.length() > 6 && commentText.substring(0, 3).equals("{{!") && commentText.substring(commentText.length() - 2, commentText.length()).equals("}}")) {
            TextRange range = new TextRange(commentNode.getTextRange().getStartOffset() + 3, commentNode.getTextRange().getEndOffset() - 2);
            descriptors.add(new FoldingDescriptor(commentNode, range));
        }
    }
    if (psi instanceof HbBlockWrapper) {
        PsiElement endOpenBlockStache = getOpenBlockCloseStacheElement(psi.getFirstChild());
        PsiElement endCloseBlockStache = getCloseBlockCloseStacheElement(psi.getLastChild());
        // if we've got a well formed block with the open and close elems we need, define a region to fold
        if (endOpenBlockStache != null && endCloseBlockStache != null) {
            int endOfFirstOpenStacheLine = document.getLineEndOffset(document.getLineNumber(psi.getTextRange().getStartOffset()));
            // we set the start of the text we'll fold to be just before the close braces of the open stache,
            //     or, if the open stache spans multiple lines, to the end of the first line
            int foldingRangeStartOffset = Math.min(endOpenBlockStache.getTextRange().getStartOffset(), endOfFirstOpenStacheLine);
            // we set the end of the text we'll fold to be just before the final close braces in this block
            int foldingRangeEndOffset = endCloseBlockStache.getTextRange().getStartOffset();
            TextRange range = new TextRange(foldingRangeStartOffset, foldingRangeEndOffset);
            descriptors.add(new FoldingDescriptor(psi, range));
        }
    }
    PsiElement child = psi.getFirstChild();
    while (child != null) {
        appendDescriptors(child, descriptors, document);
        child = child.getNextSibling();
    }
}
Also used : HbBlockWrapper(com.dmarcotte.handlebars.psi.HbBlockWrapper) FoldingDescriptor(com.intellij.lang.folding.FoldingDescriptor) ASTNode(com.intellij.lang.ASTNode) TextRange(com.intellij.openapi.util.TextRange) PsiElement(com.intellij.psi.PsiElement)

Example 75 with PsiElement

use of com.intellij.psi.PsiElement in project buck by facebook.

the class BuckBuildUtil method getPropertyValue.

/**
   * Get the value of a property in a specific buck rule body.
   * TODO(#7908675): We should use Buck's own classes for it.
   */
public static String getPropertyValue(BuckRuleBody body, String name) {
    if (body == null) {
        return null;
    }
    PsiElement[] children = body.getChildren();
    for (PsiElement child : children) {
        if (BuckPsiUtils.testType(child, BuckTypes.PROPERTY)) {
            PsiElement lvalue = child.getFirstChild();
            PsiElement propertyName = lvalue.getFirstChild();
            if (propertyName != null && propertyName.getText().equals(name)) {
                BuckExpression expression = (BuckExpression) BuckPsiUtils.findChildWithType(child, BuckTypes.EXPRESSION);
                return expression != null ? BuckPsiUtils.getStringValueFromExpression(expression) : null;
            }
        }
    }
    return null;
}
Also used : BuckExpression(com.facebook.buck.intellij.ideabuck.lang.psi.BuckExpression) PsiElement(com.intellij.psi.PsiElement)

Aggregations

PsiElement (com.intellij.psi.PsiElement)3198 Nullable (org.jetbrains.annotations.Nullable)493 PsiFile (com.intellij.psi.PsiFile)474 NotNull (org.jetbrains.annotations.NotNull)442 TextRange (com.intellij.openapi.util.TextRange)239 PsiReference (com.intellij.psi.PsiReference)227 Project (com.intellij.openapi.project.Project)222 VirtualFile (com.intellij.openapi.vfs.VirtualFile)210 ArrayList (java.util.ArrayList)195 ASTNode (com.intellij.lang.ASTNode)142 XmlTag (com.intellij.psi.xml.XmlTag)134 PsiClass (com.intellij.psi.PsiClass)115 Editor (com.intellij.openapi.editor.Editor)112 Document (com.intellij.openapi.editor.Document)109 PsiWhiteSpace (com.intellij.psi.PsiWhiteSpace)85 PsiDirectory (com.intellij.psi.PsiDirectory)80 IElementType (com.intellij.psi.tree.IElementType)78 Module (com.intellij.openapi.module.Module)77 PsiMethod (com.intellij.psi.PsiMethod)73 UsageInfo (com.intellij.usageView.UsageInfo)70