Ejemplo n.º 1
0
  private VerticalInfo createVerticalInfo(LogicalPosition position) {
    Document document = myEditor.getDocument();
    int logicalLine = position.line;
    if (logicalLine >= document.getLineCount()) {
      logicalLine = Math.max(0, document.getLineCount() - 1);
    }
    int startOffset = document.getLineStartOffset(logicalLine);
    int endOffset = document.getLineEndOffset(logicalLine);

    // There is a possible case that active logical line is represented on multiple lines due to
    // soft wraps processing.
    // We want to highlight those visual lines as 'active' then, so, we calculate 'y' position for
    // the logical line start
    // and height in accordance with the number of occupied visual lines.
    VisualPosition visualPosition =
        myEditor.offsetToVisualPosition(document.getLineStartOffset(logicalLine));
    int y = myEditor.visualPositionToXY(visualPosition).y;
    int lineHeight = myEditor.getLineHeight();
    int height = lineHeight;
    List<? extends SoftWrap> softWraps =
        myEditor.getSoftWrapModel().getSoftWrapsForRange(startOffset, endOffset);
    for (SoftWrap softWrap : softWraps) {
      height += StringUtil.countNewLines(softWrap.getText()) * lineHeight;
    }

    return new VerticalInfo(y, height);
  }
Ejemplo n.º 2
0
  @SuppressWarnings("ForLoopThatDoesntUseLoopVariable")
  private static void indentPlainTextBlock(
      final Document document, final int startOffset, final int endOffset, final int indentLevel) {
    CharSequence chars = document.getCharsSequence();
    int spaceEnd = CharArrayUtil.shiftForward(chars, startOffset, " \t");
    int line = document.getLineNumber(startOffset);
    if (spaceEnd > endOffset
        || indentLevel <= 0
        || line >= document.getLineCount() - 1
        || chars.charAt(spaceEnd) == '\n') {
      return;
    }

    int linesToAdjustIndent = 0;
    for (int i = line + 1; i < document.getLineCount(); i++) {
      if (document.getLineStartOffset(i) >= endOffset) {
        break;
      }
      linesToAdjustIndent++;
    }

    String indentString = StringUtil.repeatSymbol(' ', indentLevel);

    for (; linesToAdjustIndent > 0; linesToAdjustIndent--) {
      int lineStartOffset = document.getLineStartOffset(++line);
      document.insertString(lineStartOffset, indentString);
    }
  }
Ejemplo n.º 3
0
  public CaretModelImpl(EditorImpl editor) {
    myEditor = editor;
    myLogicalCaret = new LogicalPosition(0, 0);
    myVisibleCaret = new VisualPosition(0, 0);
    myCaretInfo = new VerticalInfo(0, 0);
    myOffset = 0;
    myVisualLineStart = 0;
    Document doc = editor.getDocument();
    myVisualLineEnd =
        doc.getLineCount() > 1
            ? doc.getLineStartOffset(1)
            : doc.getLineCount() == 0 ? 0 : doc.getLineEndOffset(0);
    DocumentBulkUpdateListener bulkUpdateListener =
        new DocumentBulkUpdateListener() {
          @Override
          public void updateStarted(Document doc) {
            if (doc != myEditor.getDocument()) return;
            savedBeforeBulkCaretMarker = doc.createRangeMarker(myOffset, myOffset);
          }

          @Override
          public void updateFinished(Document doc) {
            if (doc != myEditor.getDocument() || myIsInUpdate) return;
            if (savedBeforeBulkCaretMarker != null && savedBeforeBulkCaretMarker.isValid()) {
              moveToOffset(savedBeforeBulkCaretMarker.getStartOffset());
            }
            releaseBulkCaretMarker();
          }
        };
    ApplicationManager.getApplication()
        .getMessageBus()
        .connect(this)
        .subscribe(DocumentBulkUpdateListener.TOPIC, bulkUpdateListener);
  }
