Skip to content

Commit

Permalink
chore: add StringsTest, TextUtilsTest
Browse files Browse the repository at this point in the history
  • Loading branch information
sebthom committed Feb 2, 2024
1 parent 0497113 commit cfbad5c
Show file tree
Hide file tree
Showing 6 changed files with 501 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
*
* Contributors:
* - Microsoft Corporation: Initial code, written in TypeScript, licensed under MIT license
* - Sebastian Thomschke - translation and adaptation to Java
* - Sebastian Thomschke (Vegard IT) - translation and adaptation to Java
*/
package org.eclipse.tm4e.languageconfiguration.internal.utils;

Expand All @@ -32,50 +32,53 @@ public static String escapeRegExpCharacters(final String value) {
}

/**
* @returns first index of the string that is not whitespace or -1 if string is empty or contains only whitespaces
* @returns first index of the string that is not whitespace; or -1 if string is empty or contains only whitespaces.
*/
public static int firstNonWhitespaceIndex(final String text) {
for (int i = 0, len = text.length(); i < len; i++) {
final char ch = text.charAt(i);
if (!Character.isWhitespace(ch)) {
if (!Character.isWhitespace(ch))
return i;
}
}
return -1;
}

/**
* @return the leading whitespace of the string. If the string contains only whitespaces, returns entire string.
* @return the leading whitespace of the string; or the entire string if the string contains only whitespaces.
*/
public static String getLeadingWhitespace(final String searchIn) {
return getLeadingWhitespace(searchIn, 0, searchIn.length());
}

/**
* @return the leading whitespace of the string. If the string contains only whitespaces, returns entire string.
* @return the leading whitespace of the string; or the entire string if the string contains only whitespaces.
*/
public static String getLeadingWhitespace(final String searchIn, final int startAt) {
return getLeadingWhitespace(searchIn, startAt, searchIn.length());
}

/**
* @return the leading whitespace of the string; or the entire string if the string contains only whitespaces.
*/
public static String getLeadingWhitespace(final String searchIn, final int startAt, final int endAt) {
for (int i = startAt; i < endAt; i++) {
final char ch = searchIn.charAt(endAt);
if (ch != ' ' && ch != '\t') {
final char ch = searchIn.charAt(i);
if (!Character.isWhitespace(ch))
return searchIn.substring(startAt, i);
}
}
return searchIn.substring(startAt, endAt);
}

/**
* @return the last index of the string that is not whitespace.
* If the string is empty or contains only whitespaces, returns -1.
* @return the last index of the string that is not whitespace; or -1 if string is empty or contains only whitespaces.
* Defaults to starting from the end of the string.
*/
public static int lastNonWhitespaceIndex(final String str) {
return lastNonWhitespaceIndex(str, str.length() - 1);
}

/**
* @return the last index of the string that is not whitespace.
* If the string is empty or contains only whitespaces, returns -1.
* @return the last index of the string that is not whitespace; or -1 if string is empty or contains only whitespaces.
*/
public static int lastNonWhitespaceIndex(final String str, final int startIndex) {
for (int i = startIndex; i >= 0; i--) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,20 @@
public final class TextEditorPrefs {

public static CursorConfiguration getCursorConfiguration(final @Nullable ITextEditor editor) {
final var editorPrefStore = editor == null ? null : editor.getAdapter(IPreferenceStore.class);
final var editorPrefStore = editor == null
? null
: editor.getAdapter(IPreferenceStore.class);

final var useSpacesPrefStore = editorPrefStore != null && editorPrefStore.contains(EDITOR_SPACES_FOR_TABS)
? editorPrefStore
: EditorsUI.getPreferenceStore();

final var tabWidthPrefStore = editorPrefStore != null && editorPrefStore.contains(EDITOR_TAB_WIDTH)
? editorPrefStore
: EditorsUI.getPreferenceStore();
return new CursorConfiguration(useSpacesPrefStore.getBoolean(EDITOR_SPACES_FOR_TABS),

return new CursorConfiguration(
useSpacesPrefStore.getBoolean(EDITOR_SPACES_FOR_TABS),
tabWidthPrefStore.getInt(EDITOR_TAB_WIDTH));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,28 +35,6 @@ private static boolean isLegalLineDelimiter(final IDocument doc, final String de
return false;
}

/**
* Returns the start of searchFor at the offset in the searchIn.
* If the searchFor is not in the searchIn at the offset, returns -1.
* <p>
* Example:
*
* <pre>
* text="apple banana", offset=8, string="banana" -> returns 6
* </pre>
*/
public static int startIndexOfOffsetTouchingString(final String searchIn, final int offset, final String searchFor) {
final int start = Math.max(0, offset - searchFor.length());
int end = offset + searchFor.length();
end = end >= searchIn.length() ? searchIn.length() : end;
try {
final int indexInSubtext = searchIn.substring(start, end).indexOf(searchFor);
return indexInSubtext == -1 ? -1 : start + indexInSubtext;
} catch (final IndexOutOfBoundsException e) {
return -1;
}
}

public static String getIndentationFromWhitespace(final String whitespace, final CursorConfiguration cursorCfg) {
final var tab = "\t";
int indentOffset = 0;
Expand All @@ -80,7 +58,7 @@ public static String getIndentationFromWhitespace(final String whitespace, final

/**
* @see <a href=
* "https://github.com/microsoft/vscode/blob/ba2cf46e20df3edf77bdd905acde3e175d985f70/src/vs/base/common/strings.ts">
* "https://github.com/microsoft/vscode/blob/ba2cf46e20df3edf77bdd905acde3e175d985f70/src/vs/editor/common/languages/languageConfigurationRegistry.ts">
* github.com/microsoft/vscode/blob/main/src/vs/editor/common/languages/languageConfigurationRegistry.ts</a>
*/
public static String getIndentationAtPosition(final IDocument doc, final int offset) {
Expand Down Expand Up @@ -120,6 +98,12 @@ private static int findEndOfWhiteSpace(final IDocument doc, final int startAt, f
return endAt;
}

public static String getLeadingWhitespace(final IDocument doc, int lineIndex) throws BadLocationException {
final int lineStartOffset = doc.getLineOffset(lineIndex);
final int lineLength = doc.getLineLength(lineIndex);
return doc.get(lineStartOffset, findEndOfWhiteSpace(doc, lineStartOffset, lineStartOffset + lineLength) - lineStartOffset);
}

/**
* Determines if all the characters at any offset of the specified document line are the whitespace characters.
*
Expand All @@ -145,6 +129,22 @@ public static boolean isBlankLine(final IDocument doc, final int lineIndex) {
return true;
}

public static boolean isEmptyLine(final IDocument doc, final int lineIndex) {
try {
final int lineLength = doc.getLineLength(lineIndex);
if (lineLength > 2)
return false;
if (lineLength == 0)
return true;

final int lineOffset = doc.getLineOffset(lineIndex);
return isLegalLineDelimiter(doc, doc.get(lineOffset, lineLength));
} catch (final BadLocationException e) {
// Ignore, forcing a positive result
return true;
}
}

private TextUtils() {
}
}
Loading

0 comments on commit cfbad5c

Please sign in to comment.