Java Code Examples for com.intellij.psi.util.PsiUtilCore#getElementAtOffset()

The following examples show how to use com.intellij.psi.util.PsiUtilCore#getElementAtOffset() . 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: AnnotationUseImporter.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
public static void insertUse(InsertionContext context, String fqnAnnotation) {
    PsiElement element = PsiUtilCore.getElementAtOffset(context.getFile(), context.getStartOffset());
    PhpPsiElement scopeForUseOperator = PhpCodeInsightUtil.findScopeForUseOperator(element);

    if(null == scopeForUseOperator) {
        return;
    }

    // PhpCodeInsightUtil.canImport:
    // copied from PhpReferenceInsertHandler; throws an error on PhpContractUtil because of "fully qualified names only"
    // but that is catch on phpstorm side already; looks fixed now so use fqn

    if(!fqnAnnotation.startsWith("\\")) {
        fqnAnnotation = "\\" + fqnAnnotation;
    }

    // this looks suitable! :)
    if(PhpCodeInsightUtil.alreadyImported(scopeForUseOperator, fqnAnnotation) == null) {
        PsiDocumentManager.getInstance(context.getProject()).commitDocument(context.getDocument());
        PhpAliasImporter.insertUseStatement(fqnAnnotation, scopeForUseOperator);
        PsiDocumentManager.getInstance(context.getProject()).doPostponedOperationsAndUnblockDocument(context.getDocument());
    }
}
 
Example 2
Source File: CustomTemplateCallback.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Nonnull
public static PsiElement getContext(@Nonnull PsiFile file, int offset, boolean searchInInjectedFragment) {
  PsiElement element = null;
  if (searchInInjectedFragment && !InjectedLanguageManager.getInstance(file.getProject()).isInjectedFragment(file)) {
    PsiDocumentManager documentManager = PsiDocumentManager.getInstance(file.getProject());
    Document document = documentManager.getDocument(file);
    if (document != null && !documentManager.isCommitted(document)) {
      LOGGER.error("Trying to access to injected template context on uncommited document, offset = " + offset, AttachmentFactory.createAttachment(file.getVirtualFile()));
    }
    else {
      element = InjectedLanguageManager.getInstance(file.getProject()).findInjectedElementAt(file, offset);
    }
  }
  if (element == null) {
    element = PsiUtilCore.getElementAtOffset(file, offset);
  }
  return element;
}
 
Example 3
Source File: Utils.java    From silex-idea-plugin with MIT License 5 votes vote down vote up
@Override
public void handleInsert(InsertionContext context, LookupElement item) {
    PsiElement element = PsiUtilCore.getElementAtOffset(context.getFile(), context.getStartOffset());
    context.getDocument().deleteString(context.getStartOffset() + item.getLookupString().length(), context.getStartOffset() + element.getText().length());
    // move caret after ]
    context.getEditor().getCaretModel().moveCaretRelatively(2, 0, false, false, true);
}
 
Example 4
Source File: EnterAfterUnmatchedBraceHandler.java    From consulo with Apache License 2.0 4 votes vote down vote up
/**
 * Current handler inserts closing curly brace (right brace) if necessary. There is a possible case that it should be located
 * more than one line forward.
 * <p/>
 * <b>Example</b>
 * <pre>
 *     if (test1()) {
 *     } else {<caret> if (test2()) {
 *         foo();
 *     }
 * </pre>
 * <p/>
 * We want to get this after the processing:
 * <pre>
 *     if (test1()) {
 *     } else {
 *         if (test2()) {
 *             foo();
 *         }
 *     }
 * </pre>
 * I.e. closing brace should be inserted two lines below current caret line. Hence, we need to calculate correct offset
 * to use for brace inserting. This method is responsible for that.
 * <p/>
 * In essence it inspects PSI structure and finds PSE elements with the max length that starts at caret offset. End offset
 * of that element is used as an insertion point.
 *
 * @param file   target PSI file
 * @param text   text from the given file
 * @param offset target offset where line feed will be inserted
 * @return pair of (element, offset). The element is the '}' owner, if applicable; the offset is the position for inserting closing brace
 */
protected Pair<PsiElement, Integer> calculateOffsetToInsertClosingBrace(@Nonnull PsiFile file, @Nonnull CharSequence text, final int offset) {
  PsiElement element = PsiUtilCore.getElementAtOffset(file, offset);
  ASTNode node = element.getNode();
  if (node != null && node.getElementType() == TokenType.WHITE_SPACE) {
    return Pair.create(null, CharArrayUtil.shiftForwardUntil(text, offset, "\n"));
  }
  for (PsiElement parent = element.getParent(); parent != null; parent = parent.getParent()) {
    ASTNode parentNode = parent.getNode();
    if (parentNode == null || parentNode.getStartOffset() != offset) {
      break;
    }
    element = parent;
  }
  if (element.getTextOffset() != offset) {
    return Pair.create(null, CharArrayUtil.shiftForwardUntil(text, offset, "\n"));
  }
  return Pair.create(element, calculateOffsetToInsertClosingBraceInsideElement(element));
}
 
Example 5
Source File: GotoTestOrCodeHandler.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Nonnull
public static PsiElement getSelectedElement(Editor editor, PsiFile file) {
  return PsiUtilCore.getElementAtOffset(file, editor.getCaretModel().getOffset());
}