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

The following examples show how to use com.intellij.openapi.editor.Editor#isDisposed() . 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: DesktopPsiAwareTextEditorProvider.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Nonnull
@Override
public TextEditorState getStateImpl(final Project project, @Nonnull final Editor editor, @Nonnull final FileEditorStateLevel level) {
  final TextEditorState state = super.getStateImpl(project, editor, level);
  // Save folding only on FULL level. It's very expensive to commit document on every
  // type (caused by undo).
  if (FileEditorStateLevel.FULL == level) {
    // Folding
    if (project != null && !project.isDisposed() && !editor.isDisposed() && project.isInitialized()) {
      state.setFoldingState(CodeFoldingManager.getInstance(project).saveFoldingState(editor));
    }
    else {
      state.setFoldingState(null);
    }
  }

  return state;
}
 
Example 2
Source File: ErrorStripeUpdateManager.java    From consulo with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("WeakerAccess") // Used in Rider
protected void setOrRefreshErrorStripeRenderer(@Nonnull EditorMarkupModel editorMarkupModel, @Nullable PsiFile file) {
  ApplicationManager.getApplication().assertIsDispatchThread();
  if (!editorMarkupModel.isErrorStripeVisible() || !DaemonCodeAnalyzer.getInstance(myProject).isHighlightingAvailable(file)) {
    return;
  }
  ErrorStripeRenderer renderer = editorMarkupModel.getErrorStripeRenderer();
  if (renderer instanceof TrafficLightRenderer) {
    TrafficLightRenderer tlr = (TrafficLightRenderer)renderer;
    DesktopEditorMarkupModelImpl markupModelImpl = (DesktopEditorMarkupModelImpl)editorMarkupModel;
    tlr.refresh(markupModelImpl);
    markupModelImpl.repaintTrafficLightIcon();
    if (tlr.isValid()) return;
  }
  Editor editor = editorMarkupModel.getEditor();
  if (editor.isDisposed()) return;

  editorMarkupModel.setErrorStripeRenderer(createRenderer(editor, file));
}
 
Example 3
Source File: ShowUsagesAction.java    From consulo with Apache License 2.0 6 votes vote down vote up
private void showHint(@Nullable final Editor editor,
                      @Nonnull String hint,
                      @Nonnull FindUsagesHandler handler,
                      @Nonnull final RelativePoint popupPosition,
                      int maxUsages,
                      @Nonnull FindUsagesOptions options,
                      boolean isWarning) {
  Runnable runnable = () -> {
    if (!handler.getPsiElement().isValid()) return;

    JComponent label = createHintComponent(hint, handler, popupPosition, editor, ShowUsagesAction::hideHints, maxUsages, options, isWarning);
    if (editor == null || editor.isDisposed() || !editor.getComponent().isShowing()) {
      HintManager.getInstance()
              .showHint(label, popupPosition, HintManager.HIDE_BY_ANY_KEY | HintManager.HIDE_BY_TEXT_CHANGE | HintManager.HIDE_BY_SCROLLING, 0);
    }
    else {
      HintManager.getInstance().showInformationHint(editor, label);
    }
  };
  if (editor == null) {
    runnable.run();
  }
  else {
    DesktopAsyncEditorLoader.performWhenLoaded(editor, runnable);
  }
}
 
Example 4
Source File: InjectedLanguageUtil.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Nonnull
public static Editor getInjectedEditorForInjectedFile(@Nonnull Editor hostEditor, @Nonnull Caret hostCaret, @Nullable final PsiFile injectedFile) {
  if (injectedFile == null || hostEditor instanceof EditorWindow || hostEditor.isDisposed()) return hostEditor;
  Project project = hostEditor.getProject();
  if (project == null) project = injectedFile.getProject();
  Document document = PsiDocumentManager.getInstance(project).getDocument(injectedFile);
  if (!(document instanceof DocumentWindowImpl)) return hostEditor;
  DocumentWindowImpl documentWindow = (DocumentWindowImpl)document;
  if (hostCaret.hasSelection()) {
    int selstart = hostCaret.getSelectionStart();
    if (selstart != -1) {
      int selend = Math.max(selstart, hostCaret.getSelectionEnd());
      if (!documentWindow.containsRange(selstart, selend)) {
        // selection spreads out the injected editor range
        return hostEditor;
      }
    }
  }
  if (!documentWindow.isValid()) {
    return hostEditor; // since the moment we got hold of injectedFile and this moment call, document may have been dirtied
  }
  return EditorWindowImpl.create(documentWindow, (DesktopEditorImpl)hostEditor, injectedFile);
}
 
