Java Code Examples for com.intellij.openapi.editor.ex.EditorEx#getDocument()

The following examples show how to use com.intellij.openapi.editor.ex.EditorEx#getDocument() . 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: FilteredIndentsHighlightingPass.java    From flutter-intellij with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public static void onWidgetIndentsChanged(EditorEx editor, WidgetIndentHitTester oldHitTester, WidgetIndentHitTester newHitTester) {
  final List<RangeHighlighter> highlighters = editor.getUserData(INDENT_HIGHLIGHTERS_IN_EDITOR_KEY);
  if (highlighters != null) {
    final Document doc = editor.getDocument();
    final int textLength = doc.getTextLength();
    for (RangeHighlighter highlighter : highlighters) {
      if (!highlighter.isValid()) {
        continue;
      }
      final LineRange range = getGuideLineRange(editor, highlighter);
      if (range != null) {
        final boolean before = WidgetIndentsHighlightingPass.isIndentGuideHidden(oldHitTester, range);
        final boolean after = WidgetIndentsHighlightingPass.isIndentGuideHidden(newHitTester, range);
        if (before != after) {
          int safeStart = min(highlighter.getStartOffset(), textLength);
          int safeEnd = min(highlighter.getEndOffset(), textLength);
          if (safeEnd > safeStart) {
            editor.repaint(safeStart, safeEnd);
          }
        }
      }
    }
  }
}
 
Example 2
Source File: WidgetIndentsHighlightingPass.java    From flutter-intellij with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
WidgetIndentsHighlightingPass(
  @NotNull Project project,
  @NotNull EditorEx editor,
  boolean convertOffsets,
  FlutterDartAnalysisServer flutterDartAnalysisService,
  InspectorGroupManagerService inspectorGroupManagerService,
  EditorMouseEventService editorEventService,
  EditorPositionService editorPositionService
) {
  this.myDocument = editor.getDocument();
  this.myEditor = editor;
  this.myProject = project;
  this.myFile = editor.getVirtualFile();
  this.convertOffsets = convertOffsets;
  this.editorEventService = editorEventService;
  context = new WidgetEditingContext(
    project, flutterDartAnalysisService, inspectorGroupManagerService, editorPositionService);
  psiFile = PsiDocumentManager.getInstance(myProject).getPsiFile(myDocument);
  final WidgetIndentsPassData data = getIndentsPassData();
  setIndentsPassData(editor, data);
}
 
Example 3
Source File: FilteredIndentsHighlightingPass.java    From flutter-intellij with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public static void onWidgetIndentsChanged(EditorEx editor, WidgetIndentHitTester oldHitTester, WidgetIndentHitTester newHitTester) {
  final List<RangeHighlighter> highlighters = editor.getUserData(INDENT_HIGHLIGHTERS_IN_EDITOR_KEY);
  if (highlighters != null) {
    final Document doc = editor.getDocument();
    final int textLength = doc.getTextLength();
    for (RangeHighlighter highlighter : highlighters) {
      if (!highlighter.isValid()) {
        continue;
      }
      final LineRange range = getGuideLineRange(editor, highlighter);
      if (range != null) {
        final boolean before = WidgetIndentsHighlightingPass.isIndentGuideHidden(oldHitTester, range);
        final boolean after = WidgetIndentsHighlightingPass.isIndentGuideHidden(newHitTester, range);
        if (before != after) {
          int safeStart = min(highlighter.getStartOffset(), textLength);
          int safeEnd = min(highlighter.getEndOffset(), textLength);
          if (safeEnd > safeStart) {
            editor.repaint(safeStart, safeEnd);
          }
        }
      }
    }
  }
}
 
Example 4
Source File: WidgetIndentsHighlightingPass.java    From flutter-intellij with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
WidgetIndentsHighlightingPass(
  @NotNull Project project,
  @NotNull EditorEx editor,
  boolean convertOffsets,
  FlutterDartAnalysisServer flutterDartAnalysisService,
  InspectorGroupManagerService inspectorGroupManagerService,
  EditorMouseEventService editorEventService,
  EditorPositionService editorPositionService
) {
  this.myDocument = editor.getDocument();
  this.myEditor = editor;
  this.myProject = project;
  this.myFile = editor.getVirtualFile();
  this.convertOffsets = convertOffsets;
  this.editorEventService = editorEventService;
  context = new WidgetEditingContext(
    project, flutterDartAnalysisService, inspectorGroupManagerService, editorPositionService);
  psiFile = PsiDocumentManager.getInstance(myProject).getPsiFile(myDocument);
  final WidgetIndentsPassData data = getIndentsPassData();
  setIndentsPassData(editor, data);
}
 
