Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrapped long line and folding performance #513

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ else if (p1>doc.getLength()) {
// token types, etc.), and get the x-location (in pixels) of the
// beginning of this new token list.
TokenSubList subList = TokenUtils.getSubTokenList(t, p0, e, textArea,
0, TEMP_TOKEN);
0, TEMP_TOKEN, false, 0);
t = subList.tokenList;

rect = t.listOffsetToView(textArea, e, p1, x0, rect);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,19 @@ int getOffsetBeforeX(RSyntaxTextArea textArea, TabExpander e,
*/
float getWidth(RSyntaxTextArea textArea, TabExpander e, float x0);

/**
* Returns the width of this token given the specified parameters.
*
* @param textArea The text area in which the token is being painted.
* @param e Describes how to expand tabs. This parameter cannot be
* <code>null</code>.
* @param x0 The pixel-location at which the token begins. This is needed
* because of tabs.
* @param maxWidth the maximum width we care about measuring
* @return The width of the token, in pixels.
* @see #getWidthUpTo
*/
float getWidth(RSyntaxTextArea textArea, TabExpander e, float x0, float maxWidth);

/**
* Returns the width of a specified number of characters in this token.
Expand All @@ -318,6 +331,23 @@ int getOffsetBeforeX(RSyntaxTextArea textArea, TabExpander e,
float getWidthUpTo(int numChars, RSyntaxTextArea textArea,
TabExpander e, float x0);

/**
* Returns the width of a specified number of characters in this token.
* For example, for the token "while", specifying a value of <code>3</code>
* here returns the width of the "whi" portion of the token.
*
* @param numChars The number of characters for which to get the width.
* @param textArea The text area in which the token is being painted.
* @param e How to expand tabs. This value cannot be <code>null</code>.
* @param x0 The pixel-location at which this token begins. This is needed
* because of tabs.
* @param maxWidth the maximum width we care about measuring
* @return The width of the specified number of characters in this token.
* @see #getWidth
*/
float getWidthUpTo(int numChars, RSyntaxTextArea textArea,
TabExpander e, float x0, float maxWidth);


/**
* Returns whether this token's lexeme matches a specific character array.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,10 @@ public float getWidth(RSyntaxTextArea textArea, TabExpander e, float x0) {
return getWidthUpTo(textCount, textArea, e, x0);
}

@Override
public float getWidth(RSyntaxTextArea textArea, TabExpander e, float x0, float maxWidth) {
return getWidthUpTo(textCount, textArea, e, x0, maxWidth);
}

@Override
public float getWidthUpTo(int numChars, RSyntaxTextArea textArea,
Expand Down Expand Up @@ -603,6 +607,51 @@ public float getWidthUpTo(int numChars, RSyntaxTextArea textArea,
return width - x0;
}

@Override
public float getWidthUpTo(int numChars, RSyntaxTextArea textArea,
TabExpander e, float x0, float maxWidth) {
float width = x0;
FontMetrics fm = textArea.getFontMetricsForTokenType(getType());
if (fm != null) {
int w;
int currentStart = textOffset;
final int endBefore = textOffset + numChars;
for (int i = currentStart; i < endBefore; i++) {
if (text[i] == '\t') {
// Since TokenMaker implementations usually group all
// adjacent whitespace into a single token, there
// aren't usually any characters to compute a width
// for here, so we check before calling.
w = i - currentStart;
if (w > 0) {
width += fm.charsWidth(text, currentStart, w);
}
currentStart = i + 1;
width = e.nextTabStop(width, 0);

if (width - x0 >= maxWidth) {
return maxWidth;
}
} else if ((i - currentStart) > 50) {
w = i - currentStart + 1;
width += fm.charsWidth(text, currentStart, w);
currentStart = i + 1;

if (width - x0 >= maxWidth) {
return maxWidth;
}
}
}

// Most (non-whitespace) tokens will have characters at this
// point to get the widths for, so we don't check for w>0 (mini-
// optimization).
w = endBefore - currentStart;
width += fm.charsWidth(text, currentStart, w);
}
return width - x0;
}


@Override
public int hashCode() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public static TokenSubList getSubTokenList(Token tokenList, int pos,
TabExpander e,
final RSyntaxTextArea textArea,
float x0) {
return getSubTokenList(tokenList, pos, e, textArea, x0, null);
return getSubTokenList(tokenList, pos, e, textArea, x0, null, true, Float.MAX_VALUE);
}


Expand Down Expand Up @@ -100,6 +100,8 @@ public static TokenSubList getSubTokenList(Token tokenList, int pos,
* @param tempToken A temporary token to use when creating the token list
* result. This may be <code>null</code> but callers can pass in
* a "buffer" token for performance if desired.
* @param careAboutWidth whether we need to know the width or the result or not
* @param maxWidth the maximum width we care about measuring, if we care about width
* @return Information about the "sub" token list. This will be
* <code>null</code> if <code>pos</code> was not a valid offset
* into the token list.
Expand All @@ -109,7 +111,10 @@ public static TokenSubList getSubTokenList(Token tokenList, int pos,
TabExpander e,
final RSyntaxTextArea textArea,
float x0,
TokenImpl tempToken) {
TokenImpl tempToken,
boolean careAboutWidth,
float maxWidth) {


if (tempToken==null) {
tempToken = new TokenImpl();
Expand All @@ -119,7 +124,9 @@ public static TokenSubList getSubTokenList(Token tokenList, int pos,
// Loop through the token list until you find the one that contains
// pos. Remember the cumulative width of all of these tokens.
while (t!=null && t.isPaintable() && !t.containsPosition(pos)) {
x0 += t.getWidth(textArea, e, x0);
if (careAboutWidth) {
x0 += t.getWidth(textArea, e, x0, maxWidth);
}
t = t.getNextToken();
}

Expand All @@ -129,7 +136,9 @@ public static TokenSubList getSubTokenList(Token tokenList, int pos,
if (t.getOffset()!=pos) {
// Number of chars between p0 and token start.
int difference = pos - t.getOffset();
x0 += t.getWidthUpTo(t.length()-difference+1, textArea, e, x0);
if (careAboutWidth) {
x0 += t.getWidthUpTo(t.length()-difference+1, textArea, e, x0, maxWidth);
}
tempToken.copyFrom(t);
tempToken.makeStartAt(pos);

Expand Down
Loading