Java Code Examples for com.intellij.openapi.editor.Editor#getScrollingModel()

The following examples show how to use com.intellij.openapi.editor.Editor#getScrollingModel() . 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: 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 2
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 3
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 4
Source File: EditorUtils.java    From KJump with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@NotNull
public static TextRange getVisibleRangeOffset(@NotNull Editor editor) {
    ScrollingModel scrollingModel = editor.getScrollingModel();
    Rectangle visibleArea = scrollingModel.getVisibleArea();

    LogicalPosition startLog = editor.xyToLogicalPosition(new Point(0, visibleArea.y));
    LogicalPosition lastLog = editor.xyToLogicalPosition(new Point(0, visibleArea.y + visibleArea.height));

    int startOff = editor.logicalPositionToOffset(startLog);
    int endOff = editor.logicalPositionToOffset(new LogicalPosition(lastLog.line + 1, lastLog.column));
    return new TextRange(startOff, endOff);
}
 
Example 5
Source File: JSGraphQLToggleVariablesAction.java    From js-graphql-intellij-plugin with MIT License 5 votes vote down vote up
@Override
public void setSelected(AnActionEvent e, boolean state) {

    final Editor variablesEditor = getVariablesEditor(e);
    if (variablesEditor != null) {

        final Editor queryEditor = variablesEditor.getUserData(JSGraphQLLanguageUIProjectService.GRAPH_QL_QUERY_EDITOR);
        if(queryEditor == null) {
            // not linked to a query editor
            return;
        }

        final ScrollingModel scroll = queryEditor.getScrollingModel();
        final int currentScroll = scroll.getVerticalScrollOffset();

        variablesEditor.putUserData(JS_GRAPH_QL_VARIABLES_MODEL, state ? Boolean.TRUE : Boolean.FALSE);
        variablesEditor.getComponent().setVisible(state);

        if (state) {
            variablesEditor.getContentComponent().grabFocus();
        } else {
            queryEditor.getContentComponent().grabFocus();
        }

        // restore scroll position after the editor has had a chance to re-layout
        ApplicationManager.getApplication().invokeLater(() -> {
            UIUtil.invokeLaterIfNeeded(() -> scroll.scrollVertically(currentScroll));
        });

    }

}
 
Example 6
Source File: ParticleContainerManager.java    From power-mode-intellij-plugin with Apache License 2.0 5 votes vote down vote up
private void updateInUI(Editor editor) {
    VisualPosition visualPosition = editor.getCaretModel().getVisualPosition();
    Point point = editor.visualPositionToXY(visualPosition);
    ScrollingModel scrollingModel = editor.getScrollingModel();
    point.x = point.x - scrollingModel.getHorizontalScrollOffset();
    point.y = point.y - scrollingModel.getVerticalScrollOffset();
    final ParticleContainer particleContainer = particleContainers.get(editor);
    if (particleContainer != null) {
        particleContainer.update(point);
    }
}
 
Example 7
Source File: MyActionUtils.java    From intellij-plugin-v4 with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static void moveCursor(Editor editor, int cursorOffset) {
	CaretModel caretModel = editor.getCaretModel();
	caretModel.moveToOffset(cursorOffset);
	ScrollingModel scrollingModel = editor.getScrollingModel();
	scrollingModel.scrollToCaret(ScrollType.MAKE_VISIBLE);
	editor.getContentComponent().requestFocus();
}
 
Example 8
Source File: SyncScrollSupport.java    From consulo with Apache License 2.0 5 votes vote down vote up
public static void scrollEditor(@Nonnull Editor editor, int logicalLine) {
  editor.getCaretModel().moveToLogicalPosition(new LogicalPosition(logicalLine, 0));
  ScrollingModel scrollingModel = editor.getScrollingModel();
  scrollingModel.disableAnimation();
  scrollingModel.scrollToCaret(ScrollType.CENTER);
  scrollingModel.enableAnimation();
}
 
Example 9
Source File: CaretVisualPositionKeeper.java    From consulo with Apache License 2.0 5 votes vote down vote up
public void restoreOriginalLocation() {
  for (Map.Entry<Editor, Integer> e : myCaretRelativeVerticalPositions.entrySet()) {
    Editor editor = e.getKey();
    int relativePosition = e.getValue();
    Point caretLocation = editor.visualPositionToXY(editor.getCaretModel().getVisualPosition());
    int scrollOffset = caretLocation.y - relativePosition;
    ScrollingModel scrollingModel = editor.getScrollingModel();
    Rectangle targetArea = scrollingModel.getVisibleAreaOnScrollingFinished();
    // when animated scrolling is in progress, we'll not stop it immediately
    boolean useAnimation = !targetArea.equals(scrollingModel.getVisibleArea());
    if (!useAnimation) scrollingModel.disableAnimation();
    scrollingModel.scroll(targetArea.x, scrollOffset);
    if (!useAnimation) scrollingModel.enableAnimation();
  }
}
 
Example 10
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 11
Source File: SyncScrollSupport.java    From consulo with Apache License 2.0 4 votes vote down vote up
private static void doScrollVertically(@Nonnull Editor editor, int offset, boolean animated) {
  ScrollingModel model = editor.getScrollingModel();
  if (!animated) model.disableAnimation();
  model.scrollVertically(offset);
  if (!animated) model.enableAnimation();
}
 
Example 12
Source File: SyncScrollSupport.java    From consulo with Apache License 2.0 4 votes vote down vote up
private static void doScrollHorizontally(@Nonnull Editor editor, int offset, boolean animated) {
  ScrollingModel model = editor.getScrollingModel();
  if (!animated) model.disableAnimation();
  model.scrollHorizontally(offset);
  if (!animated) model.enableAnimation();
}
 
Example 13
Source File: DesktopEditorWindow.java    From consulo with Apache License 2.0 4 votes vote down vote up
/**
 * Tries to setup caret and viewport for the given editor from the selected one.
 *
 * @param toSync editor to setup caret and viewport for
 */
private void syncCaretIfPossible(@Nullable FileEditor[] toSync) {
  if (toSync == null) {
    return;
  }

  final DesktopEditorWithProviderComposite from = getSelectedEditor();
  if (from == null) {
    return;
  }

  final FileEditor caretSource = from.getSelectedEditor();
  if (!(caretSource instanceof TextEditor)) {
    return;
  }

  final Editor editorFrom = ((TextEditor)caretSource).getEditor();
  final int offset = editorFrom.getCaretModel().getOffset();
  if (offset <= 0) {
    return;
  }

  final int scrollOffset = editorFrom.getScrollingModel().getVerticalScrollOffset();

  for (FileEditor fileEditor : toSync) {
    if (!(fileEditor instanceof TextEditor)) {
      continue;
    }
    final Editor editor = ((TextEditor)fileEditor).getEditor();
    if (editorFrom.getDocument() == editor.getDocument()) {
      editor.getCaretModel().moveToOffset(offset);
      final ScrollingModel scrollingModel = editor.getScrollingModel();
      scrollingModel.scrollVertically(scrollOffset);

      SwingUtilities.invokeLater(() -> {
        if (!editor.isDisposed()) {
          scrollingModel.scrollToCaret(ScrollType.MAKE_VISIBLE);
        }
      });
    }
  }
}