Java Code Examples for com.intellij.psi.FileViewProvider#findElementAt()

The following examples show how to use com.intellij.psi.FileViewProvider#findElementAt() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: XQueryXmlGtTypedHandler.java    From intellij-xquery with Apache License 2.0 6 votes vote down vote up
@Override
public Result beforeCharTyped(final char c, final Project project, final Editor editor, final PsiFile editedFile, final FileType fileType) {
    if ((editedFile.getLanguage() instanceof XQueryLanguage) && c == '>') {
        PsiDocumentManager.getInstance(project).commitAllDocuments();
        PsiFile file = PsiDocumentManager.getInstance(project).getPsiFile(editor.getDocument());
        if (file == null) return Result.CONTINUE;
        final int offset = editor.getCaretModel().getOffset();
        FileViewProvider provider = file.getViewProvider();
        PsiElement element = provider.findElementAt(offset, XQueryLanguage.class);

        if (element != null && element.getNode() != null && (
                element.getNode().getElementType() == XQueryTypes.XMLTAGEND ||
                        element.getNode().getElementType() == XQueryTypes.XMLEMPTYELEMENTEND)) {
            EditorModificationUtil.moveCaretRelatively(editor, 1);
            editor.getScrollingModel().scrollToCaret(ScrollType.RELATIVE);
            return Result.STOP;
        }
    }
    return Result.CONTINUE;
}
 
Example 2
Source File: XQueryXmlSlashTypedHandler.java    From intellij-xquery with Apache License 2.0 6 votes vote down vote up
@Override
public Result beforeCharTyped(final char c, final Project project, final Editor editor, final PsiFile editedFile, final FileType fileType) {
    if ((editedFile.getLanguage() instanceof XQueryLanguage) && c == '/') {
        PsiDocumentManager.getInstance(project).commitAllDocuments();
        PsiFile file = PsiDocumentManager.getInstance(project).getPsiFile(editor.getDocument());
        if (file == null) return Result.CONTINUE;
        final int offset = editor.getCaretModel().getOffset();
        FileViewProvider provider = file.getViewProvider();
        PsiElement element = provider.findElementAt(offset, XQueryLanguage.class);
        if (isAtTheSlashOfClosingOfEmptyTag(offset, element)) {
            moveCaretByOne(editor, offset);
            return Result.STOP;
        }
    }
    return Result.CONTINUE;
}
 
Example 3
Source File: DustTypedHandler.java    From Intellij-Dust with MIT License 6 votes vote down vote up
/**
 * When appropriate, automatically reduce the indentation for else tags "{:else}"
 */
private void adjustFormatting(Project project, int offset, Editor editor, PsiFile file, FileViewProvider provider) {
  PsiElement elementAtCaret = provider.findElementAt(offset - 1, DustLanguage.class);
  PsiElement elseParent = PsiTreeUtil.findFirstParent(elementAtCaret, true, new Condition<PsiElement>() {
    @Override
    public boolean value(PsiElement element) {
      return element != null
          && (element instanceof DustElseTag);
    }
  });

  // run the formatter if the user just completed typing a SIMPLE_INVERSE or a CLOSE_BLOCK_STACHE
  if (elseParent != null) {
    // grab the current caret position (AutoIndentLinesHandler is about to mess with it)
    PsiDocumentManager.getInstance(project).commitAllDocuments();
    CaretModel caretModel = editor.getCaretModel();
    CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(project);
    codeStyleManager.adjustLineIndent(file, editor.getDocument().getLineStartOffset(caretModel.getLogicalPosition().line));
  }
}
 
Example 4
Source File: FunctionDeclarationBracesBodyHandler.java    From intellij-xquery with Apache License 2.0 5 votes vote down vote up
@Override
public Result charTyped(char c, Project project, @NotNull Editor editor, @NotNull PsiFile editedFile) {
    if ((editedFile.getLanguage() instanceof XQueryLanguage) && (c == '{' || c == '}')) {
        PsiDocumentManager.getInstance(project).commitAllDocuments();
        PsiFile file = PsiDocumentManager.getInstance(project).getPsiFile(editor.getDocument());
        if (file == null) return Result.CONTINUE;
        FileViewProvider provider = file.getViewProvider();
        final int offset = editor.getCaretModel().getOffset();
        PsiElement element = provider.findElementAt(offset - 1, XQueryLanguage.class);
        if (element == null) return Result.CONTINUE;
        if (!(element.getLanguage() instanceof XQueryLanguage)) return Result.CONTINUE;
        ASTNode prevLeaf = element.getNode();
        if (prevLeaf == null) return Result.CONTINUE;
        final String prevLeafText = prevLeaf.getText();

        if (isInFunctionBodyAfterInsertionOfMatchingRightBrace(element, prevLeafText)) {
            if (c == '{') {
                editor.getDocument().insertString(offset + 1, ";");
            } else {
                EditorModificationUtil.insertStringAtCaret(editor, ";", false);
            }
        }

    }

    return Result.CONTINUE;
}
 
