コード例 #1
0
  /**
   * Configures given editor to wrap at given width, assuming characters are of given width
   *
   * @return whether any actual wraps of editor contents were created as a result of turning on soft
   *     wraps
   */
  @TestOnly
  public static boolean configureSoftWraps(
      Editor editor, final int visibleWidth, final int charWidthInPixels) {
    editor.getSettings().setUseSoftWraps(true);
    SoftWrapModelImpl model = (SoftWrapModelImpl) editor.getSoftWrapModel();
    model.setSoftWrapPainter(
        new SoftWrapPainter() {
          @Override
          public int paint(
              @NotNull Graphics g,
              @NotNull SoftWrapDrawingType drawingType,
              int x,
              int y,
              int lineHeight) {
            return charWidthInPixels;
          }

          @Override
          public int getDrawingHorizontalOffset(
              @NotNull Graphics g,
              @NotNull SoftWrapDrawingType drawingType,
              int x,
              int y,
              int lineHeight) {
            return charWidthInPixels;
          }

          @Override
          public int getMinDrawingWidth(@NotNull SoftWrapDrawingType drawingType) {
            return charWidthInPixels;
          }

          @Override
          public boolean canUse() {
            return true;
          }

          @Override
          public void reinit() {}
        });
    model.reinitSettings();

    SoftWrapApplianceManager applianceManager = model.getApplianceManager();
    applianceManager.setWidthProvider(
        new SoftWrapApplianceManager.VisibleAreaWidthProvider() {
          @Override
          public int getVisibleAreaWidth() {
            return visibleWidth;
          }
        });
    model.setEditorTextRepresentationHelper(
        new DefaultEditorTextRepresentationHelper(editor) {
          @Override
          public int charWidth(char c, int fontType) {
            return charWidthInPixels;
          }
        });
    applianceManager.registerSoftWrapIfNecessary();
    return !model.getRegisteredSoftWraps().isEmpty();
  }
コード例 #2
0
 public static void setEditorVisibleSize(Editor editor, int widthInChars, int heightInChars) {
   Dimension size =
       new Dimension(
           widthInChars * EditorUtil.getSpaceWidth(Font.PLAIN, editor),
           heightInChars * editor.getLineHeight());
   ((EditorEx) editor).getScrollPane().getViewport().setExtentSize(size);
 }
コード例 #3
0
 @NotNull
 private static DataContext createEditorContext(@NotNull Editor editor) {
   Object e = editor;
   Object hostEditor =
       editor instanceof EditorWindow ? ((EditorWindow) editor).getDelegate() : editor;
   Map<String, Object> map =
       ContainerUtil.newHashMap(
           Pair.create(CommonDataKeys.HOST_EDITOR.getName(), hostEditor),
           Pair.createNonNull(CommonDataKeys.EDITOR.getName(), e));
   DataContext parent = DataManager.getInstance().getDataContext(editor.getContentComponent());
   return SimpleDataContext.getSimpleContext(map, parent);
 }
コード例 #4
0
 public static void performTypingAction(Editor editor, char c) {
   EditorActionManager actionManager = EditorActionManager.getInstance();
   if (c == BACKSPACE_FAKE_CHAR) {
     executeAction(editor, IdeActions.ACTION_EDITOR_BACKSPACE);
   } else if (c == SMART_ENTER_FAKE_CHAR) {
     executeAction(editor, IdeActions.ACTION_EDITOR_COMPLETE_STATEMENT);
   } else if (c == SMART_LINE_SPLIT_CHAR) {
     executeAction(editor, IdeActions.ACTION_EDITOR_SPLIT);
   } else if (c == '\n') {
     executeAction(editor, IdeActions.ACTION_EDITOR_ENTER);
   } else {
     TypedAction action = actionManager.getTypedAction();
     action.actionPerformed(
         editor, c, DataManager.getInstance().getDataContext(editor.getContentComponent()));
   }
 }
コード例 #5
0
 public static FoldRegion addFoldRegion(
     @NotNull Editor editor,
     final int startOffset,
     final int endOffset,
     final String placeholder,
     final boolean collapse) {
   final FoldingModel foldingModel = editor.getFoldingModel();
   final Ref<FoldRegion> ref = new Ref<FoldRegion>();
   foldingModel.runBatchFoldingOperation(
       new Runnable() {
         @Override
         public void run() {
           FoldRegion region = foldingModel.addFoldRegion(startOffset, endOffset, placeholder);
           assertNotNull(region);
           region.setExpanded(!collapse);
           ref.set(region);
         }
       });
   return ref.get();
 }