Ejemplo n.º 4
0
 private static LineRange expandLineRangeToCoverPsiElements(
     final LineRange range, Editor editor, final PsiFile file) {
   Pair<PsiElement, PsiElement> psiRange = getElementRange(editor, file, range);
   if (psiRange == null) return null;
   final PsiElement parent =
       PsiTreeUtil.findCommonParent(psiRange.getFirst(), psiRange.getSecond());
   Pair<PsiElement, PsiElement> elementRange =
       getElementRange(parent, psiRange.getFirst(), psiRange.getSecond());
   if (elementRange == null) return null;
   int endOffset = elementRange.getSecond().getTextRange().getEndOffset();
   Document document = editor.getDocument();
   if (endOffset > document.getTextLength()) {
     LOG.assertTrue(!PsiDocumentManager.getInstance(file.getProject()).isUncommited(document));
     LOG.assertTrue(PsiDocumentManagerImpl.checkConsistency(file, document));
   }
   int endLine;
   if (endOffset == document.getTextLength()) {
     endLine = document.getLineCount();
   } else {
     endLine = editor.offsetToLogicalPosition(endOffset).line + 1;
     endLine = Math.min(endLine, document.getLineCount());
   }
   int startLine =
       Math.min(
           range.startLine,
           editor.offsetToLogicalPosition(elementRange.getFirst().getTextOffset()).line);
   endLine = Math.max(endLine, range.endLine);
   return new LineRange(startLine, endLine);
 }
  /**
   * Emulates pressing <code>Enter</code> at current caret position.
   *
   * @param editor target editor
   * @param project target project
   * @param shifts two-elements array which is expected to be filled with the following info: 1. The
   *     first element holds added lines number; 2. The second element holds added symbols number;
   */
  private static void emulateEnter(
      @NotNull final Editor editor, @NotNull Project project, int[] shifts) {
    final DataContext dataContext = prepareContext(editor.getComponent(), project);
    int caretOffset = editor.getCaretModel().getOffset();
    Document document = editor.getDocument();
    SelectionModel selectionModel = editor.getSelectionModel();
    int startSelectionOffset = 0;
    int endSelectionOffset = 0;
    boolean restoreSelection = selectionModel.hasSelection();
    if (restoreSelection) {
      startSelectionOffset = selectionModel.getSelectionStart();
      endSelectionOffset = selectionModel.getSelectionEnd();
      selectionModel.removeSelection();
    }
    int textLengthBeforeWrap = document.getTextLength();
    int lineCountBeforeWrap = document.getLineCount();

    DataManager.getInstance()
        .saveInDataContext(dataContext, WRAP_LONG_LINE_DURING_FORMATTING_IN_PROGRESS_KEY, true);
    CommandProcessor commandProcessor = CommandProcessor.getInstance();
    try {
      Runnable command =
          new Runnable() {
            @Override
            public void run() {
              EditorActionManager.getInstance()
                  .getActionHandler(IdeActions.ACTION_EDITOR_ENTER)
                  .execute(editor, dataContext);
            }
          };
      if (commandProcessor.getCurrentCommand() == null) {
        commandProcessor.executeCommand(editor.getProject(), command, WRAP_LINE_COMMAND_NAME, null);
      } else {
        command.run();
      }
    } finally {
      DataManager.getInstance()
          .saveInDataContext(dataContext, WRAP_LONG_LINE_DURING_FORMATTING_IN_PROGRESS_KEY, null);
    }
    int symbolsDiff = document.getTextLength() - textLengthBeforeWrap;
    if (restoreSelection) {
      int newSelectionStart = startSelectionOffset;
      int newSelectionEnd = endSelectionOffset;
      if (startSelectionOffset >= caretOffset) {
        newSelectionStart += symbolsDiff;
      }
      if (endSelectionOffset >= caretOffset) {
        newSelectionEnd += symbolsDiff;
      }
      selectionModel.setSelection(newSelectionStart, newSelectionEnd);
    }
    shifts[0] = document.getLineCount() - lineCountBeforeWrap;
    shifts[1] = symbolsDiff;
  }
  public void testSoftWrapsRecalculationInASpecificCase() throws Exception {
    configureFromFileText(
        getTestName(false) + ".java",
        "<selection>class Foo {\n"
            + "\t@Override\n"
            + "\tpublic boolean equals(Object other) {\n"
            + "\t\treturn this == other;\n"
            + "\t}\n"
            + "}</selection>");
    CodeFoldingManager.getInstance(ourProject).buildInitialFoldings(myEditor);
    configureSoftWraps(32);

    // verify initial state
    assertEquals(4, EditorUtil.getTabSize(myEditor));
    assertEquals(
        "[FoldRegion +(59:64), placeholder=' { ', FoldRegion +(85:88), placeholder=' }']",
        myEditor.getFoldingModel().toString());
    verifySoftWrapPositions(52, 85);

    Document document = myEditor.getDocument();
    for (int i = document.getLineCount() - 1; i >= 0; i--) {
      document.insertString(document.getLineStartOffset(i), "//");
    }

    verifySoftWrapPositions(58, 93);
  }
  private void collectNonCoveredFileInfo(
      final File outputFile,
      final List<RangeHighlighter> highlighters,
      final MarkupModel markupModel,
      final TreeMap<Integer, LineData> executableLines,
      final boolean coverageByTestApplicable) {
    final CoverageSuitesBundle coverageSuite =
        CoverageDataManager.getInstance(myProject).getCurrentSuitesBundle();
    if (coverageSuite == null) return;
    final TIntIntHashMap mapping;
    if (outputFile.lastModified() < getVirtualFile().getTimeStamp()) {
      mapping = getOldToNewLineMapping(outputFile.lastModified());
      if (mapping == null) return;
    } else {
      mapping = null;
    }

    final List<Integer> uncoveredLines =
        coverageSuite
            .getCoverageEngine()
            .collectSrcLinesForUntouchedFile(outputFile, coverageSuite);

    final int lineCount = myDocument.getLineCount();
    if (uncoveredLines == null) {
      for (int lineNumber = 0; lineNumber < lineCount; lineNumber++) {
        addHighlighter(
            outputFile,
            highlighters,
            markupModel,
            executableLines,
            coverageByTestApplicable,
            coverageSuite,
            lineNumber,
            lineNumber);
      }
    } else {
      for (int lineNumber : uncoveredLines) {
        if (lineNumber >= lineCount) {
          continue;
        }

        final int updatedLineNumber = mapping != null ? mapping.get(lineNumber) : lineNumber;

        addHighlighter(
            outputFile,
            highlighters,
            markupModel,
            executableLines,
            coverageByTestApplicable,
            coverageSuite,
            lineNumber,
            updatedLineNumber);
      }
    }
  }
 private static boolean lineContainsNonSpaces(final Document document, final int line) {
   if (line >= document.getLineCount()) {
     return false;
   }
   int lineStartOffset = document.getLineStartOffset(line);
   int lineEndOffset = document.getLineEndOffset(line);
   @NonNls
   String text =
       document.getCharsSequence().subSequence(lineStartOffset, lineEndOffset).toString();
   return text.trim().length() != 0;
 }
 int getNavigationOffset() {
   Document document = getDocument();
   if (document == null) return -1;
   int offset = getUsageInfo().getNavigationOffset();
   if (offset == -1) offset = myOffset;
   if (offset >= document.getTextLength()) {
     int line = Math.max(0, Math.min(myLineNumber, document.getLineCount() - 1));
     offset = document.getLineStartOffset(line);
   }
   return offset;
 }
 private void setStructureViewSelectionFromPropertiesFile(@NotNull Editor propertiesFileEditor) {
   int line = propertiesFileEditor.getCaretModel().getLogicalPosition().line;
   Document document = propertiesFileEditor.getDocument();
   if (line >= document.getLineCount()) {
     return;
   }
   final String propertyName = getPropertyName(document, line);
   if (propertyName == null) {
     return;
   }
   setStructureViewSelection(propertyName);
 }
