Search in sources :

Example 1 with MacroEmptyTag

use of ool.intellij.plugin.psi.MacroEmptyTag in project oxy-template-support-plugin by mutant-industries.

the class ParamQuoteHandler method beforeCharTyped.

@Override
public Result beforeCharTyped(char c, Project project, @NotNull Editor editor, @NotNull PsiFile file, FileType fileType) {
    FileViewProvider provider = file.getViewProvider();
    int offset;
    if (!(provider instanceof OxyTemplateFileViewProvider) || (offset = editor.getCaretModel().getOffset()) < 1) {
        return Result.CONTINUE;
    }
    PsiElement elementAt;
    if (c == '"' || c == '\'') {
        // <%@ layout "_ %> -> <%@ layout "_" %>, <m:foo.bar param="_ -> <m:foo.bar param="_"
        PsiDocumentManager.getInstance(project).commitDocument(editor.getDocument());
        if ((elementAt = provider.findElementAt(offset - 1, OxyTemplate.INSTANCE)) == null) {
            return Result.CONTINUE;
        }
        if (OxyTemplateParserDefinition.WHITE_SPACES.contains(elementAt.getNode().getElementType())) {
            elementAt = elementAt.getPrevSibling();
            if (elementAt instanceof MacroEmptyTag) {
                elementAt = elementAt.getLastChild();
            }
            if (elementAt instanceof MacroAttribute) {
                elementAt = elementAt.getLastChild();
                if (elementAt instanceof PsiErrorElement) {
                    elementAt = elementAt.getPrevSibling();
                }
            } else if (elementAt instanceof DirectiveStatement) {
                if (c == '"' && ((DirectiveStatement) elementAt).getDirectiveParamWrapperList().size() == 0) {
                    editor.getDocument().insertString(offset, String.valueOf(c));
                }
            } else if (c == '"' && elementAt instanceof PsiErrorElement && elementAt.getPrevSibling().getNode().getElementType() == OxyTemplateTypes.T_DIRECTIVE) {
                editor.getDocument().insertString(offset, String.valueOf(c));
            }
        }
        if (elementAt.getNode().getElementType() == OxyTemplateTypes.T_MACRO_PARAM_ASSIGNMENT) {
            editor.getDocument().insertString(offset, String.valueOf(c));
        } else if (c == '"' && elementAt.getNode().getElementType() == OxyTemplateTypes.T_DIRECTIVE && elementAt.getNextSibling().getNode().getStartOffset() == offset) {
            editor.getDocument().insertString(offset, String.valueOf(c));
        }
    } else if (c == '=') {
        // <m:foo.bar param=_ -> <m:foo.bar param="_"
        PsiDocumentManager.getInstance(project).commitDocument(editor.getDocument());
        if ((elementAt = provider.findElementAt(offset - 1, OxyTemplate.INSTANCE)) == null) {
            return Result.CONTINUE;
        }
        if (elementAt.getNode().getElementType() == OxyTemplateTypes.T_MACRO_PARAM_NAME && elementAt.getNextSibling() == null && (elementAt.getNode().getStartOffset() + elementAt.getTextLength()) == offset) {
            editor.getDocument().insertString(offset, "=\"\"");
            editor.getCaretModel().moveToOffset(offset + 2);
            return Result.STOP;
        }
    }
    return Result.CONTINUE;
}
Also used : PsiErrorElement(com.intellij.psi.PsiErrorElement) FileViewProvider(com.intellij.psi.FileViewProvider) OxyTemplateFileViewProvider(ool.intellij.plugin.file.OxyTemplateFileViewProvider) MacroEmptyTag(ool.intellij.plugin.psi.MacroEmptyTag) MacroAttribute(ool.intellij.plugin.psi.MacroAttribute) OxyTemplateFileViewProvider(ool.intellij.plugin.file.OxyTemplateFileViewProvider) PsiElement(com.intellij.psi.PsiElement) DirectiveStatement(ool.intellij.plugin.psi.DirectiveStatement)

Aggregations

FileViewProvider (com.intellij.psi.FileViewProvider)1 PsiElement (com.intellij.psi.PsiElement)1 PsiErrorElement (com.intellij.psi.PsiErrorElement)1 OxyTemplateFileViewProvider (ool.intellij.plugin.file.OxyTemplateFileViewProvider)1 DirectiveStatement (ool.intellij.plugin.psi.DirectiveStatement)1 MacroAttribute (ool.intellij.plugin.psi.MacroAttribute)1 MacroEmptyTag (ool.intellij.plugin.psi.MacroEmptyTag)1