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

The following examples show how to use com.intellij.openapi.editor.event.CaretListener. 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: 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 #2
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 #3
Source File: CaretModelWindow.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public void removeCaretListener(@Nonnull final CaretListener listener) {
  CaretListener wrapper = myCaretListeners.removeWrapper(listener);
  if (wrapper != null) {
    myDelegate.removeCaretListener(wrapper);
  }
}
 
Example #4
Source File: CaretModelWindow.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public void addCaretListener(@Nonnull final CaretListener listener) {
  CaretListener wrapper = new CaretListener() {
    @Override
    public void caretPositionChanged(@Nonnull CaretEvent e) {
      if (!myEditorWindow.getDocument().isValid()) return; // injected document can be destroyed by now
      Caret caret = e.getCaret();
      assert caret != null;
      CaretEvent event = new CaretEvent(createInjectedCaret(caret), myEditorWindow.hostToInjected(e.getOldPosition()), myEditorWindow.hostToInjected(e.getNewPosition()));
      listener.caretPositionChanged(event);
    }
  };
  myCaretListeners.registerWrapper(listener, wrapper);
  myDelegate.addCaretListener(wrapper);
}
 
Example #5
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 #6
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 #7
Source File: TextComponentCaretModel.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Override
public void removeCaretListener(@Nonnull final CaretListener listener) {
  throw new UnsupportedOperationException("Not implemented");
}
 
Example #8
Source File: CaretModelWindow.java    From consulo with Apache License 2.0 4 votes vote down vote up
public void disposeModel() {
  for (CaretListener wrapper : myCaretListeners.wrappers()) {
    myDelegate.removeCaretListener(wrapper);
  }
  myCaretListeners.clear();
}
 
Example #9
Source File: DesktopCaretModelImpl.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Override
public void removeCaretListener(@Nonnull CaretListener listener) {
  myCaretListeners.removeListener(listener);
}
 
Example #10
Source File: DesktopCaretModelImpl.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Override
public void addCaretListener(@Nonnull final CaretListener listener) {
  myCaretListeners.addListener(listener);
}
 
Example #11
Source File: TextComponentCaretModel.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Override
public void addCaretListener(@Nonnull final CaretListener listener) {
  throw new UnsupportedOperationException("Not implemented");
}
 
Example #12
Source File: JSGraphQLQueryContextHighlightVisitor.java    From js-graphql-intellij-plugin with MIT License 4 votes vote down vote up
/**
 * Highlights the operation, if any, that wraps the current caret position.
 * Fragments used from the operation are highlighted recursively.
 */
