com.intellij.openapi.editor.ScrollType Java Examples

The following examples show how to use com.intellij.openapi.editor.ScrollType. 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: TextStartAction.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Override
public void execute(Editor editor, DataContext dataContext) {
  editor.getCaretModel().removeSecondaryCarets();
  editor.getCaretModel().moveToOffset(0);
  editor.getSelectionModel().removeSelection();

  ScrollingModel scrollingModel = editor.getScrollingModel();
  scrollingModel.disableAnimation();
  scrollingModel.scrollToCaret(ScrollType.RELATIVE);
  scrollingModel.enableAnimation();

  Project project = dataContext.getData(CommonDataKeys.PROJECT);
  if (project != null) {
    IdeDocumentHistory instance = IdeDocumentHistory.getInstance(project);
    if (instance != null) {
      instance.includeCurrentCommandAsNavigation();
    }
  }
}
 
Example #2
Source File: TwosideTextDiffViewer.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Override
public void actionPerformed(@Nonnull AnActionEvent e) {
  Side currentSide = getCurrentSide();
  Side targetSide = currentSide.other();

  EditorEx currentEditor = getEditor(currentSide);
  EditorEx targetEditor = getEditor(targetSide);

  if (myScrollToPosition) {
    LineCol position = transferPosition(currentSide, LineCol.fromCaret(currentEditor));
    targetEditor.getCaretModel().moveToOffset(position.toOffset(targetEditor));
  }

  setCurrentSide(targetSide);
  targetEditor.getScrollingModel().scrollToCaret(ScrollType.MAKE_VISIBLE);

  DiffUtil.requestFocus(getProject(), getPreferredFocusedComponent());
}
 
Example #3
Source File: TemplateManagerImpl.java    From consulo with Apache License 2.0 6 votes vote down vote up
public void startTemplateWithPrefix(final Editor editor,
                                    final TemplateImpl template,
                                    final int templateStart,
                                    @Nullable final PairProcessor<String, String> processor,
                                    @Nullable final String argument) {
  final int caretOffset = editor.getCaretModel().getOffset();
  final TemplateState templateState = initTemplateState(editor);
  CommandProcessor commandProcessor = CommandProcessor.getInstance();
  commandProcessor.executeCommand(myProject, () -> {
    editor.getDocument().deleteString(templateStart, caretOffset);
    editor.getCaretModel().moveToOffset(templateStart);
    editor.getScrollingModel().scrollToCaret(ScrollType.RELATIVE);
    editor.getSelectionModel().removeSelection();
    Map<String, String> predefinedVarValues = null;
    if (argument != null) {
      predefinedVarValues = new HashMap<>();
      predefinedVarValues.put(TemplateImpl.ARG, argument);
    }
    templateState.start(template, processor, predefinedVarValues);
  }, CodeInsightBundle.message("insert.code.template.command"), null);
}
 
Example #4
Source File: HighlightSymbolAction.java    From emacsIDEAs with Apache License 2.0 6 votes vote down vote up
public void actionPerformed(AnActionEvent e, boolean searchForward) {
    Project project = e.getData(PlatformDataKeys.PROJECT);
    if (!ToolWindowManager.getInstance(project).isEditorComponentActive()) {
        ToolWindowManager.getInstance(project).activateEditorComponent();
        return;
    }

    if (super.initAction(e)) {
        int nextSymbolOffset = getNextSymbolOffset(searchForward, project);
        if (-1 != nextSymbolOffset) {
            _editor.getCaretModel().moveToOffset(nextSymbolOffset);
            _editor.getScrollingModel().scrollToCaret(ScrollType.MAKE_VISIBLE);
            _editor.getSelectionModel().selectWordAtCaret(false);
        }
    }

    super.cleanupSetupsInAndBackToNormalEditingMode();
}
 