Ejemplo n.º 11
0
 private void updatedMovedIntoEnd(
     final Document document, @NotNull final MoveInfo info, final int offset) {
   if (offset + 1 < document.getTextLength()) {
     final int line = document.getLineNumber(offset + 1);
     final LineRange toMove2 = info.toMove2;
     if (toMove2 == null) return;
     info.toMove2 =
         new LineRange(
             toMove2.startLine,
             Math.min(Math.max(line, toMove2.endLine), document.getLineCount() - 1));
   }
 }
  private void setPropertiesFileSelectionFromStructureView(@NotNull Editor propertiesFileEditor) {
    String selectedPropertyName = getSelectedPropertyName();
    if (selectedPropertyName == null) {
      return;
    }

    Document document = propertiesFileEditor.getDocument();
    for (int i = 0; i < document.getLineCount(); i++) {
      String propertyName = getPropertyName(document, i);
      if (selectedPropertyName.equals(propertyName)) {
        propertiesFileEditor.getCaretModel().moveToLogicalPosition(new LogicalPosition(i, 0));
        return;
      }
    }
  }
Ejemplo n.º 13
0
 private static TextRange calculateLimitRange(
     final PsiFile file, final Document doc, final int line) {
   final int offset = doc.getLineStartOffset(line);
   if (offset > 0) {
     PsiMethod method =
         PsiTreeUtil.getParentOfType(file.findElementAt(offset), PsiMethod.class, false);
     if (method != null) {
       final TextRange elemRange = method.getTextRange();
       return new TextRange(
           doc.getLineNumber(elemRange.getStartOffset()),
           doc.getLineNumber(elemRange.getEndOffset()));
     }
   }
   return new TextRange(0, doc.getLineCount() - 1);
 }
