Search in sources :

Example 6 with CfmlTag

use of com.intellij.coldFusion.model.psi.CfmlTag in project intellij-plugins by JetBrains.

the class CfmlTypedHandler method insertCloseTagIfNeeded.

public static boolean insertCloseTagIfNeeded(Editor editor, PsiFile file, Project project) {
    final Document document = editor.getDocument();
    final PsiDocumentManager documentManager = PsiDocumentManager.getInstance(project);
    int offset = editor.getCaretModel().getOffset();
    documentManager.commitDocument(document);
    char charAtOffset = DocumentUtils.getCharAt(document, offset);
    if (charAtOffset != '>') {
        EditorModificationUtil.insertStringAtCaret(editor, ">", true, 0);
    }
    EditorModificationUtil.moveCaretRelatively(editor, 1);
    ++offset;
    if (DocumentUtils.getCharAt(document, offset - 2) == '/') {
        return false;
    }
    HighlighterIterator iterator = ((EditorEx) editor).getHighlighter().createIterator(offset - 2);
    while (!iterator.atEnd() && !iterator.getTokenType().equals(CfmlTokenTypes.CF_TAG_NAME)) {
        if (CfmlUtil.isControlToken(iterator.getTokenType())) {
            return false;
        }
        iterator.retreat();
    }
    if (!iterator.atEnd()) {
        iterator.retreat();
        if (!iterator.atEnd() && iterator.getTokenType().equals(CfmlTokenTypes.LSLASH_ANGLEBRACKET)) {
            return false;
        }
        iterator.advance();
    }
    if (iterator.atEnd()) {
        return false;
    }
    String tagName = document.getCharsSequence().subSequence(iterator.getStart(), iterator.getEnd()).toString();
    if (CfmlUtil.isSingleCfmlTag(tagName, project) || CfmlUtil.isUserDefined(tagName)) {
        return false;
    }
    PsiElement tagElement = file.findElementAt(iterator.getStart());
    while (tagElement != null && !(tagElement instanceof CfmlTag)) {
        tagElement = tagElement.getParent();
    }
    if (tagElement == null) {
        return false;
    }
    boolean doInsertion = false;
    if (tagElement.getLastChild() instanceof PsiErrorElement) {
        doInsertion = true;
    } else {
        iterator = ((EditorEx) editor).getHighlighter().createIterator(0);
        while (!iterator.atEnd() && iterator.getStart() < offset) {
            if (iterator.getTokenType() == CfmlTokenTypes.CF_TAG_NAME) {
                String currentTagName = document.getCharsSequence().subSequence(iterator.getStart(), iterator.getEnd()).toString();
                if (tagName.equals(currentTagName)) {
                    PsiElement currentTagElement = file.findElementAt(iterator.getStart());
                    currentTagElement = PsiTreeUtil.getParentOfType(currentTagElement, CfmlTag.class);
                    if (currentTagElement.getLastChild() instanceof PsiErrorElement) {
                        doInsertion = true;
                        break;
                    }
                }
            }
            iterator.advance();
        }
    }
    // tag name in lowercase
    String tagNameFromPsi = ((CfmlTag) tagElement).getTagName();
    if (doInsertion && CfmlUtil.isEndTagRequired(tagNameFromPsi, project)) {
        if (!Comparing.equal(tagNameFromPsi, tagName, false)) {
            // use tagName because it has proper case
            tagName = tagNameFromPsi;
        }
        EditorModificationUtil.insertStringAtCaret(editor, "</" + tagName + ">", true, 0);
        return true;
    }
    return false;
}
Also used : PsiErrorElement(com.intellij.psi.PsiErrorElement) EditorEx(com.intellij.openapi.editor.ex.EditorEx) CfmlTag(com.intellij.coldFusion.model.psi.CfmlTag) Document(com.intellij.openapi.editor.Document) HighlighterIterator(com.intellij.openapi.editor.highlighter.HighlighterIterator) PsiElement(com.intellij.psi.PsiElement) PsiDocumentManager(com.intellij.psi.PsiDocumentManager)

Aggregations

CfmlTag (com.intellij.coldFusion.model.psi.CfmlTag)6 PsiElement (com.intellij.psi.PsiElement)5 CfmlAttributeImpl (com.intellij.coldFusion.model.psi.impl.CfmlAttributeImpl)2 ASTNode (com.intellij.lang.ASTNode)2 HighlighterIterator (com.intellij.openapi.editor.highlighter.HighlighterIterator)2 TailType (com.intellij.codeInsight.TailType)1 CreateFileFix (com.intellij.codeInsight.daemon.quickFix.CreateFileFix)1 LocalQuickFix (com.intellij.codeInspection.LocalQuickFix)1 CfmlFile (com.intellij.coldFusion.model.files.CfmlFile)1 CfmlAttributeDescription (com.intellij.coldFusion.model.info.CfmlAttributeDescription)1 CfmlComponent (com.intellij.coldFusion.model.psi.CfmlComponent)1 CfmlFunctionImpl (com.intellij.coldFusion.model.psi.impl.CfmlFunctionImpl)1 CfmlPropertyImpl (com.intellij.coldFusion.model.psi.impl.CfmlPropertyImpl)1 CfmlTagFunctionImpl (com.intellij.coldFusion.model.psi.impl.CfmlTagFunctionImpl)1 FoldingDescriptor (com.intellij.lang.folding.FoldingDescriptor)1 Document (com.intellij.openapi.editor.Document)1 Editor (com.intellij.openapi.editor.Editor)1 EditorEx (com.intellij.openapi.editor.ex.EditorEx)1 TextRange (com.intellij.openapi.util.TextRange)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1