Example #5
Source File: ClassRefactoringHandlerBase.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Override
public void invoke(@Nonnull Project project, Editor editor, PsiFile file, DataContext dataContext) {
  int offset = editor.getCaretModel().getOffset();
  editor.getScrollingModel().scrollToCaret(ScrollType.MAKE_VISIBLE);
  final PsiElement position = file.findElementAt(offset);
  PsiElement element = position;

  while (true) {
    if (element == null || element instanceof PsiFile) {
      String message = RefactoringBundle
        .getCannotRefactorMessage(getInvalidPositionMessage());
      CommonRefactoringUtil.showErrorHint(project, editor, message, getTitle(), getHelpId());
      return;
    }

    if (!CommonRefactoringUtil.checkReadOnlyStatus(project, element)) return;

    if (acceptsElement(element)) {
      invoke(project, new PsiElement[]{position}, dataContext);
      return;
    }
    element = element.getParent();
  }
}
 
Example #6
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 #7
Source File: LombokElementRenameHandler.java    From lombok-intellij-plugin with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public void invoke(@NotNull Project project, Editor editor, PsiFile file, DataContext dataContext) {
  PsiElement element = PsiElementRenameHandler.getElement(dataContext);
  if (null == element) {
    element = BaseRefactoringAction.getElementAtCaret(editor, file);
  }

  if (null != element) {
    RenamePsiElementProcessor processor = RenamePsiElementProcessor.forElement(element);
    element = processor.substituteElementToRename(element, editor);
  }

  if (null != element) {
    editor.getScrollingModel().scrollToCaret(ScrollType.MAKE_VISIBLE);
    PsiElement nameSuggestionContext = file.getViewProvider().findElementAt(editor.getCaretModel().getOffset());
    PsiElementRenameHandler.invoke(element, project, nameSuggestionContext, editor);
  }
}
 
Example #8
Source File: ExecutionPointHighlighter.java    From consulo with Apache License 2.0 6 votes vote down vote up
public void show(final @Nonnull XSourcePosition position, final boolean notTopFrame, @Nullable final GutterIconRenderer gutterIconRenderer) {
  updateRequested.set(false);
  TransactionGuard.submitTransaction(myProject, () -> {
    updateRequested.set(false);

    mySourcePosition = position;

    clearDescriptor();
    myOpenFileDescriptor = XSourcePositionImpl.createOpenFileDescriptor(myProject, position);
    if (!XDebuggerSettingManagerImpl.getInstanceImpl().getGeneralSettings().isScrollToCenter()) {
      myOpenFileDescriptor.setScrollType(notTopFrame ? ScrollType.CENTER : ScrollType.MAKE_VISIBLE);
    }
    //see IDEA-125645 and IDEA-63459
    //myOpenFileDescriptor.setUseCurrentWindow(true);

    myGutterIconRenderer = gutterIconRenderer;
    myNotTopFrame = notTopFrame;

    doShow(true);
  });
}
 
Example #9
Source File: ExtractInterfaceHandler.java    From intellij-haxe with Apache License 2.0 6 votes vote down vote up
public void invoke(@NotNull Project project, Editor editor, PsiFile file, DataContext dataContext) {
  int offset = editor.getCaretModel().getOffset();
  editor.getScrollingModel().scrollToCaret(ScrollType.MAKE_VISIBLE);
  PsiElement element = file.findElementAt(offset);
  while (true) {
    if (element == null || element instanceof PsiFile) {
      String message = RefactoringBundle.getCannotRefactorMessage(RefactoringBundle.message("error.wrong.caret.position.class"));
      CommonRefactoringUtil.showErrorHint(project, editor, message, REFACTORING_NAME, HelpID.EXTRACT_INTERFACE);
      return;
    }
    if (element instanceof PsiClass && !(element instanceof PsiAnonymousClass)) {
      invoke(project, new PsiElement[]{element}, dataContext);
      return;
    }
    element = element.getParent();
  }
}
 