Ejemplo n.º 14
0
  public static List<PsiLambdaExpression> collectLambdas(
      @NotNull SourcePosition position, final boolean onlyOnTheLine) {
    ApplicationManager.getApplication().assertReadAccessAllowed();
    PsiFile file = position.getFile();
    final int line = position.getLine();
    final Document document = PsiDocumentManager.getInstance(file.getProject()).getDocument(file);
    if (document == null || line >= document.getLineCount()) {
      return Collections.emptyList();
    }
    PsiElement element = position.getElementAt();
    final TextRange lineRange = DocumentUtil.getLineTextRange(document, line);
    do {
      PsiElement parent = element.getParent();
      if (parent == null || (parent.getTextOffset() < lineRange.getStartOffset())) {
        break;
      }
      element = parent;
    } while (true);

    final List<PsiLambdaExpression> lambdas = new ArrayList<PsiLambdaExpression>(3);
    final PsiElementVisitor lambdaCollector =
        new JavaRecursiveElementVisitor() {
          @Override
          public void visitLambdaExpression(PsiLambdaExpression expression) {
            super.visitLambdaExpression(expression);
            if (!onlyOnTheLine || getFirstElementOnTheLine(expression, document, line) != null) {
              lambdas.add(expression);
            }
          }
        };
    element.accept(lambdaCollector);
    // add initial lambda if we're inside already
    PsiElement method = getContainingMethod(element);
    if (method instanceof PsiLambdaExpression) {
      lambdas.add((PsiLambdaExpression) method);
    }
    for (PsiElement sibling = getNextElement(element);
        sibling != null;
        sibling = getNextElement(sibling)) {
      if (!intersects(lineRange, sibling)) {
        break;
      }
      sibling.accept(lambdaCollector);
    }
    return lambdas;
  }
 @Override
 @NotNull
 public List<? extends SoftWrap> getSoftWrapsForLine(int documentLine) {
   if (!isSoftWrappingEnabled() || documentLine < 0) {
     return Collections.emptyList();
   }
   Document document = myEditor.getDocument();
   if (documentLine >= document.getLineCount()) {
     return Collections.emptyList();
   }
   int start = document.getLineStartOffset(documentLine);
   int end = document.getLineEndOffset(documentLine);
   return getSoftWrapsForRange(
       start,
       end
           + 1 /* it's theoretically possible that soft wrap is registered just before the line feed,
                * hence, we add '1' here assuming that end line offset points to line feed symbol */);
 }
 private static boolean isTestClass(PsiFile file, Editor editor) {
   PsiElement element1 = null;
   final SelectionModel selectionModel = editor.getSelectionModel();
   if (selectionModel.hasSelection()) {
     element1 = file.findElementAt(selectionModel.getSelectionStart());
   } else {
     final CaretModel caretModel = editor.getCaretModel();
     final Document document = editor.getDocument();
     int lineNumber = document.getLineNumber(caretModel.getOffset());
     if ((lineNumber >= 0) && (lineNumber < document.getLineCount())) {
       element1 = file.findElementAt(document.getLineStartOffset(lineNumber));
     }
   }
   if (element1 != null) {
     final PyClass clazz = PyUtil.getContainingClassOrSelf(element1);
     if (clazz != null && PythonUnitTestUtil.isTestCaseClass(clazz, null)) return true;
   }
   return false;
 }
  private int yPositionToOffset(int y, boolean beginLine) {
    if (myEditorScrollbarTop == -1 || myEditorTargetHeight == -1) {
      recalcEditorDimensions();
    }
    final int safeY = Math.max(0, y - myEditorScrollbarTop);
    VisualPosition visual;
    if (myEditorSourceHeight < myEditorTargetHeight) {
      visual = myEditor.xyToVisualPosition(new Point(0, safeY));
    } else {
      float fraction = Math.max(0, Math.min(1, safeY / (float) myEditorTargetHeight));
      final int lineCount = myEditorSourceHeight / myEditor.getLineHeight();
      visual = new VisualPosition((int) (fraction * lineCount), 0);
    }
    int line = myEditor.visualToLogicalPosition(visual).line;
    Document document = myEditor.getDocument();
    if (line < 0) return 0;
    if (line >= document.getLineCount()) return document.getTextLength();

    return beginLine ? document.getLineStartOffset(line) : document.getLineEndOffset(line);
  }
Ejemplo n.º 18
0
  @NotNull
  private static String[] getUpToDateLines(final Document document) {
    final Ref<String[]> linesRef = new Ref<String[]>();
    final Runnable runnable =
        () -> {
          final int lineCount = document.getLineCount();
          final String[] lines = new String[lineCount];
          final CharSequence chars = document.getCharsSequence();
          for (int i = 0; i < lineCount; i++) {
            lines[i] =
                chars
                    .subSequence(document.getLineStartOffset(i), document.getLineEndOffset(i))
                    .toString();
          }
          linesRef.set(lines);
        };
    ApplicationManager.getApplication().runReadAction(runnable);

    return linesRef.get();
  }
