Java Code Examples for com.intellij.openapi.editor.ex.util.EditorUtil#getTabSize()

The following examples show how to use com.intellij.openapi.editor.ex.util.EditorUtil#getTabSize() . 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: SoftWrapModelImpl.java    From consulo with Apache License 2.0 6 votes vote down vote up
/**
 * Called on editor settings change. Current model is expected to drop all cached information about the settings if any.
 */
public void reinitSettings() {
  boolean softWrapsUsedBefore = myUseSoftWraps;
  myUseSoftWraps = areSoftWrapsEnabledInEditor();

  int tabWidthBefore = myTabWidth;
  myTabWidth = EditorUtil.getTabSize(myEditor);

  boolean fontsChanged = false;
  if (!myFontPreferences.equals(myEditor.getColorsScheme().getFontPreferences()) && myEditorTextRepresentationHelper instanceof DefaultEditorTextRepresentationHelper) {
    fontsChanged = true;
    myEditor.getColorsScheme().getFontPreferences().copyTo(myFontPreferences);
    ((DefaultEditorTextRepresentationHelper)myEditorTextRepresentationHelper).clearSymbolWidthCache();
    myPainter.reinit();
  }

  if (myUseSoftWraps != softWrapsUsedBefore || tabWidthBefore >= 0 && myTabWidth != tabWidthBefore || fontsChanged) {
    myApplianceManager.reset();
    myDeferredFoldRegions.clear();
    myStorage.removeAll();
    myEditor.myView.reinitSettings();
    myEditor.getScrollingModel().scrollToCaret(ScrollType.CENTER);
  }
}
 
Example 2
Source File: EditorWindowImpl.java    From consulo with Apache License 2.0 6 votes vote down vote up
private int calcOffset(int col, int lineNumber, int lineStartOffset) {
  CharSequence text = myDocumentWindow.getImmutableCharSequence();
  int tabSize = EditorUtil.getTabSize(myDelegate);
  int end = myDocumentWindow.getLineEndOffset(lineNumber);
  int currentColumn = 0;
  for (int i = lineStartOffset; i < end; i++) {
    char c = text.charAt(i);
    if (c == '\t') {
      currentColumn = (currentColumn / tabSize + 1) * tabSize;
    }
    else {
      currentColumn++;
    }
    if (col < currentColumn) return i;
  }
  return end;
}
 
Example 3
Source File: EditorView.java    From consulo with Apache License 2.0 5 votes vote down vote up
public int getTabSize() {
  synchronized (myLock) {
    if (myTabSize < 0) {
      myTabSize = EditorUtil.getTabSize(myEditor);
    }
    return myTabSize;
  }
}
 
Example 4
Source File: BaseIndentEnterHandler.java    From consulo with Apache License 2.0 5 votes vote down vote up
private static int calcLogicalLength(Editor editor, CharSequence lineIndent) {
  int result = 0;
  for (int i = 0; i < lineIndent.length(); i++) {
    if (lineIndent.charAt(i) == '\t') {
      result += EditorUtil.getTabSize(editor);
    } else {
      result++;
    }
  }
  return result;
}
 
Example 5
Source File: CodeFormatterFacade.java    From consulo with Apache License 2.0 4 votes vote down vote up
public void doWrapLongLinesIfNecessary(@Nonnull final Editor editor, @Nonnull final Project project, @Nonnull Document document,
                                       int startOffset, int endOffset, List<? extends TextRange> enabledRanges) {
  // 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.
  int cumulativeShift = 0;

  for (int line = startLine; line < maxLine; line++) {
    int startLineOffset = document.getLineStartOffset(line);
    int endLineOffset = document.getLineEndOffset(line);
    if (!canWrapLine(Math.max(startOffsetToUse, startLineOffset), Math.min(endOffsetToUse, endLineOffset), cumulativeShift, enabledRanges)) {
      continue;
    }

    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);

    //If number of inserted symbols on new line after wrapping more or equal then symbols left on previous line
    //there was no point to wrapping it, so reverting to before wrapping version
    if (shifts[1] - 1 >= wrapOffset - startLineOffset) {
      document.deleteString(wrapOffset, wrapOffset + shifts[1]);
    }
    else {
      // We know that number of lines is just increased, hence, update the data accordingly.
      maxLine += shifts[0];
      endOffsetToUse += shifts[1];
      cumulativeShift += shifts[1];
    }

  }
}