Package com.intellij.openapi.editor

Examples of com.intellij.openapi.editor.SelectionModel


  protected AbstractStringManipAction(boolean setupHandler) {
    super(null);
    if (setupHandler) {
      this.setupHandler(new EditorWriteActionHandler() {
        public void executeWriteAction(Editor editor, DataContext dataContext) {
          final SelectionModel selectionModel = editor.getSelectionModel();
          String selectedText = selectionModel.getSelectedText();

          boolean allLinSelected = false;
          if (selectedText == null) {
            selectionModel.selectLineAtCaret();
            selectedText = selectionModel.getSelectedText();
            allLinSelected = true;

            if (selectedText == null) {
              return;
            }
          } else if (selectedText.endsWith("\n")) {
            allLinSelected = true;

          }
          String[] textParts = selectedText.split("\n");
          if (selectionModel.hasBlockSelection()) {
            int[] blockStarts = selectionModel.getBlockSelectionStarts();
            int[] blockEnds = selectionModel.getBlockSelectionEnds();

            int plusOffset = 0;

            for (int i = 0; i < textParts.length; i++) {

              String newTextPart = transform(textParts[i]);
              if (allLinSelected) {
                newTextPart += "\n";
              }

              editor.getDocument().replaceString(blockStarts[i] + plusOffset, blockEnds[i] + plusOffset,
                  newTextPart);

              int realOldTextLength = blockEnds[i] - blockStarts[i];
              plusOffset += newTextPart.length() - realOldTextLength;
            }
          } else {
            for (int i = 0; i < textParts.length; i++) {
              textParts[i] = transform(textParts[i]);
            }

            final String s = StringUtils.join(textParts, '\n');
            editor.getDocument().replaceString(selectionModel.getSelectionStart(),
                selectionModel.getSelectionEnd(), s);
            if (allLinSelected) {
              editor.getDocument().insertString(selectionModel.getSelectionEnd(), "\n");
            }
          }
        }
      });
    }
View Full Code Here


                final int line = caretModel.getLogicalPosition().line;
                final int column = caretModel.getLogicalPosition().column;
        long offset = caretModel.getOffset();

        final SelectionModel selectionModel = editor.getSelectionModel();
        boolean hasSelection = selectionModel.hasSelection();
        if (hasSelection == false) {
                    selectionModel.selectLineAtCaret();
                }
                final String selectedText = selectionModel.getSelectedText();

                if (selectedText != null) {
                    String[] textParts = StringUtil
                            .splitPreserveAllTokens(selectedText, DuplicatUtils.SIMPLE_NUMBER_REGEX);
                    for (int i = 0; i < textParts.length; i++) {
                        textParts[i] = DuplicatUtils.simpleInc(textParts[i]);
                    }

                    final String s = StringUtils.join(textParts);
                    editor.getDocument().insertString(selectionModel.getSelectionEnd(), s);

          if (hasSelection) {
            long selectionStart = selectionModel.getSelectionStart();
            long selectionEnd = selectionModel.getSelectionEnd();
            long length = s.length();
            caretModel.moveToOffset((int) (offset+length));
            selectionModel.setSelection((int) (selectionStart + length), (int) (selectionEnd + length));
            editor.getScrollingModel().scrollToCaret(ScrollType.RELATIVE);
          } else {
            selectionModel.removeSelection();
            caretModel.moveToLogicalPosition(new LogicalPosition(line + 1, column));
          }
                }
            }
        });
