Search in sources :

Example 1 with MacroAttribute

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

the class InnerJsTypeEvaluator method checkForEachDefinition.

// -----------------------------------------------------------------------------------------------------------------
@Nullable
private static JSType checkForEachDefinition(@NotNull final PsiElement element) {
    PsiElement elementLocal = element;
    if (elementLocal.getParent() instanceof JSVarStatement) {
        elementLocal = elementLocal.getParent();
    }
    // repeat macro - var keyword is missing
    if (elementLocal instanceof PsiPackage || !(elementLocal.getFirstChild() instanceof JSVariable)) {
        return null;
    }
    PsiElement elementAt = elementLocal.getContainingFile().getViewProvider().findElementAt(elementLocal.getNode().getStartOffset(), OxyTemplate.INSTANCE);
    assert elementAt != null;
    MacroAttribute attribute = PsiTreeUtil.getParentOfType(elementAt, MacroAttribute.class);
    if (attribute == null || !MacroIndex.REPEAT_MACRO_VARIABLE_DEFINITION.equals(attribute.getMacroParamName().getText())) {
        return null;
    }
    MacroCall macroCall = PsiTreeUtil.getParentOfType(attribute, MacroCall.class);
    assert macroCall != null;
    for (MacroAttribute macroAttribute : macroCall.getMacroAttributeList()) {
        if (MacroIndex.REPEAT_MACRO_LIST_DEFINITION.equals(macroAttribute.getMacroParamName().getText())) {
            MacroParam macroParam;
            if ((macroParam = macroAttribute.getMacroParam()) == null) {
                return null;
            }
            PsiElement list = elementLocal.getContainingFile().getViewProvider().findElementAt(macroParam.getNode().getStartOffset() + macroParam.getTextLength() - 1, OxyTemplateInnerJs.INSTANCE);
            JSReferenceExpression statement = PsiTreeUtil.getParentOfType(list, JSReferenceExpression.class);
            if (statement != null) {
                JSSimpleTypeProcessor typeProcessor = new JSSimpleTypeProcessor();
                JSTypeEvaluator.evaluateTypes(statement, statement.getContainingFile(), typeProcessor);
                if (typeProcessor.getType() instanceof JSArrayTypeImpl) {
                    return ((JSArrayTypeImpl) typeProcessor.getType()).getType();
                }
            }
        }
    }
    return null;
}
Also used : JSReferenceExpression(com.intellij.lang.javascript.psi.JSReferenceExpression) JSArrayTypeImpl(com.intellij.lang.javascript.psi.types.JSArrayTypeImpl) MacroAttribute(ool.intellij.plugin.psi.MacroAttribute) PsiPackage(com.intellij.psi.PsiPackage) JSSimpleTypeProcessor(com.intellij.lang.javascript.psi.resolve.JSSimpleTypeProcessor) JSVarStatement(com.intellij.lang.javascript.psi.JSVarStatement) JSVariable(com.intellij.lang.javascript.psi.JSVariable) PsiElement(com.intellij.psi.PsiElement) MacroCall(ool.intellij.plugin.psi.MacroCall) MacroParam(ool.intellij.plugin.psi.MacroParam) Nullable(org.jetbrains.annotations.Nullable)

Example 2 with MacroAttribute

use of ool.intellij.plugin.psi.MacroAttribute 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

PsiElement (com.intellij.psi.PsiElement)2 MacroAttribute (ool.intellij.plugin.psi.MacroAttribute)2 JSReferenceExpression (com.intellij.lang.javascript.psi.JSReferenceExpression)1 JSVarStatement (com.intellij.lang.javascript.psi.JSVarStatement)1 JSVariable (com.intellij.lang.javascript.psi.JSVariable)1 JSSimpleTypeProcessor (com.intellij.lang.javascript.psi.resolve.JSSimpleTypeProcessor)1 JSArrayTypeImpl (com.intellij.lang.javascript.psi.types.JSArrayTypeImpl)1 FileViewProvider (com.intellij.psi.FileViewProvider)1 PsiErrorElement (com.intellij.psi.PsiErrorElement)1 PsiPackage (com.intellij.psi.PsiPackage)1 OxyTemplateFileViewProvider (ool.intellij.plugin.file.OxyTemplateFileViewProvider)1 DirectiveStatement (ool.intellij.plugin.psi.DirectiveStatement)1 MacroCall (ool.intellij.plugin.psi.MacroCall)1 MacroEmptyTag (ool.intellij.plugin.psi.MacroEmptyTag)1 MacroParam (ool.intellij.plugin.psi.MacroParam)1 Nullable (org.jetbrains.annotations.Nullable)1