Ejemplo n.º 19
0
    @Override
    protected void onResultReady(@NotNull Location requestInfo, String resultText) {
      Editor editor = requestInfo.getEditor();
      assert editor != null;

      if (resultText == null) {
        return;
      }

      setText(resultText);

      int fileStartOffset = requestInfo.getStartOffset();
      int fileEndOffset = requestInfo.getEndOffset();

      Document document = editor.getDocument();
      int startLine = document.getLineNumber(fileStartOffset);
      int endLine = document.getLineNumber(fileEndOffset);
      if (endLine > startLine
          && fileEndOffset > 0
          && document.getCharsSequence().charAt(fileEndOffset - 1) == '\n') {
        endLine--;
      }

      Document byteCodeDocument = myEditor.getDocument();

      Pair<Integer, Integer> linesRange = mapLines(byteCodeDocument.getText(), startLine, endLine);
      int endSelectionLineIndex = Math.min(linesRange.second + 1, byteCodeDocument.getLineCount());

      int startOffset = byteCodeDocument.getLineStartOffset(linesRange.first);
      int endOffset =
          Math.min(
              byteCodeDocument.getLineStartOffset(endSelectionLineIndex),
              byteCodeDocument.getTextLength());

      myEditor.getCaretModel().moveToOffset(endOffset);
      myEditor.getScrollingModel().scrollToCaret(ScrollType.MAKE_VISIBLE);
      myEditor.getCaretModel().moveToOffset(startOffset);
      myEditor.getScrollingModel().scrollToCaret(ScrollType.MAKE_VISIBLE);

      myEditor.getSelectionModel().setSelection(startOffset, endOffset);
    }
  public void doClick(final MouseEvent e, final int width) {
    RangeHighlighter marker = getNearestRangeHighlighter(e, width);
    if (marker == null) return;
    int offset = marker.getStartOffset();

    final Document doc = myEditor.getDocument();
    if (doc.getLineCount() > 0) {
      // Necessary to expand folded block even if navigating just before one
      // Very useful when navigating to first unused import statement.
      int lineEnd = doc.getLineEndOffset(doc.getLineNumber(offset));
      myEditor.getCaretModel().moveToOffset(lineEnd);
    }

    myEditor.getCaretModel().moveToOffset(offset);
    myEditor.getSelectionModel().removeSelection();
    ScrollingModel scrollingModel = myEditor.getScrollingModel();
    scrollingModel.disableAnimation();
    scrollingModel.scrollToCaret(ScrollType.CENTER);
    scrollingModel.enableAnimation();
    fireErrorMarkerClicked(marker, e);
  }
Ejemplo n.º 21
0
  private int updateMovedRegionEnd(
      final Document document,
      int movedLineStart,
      final int valueStart,
      @NotNull final MoveInfo info,
      final boolean down) {
    final int line = document.getLineNumber(valueStart);
    final LineRange toMove = info.toMove;
    int delta = line - toMove.endLine;
    info.toMove = new LineRange(toMove.startLine, Math.max(line, toMove.endLine));

    // update moved range
    if (delta > 0 && down) {
      final LineRange toMove2 = info.toMove2;
      info.toMove2 =
          new LineRange(
              toMove2.startLine + delta,
              Math.min(toMove2.endLine + delta, document.getLineCount() - 1));
      movedLineStart = document.getLineStartOffset(toMove.startLine);
    }
    return movedLineStart;
  }
  @Nullable
  protected static RangeHighlighter createHighlighter(
      @NotNull Project project, @NotNull Document document, int lineIndex) {
    if (lineIndex < 0 || lineIndex >= document.getLineCount()) {
      return null;
    }

    EditorColorsScheme scheme = EditorColorsManager.getInstance().getGlobalScheme();
    TextAttributes attributes = scheme.getAttributes(DebuggerColors.BREAKPOINT_ATTRIBUTES);

    RangeHighlighter highlighter =
        ((MarkupModelEx) DocumentMarkupModel.forDocument(document, project, true))
            .addPersistentLineHighlighter(
                lineIndex, DebuggerColors.BREAKPOINT_HIGHLIGHTER_LAYER, attributes);
    if (highlighter == null || !highlighter.isValid()) {
      return null;
    }
    highlighter.putUserData(DebuggerColors.BREAKPOINT_HIGHLIGHTER_KEY, Boolean.TRUE);
    highlighter.setErrorStripeTooltip(
        DebuggerBundle.message("breakpoint.tooltip.text", lineIndex + 1));
    return highlighter;
  }