Example #10
Source File: HaxePushDownHandler.java    From intellij-haxe with Apache License 2.0 6 votes vote down vote up
@Override
public void invoke(@NotNull Project project, Editor editor, PsiFile file, DataContext context) {
  int offset = editor.getCaretModel().getOffset();
  editor.getScrollingModel().scrollToCaret(ScrollType.MAKE_VISIBLE);
  PsiElement element = file.findElementAt(offset);
  while (true) {
    if (element == null || element instanceof PsiFile) {
      String message = RefactoringBundle.getCannotRefactorMessage(
        RefactoringBundle.message("the.caret.should.be.positioned.inside.a.class.to.push.members.from"));
      CommonRefactoringUtil.showErrorHint(project, editor, message, REFACTORING_NAME, HelpID.MEMBERS_PUSH_DOWN);
      return;
    }

    if (element instanceof HaxeClassDeclaration || element instanceof HaxeFieldDeclaration || element instanceof HaxeMethod) {
      //if (element instanceof JspClass) {
      //  RefactoringMessageUtil.showNotSupportedForJspClassesError(project, editor, REFACTORING_NAME, HelpID.MEMBERS_PUSH_DOWN);
      //  return;
      //}
      invoke(project, new PsiElement[]{element}, context);
      return;
    }
    element = element.getParent();
  }
}
 
Example #11
Source File: ExtractSuperclassHandler.java    From intellij-haxe with Apache License 2.0 6 votes vote down vote up
@Override
public void invoke(@NotNull Project project, Editor editor, PsiFile file, DataContext dataContext) {
  editor.getScrollingModel().scrollToCaret(ScrollType.MAKE_VISIBLE);
  int offset = editor.getCaretModel().getOffset();
  PsiElement element = file.findElementAt(offset);
  while (true) {
    if (element == null || element instanceof PsiFile) {
      String message = RefactoringBundle.getCannotRefactorMessage(RefactoringBundle.message("error.wrong.caret.position.class"));
      CommonRefactoringUtil.showErrorHint(project, editor, message, REFACTORING_NAME, HelpID.EXTRACT_SUPERCLASS);
      return;
    }
    if (element instanceof PsiClass) {
      invoke(project, new PsiElement[]{element}, dataContext);
      return;
    }
    element = element.getParent();
  }
}
 
Example #12
Source File: CSharpTypedHandler.java    From consulo-csharp with Apache License 2.0 6 votes vote down vote up
private static boolean handleSemicolon(Editor editor)
{
	int offset = editor.getCaretModel().getOffset();
	if(offset == editor.getDocument().getTextLength())
	{
		return false;
	}

	char charAt = editor.getDocument().getCharsSequence().charAt(offset);
	if(charAt != ';')
	{
		return false;
	}

	editor.getCaretModel().moveToOffset(offset + 1);
	editor.getScrollingModel().scrollToCaret(ScrollType.RELATIVE);
	return true;
}
 
Example #13
Source File: PsiElementRenameHandler.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Override
public void invoke(@Nonnull Project project, Editor editor, PsiFile file, DataContext dataContext) {
  PsiElement element = getElement(dataContext);
  if (element == null) {
    element = BaseRefactoringAction.getElementAtCaret(editor, file);
  }

  if (ApplicationManager.getApplication().isUnitTestMode()) {
    final String newName = dataContext.getData(DEFAULT_NAME);
    if (newName != null) {
      rename(element, project, element, editor, newName);
      return;
    }
  }

  editor.getScrollingModel().scrollToCaret(ScrollType.MAKE_VISIBLE);
  final PsiElement nameSuggestionContext = InjectedLanguageUtil.findElementAtNoCommit(file, editor.getCaretModel().getOffset());
  invoke(element, project, nameSuggestionContext, editor);
}
 
Example #14
Source File: VariableInplaceRenameHandler.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Override
public void invoke(@Nonnull final Project project, final Editor editor, final PsiFile file, final DataContext dataContext) {
  PsiElement element = PsiElementRenameHandler.getElement(dataContext);
  if (element == null) {
    if (LookupManager.getActiveLookup(editor) != null) {
      final PsiElement elementUnderCaret = file.findElementAt(editor.getCaretModel().getOffset());
      if (elementUnderCaret != null) {
        final PsiElement parent = elementUnderCaret.getParent();
        if (parent instanceof PsiReference) {
          element = ((PsiReference)parent).resolve();
        } else {
          element = PsiTreeUtil.getParentOfType(elementUnderCaret, PsiNamedElement.class);
        }
      }
      if (element == null) return;
    } else {
      return;
    }
  }
  editor.getScrollingModel().scrollToCaret(ScrollType.MAKE_VISIBLE);
  if (checkAvailable(element, editor, dataContext)) {
    doRename(element, editor, dataContext);
  }
}
 
