Search in sources :

Example 1 with DirectiveStatement

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

the class RedundantIncludeInspection method buildVisitor.

@NotNull
@Override
public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, boolean isOnTheFly) {
    return new OxyTemplateElementVisitor() {

        @Override
        public void visitDirectiveParamFileReference(@NotNull DirectiveParamFileReference fileReference) {
            final DirectiveStatement directiveStatement = PsiTreeUtil.getParentOfType(fileReference, DirectiveStatement.class);
            assert directiveStatement != null;
            if (!IncludeOnceDirective.NAME.equals(directiveStatement.getName()) || IncludeOptimizer.ignore(directiveStatement)) {
                return;
            }
            PsiReference[] references = fileReference.getReferences();
            PsiFile referencedFile = null;
            for (PsiReference reference : references) {
                if (reference instanceof FileReference && reference.resolve() instanceof PsiFile) {
                    referencedFile = (PsiFile) reference.resolve();
                    break;
                }
            }
            if (referencedFile == null) {
                return;
            }
            if (!directiveStatement.getContainingFile().getVirtualFile().getPath().equals(referencedFile.getVirtualFile().getPath())) {
                for (Map.Entry<PsiElement, JSElement> entry : OxyTemplateHelper.getUsedJsMacros(fileReference.getContainingFile()).entrySet()) {
                    if (entry.getValue().getContainingFile().getVirtualFile().getPath().equals(referencedFile.getVirtualFile().getPath())) {
                        return;
                    }
                }
            }
            holder.registerProblem(directiveStatement, getDisplayName(), ProblemHighlightType.LIKE_UNUSED_SYMBOL, new DeleteElementQuickFix());
        }
    };
}
Also used : PsiReference(com.intellij.psi.PsiReference) NotNull(org.jetbrains.annotations.NotNull) OxyTemplateElementVisitor(ool.intellij.plugin.psi.OxyTemplateElementVisitor) JSElement(com.intellij.lang.javascript.psi.JSElement) DeleteElementQuickFix(ool.intellij.plugin.editor.inspection.fix.DeleteElementQuickFix) PsiFile(com.intellij.psi.PsiFile) DirectiveParamFileReference(ool.intellij.plugin.psi.DirectiveParamFileReference) FileReference(com.intellij.psi.impl.source.resolve.reference.impl.providers.FileReference) DirectiveParamFileReference(ool.intellij.plugin.psi.DirectiveParamFileReference) Map(java.util.Map) PsiElement(com.intellij.psi.PsiElement) DirectiveStatement(ool.intellij.plugin.psi.DirectiveStatement) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with DirectiveStatement

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

the class MissingIncludeDirectiveQuickFix method applyFix.

public void applyFix(Project project) {
    final DirectiveStatement includeDirective = OxyTemplateElementFactory.createDirectiveStatement(project, directiveName, includePath);
    OxyTemplateHelper.addDirective(includeDirective, macroCallMissingInclude.getContainingFile().getViewProvider().getPsi(OxyTemplate.INSTANCE));
}
Also used : DirectiveStatement(ool.intellij.plugin.psi.DirectiveStatement)

Example 3 with DirectiveStatement

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

the class IncludeAutoInsert method handleInsert.

@Override
public void handleInsert(InsertionContext context, LookupElementDecorator<LookupElement> item) {
    item.getDelegate().handleInsert(context);
    if (!(item.getObject() instanceof JSElement)) {
        return;
    }
    JSElement macroDefinition = (JSElement) item.getObject();
    if (OxyTemplateHelper.isJsMacroMissingInclude(context.getFile().getViewProvider().getPsi(OxyTemplate.INSTANCE), macroDefinition)) {
        RelativePathCalculator pathCalculator = new RelativePathCalculator(context.getFile().getVirtualFile().getPath(), macroDefinition.getContainingFile().getVirtualFile().getPath());
        pathCalculator.execute();
        DirectiveStatement includeDirective = OxyTemplateElementFactory.createDirectiveStatement(context.getProject(), IncludeOnceDirective.NAME, pathCalculator.getResult());
        OxyTemplateHelper.addDirective(includeDirective, context.getFile());
    }
}
Also used : JSElement(com.intellij.lang.javascript.psi.JSElement) RelativePathCalculator(ool.intellij.plugin.file.RelativePathCalculator) DirectiveStatement(ool.intellij.plugin.psi.DirectiveStatement)

Example 4 with DirectiveStatement

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

DirectiveStatement (ool.intellij.plugin.psi.DirectiveStatement)4 JSElement (com.intellij.lang.javascript.psi.JSElement)2 PsiElement (com.intellij.psi.PsiElement)2 FileViewProvider (com.intellij.psi.FileViewProvider)1 PsiErrorElement (com.intellij.psi.PsiErrorElement)1 PsiFile (com.intellij.psi.PsiFile)1 PsiReference (com.intellij.psi.PsiReference)1 FileReference (com.intellij.psi.impl.source.resolve.reference.impl.providers.FileReference)1 Map (java.util.Map)1 DeleteElementQuickFix (ool.intellij.plugin.editor.inspection.fix.DeleteElementQuickFix)1 OxyTemplateFileViewProvider (ool.intellij.plugin.file.OxyTemplateFileViewProvider)1 RelativePathCalculator (ool.intellij.plugin.file.RelativePathCalculator)1 DirectiveParamFileReference (ool.intellij.plugin.psi.DirectiveParamFileReference)1 MacroAttribute (ool.intellij.plugin.psi.MacroAttribute)1 MacroEmptyTag (ool.intellij.plugin.psi.MacroEmptyTag)1 OxyTemplateElementVisitor (ool.intellij.plugin.psi.OxyTemplateElementVisitor)1 NotNull (org.jetbrains.annotations.NotNull)1