Package com.intellij.openapi.editor

Examples of com.intellij.openapi.editor.CaretModel


        // Column mode not supported
        if (editor.isColumnMode()) {
          return;
        }
        final CaretModel caretModel = editor.getCaretModel();

        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.simpleDec(textParts[i]);
          }

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

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


                //Column mode not supported
                if (editor.isColumnMode()) {
                    return;
                }
                final CaretModel caretModel = editor.getCaretModel();

                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 PsiElement findMatchingElement( @NotNull PsiFile file, @NotNull Editor editor )
    {
        CaretModel caretModel = editor.getCaretModel();
        int position = caretModel.getOffset();
        PsiElement element = file.findElementAt( position );
        return findMatchingElement( element );
    }
View Full Code Here

    }
    else {
      if (smartIntroduce(operation)) {
        return;
      }
      final CaretModel caretModel = editor.getCaretModel();
      final Document document = editor.getDocument();
      int lineNumber = document.getLineNumber(caretModel.getOffset());
      if ((lineNumber >= 0) && (lineNumber < document.getLineCount())) {
        element1 = file.findElementAt(document.getLineStartOffset(lineNumber));
        element2 = file.findElementAt(document.getLineEndOffset(lineNumber) - 1);
      }
    }
View Full Code Here

    // run the formatter if the user just completed typing a SIMPLE_INVERSE or a CLOSE_BLOCK_STACHE
    if (closeOrSimpleInverseParent != null) {
      // grab the current caret position (AutoIndentLinesHandler is about to mess with it)
      PsiDocumentManager.getInstance(project).commitDocument(editor.getDocument());
      CaretModel caretModel = editor.getCaretModel();
      CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(project);
      codeStyleManager.adjustLineIndent(file, editor.getDocument().getLineStartOffset(caretModel.getLogicalPosition().line));
    }
  }
View Full Code Here

  }

  private void restoreVisualColumn(Editor editor, int visualColumnToRestore) {
    if (visualColumnToRestore < 0) {
    } else {
      CaretModel caretModel = editor.getCaretModel();
      VisualPosition position = caretModel.getVisualPosition();
      if (visualColumnToRestore != position.column) {
        caretModel.moveToVisualPosition(new VisualPosition(position.line, visualColumnToRestore));
      }
    }
  }
View Full Code Here

    }
    return moveCaret(editor, tailOffset, 1);
  }

  protected static int moveCaret(final Editor editor, final int tailOffset, final int delta) {
    final CaretModel model = editor.getCaretModel();
    if (model.getOffset() == tailOffset) {
      model.moveToOffset(tailOffset + delta);
    }
    return tailOffset + delta;
  }
View Full Code Here

    PsiElement lastChild = psiFile.getLastChild();
    int endOffset = lastChild.getTextRange().getEndOffset();

    boolean needNewline = !(lastChild instanceof PsiWhiteSpace && StringUtil.countNewLines(lastChild.getText()) > 0);

    CaretModel caretModel = editor.getCaretModel();
    ErlangFunction function = PsiTreeUtil.getParentOfType(psiFile.findElementAt(caretModel.getOffset()), ErlangFunction.class);
    String name = function != null ? function.getName() : "name";
    caretModel.moveToOffset(endOffset);
    insertTestFunction(project, editor, name, needNewline);
  }
View Full Code Here

        String endText = "\n\nend" + (addComma ? "," : "");
        editor.getDocument().insertString(offset, endText);

        PsiDocumentManager.getInstance(project).commitDocument(editor.getDocument());
        CodeStyleManager.getInstance(project).adjustLineIndent(file, TextRange.from(offset, endText.length()));
        CaretModel caretModel = editor.getCaretModel();
        caretModel.moveCaretRelatively(0, 1, false, false, true);
        CodeStyleManager.getInstance(project).adjustLineIndent(file, caretModel.getOffset());
      }
    });
  }
View Full Code Here

        }

        // Process input and add to history
        final Editor editor = console.getCurrentEditor();
        final Document document = editor.getDocument();
        final CaretModel caretModel = editor.getCaretModel();
        final int offset = caretModel.getOffset();
        final String text = document.getText();

        if (!text.substring(offset).trim().isEmpty()) {
            final String before = text.substring(0, offset);
            final String after = text.substring(offset);
            final FileASTNode node = console.getFile().getNode();
            final Project project = editor.getProject();
            final int indent = indentHelper.getIndent(project, JavaFileType.INSTANCE, node);
            final String spaces = IndentHelperImpl.fillIndent(project, JavaFileType.INSTANCE, indent);
            final String newText = before + "\n" + spaces + after;

            new WriteCommandAction(JavaREPLConsoleExecuteActionHandler.this.project) {
                @Override
                protected void run(Result result) throws Throwable {
                    console.setInputText(newText);
                    caretModel.moveToOffset(offset + indent + 1);
                }
            }.execute();

            return;
        }
View Full Code Here

TOP

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

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.