Example 5
Source File: TabOutScopesTrackerImpl.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Override
public void registerEmptyScope(@Nonnull Editor editor, int offset, int tabOutOffset) {
  ApplicationManager.getApplication().assertIsDispatchThread();
  if (editor.isDisposed()) throw new IllegalArgumentException("Editor is already disposed");
  if (tabOutOffset <= offset) throw new IllegalArgumentException("tabOutOffset should be larger than offset");

  if (!CodeInsightSettings.getInstance().TAB_EXITS_BRACKETS_AND_QUOTES) return;

  if (editor instanceof EditorWindow) {
    DocumentWindow documentWindow = ((EditorWindow)editor).getDocument();
    offset = documentWindow.injectedToHost(offset);
    editor = ((EditorWindow)editor).getDelegate();
  }
  if (!(editor instanceof DesktopEditorImpl)) return;

  Tracker tracker = Tracker.forEditor((DesktopEditorImpl)editor, true);
  tracker.registerScope(offset, tabOutOffset - offset);
}
 
Example 6
Source File: ActiveEditorsOutlineService.java    From flutter-intellij with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Gets all of the {@link EditorEx} editors open to Dart files.
 */
public List<EditorEx> getActiveDartEditors() {
  if (project.isDisposed()) {
    return Collections.emptyList();
  }
  final FileEditor[] editors = FileEditorManager.getInstance(project).getSelectedEditors();
  final List<EditorEx> dartEditors = new ArrayList<>();
  for (FileEditor fileEditor : editors) {
    if (!(fileEditor instanceof TextEditor)) continue;
    final TextEditor textEditor = (TextEditor)fileEditor;
    final Editor editor = textEditor.getEditor();
    if (editor instanceof EditorEx && !editor.isDisposed()) {
      dartEditors.add((EditorEx)editor);
    }
  }
  return dartEditors;
}
 
Example 7
Source File: WidgetEditToolbar.java    From flutter-intellij with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
Editor getCurrentEditor() {
  final VirtualFile file = activeFile.getValue();
  if (file == null) return null;

  final FileEditor fileEditor = FileEditorManager.getInstance(project).getSelectedEditor(file);
  if (fileEditor instanceof TextEditor) {
    final TextEditor textEditor = (TextEditor)fileEditor;
    final Editor editor = textEditor.getEditor();
    if (!editor.isDisposed()) {
      return editor;
    }
  }
  return null;
}
 
Example 8
Source File: ImageOrColorPreviewManager.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nonnull
private static Collection<PsiElement> getPsiElementsAt(Point point, Editor editor) {
  if (editor.isDisposed()) {
    return Collections.emptySet();
  }

  Project project = editor.getProject();
  if (project == null || project.isDisposed()) {
    return Collections.emptySet();
  }

  final PsiDocumentManager documentManager = PsiDocumentManager.getInstance(project);
  final Document document = editor.getDocument();
  PsiFile psiFile = documentManager.getPsiFile(document);
  if (psiFile == null || psiFile instanceof PsiCompiledElement || !psiFile.isValid()) {
    return Collections.emptySet();
  }

  final Set<PsiElement> elements = Collections.newSetFromMap(ContainerUtil.createWeakMap());
  final int offset = editor.logicalPositionToOffset(editor.xyToLogicalPosition(point));
  if (documentManager.isCommitted(document)) {
    ContainerUtil.addIfNotNull(elements, InjectedLanguageUtil.findElementAtNoCommit(psiFile, offset));
  }
  for (PsiFile file : psiFile.getViewProvider().getAllFiles()) {
    ContainerUtil.addIfNotNull(elements, file.findElementAt(offset));
  }

  return elements;
}
 
Example 9
Source File: PreviewArea.java    From flutter-intellij with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public void select(@NotNull List<FlutterOutline> outlines, Editor editor) {
  if (editor.isDisposed()) return;

  final InspectorService.ObjectGroup group = getObjectGroup();
  if (group != null && outlines.size() > 0) {
    FlutterOutline outline = findWidgetToHighlight(outlines.get(0));
    if (outline == null) return;
    final InspectorService.Location location = InspectorService.Location.outlineToLocation(editor, outline);
    if (location == null) return;
    group.setSelection(location, false, true);
  }
}
 
Example 10
Source File: ViewportAdjustmentExecutor.java    From saros with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Applies any queued viewport changes to the editor representing the given virtual file. Does
 * nothing if the given file is null or if there are no queued viewport changes.
 *
 * @param virtualFile the file whose editor viewport to adjust
 */