@Override
public boolean analyze(final @NotNull PsiFile file, boolean updateWholeFile, @NotNull HighlightInfoHolder holder, @NotNull Runnable action) {

    // run the default pass first (DefaultHighlightVisitor) which calls annotators etc.
    action.run();

    final PsiElement operationAtCursor = getOperationAtCursor(file);
    if (operationAtCursor != null && hasMultipleVisibleTopLevelElement(file)) {

        // store the range of the current operation for use in the caret listener
        file.putUserData(QUERY_OPERATION_TEXT_RANGE, operationAtCursor.getTextRange());

        final Color borderColor = EditorColorsManager.getInstance().getGlobalScheme().getColor(EditorColors.TEARLINE_COLOR);
        final TextAttributes textAttributes = new TextAttributes(null, null, borderColor, EffectType.ROUNDED_BOX, Font.PLAIN);
        final Map<String, GraphQLFragmentDefinition> foundFragments = Maps.newHashMap();
        findFragmentsInsideOperation(operationAtCursor, foundFragments, null);
        for (PsiElement psiElement : file.getChildren()) {
            boolean showAsUsed = false;
            if (psiElement instanceof GraphQLFragmentDefinition) {
                GraphQLFragmentDefinition definition = (GraphQLFragmentDefinition) psiElement;
                if (definition.getOriginalElement() instanceof GraphQLFragmentDefinition) {
                    // use the original PSI to compare since a separate editor tab has its own version of the PSI
                    definition = (GraphQLFragmentDefinition) definition.getOriginalElement();
                }
                showAsUsed = foundFragments.containsKey(getFragmentKey(definition));
            } else if (psiElement == operationAtCursor) {
                showAsUsed = true;
            }
            if (showAsUsed) {
                final TextRange range = psiElement.getTextRange();
                final Annotation annotation = new Annotation(range.getStartOffset(), range.getEndOffset(), HighlightSeverity.INFORMATION, ELEMENT_INCLUDED_MESSAGE, null);
                annotation.setEnforcedTextAttributes(textAttributes);
                holder.add(HighlightInfo.fromAnnotation(annotation));
            }
        }

    } else {
        file.putUserData(QUERY_OPERATION_TEXT_RANGE, null);
    }


    // find the editor that was highlighted and listen for caret changes to update the active operation
    UIUtil.invokeLaterIfNeeded(() -> {
        final FileEditor fileEditor = FileEditorManager.getInstance(file.getProject()).getSelectedEditor(file.getVirtualFile());
        if (fileEditor instanceof TextEditor) {
            final Editor editor = ((TextEditor) fileEditor).getEditor();
            if (!Boolean.TRUE.equals(editor.getUserData(QUERY_HIGHLIGHT_LISTENER_ADDED))) {
                editor.getCaretModel().addCaretListener(new CaretListener() {
                    @Override
                    public void caretPositionChanged(CaretEvent e) {
                        // re-highlight when the operation changes

                        final Editor currentEditor = e.getEditor();
                        final Project project = currentEditor.getProject();
                        if (project != null) {

                            final PsiFile psiFile = PsiDocumentManager.getInstance(project).getPsiFile(currentEditor.getDocument());
                            if (psiFile != null) {

                                final TextRange previousOperationRange = psiFile.getUserData(QUERY_OPERATION_TEXT_RANGE);
                                psiFile.putUserData(QUERY_FROM_SELECTION, currentEditor.getSelectionModel().hasSelection());

                                boolean sameOperation = false;
                                boolean hadOperation = (previousOperationRange != null);
                                if (hadOperation) {
                                    // check if we're still inside the range of the previously highlighted op
                                    final int newOffset = currentEditor.logicalPositionToOffset(e.getNewPosition());
                                    sameOperation = previousOperationRange.contains(newOffset);
                                    if (sameOperation && !Boolean.TRUE.equals(psiFile.getUserData(QUERY_FROM_SELECTION))) {
                                        // still the same op, and we didn't select text before, so no need to proceed
                                        return;
                                    }
                                }

                                // remove existing unused query text range highlights
                                removeHighlights(currentEditor, project);

                                if (!sameOperation) {
                                    // moved to somewhere outside the previous operation
                                    if (hadOperation || getOperationAtCursor(psiFile) != null) {
                                        // perform a new highlighting pass
                                        DaemonCodeAnalyzer.getInstance(project).restart(psiFile);
                                    }
                                }
                            }
                        }
                    }
                });
                // finally indicate we've added the listener
                editor.putUserData(QUERY_HIGHLIGHT_LISTENER_ADDED, true);
            }
        }
    });

    return true;
}
 
Example #13
Source File: CaretModel.java    From consulo with Apache License 2.0 2 votes vote down vote up
/**
 * Removes a listener for receiving notifications about caret movement and caret addition/removal
 *
 * @param listener the listener instance.
 */
void removeCaretListener(@Nonnull CaretListener listener);
 
Example #14
Source File: CaretModel.java    From consulo with Apache License 2.0 2 votes vote down vote up
/**
 * Adds a listener for receiving notifications about caret movement and caret addition/removal.
 * The listener is removed when the given parent disposable is disposed.
 *
 * @param listener the listener instance.
 */
default void addCaretListener(@Nonnull CaretListener listener, @Nonnull Disposable parentDisposable) {
  addCaretListener(listener);
  Disposer.register(parentDisposable, () -> removeCaretListener(listener));
}
 
Example #15
Source File: CaretModel.java    From consulo with Apache License 2.0 2 votes vote down vote up
/**
 * Adds a listener for receiving notifications about caret movement and caret addition/removal
 *
 * @param listener the listener instance.
 */
void addCaretListener(@Nonnull CaretListener listener);
 
Example #16
Source File: WebCaretModelImpl.java    From consulo with Apache License 2.0 2 votes vote down vote up
@Override
public void addCaretListener(@Nonnull CaretListener listener) {

}
 
Example #17
Source File: WebCaretModelImpl.java    From consulo with Apache License 2.0 2 votes vote down vote up
@Override
public void removeCaretListener(@Nonnull CaretListener listener) {

}