View Full Code Here

  @Nullable
  private static PsiElement findTargetElement(Editor editor, PsiFile file) {
    int offset = editor.getCaretModel().getOffset();
    PsiElement endElement = file.findElementAt(offset);
    SelectionModel selectionModel = editor.getSelectionModel();
    if (selectionModel.hasSelection() && selectionModel.getSelectionStart() < offset) {
      PsiElement startElement = file.findElementAt(selectionModel.getSelectionStart());
      if (startElement != null && startElement != endElement && startElement.getTextRange().getEndOffset() == offset) {
        return startElement;
      }
    }
    return endElement;
View Full Code Here

  @Override
  public void invoke(@NotNull final Project project, final Editor editor, final PsiFile file, @Nullable DataContext dataContext) {
    if (!(file instanceof BnfFileImpl)) return;

    BnfFile bnfFile = (BnfFileImpl)file;
    SelectionModel selectionModel = editor.getSelectionModel();
    int[] starts = selectionModel.getBlockSelectionStarts();
    int[] ends = selectionModel.getBlockSelectionEnds();
    if (starts.length == 0) return;

    int startOffset = starts[0];
    int endOffset = ends[ends.length-1];
    final BnfRule currentRule = PsiTreeUtil.getParentOfType(file.findElementAt(startOffset), BnfRule.class);
    BnfExpression parentExpression = currentRule != null ? findParentExpression(bnfFile, startOffset, endOffset) : null;
    if (parentExpression == null) {
      CommonRefactoringUtil.showErrorHint(editor.getProject(), editor, RefactoringBundle.message("refactoring.introduce.context.error"), "Error", null);
      return;
    }

    if (!selectionModel.hasSelection()) {
      List<BnfExpression> expressions = ContainerUtil.newArrayList();
      while (parentExpression != null) {
        expressions.add(parentExpression);
        parentExpression = PsiTreeUtil.getParentOfType(parentExpression, BnfExpression.class);
      }
View Full Code Here

    public void actionPerformed(AnActionEvent event) {
        DataContext context = event.getDataContext();
        Editor editor = DataKeys.EDITOR.getData(context);

        String selectedText = null;
        SelectionModel selection = null;
        if (editor != null) {
            selection = editor.getSelectionModel();
            if (selection.hasSelection()) {
                selectedText = selection.getSelectedText();
            }

            if (selectedText != null && selectedText.trim().length() > 0) {

      Document doc = editor.getDocument();
View Full Code Here

    public void actionPerformed(AnActionEvent event) {
        DataContext context = event.getDataContext();
        Editor editor = DataKeys.EDITOR.getData(context);

        String selectedText = null;
        SelectionModel selection = null;
        if (editor != null) {
            selection = editor.getSelectionModel();
            if (selection.hasSelection()) {
                selectedText = selection.getSelectedText();
            }

            if (selectedText != null && selectedText.trim().length() > 0) {

                Document doc = editor.getDocument();
View Full Code Here

  public void invoke(@NotNull Project project, @NotNull PsiElement[] elements, DataContext dataContext) {
  }

  @Override
  public void invoke(@NotNull Project project, Editor editor, PsiFile file, DataContext dataContext) {
    final SelectionModel selectionModel = editor.getSelectionModel();
    if (!selectionModel.hasSelection()) selectionModel.selectLineAtCaret();

    final PsiElement[] elements =
      DartRefactoringUtil.findStatementsInRange(file, selectionModel.getSelectionStart(), selectionModel.getSelectionEnd());

    if (elements.length == 0 || (elements.length == 1 && elements[0] instanceof DartExpression)) {
      // todo
      CommonRefactoringUtil.showErrorHint(
        project,
        editor,
        RefactoringBundle.getCannotRefactorMessage(DartBundle.message("dart.refactoring.extract.method.from.expression.error")),
        DartBundle.message("dart.refactoring.extract.method.error"),
        null
      );
      return;
    }

    final DartControlFlow controlFlow = DartControlFlow.analyze(elements);

    selectionModel.isBlockSelectionGuarded();

    if (controlFlow.getReturnValues().size() > 1) {
      CommonRefactoringUtil.showErrorHint(
        project,
        editor,
View Full Code Here

      }
    }

    PsiElement element1 = null;
    PsiElement element2 = null;
    final SelectionModel selectionModel = editor.getSelectionModel();
    if (selectionModel.hasSelection()) {
      element1 = file.findElementAt(selectionModel.getSelectionStart());
      element2 = file.findElementAt(selectionModel.getSelectionEnd() - 1);
      if (element1 instanceof PsiWhiteSpace) {
        int startOffset = element1.getTextRange().getEndOffset();
        element1 = file.findElementAt(startOffset);
      }
      if (element2 instanceof PsiWhiteSpace) {
View Full Code Here

  @Nullable
  private static String getSelectionText(AnActionEvent e) {
    Editor editor = CommonDataKeys.EDITOR.getData(e.getDataContext());
    if (editor != null) {
      SelectionModel model = editor.getSelectionModel();
      return model.getSelectedText();
    }
    return null;
  }
View Full Code Here

            final GoFile file = (GoFile) psiFile;

            PsiDocumentManager.getInstance(project).commitAllDocuments();

            final SelectionModel sm = editor.getSelectionModel();
            if (sm.hasSelection()) {
                introduce(project, editor, file, sm.getSelectionStart(), sm.getSelectionEnd());
                return;
            }

            // If nothing is selected in editor, find all potential expressions.
            int offset = editor.getCaretModel().getOffset();
View Full Code Here

TOP

Related Classes of com.intellij.openapi.editor.SelectionModel

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.