com.intellij.openapi.editor.event.CaretEvent Java Examples

The following examples show how to use com.intellij.openapi.editor.event.CaretEvent. 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: DesktopCaretImpl.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Nullable
CaretEvent moveToLogicalPosition(@Nonnull LogicalPosition pos, boolean locateBeforeSoftWrap, @Nullable StringBuilder debugBuffer, boolean adjustForInlays, boolean fireListeners) {
  if (mySkipChangeRequests) {
    return null;
  }
  if (myReportCaretMoves) {
    LOG.error("Unexpected caret move request");
  }
  if (!myEditor.isStickySelection() && !myEditor.getDocument().isInEventsHandling() && !pos.equals(myLogicalCaret)) {
    CopyPasteManager.getInstance().stopKillRings();
  }

  myReportCaretMoves = true;
  try {
    return doMoveToLogicalPosition(pos, locateBeforeSoftWrap, debugBuffer, adjustForInlays, fireListeners);
  }
  finally {
    myReportCaretMoves = false;
  }
}
 
Example #2
Source File: LocalCaretPositionChangeHandler.java    From saros with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Calls {@link EditorManager#generateSelection(IFile, Editor, int, int)}.
 *
 * <p>This method relies on the EditorPool to filter editor events.
 *
 * <p>Does nothing if the editor of the event is contained in {@link #editorsWithSelection}.
 *
 * @param event the event to react to
 * @see CaretListener#caretPositionChanged(CaretEvent)
 * @see #disableForEditorsWithSelection(SelectionEvent)
 */
private void handleCaretPositionChange(@NotNull CaretEvent event) {
  assert enabled : "the selection changed listener was triggered while it was disabled";

  Editor editor = event.getEditor();

  if (editorsWithSelection.contains(editor)) {
    return;
  }

  Caret caret = event.getCaret();
  IFile file = editorManager.getFileForOpenEditor(editor.getDocument());

  if (file == null || caret == null) {
    return;
  }

  if (!caret.equals(caret.getCaretModel().getPrimaryCaret())) {
    log.debug("filtered out caret event for non-primary caret " + caret);
    return;
  }

  int caretPosition = caret.getOffset();

  editorManager.generateSelection(file, editor, caretPosition, caretPosition);
}
 
Example #3
Source File: JSGraphQLQueryContextCaretListener.java    From js-graphql-intellij-plugin with MIT License 6 votes vote down vote up
@Override
public void runActivity(@NotNull Project project) {
    if (!ApplicationManager.getApplication().isHeadlessEnvironment()) {
        final EditorEventMulticaster eventMulticaster = EditorFactory.getInstance().getEventMulticaster();
        final PsiDocumentManager psiDocumentManager = PsiDocumentManager.getInstance(project);
        eventMulticaster.addCaretListener(new CaretListener() {
            @Override
            public void caretPositionChanged(CaretEvent e) {
                final PsiFile psiFile = psiDocumentManager.getPsiFile(e.getEditor().getDocument());
                if (psiFile instanceof GraphQLFile) {
                    int offset = e.getEditor().logicalPositionToOffset(e.getNewPosition());
                    psiFile.putUserData(CARET_OFFSET, offset);
                }
            }
        }, project);
    }
}
 
Example #4
Source File: SearchResults.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public void caretPositionChanged(@Nonnull CaretEvent event) {
  Caret caret = event.getCaret();
  if (caret != null && myEditor.getCaretModel().getAllCarets().size() == 1 && caret.isUpToDate()) {
    int offset = caret.getOffset();
    FindResult occurrenceAtCaret = getOccurrenceAtCaret();
    if (occurrenceAtCaret != null && occurrenceAtCaret != myCursor) {
      moveCursorTo(occurrenceAtCaret, false, false);
      myEditor.getCaretModel().moveToOffset(offset);
    }
  }
  notifyCursorMoved();
}
 
Example #5
Source File: LSPCaretListenerImpl.java    From lsp4intellij with Apache License 2.0 5 votes vote down vote up
@Override
public void caretPositionChanged(CaretEvent e) {
    try {
        if (scheduledFuture != null && !scheduledFuture.isCancelled()) {
            scheduledFuture.cancel(false);
        }
        scheduledFuture = scheduler.schedule(this::debouncedCaretPositionChanged, DEBOUNCE_INTERVAL_MS, TimeUnit.MILLISECONDS);
    } catch (Exception err) {
        LOG.warn("Error occurred when trying to update code actions", err);
    }
}
 
Example #6
Source File: ClickNavigator.java    From consulo with Apache License 2.0 5 votes vote down vote up
public void addClickNavigator(final Editor view,
                              final SyntaxHighlighter highlighter,
                              final HighlightData[] data,
                              final boolean isBackgroundImportant) {
  addMouseMotionListener(view, highlighter, data, isBackgroundImportant);

  CaretListener listener = new CaretAdapter() {
    @Override
    public void caretPositionChanged(CaretEvent e) {
      navigate(view, true, e.getNewPosition(), highlighter, data, isBackgroundImportant);
    }
  };
  view.getCaretModel().addCaretListener(listener);
}
 
Example #7
Source File: TextEditorBasedStructureViewModel.java    From consulo with Apache License 2.0 5 votes vote down vote up
protected TextEditorBasedStructureViewModel(Editor editor, PsiFile file) {
  myEditor = editor;
  myPsiFile = file;

  myEditorCaretListener = new CaretAdapter() {
    @Override
    public void caretPositionChanged(CaretEvent e) {
      if (e.getEditor().equals(myEditor)) {
        for (FileEditorPositionListener listener : myListeners) {
          listener.onCurrentElementChanged();
        }
      }
    }
  };
}
 
Example #8
Source File: EditorComponentImpl.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public void caretPositionChanged(CaretEvent e) {
  Caret caret = e.getCaret();
  if (caret == null) {
    return;
  }
  int dot = caret.getOffset();
  int mark = caret.getLeadSelectionOffset();
  if (myCaretPos != dot) {
    ApplicationManager.getApplication().assertIsDispatchThread();
    firePropertyChange(ACCESSIBLE_CARET_PROPERTY,
                       new Integer(myCaretPos), new Integer(dot));

    if (SystemInfo.isMac) {
      // For MacOSX we also need to fire a caret event to anyone listening
      // to our Document, since *that* rather than the accessible property
      // change is the only way to trigger a speech update
      //fireJTextComponentCaretChange(dot, mark);
      fireJTextComponentCaretChange(e);
    }

    myCaretPos = dot;
  }

  if (mark != dot) {
    ApplicationManager.getApplication().assertIsDispatchThread();
    firePropertyChange(ACCESSIBLE_SELECTION_PROPERTY, null,
                       getSelectedText());
  }
}
 
Example #9
Source File: BlameStatusWidget.java    From GitToolBox with Apache License 2.0 5 votes vote down vote up
private void onCaretPositionChanged(@NotNull CaretEvent caretEvent) {
  if (shouldShow() && Objects.equals(myProject, caretEvent.getEditor().getProject())) {
    Caret caret = caretEvent.getCaret();
    if (caret == null || caret.isUpToDate()) {
      int lineIndex = caretEvent.getNewPosition().line;
      VirtualFile selectedFile = getSelectedFile();
      if (selectedFile != null) {
        updateStatus(selectedFile, lineIndex);
      }
    }
  }
}
 
Example #10
Source File: QueryHighlighterCaretListener.java    From jetbrains-plugin-graph-database-support with Apache License 2.0 5 votes vote down vote up
private void processEvent(CaretEvent e) {
    Editor editor = e.getEditor();
    Project project = editor.getProject();
    if (project == null) {
        return;
    }

    Document document = editor.getDocument();
    PsiFile psiFile = PsiDocumentManager.getInstance(project).getPsiFile(document);

    syncedElementHighlighter.highlightStatement(editor, psiFile);
}
 
Example #11
Source File: WidgetIndentsHighlightingPassFactory.java    From flutter-intellij with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public WidgetIndentsHighlightingPassFactory(@NotNull Project project) {
  this.project = project;
  flutterDartAnalysisService = FlutterDartAnalysisServer.getInstance(project);
  this.editorOutlineService = ActiveEditorsOutlineService.getInstance(project);
  this.inspectorGroupManagerService = InspectorGroupManagerService.getInstance(project);
  this.editorEventService = EditorMouseEventService.getInstance(project);
  this.editorPositionService = EditorPositionService.getInstance(project);
  this.settingsListener = new SettingsListener();
  this.outlineListener = this::updateEditor;

  TextEditorHighlightingPassRegistrar.getInstance(project)
    .registerTextEditorHighlightingPass(this, TextEditorHighlightingPassRegistrar.Anchor.AFTER, Pass.UPDATE_FOLDING, false, false);

  syncSettings(FlutterSettings.getInstance());
  FlutterSettings.getInstance().addListener(settingsListener);

  final EditorEventMulticaster eventMulticaster = EditorFactory.getInstance().getEventMulticaster();
  eventMulticaster.addCaretListener(new CaretListener() {
    @Override
    public void caretPositionChanged(@NotNull CaretEvent event) {
      final Editor editor = event.getEditor();
      if (editor.getProject() != project) return;
      if (editor.isDisposed() || project.isDisposed()) return;
      if (!(editor instanceof EditorEx)) return;
      final EditorEx editorEx = (EditorEx)editor;
      WidgetIndentsHighlightingPass.onCaretPositionChanged(editorEx, event.getCaret());
    }
  }, this);
  editorOutlineService.addListener(outlineListener);
}
 
Example #12
Source File: PreviewView.java    From flutter-intellij with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void caretPositionChanged(CaretEvent e) {
  final Caret caret = e.getCaret();
  if (caret != null) {
    ApplicationManager.getApplication().invokeLater(() -> applyEditorSelectionToTree(caret));
  }
}
 
Example #13
Source File: PreviewView.java    From flutter-intellij with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void caretPositionChanged(CaretEvent e) {
  final Caret caret = e.getCaret();
  if (caret != null) {
    ApplicationManager.getApplication().invokeLater(() -> applyEditorSelectionToTree(caret));
  }
}
 
Example #14
Source File: WidgetIndentsHighlightingPassFactory.java    From flutter-intellij with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public WidgetIndentsHighlightingPassFactory(@NotNull Project project) {
  this.project = project;
  flutterDartAnalysisService = FlutterDartAnalysisServer.getInstance(project);
  this.editorOutlineService = ActiveEditorsOutlineService.getInstance(project);
  this.inspectorGroupManagerService = InspectorGroupManagerService.getInstance(project);
  this.editorEventService = EditorMouseEventService.getInstance(project);
  this.editorPositionService = EditorPositionService.getInstance(project);
  this.settingsListener = new SettingsListener();
  this.outlineListener = this::updateEditor;

  TextEditorHighlightingPassRegistrar.getInstance(project)
    .registerTextEditorHighlightingPass(this, TextEditorHighlightingPassRegistrar.Anchor.AFTER, Pass.UPDATE_FOLDING, false, false);

  syncSettings(FlutterSettings.getInstance());
  FlutterSettings.getInstance().addListener(settingsListener);

  final EditorEventMulticaster eventMulticaster = EditorFactory.getInstance().getEventMulticaster();
  eventMulticaster.addCaretListener(new CaretListener() {
    @Override
    public void caretPositionChanged(@NotNull CaretEvent event) {
      final Editor editor = event.getEditor();
      if (editor.getProject() != project) return;
      if (editor.isDisposed() || project.isDisposed()) return;
      if (!(editor instanceof EditorEx)) return;
      final EditorEx editorEx = (EditorEx)editor;
      WidgetIndentsHighlightingPass.onCaretPositionChanged(editorEx, event.getCaret());
    }
  }, this);
  editorOutlineService.addListener(outlineListener);
}
 
Example #15
Source File: DesktopCaretModelImpl.java    From consulo with Apache License 2.0 4 votes vote down vote up
private void fireCaretRemoved(@Nonnull Caret caret) {
  myCaretListeners.getMulticaster().caretRemoved(new CaretEvent(caret, caret.getLogicalPosition(), caret.getLogicalPosition()));
}
 
Example #16
Source File: DesktopCaretModelImpl.java    From consulo with Apache License 2.0 4 votes vote down vote up
void fireCaretPositionChanged(@Nonnull CaretEvent caretEvent) {
  myCaretListeners.getMulticaster().caretPositionChanged(caretEvent);
}
 
Example #17
Source File: DesktopCaretImpl.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Override
public void moveToOffset(final int offset, final boolean locateBeforeSoftWrap) {
  assertIsDispatchThread();
  validateCallContext();
  if (mySkipChangeRequests) {
    return;
  }
  myCaretModel.doWithCaretMerging(() -> {
    LogicalPosition logicalPosition = myEditor.offsetToLogicalPosition(offset);
    CaretEvent event = moveToLogicalPosition(logicalPosition, locateBeforeSoftWrap, null, true, false);
    final LogicalPosition positionByOffsetAfterMove = myEditor.offsetToLogicalPosition(getOffset());
    if (!positionByOffsetAfterMove.equals(logicalPosition)) {
      StringBuilder debugBuffer = new StringBuilder();
      moveToLogicalPosition(logicalPosition, locateBeforeSoftWrap, debugBuffer, true, true);
      int actualOffset = getOffset();
      int textStart = Math.max(0, Math.min(offset, actualOffset) - 1);
      final DocumentEx document = myEditor.getDocument();
      int textEnd = Math.min(document.getTextLength() - 1, Math.max(offset, actualOffset) + 1);
      CharSequence text = document.getCharsSequence().subSequence(textStart, textEnd);
      int inverseOffset = myEditor.logicalPositionToOffset(logicalPosition);
      LOG.error("caret moved to wrong offset. Please submit a dedicated ticket and attach current editor's text to it.", new Throwable(), AttachmentFactory.createContext("Requested: offset=" +
                                                                                                                                                                          offset +
                                                                                                                                                                          ", logical position='" +
                                                                                                                                                                          logicalPosition +
                                                                                                                                                                          "' but actual: offset=" +
                                                                                                                                                                          actualOffset +
                                                                                                                                                                          ", logical position='" +
                                                                                                                                                                          myLogicalCaret +
                                                                                                                                                                          "' (" +
                                                                                                                                                                          positionByOffsetAfterMove +
                                                                                                                                                                          "). " +
                                                                                                                                                                          myEditor.dumpState() +
                                                                                                                                                                          "\ninterested text [" +
                                                                                                                                                                          textStart +
                                                                                                                                                                          ";" +
                                                                                                                                                                          textEnd +
                                                                                                                                                                          "): '" +
                                                                                                                                                                          text +
                                                                                                                                                                          "'\n debug trace: " +
                                                                                                                                                                          debugBuffer +
                                                                                                                                                                          "\nLogical position -> offset ('" +
                                                                                                                                                                          logicalPosition +
                                                                                                                                                                          "'->'" +
                                                                                                                                                                          inverseOffset +
                                                                                                                                                                          "')"));
    }
    if (event != null) {
      myCaretModel.fireCaretPositionChanged(event);
      EditorActionUtil.selectNonexpandableFold(myEditor);
    }
  });
}
 
Example #18
Source File: DesktopCaretImpl.java    From consulo with Apache License 2.0 4 votes vote down vote up
void doMoveToVisualPosition(@Nonnull VisualPosition pos, boolean fireListeners) {
  assertIsDispatchThread();
  checkDisposal();
  validateCallContext();
  if (mySkipChangeRequests) {
    return;
  }
  if (myReportCaretMoves) {
    LOG.error("Unexpected caret move request");
  }
  if (!myEditor.isStickySelection() && !myEditor.getDocument().isInEventsHandling() && !pos.equals(myVisibleCaret)) {
    CopyPasteManager.getInstance().stopKillRings();
  }
  updateCachedStateIfNeeded();

  myDesiredX = -1;
  int column = pos.column;
  int line = pos.line;
  boolean leanRight = pos.leansRight;

  int lastLine = myEditor.getVisibleLineCount() - 1;
  if (lastLine <= 0) {
    lastLine = 0;
  }

  if (line > lastLine) {
    line = lastLine;
  }

  EditorSettings editorSettings = myEditor.getSettings();

  if (!editorSettings.isVirtualSpace()) {
    int lineEndColumn = EditorUtil.getLastVisualLineColumnNumber(myEditor, line);
    if (column > lineEndColumn && !myEditor.getSoftWrapModel().isInsideSoftWrap(pos)) {
      column = lineEndColumn;
      leanRight = true;
    }
  }

  VisualPosition oldVisualPosition = myVisibleCaret;
  myVisibleCaret = new VisualPosition(line, column, leanRight);

  VerticalInfo oldVerticalInfo = myVerticalInfo;
  LogicalPosition oldPosition = myLogicalCaret;
  boolean oldInVirtualSpace = isInVirtualSpace();

  myLogicalCaret = myEditor.visualToLogicalPosition(myVisibleCaret);
  VisualPosition mappedPosition = myEditor.logicalToVisualPosition(myLogicalCaret);
  myVisualColumnAdjustment = mappedPosition.line == myVisibleCaret.line && myVisibleCaret.column > mappedPosition.column ? myVisibleCaret.column - mappedPosition.column : 0;
  updateOffsetsFromLogicalPosition();

  updateVisualLineInfo();

  myEditor.getFoldingModel().flushCaretPosition(this);

  setLastColumnNumber(myLogicalCaret.column);
  myDesiredSelectionStartColumn = myDesiredSelectionEndColumn = -1;
  myEditor.updateCaretCursor();
  requestRepaint(oldVerticalInfo);

  if (!oldPosition.equals(myLogicalCaret) || !oldVisualPosition.equals(myVisibleCaret)) {
    if (oldInVirtualSpace || isInVirtualSpace()) {
      myCaretModel.validateEditorSize();
    }
    if (fireListeners) {
      CaretEvent event = new CaretEvent(this, oldPosition, myLogicalCaret);
      myCaretModel.fireCaretPositionChanged(event);
    }
  }
}
 
Example #19
Source File: PreviewView.java    From flutter-intellij with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public void caretAdded(@NotNull CaretEvent e) {
}
 
Example #20
Source File: EditorComponentImpl.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Override
public void caretAdded(CaretEvent e) {
}
 
Example #21
Source File: EditorComponentImpl.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Override
public void caretRemoved(CaretEvent e) {
}
 
Example #22
Source File: SelectInEditorManagerImpl.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Override
public void caretPositionChanged(CaretEvent e) {
  releaseAll();
}
 
Example #23
Source File: SelectInEditorManagerImpl.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Override
public void caretAdded(CaretEvent e) {
}
 
Example #24
Source File: SelectInEditorManagerImpl.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Override
public void caretRemoved(CaretEvent e) {
}
 
Example #25
Source File: HighlightEditorComponent.java    From HighlightBracketPair with Apache License 2.0 4 votes vote down vote up
@Override
public void caretRemoved(CaretEvent e) {
    // ignore the event
}
 
Example #26
Source File: HighlightEditorComponent.java    From HighlightBracketPair with Apache License 2.0 4 votes vote down vote up
@Override
public void caretAdded(CaretEvent e) {
    // ignore the event
}
 
Example #27
Source File: FindUtil.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Override
public void caretPositionChanged(CaretEvent e) {
  removeAll();
}
 
Example #28
Source File: HighlightEditorComponent.java    From HighlightBracketPair with Apache License 2.0 4 votes vote down vote up
@Override
public void caretPositionChanged(CaretEvent e) {
    Editor editor = e.getEditor();
    highlightEditorCurrentPair(editor);
}
 
Example #29
Source File: DesktopCaretModelImpl.java    From consulo with Apache License 2.0 4 votes vote down vote up
private void fireCaretAdded(@Nonnull Caret caret) {
  myCaretListeners.getMulticaster().caretAdded(new CaretEvent(caret, caret.getLogicalPosition(), caret.getLogicalPosition()));
}
 
Example #30
Source File: PreviewView.java    From flutter-intellij with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public void caretRemoved(@NotNull CaretEvent e) {
}