mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-13 21:55:01 +07:00
these methods are good candidates to move them to com.intellij.util.DocumentUtil
This commit is contained in:
@@ -65,4 +65,21 @@ public final class DocumentUtil {
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public static int getFirstNonSpaceCharOffset(@NotNull Document document, int line) {
|
||||
int startOffset = document.getLineStartOffset(line);
|
||||
int endOffset = document.getLineEndOffset(line);
|
||||
return getFirstNonSpaceCharOffset(document, startOffset, endOffset);
|
||||
}
|
||||
|
||||
public static int getFirstNonSpaceCharOffset(@NotNull Document document, int startOffset, int endOffset) {
|
||||
CharSequence text = document.getImmutableCharSequence();
|
||||
for (int i = startOffset; i < endOffset; i++) {
|
||||
char c = text.charAt(i);
|
||||
if (c != ' ' && c != '\t') {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return startOffset;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -37,6 +37,7 @@ import com.intellij.openapi.util.Disposer;
|
||||
import com.intellij.openapi.util.UserDataHolderBase;
|
||||
import com.intellij.util.CommonProcessors;
|
||||
import com.intellij.util.Consumer;
|
||||
import com.intellij.util.DocumentUtil;
|
||||
import com.intellij.util.Processor;
|
||||
import com.intellij.util.containers.ContainerUtil;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
@@ -70,7 +71,7 @@ public class MarkupModelImpl extends UserDataHolderBase implements MarkupModelEx
|
||||
throw new IndexOutOfBoundsException("lineNumber:" + lineNumber + ". Must be in [0, " + (getDocument().getLineCount() - 1) + "]");
|
||||
}
|
||||
|
||||
int offset = getFirstNonSpaceCharOffset(getDocument(), lineNumber);
|
||||
int offset = DocumentUtil.getFirstNonSpaceCharOffset(getDocument(), lineNumber);
|
||||
return addRangeHighlighter(offset, offset, layer, textAttributes, HighlighterTargetArea.LINES_IN_RANGE);
|
||||
}
|
||||
|
||||
@@ -81,7 +82,7 @@ public class MarkupModelImpl extends UserDataHolderBase implements MarkupModelEx
|
||||
return null;
|
||||
}
|
||||
|
||||
int offset = getFirstNonSpaceCharOffset(getDocument(), lineNumber);
|
||||
int offset = DocumentUtil.getFirstNonSpaceCharOffset(getDocument(), lineNumber);
|
||||
return addRangeHighlighter(PersistentRangeHighlighterImpl.create(this, offset, layer, HighlighterTargetArea.LINES_IN_RANGE, textAttributes, false), null);
|
||||
}
|
||||
|
||||
@@ -89,27 +90,6 @@ public class MarkupModelImpl extends UserDataHolderBase implements MarkupModelEx
|
||||
return lineNumber >= getDocument().getLineCount() || lineNumber < 0;
|
||||
}
|
||||
|
||||
// The rationale why we don't bind to the line start offset here is that following: suppose particular breakpoint is hit
|
||||
// during debugging. We may want to type <enter> at the active line indent and highlighted string will be moved one line
|
||||
// down as well then.
|
||||
// IDEA-46403
|
||||
public static int getFirstNonSpaceCharOffset(@NotNull Document document, int line) {
|
||||
int startOffset = document.getLineStartOffset(line);
|
||||
int endOffset = document.getLineEndOffset(line);
|
||||
return getFirstNonSpaceCharOffset(document, startOffset, endOffset);
|
||||
}
|
||||
|
||||
public static int getFirstNonSpaceCharOffset(@NotNull Document document, int startOffset, int endOffset) {
|
||||
CharSequence text = document.getImmutableCharSequence();
|
||||
for (int i = startOffset; i < endOffset; i++) {
|
||||
char c = text.charAt(i);
|
||||
if (c != ' ' && c != '\t') {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return startOffset;
|
||||
}
|
||||
|
||||
// NB: Can return invalid highlighters
|
||||
@Override
|
||||
@NotNull
|
||||
|
||||
+2
-1
@@ -22,6 +22,7 @@ import com.intellij.openapi.editor.impl.event.DocumentEventImpl;
|
||||
import com.intellij.openapi.editor.markup.HighlighterTargetArea;
|
||||
import com.intellij.openapi.editor.markup.MarkupModel;
|
||||
import com.intellij.openapi.editor.markup.TextAttributes;
|
||||
import com.intellij.util.DocumentUtil;
|
||||
import com.intellij.util.diff.FilesTooBigForDiffException;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
@@ -73,7 +74,7 @@ class PersistentRangeHighlighterImpl extends RangeHighlighterImpl implements Ran
|
||||
}
|
||||
}
|
||||
if (isValid() && getTargetArea() == HighlighterTargetArea.LINES_IN_RANGE) {
|
||||
setIntervalStart(MarkupModelImpl.getFirstNonSpaceCharOffset(getDocument(), getLine()));
|
||||
setIntervalStart(DocumentUtil.getFirstNonSpaceCharOffset(getDocument(), getLine()));
|
||||
setIntervalEnd(getDocument().getLineEndOffset(getLine()));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,6 +28,7 @@ import com.intellij.psi.codeStyle.CodeStyleSettings;
|
||||
import com.intellij.psi.codeStyle.CodeStyleSettingsManager;
|
||||
import com.intellij.psi.codeStyle.CommonCodeStyleSettings;
|
||||
import com.intellij.psi.tree.TokenSet;
|
||||
import com.intellij.util.DocumentUtil;
|
||||
import com.jetbrains.python.PyElementTypes;
|
||||
import com.jetbrains.python.PyTokenTypes;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
@@ -212,7 +213,7 @@ public class PyEmacsHandler implements EmacsProcessingHandler {
|
||||
int caretOffset = context.editor.getCaretModel().getOffset();
|
||||
String newIndentString = new IndentInfo(0, newIndent, 0).generateNewWhiteSpace(context.getIndentOptions());
|
||||
int start = context.document.getLineStartOffset(context.targetLine);
|
||||
int end = getFirstNonWsSymbolOffset(context.document, context.targetLine);
|
||||
int end = DocumentUtil.getFirstNonSpaceCharOffset(context.document, context.targetLine);
|
||||
context.editor.getDocument().replaceString(start, end, newIndentString);
|
||||
if (caretOffset > start && caretOffset < end) {
|
||||
context.editor.getCaretModel().moveToOffset(start + newIndentString.length());
|
||||
@@ -308,19 +309,6 @@ public class PyEmacsHandler implements EmacsProcessingHandler {
|
||||
return result;
|
||||
}
|
||||
|
||||
private static int getFirstNonWsSymbolOffset(@NotNull Document document, int line) {
|
||||
int start = document.getLineStartOffset(line);
|
||||
int end = document.getLineEndOffset(line);
|
||||
CharSequence text = document.getCharsSequence();
|
||||
for (int i = start; i < end; i++) {
|
||||
char c = text.charAt(i);
|
||||
if (c != ' ' && c != '\t') {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return end;
|
||||
}
|
||||
|
||||
private static class LineInfo {
|
||||
|
||||
public final int line;
|
||||
|
||||
Reference in New Issue
Block a user