Example #15
Source File: EditorDocOps.java    From KodeBeagle with Apache License 2.0 6 votes vote down vote up
public final void gotoLine(final int pLineNumber, final Document document) {
    int lineNumber = pLineNumber;
    Editor projectEditor =
            FileEditorManager.getInstance(windowObjects.getProject()).getSelectedTextEditor();

    if (projectEditor != null) {
        CaretModel caretModel = projectEditor.getCaretModel();

        //document is 0-indexed
        if (lineNumber > document.getLineCount()) {
            lineNumber = document.getLineCount() - 1;
        } else {
            lineNumber = lineNumber - 1;
        }

        caretModel.moveToLogicalPosition(new LogicalPosition(lineNumber, 0));

        ScrollingModel scrollingModel = projectEditor.getScrollingModel();
        scrollingModel.scrollToCaret(ScrollType.CENTER);
    }
}
 
Example #16
Source File: CSharpIntroduceHandler.java    From consulo-csharp with Apache License 2.0 6 votes vote down vote up
@Override
@RequiredReadAction
protected void moveOffsetAfter(boolean success)
{
	super.moveOffsetAfter(success);

	if(success)
	{
		PsiNamedElement variable = getVariable();
		if(variable instanceof DotNetVariable && variable.isValid())
		{
			myEditor.getCaretModel().moveToOffset(getVariableEndOffset((DotNetVariable) variable));
			myEditor.getScrollingModel().scrollToCaret(ScrollType.MAKE_VISIBLE);
		}
	}
}
 
Example #17
Source File: InlineRefactoringActionHandler.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Override
public void invoke(@Nonnull final Project project, Editor editor, PsiFile file, DataContext dataContext) {
  editor.getScrollingModel().scrollToCaret(ScrollType.MAKE_VISIBLE);

  PsiElement element = dataContext.getData(LangDataKeys.PSI_ELEMENT);
  if (element == null) {
    element = BaseRefactoringAction.getElementAtCaret(editor, file);
  }
  if (element != null) {
    for(InlineActionHandler handler: Extensions.getExtensions(InlineActionHandler.EP_NAME)) {
      if (handler.canInlineElementInEditor(element, editor)) {
        handler.inlineElement(project, editor, element);
        return;
      }
    }

    if (invokeInliner(editor, element)) return;

    String message = RefactoringBundle.getCannotRefactorMessage(RefactoringBundle.message("error.wrong.caret.position.method.or.local.name"));
    CommonRefactoringUtil.showErrorHint(project, editor, message, REFACTORING_NAME, null);
  }
}
 
Example #18
Source File: TextEndAction.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Override
public void execute(Editor editor, DataContext dataContext) {
  editor.getCaretModel().removeSecondaryCarets();
  int offset = editor.getDocument().getTextLength();
  if (editor instanceof DesktopEditorImpl) {
    editor.getCaretModel().moveToLogicalPosition(editor.offsetToLogicalPosition(offset).leanForward(true));
  }
  else {
    editor.getCaretModel().moveToOffset(offset);
  }
  editor.getSelectionModel().removeSelection();

  ScrollingModel scrollingModel = editor.getScrollingModel();
  scrollingModel.disableAnimation();
  scrollingModel.scrollToCaret(ScrollType.CENTER);
  scrollingModel.enableAnimation();

  Project project = dataContext.getData(CommonDataKeys.PROJECT);
  if (project != null) {
    IdeDocumentHistory instance = IdeDocumentHistory.getInstance(project);
    if (instance != null) {
      instance.includeCurrentCommandAsNavigation();
    }
  }
}
 
Example #19
Source File: TextComponentScrollingModel.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public void scrollToCaret(@Nonnull final ScrollType scrollType) {
  final int position = myTextComponent.getCaretPosition();
  try {
    final Rectangle rectangle = myTextComponent.modelToView(position);
    myTextComponent.scrollRectToVisible(rectangle);
  }
  catch (BadLocationException e) {
    throw new RuntimeException(e);
  }
}
 
