Search in sources :

Example 1 with HbLanguage

use of com.dmarcotte.handlebars.HbLanguage 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 2 with HbLanguage

use of com.dmarcotte.handlebars.HbLanguage in project intellij-plugins by JetBrains.

the class HbTypedHandler method beforeCharTyped.

@Override
public Result beforeCharTyped(char c, Project project, Editor editor, PsiFile file, FileType fileType) {
    int offset = editor.getCaretModel().getOffset();
    if (offset == 0 || offset > editor.getDocument().getTextLength()) {
        return Result.CONTINUE;
    }
    String previousChar = editor.getDocument().getText(new TextRange(offset - 1, offset));
    if (file.getLanguage() instanceof HbLanguage) {
        PsiDocumentManager.getInstance(project).commitAllDocuments();
        // we suppress the built-in "}" auto-complete when we see "{{"
        if (c == '{' && previousChar.equals("{")) {
            // since the "}" autocomplete is built in to IDEA, we need to hack around it a bit by
            // intercepting it before it is inserted, doing the work of inserting for the user
            // by inserting the '{' the user just typed...
            editor.getDocument().insertString(offset, Character.toString(c));
            // ... and position their caret after it as they'd expect...
            editor.getCaretModel().moveToOffset(offset + 1);
            // ... then finally telling subsequent responses to this charTyped to do nothing
            return Result.STOP;
        }
    }
    return Result.CONTINUE;
}
Also used : HbLanguage(com.dmarcotte.handlebars.HbLanguage) TextRange(com.intellij.openapi.util.TextRange)

Aggregations

HbLanguage (com.dmarcotte.handlebars.HbLanguage)2 TextRange (com.intellij.openapi.util.TextRange)2 ASTNode (com.intellij.lang.ASTNode)1 FileViewProvider (com.intellij.psi.FileViewProvider)1 PsiElement (com.intellij.psi.PsiElement)1