コード例 #6
0
  public static void verifyCaretAndSelectionState(
      Editor editor, CaretAndSelectionState caretState, String message) {
    boolean hasChecks = false;
    for (int i = 0; i < caretState.carets.size(); i++) {
      EditorTestUtil.CaretInfo expected = caretState.carets.get(i);
      if (expected.position != null || expected.selection != null) {
        hasChecks = true;
        break;
      }
    }
    if (!hasChecks) {
      return; // nothing to check, so we skip caret/selection assertions
    }
    String messageSuffix = message == null ? "" : (message + ": ");
    CaretModel caretModel = editor.getCaretModel();
    List<Caret> allCarets = new ArrayList<Caret>(caretModel.getAllCarets());
    assertEquals(
        messageSuffix + " Unexpected number of carets", caretState.carets.size(), allCarets.size());
    for (int i = 0; i < caretState.carets.size(); i++) {
      String caretDescription =
          caretState.carets.size() == 1
              ? ""
              : "caret " + (i + 1) + "/" + caretState.carets.size() + " ";
      Caret currentCaret = allCarets.get(i);
      int actualCaretLine = editor.getDocument().getLineNumber(currentCaret.getOffset());
      int actualCaretColumn =
          currentCaret.getOffset() - editor.getDocument().getLineStartOffset(actualCaretLine);
      LogicalPosition actualCaretPosition = new LogicalPosition(actualCaretLine, actualCaretColumn);
      int selectionStart = currentCaret.getSelectionStart();
      int selectionEnd = currentCaret.getSelectionEnd();
      LogicalPosition actualSelectionStart = editor.offsetToLogicalPosition(selectionStart);
      LogicalPosition actualSelectionEnd = editor.offsetToLogicalPosition(selectionEnd);
      CaretInfo expected = caretState.carets.get(i);
      if (expected.position != null) {
        assertEquals(
            messageSuffix + caretDescription + "unexpected caret position",
            expected.position,
            actualCaretPosition);
      }
      if (expected.selection != null) {
        LogicalPosition expectedSelectionStart =
            editor.offsetToLogicalPosition(expected.selection.getStartOffset());
        LogicalPosition expectedSelectionEnd =
            editor.offsetToLogicalPosition(expected.selection.getEndOffset());

        assertEquals(
            messageSuffix + caretDescription + "unexpected selection start",
            expectedSelectionStart,
            actualSelectionStart);
        assertEquals(
            messageSuffix + caretDescription + "unexpected selection end",
            expectedSelectionEnd,
            actualSelectionEnd);
      } else {
        assertFalse(
            messageSuffix
                + caretDescription
                + "should has no selection, but was: ("
                + actualSelectionStart
                + ", "
                + actualSelectionEnd
                + ")",
            currentCaret.hasSelection());
      }
    }
  }
コード例 #7
0
 /**
  * Applies given caret/selection state to the editor. Editor text must have been set up
  * previously.
  */
 public static void setCaretsAndSelection(Editor editor, CaretAndSelectionState caretsState) {
   CaretModel caretModel = editor.getCaretModel();
   if (caretModel.supportsMultipleCarets()) {
     List<CaretState> states = new ArrayList<CaretState>(caretsState.carets.size());
     for (CaretInfo caret : caretsState.carets) {
       states.add(
           new CaretState(
               caret.position == null
                   ? null
                   : editor.offsetToLogicalPosition(caret.getCaretOffset(editor.getDocument())),
               caret.selection == null
                   ? null
                   : editor.offsetToLogicalPosition(caret.selection.getStartOffset()),
               caret.selection == null
                   ? null
                   : editor.offsetToLogicalPosition(caret.selection.getEndOffset())));
     }
     caretModel.setCaretsAndSelections(states);
   } else {
     assertEquals("Multiple carets are not supported by the model", 1, caretsState.carets.size());
     CaretInfo caret = caretsState.carets.get(0);
     if (caret.position != null) {
       caretModel.moveToOffset(caret.getCaretOffset(editor.getDocument()));
     }
     if (caret.selection != null) {
       editor
           .getSelectionModel()
           .setSelection(caret.selection.getStartOffset(), caret.selection.getEndOffset());
     } else {
       editor.getSelectionModel().removeSelection();
     }
   }
   if (caretsState.blockSelection != null) {
     editor
         .getSelectionModel()
         .setBlockSelection(
             editor.offsetToLogicalPosition(caretsState.blockSelection.getStartOffset()),
             editor.offsetToLogicalPosition(caretsState.blockSelection.getEndOffset()));
   }
 }