Example #20
Source File: MoveHandler.java    From consulo with Apache License 2.0 5 votes vote down vote up
/**
 * called by an Action in AtomicAction when refactoring is invoked from Editor
 */
@Override
public void invoke(@Nonnull Project project, Editor editor, PsiFile file, DataContext dataContext) {
  int offset = editor.getCaretModel().getOffset();
  editor.getScrollingModel().scrollToCaret(ScrollType.MAKE_VISIBLE);
  PsiElement element = file.findElementAt(offset);
  while(true){
    if (element == null) {
      String message = RefactoringBundle.getCannotRefactorMessage(RefactoringBundle.message("the.caret.should.be.positioned.at.the.class.method.or.field.to.be.refactored"));
      CommonRefactoringUtil.showErrorHint(project, editor, message, REFACTORING_NAME, null);
      return;
    }

    if (tryToMoveElement(element, project, dataContext, null, editor)) {
      return;
    }
    final TextRange range = element.getTextRange();
    if (range != null) {
      int relative = offset - range.getStartOffset();
      final PsiReference reference = element.findReferenceAt(relative);
      if (reference != null) {
        final PsiElement refElement = reference.resolve();
        if (refElement != null && tryToMoveElement(refElement, project, dataContext, reference, editor)) return;
      }
    }

    element = element.getParent();
  }
}
 
Example #21
Source File: DirectoryAsPackageRenameHandlerBase.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public void invoke(@Nonnull final Project project, final Editor editor, final PsiFile file, final DataContext dataContext) {
  PsiElement element = adjustForRename(dataContext, PsiElementRenameHandler.getElement(dataContext));
  editor.getScrollingModel().scrollToCaret(ScrollType.MAKE_VISIBLE);
  final PsiElement nameSuggestionContext = file.findElementAt(editor.getCaretModel().getOffset());
  doRename(element, project, nameSuggestionContext, editor);
}
 
Example #22
Source File: ExtractMethodHelper.java    From consulo with Apache License 2.0 5 votes vote down vote up
private static void highlightInEditor(@Nonnull final Project project, @Nonnull final SimpleMatch match,
                                      @Nonnull final Editor editor, Map<SimpleMatch, RangeHighlighter> highlighterMap) {
  final List<RangeHighlighter> highlighters = new ArrayList<>();
  final HighlightManager highlightManager = HighlightManager.getInstance(project);
  final EditorColorsManager colorsManager = EditorColorsManager.getInstance();
  final TextAttributes attributes = colorsManager.getGlobalScheme().getAttributes(EditorColors.SEARCH_RESULT_ATTRIBUTES);
  final int startOffset = match.getStartElement().getTextRange().getStartOffset();
  final int endOffset = match.getEndElement().getTextRange().getEndOffset();
  highlightManager.addRangeHighlight(editor, startOffset, endOffset, attributes, true, highlighters);
  highlighterMap.put(match, highlighters.get(0));
  final LogicalPosition logicalPosition = editor.offsetToLogicalPosition(startOffset);
  editor.getScrollingModel().scrollTo(logicalPosition, ScrollType.MAKE_VISIBLE);
}
 
Example #23
Source File: CustomFoldingRegionsPopup.java    From consulo with Apache License 2.0 5 votes vote down vote up
private static void navigateTo(@Nonnull Editor editor, @Nonnull PsiElement element) {
  int offset = element.getTextRange().getStartOffset();
  if (offset >= 0 && offset < editor.getDocument().getTextLength()) {
    editor.getCaretModel().removeSecondaryCarets();
    editor.getCaretModel().moveToOffset(offset);
    editor.getScrollingModel().scrollToCaret(ScrollType.CENTER);
    editor.getSelectionModel().removeSelection();
  }
}
 