private void applyQueuedViewportChanges(@Nullable VirtualFile virtualFile) {
  if (virtualFile == null) {
    return;
  }

  QueuedViewPortChange queuedViewPortChange = queuedViewPortChanges.remove(virtualFile.getPath());

  if (queuedViewPortChange == null) {
    return;
  }

  LineRange range = queuedViewPortChange.getRange();
  TextSelection selection = queuedViewPortChange.getSelection();
  Editor queuedEditor = queuedViewPortChange.getEditor();

  Editor editor;

  if (queuedEditor != null && !queuedEditor.isDisposed()) {
    editor = queuedEditor;

  } else {
    editor = ProjectAPI.openEditor(project, virtualFile, false);

    if (editor == null) {
      log.warn(
          "Failed to apply queued viewport change as no text editor could be obtained for "
              + virtualFile);

      return;
    }
  }

  EDTExecutor.invokeAndWait(
      () -> localEditorManipulator.adjustViewport(editor, range, selection),
      ModalityState.defaultModalityState());
}
 
Example 11
Source File: CodeFoldingManagerImpl.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public void buildInitialFoldings(@Nonnull final Editor editor) {
  final Project project = editor.getProject();
  if (project == null || !project.equals(myProject) || editor.isDisposed()) return;
  if (!((FoldingModelEx)editor.getFoldingModel()).isFoldingEnabled()) return;
  if (!FoldingUpdate.supportsDumbModeFolding(editor)) return;

  Document document = editor.getDocument();
  PsiDocumentManager.getInstance(myProject).commitDocument(document);
  CodeFoldingState foldingState = buildInitialFoldings(document);
  if (foldingState != null) {
    foldingState.setToEditor(editor);
  }
}
 
Example 12
Source File: ValueLookupManager.java    From consulo with Apache License 2.0 5 votes vote down vote up
public void showHint(@Nonnull QuickEvaluateHandler handler, @Nonnull Editor editor, @Nonnull Point point, @Nonnull ValueHintType type) {
  myAlarm.cancelAllRequests();
  if (editor.isDisposed() || !handler.canShowHint(myProject)) {
    return;
  }

  final AbstractValueHint request = handler.createValueHint(myProject, editor, point, type);
  if (request != null) {
    if (myRequest != null && myRequest.equals(request)) {
      return;
    }

    if (!request.canShowHint()) {
      return;
    }
    if (myRequest != null && myRequest.isInsideHint(editor, point)) {
      return;
    }

    hideHint();

    myRequest = request;
    myRequest.invokeHint(new Runnable() {
      @Override
      public void run() {
        if (myRequest != null && myRequest == request) {
          myRequest = null;
        }
      }
    });
  }
}
 
Example 13
Source File: ShowParameterInfoContext.java    From consulo with Apache License 2.0 5 votes vote down vote up
private static void showParameterHint(final PsiElement element,
                                      final Editor editor,
                                      final Object[] descriptors,
                                      final Project project,
                                      @Nullable Object highlighted,
                                      final int elementStart,
                                      final ParameterInfoHandler handler,
                                      final boolean requestFocus,
                                      boolean singleParameterInfo) {
  if (editor.isDisposed() || !editor.getComponent().isVisible()) return;

  PsiDocumentManager.getInstance(project).performLaterWhenAllCommitted(() -> {
    if (editor.isDisposed() || DumbService.isDumb(project) || !element.isValid() || (!ApplicationManager.getApplication().isUnitTestMode() && !EditorActivityManager.getInstance().isVisible(editor)))
      return;

    final Document document = editor.getDocument();
    if (document.getTextLength() < elementStart) return;

    ParameterInfoController controller = ParameterInfoController.findControllerAtOffset(editor, elementStart);
    if (controller == null) {
      new ParameterInfoController(project, editor, elementStart, descriptors, highlighted, element, handler, true, requestFocus);
    }
    else {
      controller.setDescriptors(descriptors);
      controller.showHint(requestFocus, singleParameterInfo);
    }
  });
}
 
Example 14
Source File: CtrlMouseHandler.java    From consulo with Apache License 2.0 5 votes vote down vote up
public void showHint(@Nonnull LightweightHint hint, @Nonnull Editor editor) {
  if (ApplicationManager.getApplication().isUnitTestMode() || editor.isDisposed()) return;
  final HintManagerImpl hintManager = HintManagerImpl.getInstanceImpl();
  short constraint = HintManager.ABOVE;
  LogicalPosition position = editor.offsetToLogicalPosition(getOffset(editor));
  Point p = HintManagerImpl.getHintPosition(hint, editor, position, constraint);
  if (p.y - hint.getComponent().getPreferredSize().height < 0) {
    constraint = HintManager.UNDER;
    p = HintManagerImpl.getHintPosition(hint, editor, position, constraint);
  }
  hintManager.showEditorHint(hint, editor, p, HintManager.HIDE_BY_ANY_KEY | HintManager.HIDE_BY_TEXT_CHANGE | HintManager.HIDE_BY_SCROLLING, 0, false,
                             HintManagerImpl.createHintHint(editor, p, hint, constraint).setContentActive(false));
}
 