Example 5
Source File: XQueryXmlSlashTypedHandler.java    From intellij-xquery with Apache License 2.0 5 votes vote down vote up
@Override
public Result charTyped(char c, final Project project, final @NotNull Editor editor, @NotNull final PsiFile editedFile) {
    if ((editedFile.getLanguage() instanceof XQueryLanguage) && c == '/') {
        PsiDocumentManager.getInstance(project).commitAllDocuments();
        PsiFile file = PsiDocumentManager.getInstance(project).getPsiFile(editor.getDocument());
        if (file == null) return Result.CONTINUE;
        FileViewProvider provider = file.getViewProvider();
        final int offset = editor.getCaretModel().getOffset();
        PsiElement element = provider.findElementAt(offset - 1, XQueryLanguage.class);
        if (element == null) return Result.CONTINUE;
        if (!(element.getLanguage() instanceof XQueryLanguage)) return Result.CONTINUE;
        ASTNode prevLeaf = element.getNode();
        if (prevLeaf == null) return Result.CONTINUE;
        final String prevLeafText = prevLeaf.getText();
        if (isStartOfEndOfTag(prevLeaf, prevLeafText)) {
            XQueryXmlFullTag tag = PsiTreeUtil.getParentOfType(element, XQueryXmlFullTag.class);
            if (tag != null) {
                XQueryXmlTagName tagName = tag.getXmlTagNameList().get(0);
                if (hasNoClosingTagName(prevLeaf, tag, tagName)) {
                    finishClosingTag(editor, tagName);
                    return Result.STOP;
                }
            }
        }
        if (!"/".equals(prevLeafText.trim())) return Result.CONTINUE;
        prevLeaf = getPreviousNonWhiteSpaceLeaf(prevLeaf);
        if (prevLeaf == null) return Result.CONTINUE;
        if (PsiTreeUtil.getParentOfType(element, XQueryDirAttributeValue.class) != null) return Result.CONTINUE;
        if (prevLeaf.getElementType() == XQueryTypes.ELEMENTCONTENTCHAR) return Result.CONTINUE;
        XQueryEnclosedExpression parentEnclosedExpression = PsiTreeUtil.getParentOfType(element, XQueryEnclosedExpression.class, true, XQueryXmlFullTag.class);
        XQueryXmlFullTag fullTag = getParentFullTag(prevLeaf);
        if (isInEnclosedExpressionNestedInXmlTag(parentEnclosedExpression, fullTag)) return Result.CONTINUE;
        if (isInUnclosedXmlTag(fullTag)) {
            closeEmptyTag(editor);
            return Result.STOP;
        }
    }
    return Result.CONTINUE;
}
 
Example 6
Source File: DustTypedHandler.java    From Intellij-Dust with MIT License 5 votes vote down vote up
/**
 * When appropriate, auto-inserts closing tags.  i.e.  When "{#tagName}" or "{^tagName} is typed,
 *      {/tagName} is automatically inserted
 */
private void autoInsertCloseTag(Project project, int offset, Editor editor, FileViewProvider provider) {
  PsiDocumentManager.getInstance(project).commitAllDocuments();

  PsiElement elementAtCaret = provider.findElementAt(offset - 1, DustLanguage.class);

  PsiElement openTag = DustPsiUtil.findParentOpenTagElement(elementAtCaret);

  String tagName = getTagName(openTag);

  if (!tagName.trim().equals("")) {
    boolean addCloseTag = true;
    PsiElement sibling = openTag.getNextSibling();
    DustCloseTag closeTag;
    while (sibling != null && addCloseTag) {
      if (sibling instanceof DustCloseTag) {
        closeTag = (DustCloseTag) sibling;
        if (getTagName(closeTag).equals(tagName)) {
          addCloseTag = false;
        }
      }
      sibling = sibling.getNextSibling();
    }

    if (addCloseTag) {
      // insert the corresponding close tag
      editor.getDocument().insertString(offset, "{/" + tagName + "}");
    }
  }
}