Example #24
Source File: EmacsStyleIndentAction.java    From consulo with Apache License 2.0 5 votes vote down vote up
@RequiredUIAccess
@Override
public void invoke(@Nonnull final Project project, @Nonnull final Editor editor, @Nonnull final PsiFile file) {
  if (!CodeInsightUtilBase.prepareEditorForWrite(editor)) return;
  PsiDocumentManager.getInstance(project).commitAllDocuments();

  if (!FileDocumentManager.getInstance().requestWriting(editor.getDocument(), project)) {
    return;
  }

  EmacsProcessingHandler emacsProcessingHandler = LanguageEmacsExtension.INSTANCE.forLanguage(file.getLanguage());
  if (emacsProcessingHandler != null) {
    EmacsProcessingHandler.Result result = emacsProcessingHandler.changeIndent(project, editor, file);
    if (result == EmacsProcessingHandler.Result.STOP) {
      return;
    }
  }

  final Document document = editor.getDocument();
  final int startOffset = editor.getCaretModel().getOffset();
  final int line = editor.offsetToLogicalPosition(startOffset).line;
  final int col = editor.getCaretModel().getLogicalPosition().column;
  final int lineStart = document.getLineStartOffset(line);
  final int initLineEnd = document.getLineEndOffset(line);
  try{
    final CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(project);
    final int newPos = codeStyleManager.adjustLineIndent(file, lineStart);
    final int newCol = newPos - lineStart;
    final int lineInc = document.getLineEndOffset(line) - initLineEnd;
    if (newCol >= col + lineInc && newCol >= 0) {
      final LogicalPosition pos = new LogicalPosition(line, newCol);
      editor.getCaretModel().moveToLogicalPosition(pos);
      editor.getSelectionModel().removeSelection();
      editor.getScrollingModel().scrollToCaret(ScrollType.RELATIVE);
    }
  }
  catch(IncorrectOperationException e){
    LOG.error(e);
  }
}
 
Example #25
Source File: MultiCaretCodeInsightAction.java    From consulo with Apache License 2.0 5 votes vote down vote up
public void actionPerformedImpl(final Project project, final Editor hostEditor) {
  CommandProcessor.getInstance().executeCommand(project, () -> ApplicationManager.getApplication().runWriteAction(() -> {
    MultiCaretCodeInsightActionHandler handler = getHandler();
    try {
      iterateOverCarets(project, hostEditor, handler);
    }
    finally {
      handler.postInvoke();
    }
  }), getCommandName(), DocCommandGroupId.noneGroupId(hostEditor.getDocument()));

  hostEditor.getScrollingModel().scrollToCaret(ScrollType.RELATIVE);
}
 
Example #26
Source File: GotoNextErrorHandler.java    From consulo with Apache License 2.0 5 votes vote down vote up
static void navigateToError(Project project, final Editor editor, HighlightInfo info) {
  int oldOffset = editor.getCaretModel().getOffset();

  final int offset = getNavigationPositionFor(info, editor.getDocument());
  final int endOffset = info.getActualEndOffset();

  final ScrollingModel scrollingModel = editor.getScrollingModel();
  if (offset != oldOffset) {
    ScrollType scrollType = offset > oldOffset ? ScrollType.CENTER_DOWN : ScrollType.CENTER_UP;
    editor.getSelectionModel().removeSelection();
    editor.getCaretModel().removeSecondaryCarets();
    editor.getCaretModel().moveToOffset(offset);
    scrollingModel.scrollToCaret(scrollType);
  }

  scrollingModel.runActionOnScrollingFinished(
          new Runnable(){
            @Override
            public void run() {
              int maxOffset = editor.getDocument().getTextLength() - 1;
              if (maxOffset == -1) return;
              scrollingModel.scrollTo(editor.offsetToLogicalPosition(Math.min(maxOffset, endOffset)), ScrollType.MAKE_VISIBLE);
              scrollingModel.scrollTo(editor.offsetToLogicalPosition(Math.min(maxOffset, offset)), ScrollType.MAKE_VISIBLE);
            }
          }
  );

  IdeDocumentHistory.getInstance(project).includeCurrentCommandAsNavigation();
}
 
Example #27
Source File: ScrollToBottomAction.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public void execute(Editor editor, DataContext dataContext) {
  if (editor == null) {
    return;
  }
  editor.getScrollingModel().scrollTo(editor.offsetToLogicalPosition(editor.getDocument().getTextLength()), ScrollType.CENTER);
}
 