Example 5
Source File: ApplyPatchChange.java    From consulo with Apache License 2.0 6 votes vote down vote up
private void createStatusHighlighter() {
  int line1 = myPatchDeletionRange.start;
  int line2 = myPatchInsertionRange.end;

  Color color = getStatusColor();
  if (isResolved()) {
    color = ColorUtil.mix(color, myViewer.getPatchEditor().getGutterComponentEx().getBackground(), 0.6f);
  }

  String tooltip = getStatusText();

  EditorEx patchEditor = myViewer.getPatchEditor();
  Document document = patchEditor.getDocument();
  MarkupModelEx markupModel = patchEditor.getMarkupModel();
  TextRange textRange = DiffUtil.getLinesRange(document, line1, line2);

  RangeHighlighter highlighter = markupModel.addRangeHighlighter(textRange.getStartOffset(), textRange.getEndOffset(),
                                                                 HighlighterLayer.LAST, null, HighlighterTargetArea.LINES_IN_RANGE);

  PairConsumer<Editor, MouseEvent> clickHandler = getResultRange() != null ?
                                                  (e, event) -> myViewer.scrollToChange(this, Side.RIGHT, false) :
                                                  null;
  highlighter.setLineMarkerRenderer(LineStatusMarkerRenderer.createRenderer(line1, line2, color, tooltip, clickHandler));

  myHighlighters.add(highlighter);
}
 
Example 6
Source File: ApplyPatchChange.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Nullable
private MyGutterOperation createOperation(@Nonnull OperationType type) {
  if (isResolved()) return null;

  EditorEx editor = myViewer.getPatchEditor();
  Document document = editor.getDocument();

  int line = getPatchRange().start;
  int offset = line == DiffUtil.getLineCount(document) ? document.getTextLength() : document.getLineStartOffset(line);

  RangeHighlighter highlighter = editor.getMarkupModel().addRangeHighlighter(offset, offset,
                                                                             HighlighterLayer.ADDITIONAL_SYNTAX,
                                                                             null,
                                                                             HighlighterTargetArea.LINES_IN_RANGE);
  return new MyGutterOperation(highlighter, type);
}
 
Example 7
Source File: LanguageConsoleImpl.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Nonnull
public String prepareExecuteAction(boolean addToHistory, boolean preserveMarkup, boolean clearInput) {
  EditorEx editor = getCurrentEditor();
  Document document = editor.getDocument();
  String text = document.getText();
  TextRange range = new TextRange(0, document.getTextLength());
  if (!clearInput) {
    editor.getSelectionModel().setSelection(range.getStartOffset(), range.getEndOffset());
  }

  if (addToHistory) {
    addToHistoryInner(range, editor, clearInput, preserveMarkup);
  }
  else if (clearInput) {
    setInputText("");
  }
  return text;
}
 
Example 8
Source File: InlineWidgetViewModelData.java    From flutter-intellij with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public InlineWidgetViewModelData(
  WidgetIndentGuideDescriptor descriptor,
  EditorEx editor,
  WidgetEditingContext context
) {
  super(context);
  this.descriptor = descriptor;
  this.document = editor.getDocument();
  this.editor = editor;
}
 
Example 9
Source File: InlineWidgetViewModelData.java    From flutter-intellij with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public InlineWidgetViewModelData(
  WidgetIndentGuideDescriptor descriptor,
  EditorEx editor,
  WidgetEditingContext context
) {
  super(context);
  this.descriptor = descriptor;
  this.document = editor.getDocument();
  this.editor = editor;
}
 
Example 10
Source File: TextMergeChange.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nullable
private MyGutterOperation createOperation(@Nonnull ThreeSide side, @Nonnull OperationType type) {
  if (isResolved(side)) return null;

  EditorEx editor = myViewer.getEditor(side);
  Document document = editor.getDocument();

  int line = getStartLine(side);
  int offset = line == DiffUtil.getLineCount(document) ? document.getTextLength() : document.getLineStartOffset(line);

  RangeHighlighter highlighter =
          editor.getMarkupModel().addRangeHighlighter(offset, offset, HighlighterLayer.ADDITIONAL_SYNTAX, null, HighlighterTargetArea.LINES_IN_RANGE);
  return new MyGutterOperation(side, highlighter, type);
}
 
