Java Code Examples for com.intellij.openapi.editor.ex.util.EditorUtil#getSelectionInAnyMode()

The following examples show how to use com.intellij.openapi.editor.ex.util.EditorUtil#getSelectionInAnyMode() . 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: ShowExpressionTypeHandler.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Nonnull
@RequiredReadAction
private static Map<PsiElement, ExpressionTypeProvider> getExpressions(@Nonnull PsiFile file, @Nonnull Editor editor, @Nonnull Set<? extends ExpressionTypeProvider> handlers) {
  if (handlers.isEmpty()) return Collections.emptyMap();
  boolean exactRange = false;
  TextRange range = EditorUtil.getSelectionInAnyMode(editor);
  final Map<PsiElement, ExpressionTypeProvider> map = new LinkedHashMap<>();
  int offset = !range.isEmpty() ? range.getStartOffset() : TargetElementUtil.adjustOffset(file, editor.getDocument(), range.getStartOffset());
  for (int i = 0; i < 3 && map.isEmpty() && offset >= i; i++) {
    PsiElement elementAt = file.findElementAt(offset - i);
    if (elementAt == null) continue;
    for (ExpressionTypeProvider handler : handlers) {
      for (PsiElement element : ((ExpressionTypeProvider<? extends PsiElement>)handler).getExpressionsAt(elementAt)) {
        TextRange r = element.getTextRange();
        if (exactRange && !r.equals(range) || !r.contains(range)) continue;
        if (!exactRange) exactRange = r.equals(range);
        map.put(element, handler);
      }
    }
  }
  return map;
}
 
Example 2
Source File: ConsoleHistoryController.java    From consulo with Apache License 2.0 5 votes vote down vote up
protected int insertTextMultiline(String text, Editor editor, Document document) {
  TextRange selection = EditorUtil.getSelectionInAnyMode(editor);

  int start = document.getLineStartOffset(document.getLineNumber(selection.getStartOffset()));
  int end = document.getLineEndOffset(document.getLineNumber(selection.getEndOffset()));

  document.replaceString(start, end, text);
  editor.getSelectionModel().setSelection(start, start + text.length());
  return start;
}
 
Example 3
Source File: RunIdeConsoleAction.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nonnull
private static String getCommandText(@Nonnull Project project, @Nonnull Editor editor) {
  TextRange selectedRange = EditorUtil.getSelectionInAnyMode(editor);
  Document document = editor.getDocument();
  if (selectedRange.isEmpty()) {
    int line = document.getLineNumber(selectedRange.getStartOffset());
    selectedRange = TextRange.create(document.getLineStartOffset(line), document.getLineEndOffset(line));

    // try detect a non-trivial composite PSI element if there's a PSI file
    PsiFile file = PsiDocumentManager.getInstance(project).getPsiFile(editor.getDocument());
    if (file != null && file.getFirstChild() != null && file.getFirstChild() != file.getLastChild()) {
      PsiElement e1 = file.findElementAt(selectedRange.getStartOffset());
      PsiElement e2 = file.findElementAt(selectedRange.getEndOffset());
      while (e1 != e2 && (e1 instanceof PsiWhiteSpace || e1 != null && StringUtil.isEmptyOrSpaces(e1.getText()))) {
        e1 = ObjectUtils.chooseNotNull(e1.getNextSibling(), PsiTreeUtil.getDeepestFirst(e1.getParent()));
      }
      while (e1 != e2 && (e2 instanceof PsiWhiteSpace || e2 != null && StringUtil.isEmptyOrSpaces(e2.getText()))) {
        e2 = ObjectUtils.chooseNotNull(e2.getPrevSibling(), PsiTreeUtil.getDeepestLast(e2.getParent()));
      }
      if (e1 instanceof LeafPsiElement) e1 = e1.getParent();
      if (e2 instanceof LeafPsiElement) e2 = e2.getParent();
      PsiElement parent = e1 == null ? e2 : e2 == null ? e1 : PsiTreeUtil.findCommonParent(e1, e2);
      if (parent != null && parent != file) {
        selectedRange = parent.getTextRange();
      }
    }
  }
  return document.getText(selectedRange);
}