Example #28
Source File: SurroundWithHandler.java    From consulo with Apache License 2.0 5 votes vote down vote up
static void doSurround(final Project project, final Editor editor, final Surrounder surrounder, final PsiElement[] elements) {
  if (!FileDocumentManager.getInstance().requestWriting(editor.getDocument(), project)) {
    return;
  }

  try {
    PsiDocumentManager.getInstance(project).commitAllDocuments();
    int col = editor.getCaretModel().getLogicalPosition().column;
    int line = editor.getCaretModel().getLogicalPosition().line;
    if (!editor.getCaretModel().supportsMultipleCarets()) {
      LogicalPosition pos = new LogicalPosition(0, 0);
      editor.getCaretModel().moveToLogicalPosition(pos);
    }
    TextRange range = surrounder.surroundElements(project, editor, elements);
    if (range != CARET_IS_OK) {
      if (TemplateManager.getInstance(project).getActiveTemplate(editor) == null &&
          InplaceRefactoring.getActiveInplaceRenamer(editor) == null) {
        LogicalPosition pos1 = new LogicalPosition(line, col);
        editor.getCaretModel().moveToLogicalPosition(pos1);
      }
      if (range != null) {
        int offset = range.getStartOffset();
        editor.getCaretModel().removeSecondaryCarets();
        editor.getCaretModel().moveToOffset(offset);
        editor.getScrollingModel().scrollToCaret(ScrollType.RELATIVE);
        editor.getSelectionModel().setSelection(range.getStartOffset(), range.getEndOffset());
      }
    }
  }
  catch (IncorrectOperationException e) {
    LOG.error(e);
  }
}
 
Example #29
Source File: HighlightManagerImpl.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public void addOccurrenceHighlights(@Nonnull Editor editor,
                                    @Nonnull PsiReference[] occurrences,
                                    @Nonnull TextAttributes attributes,
                                    boolean hideByTextChange,
                                    Collection<RangeHighlighter> outHighlighters) {
  if (occurrences.length == 0) return;
  int flags = HIDE_BY_ESCAPE;
  if (hideByTextChange) {
    flags |= HIDE_BY_TEXT_CHANGE;
  }
  Color scrollmarkColor = getScrollMarkColor(attributes);

  int oldOffset = editor.getCaretModel().getOffset();
  int horizontalScrollOffset = editor.getScrollingModel().getHorizontalScrollOffset();
  int verticalScrollOffset = editor.getScrollingModel().getVerticalScrollOffset();
  for (PsiReference occurrence : occurrences) {
    PsiElement element = occurrence.getElement();
    int startOffset = element.getTextRange().getStartOffset();
    int start = startOffset + occurrence.getRangeInElement().getStartOffset();
    int end = startOffset + occurrence.getRangeInElement().getEndOffset();
    PsiFile containingFile = element.getContainingFile();
    Project project = element.getProject();
    // each reference can reside in its own injected editor
    Editor textEditor = InjectedLanguageUtil.openEditorFor(containingFile, project);
    if (textEditor != null) {
      addOccurrenceHighlight(textEditor, start, end, attributes, flags, outHighlighters, scrollmarkColor);
    }
  }
  editor.getCaretModel().moveToOffset(oldOffset);
  editor.getScrollingModel().scrollToCaret(ScrollType.RELATIVE);
  editor.getScrollingModel().scrollHorizontally(horizontalScrollOffset);
  editor.getScrollingModel().scrollVertically(verticalScrollOffset);
}
 
Example #30
Source File: EditorGotoLineNumberDialog.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
protected void doOKAction() {
  Coordinates coordinates = getCoordinates();
  if (coordinates == null) return;

  LogicalPosition position = new LogicalPosition(coordinates.row, coordinates.column);
  myEditor.getCaretModel().removeSecondaryCarets();
  myEditor.getCaretModel().moveToLogicalPosition(position);
  myEditor.getScrollingModel().scrollToCaret(ScrollType.CENTER);
  myEditor.getSelectionModel().removeSelection();
  IdeFocusManager.getGlobalInstance().requestFocus(myEditor.getContentComponent(), true);
  super.doOKAction();
}