Example 15
Source File: ShowUsagesAction.java    From otto-intellij-plugin with Apache License 2.0 5 votes vote down vote up
private void showHint(@NotNull String text,
    @Nullable final Editor editor,
    @NotNull final RelativePoint popupPosition,
    @NotNull FindUsagesHandler handler,
    int maxUsages,
    @NotNull FindUsagesOptions options) {
  JComponent label = createHintComponent(text, handler, popupPosition, editor, HIDE_HINTS_ACTION, maxUsages, options);
  if (editor == null || editor.isDisposed()) {
    HintManager.getInstance().showHint(label, popupPosition, HintManager.HIDE_BY_ANY_KEY |
        HintManager.HIDE_BY_TEXT_CHANGE | HintManager.HIDE_BY_SCROLLING, 0);
  }
  else {
    HintManager.getInstance().showInformationHint(editor, label);
  }
}
 
Example 16
Source File: ConsoleLogConsole.java    From aem-ide-tooling-4-intellij with Apache License 2.0 4 votes vote down vote up
void doPrintNotification(final Notification notification) {
    Editor editor = myLogEditor.getValue();
    if (editor.isDisposed()) {
        return;
    }

    Document document = editor.getDocument();
    boolean scroll = document.getTextLength() == editor.getCaretModel().getOffset() || !editor.getContentComponent().hasFocus();

    Long notificationTime = myProjectModel.getNotificationTime(notification);
    if (notificationTime == null) {
        return;
    }

    String date = DateFormatUtil.formatTimeWithSeconds(notificationTime) + " ";
    append(document, date);

    int startLine = document.getLineCount() - 1;

    ConsoleLog.LogEntry pair = ConsoleLog.formatForLog(notification, StringUtil.repeatSymbol(' ', date.length()));

    final NotificationType type = notification.getType();
    TextAttributesKey key = type == NotificationType.ERROR
        ? ConsoleViewContentType.LOG_ERROR_OUTPUT_KEY
        : type == NotificationType.INFORMATION
        ? ConsoleViewContentType.NORMAL_OUTPUT_KEY
        : ConsoleViewContentType.LOG_WARNING_OUTPUT_KEY;

    int msgStart = document.getTextLength();
    String message = pair.message;
    append(document, message);

    TextAttributes attributes = EditorColorsManager.getInstance().getGlobalScheme().getAttributes(key);
    int layer = HighlighterLayer.CARET_ROW + 1;
    editor.getMarkupModel().addRangeHighlighter(msgStart, document.getTextLength(), layer, attributes, HighlighterTargetArea.EXACT_RANGE);

    for (Pair<TextRange, HyperlinkInfo> link : pair.links) {
        final RangeHighlighter rangeHighlighter = myHyperlinkSupport.getValue()
            .createHyperlink(link.first.getStartOffset() + msgStart, link.first.getEndOffset() + msgStart, null, link.second);
        if (link.second instanceof ConsoleLog.ShowBalloon) {
            ((ConsoleLog.ShowBalloon)link.second).setRangeHighlighter(rangeHighlighter);
        }
    }

    append(document, "\n");

    if (scroll) {
        editor.getCaretModel().moveToOffset(document.getTextLength());
        editor.getScrollingModel().scrollToCaret(ScrollType.MAKE_VISIBLE);
    }

    if (notification.isImportant()) {
        highlightNotification(notification, pair.status, startLine, document.getLineCount() - 1);
    }
}
 
Example 17
Source File: EditorEventServiceBase.java    From flutter-intellij with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
EditorEx getIfValidForProject(Editor editor) {
  if (editor.getProject() != project) return null;
  if (editor.isDisposed() || project.isDisposed()) return null;
  if (!(editor instanceof EditorEx)) return null;
  return (EditorEx)editor;
}
 
Example 18
Source File: EditorBasedWidget.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Nullable
Editor getFocusedEditor() {
  Component component = getFocusedComponent();
  Editor editor = component instanceof EditorComponentImpl ? ((EditorComponentImpl)component).getEditor() : getEditor();
  return editor != null && !editor.isDisposed() ? editor : null;
}
 
Example 19
Source File: BraceHighlightingHandler.java    From consulo with Apache License 2.0 4 votes vote down vote up
private static boolean isValidEditor(@Nonnull Editor editor) {
  Project editorProject = editor.getProject();
  return editorProject != null && !editorProject.isDisposed() && !editor.isDisposed() && editor.getComponent().isShowing() && !editor.isViewer();
}
 
Example 20
Source File: CtrlMouseHandler.java    From consulo with Apache License 2.0 4 votes vote down vote up
private boolean isTaskOutdated(@Nonnull Editor editor) {
  return myDisposed || myProject.isDisposed() || editor.isDisposed() || !ApplicationManager.getApplication().isUnitTestMode() && !editor.getComponent().isShowing();
}