Example 11
Source File: FoldingModelSupport.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nullable
private static FoldRegion addFolding(@Nonnull EditorEx editor, int start, int end, boolean expanded) {
  DocumentEx document = editor.getDocument();
  final int startOffset = document.getLineStartOffset(start);
  final int endOffset = document.getLineEndOffset(end - 1);

  FoldRegion value = editor.getFoldingModel().addFoldRegion(startOffset, endOffset, PLACEHOLDER);
  if (value != null) {
    value.setExpanded(expanded);
    value.setInnerHighlightersMuted(true);
  }
  return value;
}
 
Example 12
Source File: WrapElementIterator.java    From consulo with Apache License 2.0 5 votes vote down vote up
public WrapElementIterator(EditorEx editor, int startOffset, int endOffset) {
  myDocument = editor.getDocument();
  myText = myDocument.getImmutableCharSequence();
  myIterationState = new IterationState(editor, startOffset, endOffset, null, false, true, true, false);
  myLogicalLine = myDocument.getLineNumber(startOffset);
  setElementOffsets();
}
 
Example 13
Source File: WidgetIndentsHighlightingPassFactory.java    From flutter-intellij with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
void runWidgetIndentsPass(EditorEx editor, FlutterOutline outline) {
  if (editor.isDisposed() || project.isDisposed()) {
    // The editor might have been disposed before we got a new FlutterOutline.
    // It is safe to ignore it as it isn't relevant.
    return;
  }

  if (!isShowBuildMethodGuides || outline == null) {
    // If build method guides are disabled or there is no outline to use in this pass,
    // then do nothing.
    return;
  }

  final VirtualFile file = editor.getVirtualFile();
  if (!FlutterUtils.couldContainWidgets(file)) {
    return;
  }
  // If the editor and the outline have different lengths then
  // the outline is out of date and cannot safely be displayed.
  final DocumentEx document = editor.getDocument();
  final int documentLength = document.getTextLength();
  final int outlineLength = outline.getLength();
  // TODO(jacobr): determine why we sometimes have to check against both the
  // raw outlineLength and the converted outline length for things to work
  // correctly on windows.
  if (documentLength != outlineLength &&
      documentLength != DartAnalysisServerService.getInstance(project).getConvertedOffset(file, outlineLength)) {
    // Outline is out of date. That is ok. Ignore it for now.
    // An up to date outline will probably arrive shortly. Showing an
    // outline from data inconsistent with the current
    // content will show annoying flicker. It is better to
    // instead
    return;
  }
  // We only need to convert offsets when the document and outline disagree
  // on the document length.
  final boolean convertOffsets = documentLength != outlineLength;

  WidgetIndentsHighlightingPass.run(
    project,
    editor,
    outline,
    flutterDartAnalysisService,
    inspectorGroupManagerService,
    editorEventService,
    editorPositionService,
    convertOffsets
  );
}
 
Example 14
Source File: WidgetIndentsHighlightingPassFactory.java    From flutter-intellij with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
void runWidgetIndentsPass(EditorEx editor, FlutterOutline outline) {
  if (editor.isDisposed() || project.isDisposed()) {
    // The editor might have been disposed before we got a new FlutterOutline.
    // It is safe to ignore it as it isn't relevant.
    return;
  }

  if (!isShowBuildMethodGuides || outline == null) {
    // If build method guides are disabled or there is no outline to use in this pass,
    // then do nothing.
    return;
  }

  final VirtualFile file = editor.getVirtualFile();
  if (!FlutterUtils.couldContainWidgets(file)) {
    return;
  }
  // If the editor and the outline have different lengths then
  // the outline is out of date and cannot safely be displayed.
  final DocumentEx document = editor.getDocument();
  final int documentLength = document.getTextLength();
  final int outlineLength = outline.getLength();
  // TODO(jacobr): determine why we sometimes have to check against both the
  // raw outlineLength and the converted outline length for things to work
  // correctly on windows.
  if (documentLength != outlineLength &&
      documentLength != DartAnalysisServerService.getInstance(project).getConvertedOffset(file, outlineLength)) {
    // Outline is out of date. That is ok. Ignore it for now.
    // An up to date outline will probably arrive shortly. Showing an
    // outline from data inconsistent with the current
    // content will show annoying flicker. It is better to
    // instead
    return;
  }
  // We only need to convert offsets when the document and outline disagree
  // on the document length.
  final boolean convertOffsets = documentLength != outlineLength;

  WidgetIndentsHighlightingPass.run(
    project,
    editor,
    outline,
    flutterDartAnalysisService,
    inspectorGroupManagerService,
    editorEventService,
    editorPositionService,
    convertOffsets
  );
}
 