Ejemplo n.º 23
0
  public static String getLastCommentedLines(@NotNull Document document) {
    List<CharSequence> resultLines = new ArrayList<CharSequence>();
    for (int i = document.getLineCount() - 1; i >= 0; i--) {
      int lineStart = document.getLineStartOffset(i);
      int lineEnd = document.getLineEndOffset(i);
      if (document.getCharsSequence().subSequence(lineStart, lineEnd).toString().trim().isEmpty()) {
        continue;
      }

      if ("//"
          .equals(document.getCharsSequence().subSequence(lineStart, lineStart + 2).toString())) {
        resultLines.add(document.getCharsSequence().subSequence(lineStart + 2, lineEnd));
      } else {
        break;
      }
    }
    Collections.reverse(resultLines);
    StringBuilder result = new StringBuilder();
    for (CharSequence line : resultLines) {
      result.append(line).append("\n");
    }
    result.delete(result.length() - 1, result.length());
    return result.toString();
  }
  public static boolean canAddLineBreakpoint(
      Project project, final Document document, final int lineIndex) {
    if (lineIndex < 0 || lineIndex >= document.getLineCount()) {
      return false;
    }
    final BreakpointManager breakpointManager =
        DebuggerManagerEx.getInstanceEx(project).getBreakpointManager();
    final LineBreakpoint breakpointAtLine =
        breakpointManager.findBreakpoint(
            document, document.getLineStartOffset(lineIndex), CATEGORY);
    if (breakpointAtLine != null) {
      // there already exists a line breakpoint at this line
      return false;
    }
    PsiDocumentManager.getInstance(project).commitDocument(document);

    final boolean[] canAdd = new boolean[] {false};
    XDebuggerUtil.getInstance()
        .iterateLine(
            project,
            document,
            lineIndex,
            new Processor<PsiElement>() {
              @Override
              public boolean process(PsiElement element) {
                if ((element instanceof PsiWhiteSpace)
                    || (PsiTreeUtil.getParentOfType(element, PsiComment.class, false) != null)) {
                  return true;
                }
                PsiElement child = element;
                while (element != null) {

                  final int offset = element.getTextOffset();
                  if (offset >= 0) {
                    if (document.getLineNumber(offset) != lineIndex) {
                      break;
                    }
                  }
                  child = element;
                  element = element.getParent();
                }

                if (child instanceof PsiMethod
                    && child.getTextRange().getEndOffset()
                        >= document.getLineEndOffset(lineIndex)) {
                  PsiCodeBlock body = ((PsiMethod) child).getBody();
                  if (body == null) {
                    canAdd[0] = false;
                  } else {
                    PsiStatement[] statements = body.getStatements();
                    canAdd[0] =
                        statements.length > 0
                            && document.getLineNumber(statements[0].getTextOffset()) == lineIndex;
                  }
                } else {
                  canAdd[0] = true;
                }
                return false;
              }
            });

    return canAdd[0];
  }
  private EditorFragmentComponent(
      EditorEx editor, int startLine, int endLine, boolean showFolding, boolean showGutter) {
    Document doc = editor.getDocument();
    final int endOffset =
        endLine < doc.getLineCount() ? doc.getLineEndOffset(endLine) : doc.getTextLength();
    int textWidth =
        Math.min(
            editor.getMaxWidthInRange(doc.getLineStartOffset(startLine), endOffset),
            ScreenUtil.getScreenRectangle(1, 1).width);
    LOG.assertTrue(
        textWidth > 0,
        "TextWidth: " + textWidth + "; startLine:" + startLine + "; endLine:" + endLine + ";");

    FoldingModelEx foldingModel = editor.getFoldingModel();
    boolean isFoldingEnabled = foldingModel.isFoldingEnabled();
    if (!showFolding) {
      foldingModel.setFoldingEnabled(false);
    }

    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;
    int height = y2 - y1 == 0 ? editor.getLineHeight() : y2 - y1;
    LOG.assertTrue(
        height > 0,
        "Height: "
            + height
            + "; startLine:"
            + startLine
            + "; endLine:"
            + endLine
            + "; p1:"
            + p1
            + "; p2:"
            + p2);

    int savedScrollOffset = editor.getScrollingModel().getHorizontalScrollOffset();
    if (savedScrollOffset > 0) {
      editor.stopOptimizedScrolling();
      editor.getScrollingModel().scrollHorizontally(0);
    }

    final Image textImage = new BufferedImage(textWidth, height, BufferedImage.TYPE_INT_RGB);
    Graphics textGraphics = textImage.getGraphics();

    final JComponent rowHeader;
    final Image markersImage;
    if (showGutter) {
      rowHeader = editor.getGutterComponentEx();
      markersImage =
          new BufferedImage(Math.max(1, rowHeader.getWidth()), height, BufferedImage.TYPE_INT_RGB);
      Graphics markerGraphics = markersImage.getGraphics();

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

    textGraphics.translate(0, -y1);
    textGraphics.setClip(0, y1, textWidth, height);
    final boolean wasVisible = editor.setCaretVisible(false);
    editor.setPurePaintingMode(true);
    try {
      editor.getContentComponent().paint(textGraphics);
    } finally {
      editor.setPurePaintingMode(false);
    }
    if (wasVisible) {
      editor.setCaretVisible(true);
    }

    if (!showFolding) {
      foldingModel.setFoldingEnabled(isFoldingEnabled);
    }

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

    JComponent component =
        new JComponent() {
          public Dimension getPreferredSize() {
            return new Dimension(
                textImage.getWidth(null) + (markersImage == null ? 0 : markersImage.getWidth(null)),
                textImage.getHeight(null));
          }

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

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

    final Color borderColor =
        editor.getColorsScheme().getColor(EditorColors.SELECTED_TEARLINE_COLOR);

    Border outsideBorder = BorderFactory.createLineBorder(borderColor, 1);
    Border insideBorder = BorderFactory.createEmptyBorder(2, 2, 2, 2);
    setBorder(BorderFactory.createCompoundBorder(outsideBorder, insideBorder));
  }
  /**
   * @param hideByAnyKey
   * @param x <code>x</code> coordinate in layered pane coordinate system.
   * @param y <code>y</code> coordinate in layered pane coordinate system.
   */
  @Nullable
  public static LightweightHint showEditorFragmentHintAt(
      Editor editor,
      TextRange range,
      int x,
      int y,
      boolean showUpward,
      boolean showFolding,
      boolean hideByAnyKey) {
    if (ApplicationManager.getApplication().isUnitTestMode()) return null;
    Document document = editor.getDocument();

    int startOffset = range.getStartOffset();
    int startLine = document.getLineNumber(startOffset);
    CharSequence text = document.getCharsSequence();
    // There is a possible case that we have a situation like below:
    //    line 1
    //    line 2 <fragment start>
    //    line 3<fragment end>
    // We don't want to include 'line 2' to the target fragment then.
    boolean incrementLine = false;
    for (int offset = startOffset, max = Math.min(range.getEndOffset(), text.length());
        offset < max;
        offset++) {
      char c = text.charAt(offset);
      incrementLine = StringUtil.isWhiteSpace(c);
      if (!incrementLine || c == '\n') {
        break;
      }
    }
    if (incrementLine) {
      startLine++;
    }

    int endLine =
        Math.min(document.getLineNumber(range.getEndOffset()) + 1, document.getLineCount() - 1);

    // if (editor.logicalPositionToXY(new LogicalPosition(startLine, 0)).y >=
    // editor.logicalPositionToXY(new LogicalPosition(endLine, 0)).y) return null;
    if (startLine >= endLine) return null;

    EditorFragmentComponent fragmentComponent =
        createEditorFragmentComponent(editor, startLine, endLine, showFolding, true);

    if (showUpward) {
      y -= fragmentComponent.getPreferredSize().height + 10;
      y = Math.max(0, y);
    }

    final JComponent c = editor.getComponent();
    x = SwingUtilities.convertPoint(c, new Point(-3, 0), UIUtil.getRootPane(c)).x; // IDEA-68016

    Point p = new Point(x, y);
    LightweightHint hint = new MyComponentHint(fragmentComponent);
    HintManagerImpl.getInstanceImpl()
        .showEditorHint(
            hint,
            editor,
            p,
            (hideByAnyKey ? HintManager.HIDE_BY_ANY_KEY : 0)
                | HintManager.HIDE_BY_TEXT_CHANGE
                | HintManager.HIDE_BY_MOUSEOVER,
            0,
            false,
            new HintHint(editor, p));
    return hint;
  }
  public void doWrapLongLinesIfNecessary(
      @NotNull final Editor editor,
      @NotNull final Project project,
      @NotNull Document document,
      int startOffset,
      int endOffset) {
    // Normalization.
    int startOffsetToUse = Math.min(document.getTextLength(), Math.max(0, startOffset));
    int endOffsetToUse = Math.min(document.getTextLength(), Math.max(0, endOffset));

    LineWrapPositionStrategy strategy = LanguageLineWrapPositionStrategy.INSTANCE.forEditor(editor);
    CharSequence text = document.getCharsSequence();
    int startLine = document.getLineNumber(startOffsetToUse);
    int endLine = document.getLineNumber(Math.max(0, endOffsetToUse - 1));
    int maxLine = Math.min(document.getLineCount(), endLine + 1);
    int tabSize = EditorUtil.getTabSize(editor);
    if (tabSize <= 0) {
      tabSize = 1;
    }
    int spaceSize = EditorUtil.getSpaceWidth(Font.PLAIN, editor);
    int[] shifts = new int[2];
    // shifts[0] - lines shift.
    // shift[1] - offset shift.

    for (int line = startLine; line < maxLine; line++) {
      int startLineOffset = document.getLineStartOffset(line);
      int endLineOffset = document.getLineEndOffset(line);
      final int preferredWrapPosition =
          calculatePreferredWrapPosition(
              editor, text, tabSize, spaceSize, startLineOffset, endLineOffset, endOffsetToUse);

      if (preferredWrapPosition < 0 || preferredWrapPosition >= endLineOffset) {
        continue;
      }
      if (preferredWrapPosition >= endOffsetToUse) {
        return;
      }

      // We know that current line exceeds right margin if control flow reaches this place, so, wrap
      // it.
      int wrapOffset =
          strategy.calculateWrapPosition(
              document,
              editor.getProject(),
              Math.max(startLineOffset, startOffsetToUse),
              Math.min(endLineOffset, endOffsetToUse),
              preferredWrapPosition,
              false,
              false);
      if (wrapOffset < 0 // No appropriate wrap position is found.
          // No point in splitting line when its left part contains only white spaces, example:
          //    line start -> |                   | <- right margin
          //                  |   aaaaaaaaaaaaaaaa|aaaaaaaaaaaaaaaaaaaa() <- don't want to wrap this
          // line even if it exceeds right margin
          || CharArrayUtil.shiftBackward(text, startLineOffset, wrapOffset - 1, " \t")
              < startLineOffset) {
        continue;
      }

      // Move caret to the target position and emulate pressing <enter>.
      editor.getCaretModel().moveToOffset(wrapOffset);
      emulateEnter(editor, project, shifts);

      // We know that number of lines is just increased, hence, update the data accordingly.
      maxLine += shifts[0];
      endOffsetToUse += shifts[1];
    }
  }
 protected PsiBasedStripTrailingSpacesFilter(@NotNull Document document) {
   myDocument = document;
   myDisabledLinesBitSet = new BitSet(document.getLineCount());
 }
Ejemplo n.º 29
0
  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;

    EventLog.LogEntry pair =
        EventLog.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) {
      myHyperlinkSupport
          .getValue()
          .addHyperlink(
              link.first.getStartOffset() + msgStart,
              link.first.getEndOffset() + msgStart,
              null,
              link.second);
    }

    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);
    }
  }
    private boolean insertDocAsterisk(
        int lineStart,
        boolean docAsterisk,
        boolean previousLineIndentUsed,
        CodeDocumentationAwareCommenter commenter) {
      PsiElement atLineStart = myFile.findElementAt(lineStart);
      if (atLineStart == null) return false;

      final String linePrefix = commenter.getDocumentationCommentLinePrefix();
      final String docPrefix = commenter.getDocumentationCommentPrefix();

      final String text = atLineStart.getText();
      final TextRange textRange = atLineStart.getTextRange();

      if (text.equals(linePrefix)
          || text.equals(docPrefix)
          || docPrefix != null
              && text.regionMatches(
                  lineStart - textRange.getStartOffset(), docPrefix, 0, docPrefix.length())
          || linePrefix != null
              && text.regionMatches(
                  lineStart - textRange.getStartOffset(), linePrefix, 0, linePrefix.length())) {
        PsiElement element = myFile.findElementAt(myOffset);
        if (element == null) return false;

        PsiComment comment =
            element instanceof PsiComment
                ? (PsiComment) element
                : PsiTreeUtil.getParentOfType(element, PsiComment.class, false);
        if (comment != null) {
          int commentEnd = comment.getTextRange().getEndOffset();
          if (myOffset >= commentEnd) {
            docAsterisk = false;
          } else {
            removeTrailingSpaces(myDocument, myOffset);
            String toInsert =
                previousLineIndentUsed
                    ? "*"
                    : CodeDocumentationUtil.createDocCommentLine("", getProject(), commenter);
            myDocument.insertString(myOffset, toInsert);
            PsiDocumentManager.getInstance(getProject()).commitAllDocuments();
          }
        } else {
          docAsterisk = false;
        }
      } else if (linePrefix != null
          && atLineStart instanceof PsiComment
          && ((PsiComment) atLineStart).getTokenType() == commenter.getBlockCommentTokenType()) {
        // Check if C-Style comment already uses asterisks.
        boolean usesAstersk = false;
        int commentLine = myDocument.getLineNumber(textRange.getStartOffset());
        if (commentLine < myDocument.getLineCount() - 1 && textRange.getEndOffset() >= myOffset) {
          int nextLineOffset = myDocument.getLineStartOffset(commentLine + 1);
          if (nextLineOffset < textRange.getEndOffset()) {
            final CharSequence chars = myDocument.getCharsSequence();
            nextLineOffset = CharArrayUtil.shiftForward(chars, nextLineOffset, " \t");
            usesAstersk = CharArrayUtil.regionMatches(chars, nextLineOffset, linePrefix);
          }
        }
        if (usesAstersk) {
          removeTrailingSpaces(myDocument, myOffset);
          myDocument.insertString(myOffset, linePrefix + " ");
          PsiDocumentManager.getInstance(getProject()).commitAllDocuments();
        }
        docAsterisk = usesAstersk;
      } else {
        docAsterisk = false;
      }
      return docAsterisk;
    }