Example 15
Source File: EditorFragmentComponent.java    From consulo with Apache License 2.0 4 votes vote down vote up
private void doInit(Component anchorComponent, EditorEx editor, int startLine, int endLine, boolean showFolding, boolean showGutter) {
  boolean newRendering = editor instanceof DesktopEditorImpl;
  int savedScrollOffset = newRendering ? 0 : editor.getScrollingModel().getHorizontalScrollOffset();

  FoldingModelEx foldingModel = editor.getFoldingModel();
  boolean isFoldingEnabled = foldingModel.isFoldingEnabled();
  if (!showFolding) {
    foldingModel.setFoldingEnabled(false);
  }
  int textImageWidth;
  int markersImageWidth;
  int textImageHeight;
  BufferedImage textImage;
  BufferedImage markersImage;
  JComponent rowHeader;
  try {
    Document doc = editor.getDocument();
    int endOffset = endLine < doc.getLineCount() ? doc.getLineEndOffset(Math.max(0, endLine - 1)) : doc.getTextLength();
    int widthAdjustment = newRendering ? EditorUtil.getSpaceWidth(Font.PLAIN, editor) : 0;
    textImageWidth = Math.min(editor.getMaxWidthInRange(doc.getLineStartOffset(startLine), endOffset) + widthAdjustment, getWidthLimit(editor));

    Point p1 = editor.logicalPositionToXY(new LogicalPosition(startLine, 0));
    Point p2 = editor.logicalPositionToXY(new LogicalPosition(Math.max(endLine, startLine + 1), 0));
    int y1 = p1.y;
    int y2 = p2.y;
    textImageHeight = y2 - y1 == 0 ? editor.getLineHeight() : y2 - y1;
    LOG.assertTrue(textImageHeight > 0, "Height: " + textImageHeight + "; startLine:" + startLine + "; endLine:" + endLine + "; p1:" + p1 + "; p2:" + p2);

    if (savedScrollOffset > 0) {
      editor.getScrollingModel().scrollHorizontally(0);
    }

    textImage = UIUtil.createImage(anchorComponent == null ? editor.getContentComponent() : anchorComponent, textImageWidth, textImageHeight, BufferedImage.TYPE_INT_RGB);
    Graphics textGraphics = textImage.getGraphics();
    EditorUIUtil.setupAntialiasing(textGraphics);

    if (showGutter) {
      rowHeader = editor.getGutterComponentEx();
      markersImageWidth = Math.max(1, rowHeader.getWidth());

      markersImage = UIUtil.createImage(editor.getComponent(), markersImageWidth, textImageHeight, BufferedImage.TYPE_INT_RGB);
      Graphics markerGraphics = markersImage.getGraphics();
      EditorUIUtil.setupAntialiasing(markerGraphics);

      markerGraphics.translate(0, -y1);
      markerGraphics.setClip(0, y1, rowHeader.getWidth(), textImageHeight);
      markerGraphics.setColor(getBackgroundColor(editor));
      markerGraphics.fillRect(0, y1, rowHeader.getWidth(), textImageHeight);
      rowHeader.paint(markerGraphics);
    }
    else {
      markersImageWidth = 0;
      rowHeader = null;
      markersImage = null;
    }

    textGraphics.translate(0, -y1);
    textGraphics.setClip(0, y1, textImageWidth, textImageHeight);
    boolean wasVisible = editor.setCaretVisible(false);
    editor.getContentComponent().paint(textGraphics);
    if (wasVisible) {
      editor.setCaretVisible(true);
    }
  }
  finally {
    if (!showFolding) {
      foldingModel.setFoldingEnabled(isFoldingEnabled);
    }
  }

  if (savedScrollOffset > 0) {
    editor.getScrollingModel().scrollHorizontally(savedScrollOffset);
  }

  JComponent component = new JComponent() {
    @Override
    public Dimension getPreferredSize() {
      return new Dimension(textImageWidth + markersImageWidth, textImageHeight);
    }

    @Override
    protected void paintComponent(Graphics graphics) {
      if (markersImage != null) {
        UIUtil.drawImage(graphics, markersImage, 0, 0, null);
        UIUtil.drawImage(graphics, textImage, rowHeader.getWidth(), 0, null);
      }
      else {
        UIUtil.drawImage(graphics, textImage, 0, 0, null);
      }
    }
  };

  setLayout(new BorderLayout());
  add(component);

  setBorder(createEditorFragmentBorder(editor));
}
 
Example 16
Source File: DiffMarkup.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Nullable
public Document getDocument() {
  EditorEx editor = getEditor();
  return editor == null ? null : editor.getDocument();
}