Skip to content

Commit b6d9b08

Browse files
committed
perf: avoid calling doc.getLineLength(lineIndex) for each token
1 parent ba5633f commit b6d9b08

1 file changed

Lines changed: 18 additions & 14 deletions

File tree

  • org.eclipse.tm4e.ui/src/main/java/org/eclipse/tm4e/ui/text

org.eclipse.tm4e.ui/src/main/java/org/eclipse/tm4e/ui/text/Colorizer.java

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,9 @@ void colorize(final ModelTokensChangedEvent event) {
7777
colorize(region, docModel);
7878
} catch (final BadLocationException ex) {
7979
// This is an expected state, only log when tracing is enabled.
80-
if (TMUIPlugin.isLogTraceEnabled())
80+
if (TMUIPlugin.isLogTraceEnabled()) {
8181
TMUIPlugin.logError(ex);
82+
}
8283
}
8384
}
8485
}
@@ -92,9 +93,10 @@ void colorize(final IRegion damageRegion, final ITMDocumentModel tmModel) throws
9293
final int toLineIndex = doc.getLineOfOffset(damageRegion.getOffset() + damageRegion.getLength());
9394

9495
// Refresh the UI Presentation
95-
if (TMUIPlugin.isLogTraceEnabled())
96+
if (TMUIPlugin.isLogTraceEnabled()) {
9697
TMUIPlugin.logTrace("Colorize lines from " + (fromLineIndex + 1) + " to " + (toLineIndex + 1));
97-
final var presentation = new TextPresentation(damageRegion, 1000);
98+
}
99+
final var presentation = new TextPresentation(damageRegion, 1_000);
98100
Exception error = null;
99101

100102
final var theme = this.theme;
@@ -110,11 +112,13 @@ void colorize(final IRegion damageRegion, final ITMDocumentModel tmModel) throws
110112
for (int lineIndex = fromLineIndex; lineIndex <= toLineIndex; lineIndex++) {
111113
tokens = tmModel.getLineTokens(lineIndex);
112114
if (tokens == null) {
113-
if (TMUIPlugin.isLogTraceEnabled())
115+
if (TMUIPlugin.isLogTraceEnabled()) {
114116
TMUIPlugin.logTrace("TextMate tokens not yet available for line " + lineIndex);
117+
}
115118
continue;
116119
}
117120
final int startLineOffset = doc.getLineOffset(lineIndex);
121+
final int lineLength = doc.getLineLength(lineIndex);
118122
int nextTokenIndex = 0;
119123
for (final TMToken currentToken : tokens) {
120124
nextTokenIndex++;
@@ -134,7 +138,7 @@ void colorize(final IRegion damageRegion, final ITMDocumentModel tmModel) throws
134138
tokenStartIndex = damageRegion.getOffset() - startLineOffset;
135139
final IToken token = theme == null ? ITokenProvider.DEFAULT_TOKEN : theme.getToken(currentToken.type);
136140
lastAttribute = getTokenTextAttribute(token);
137-
length += getTokenLength(tokenStartIndex, nextToken, lineIndex, doc);
141+
length += getTokenLength(tokenStartIndex, nextToken, lineLength);
138142
firstToken = false;
139143
// ignore it
140144
continue;
@@ -150,7 +154,7 @@ else if (isTokenAfterRegion(currentToken, startLineOffset, damageRegion)) {
150154
final IToken token = theme == null ? ITokenProvider.DEFAULT_TOKEN : theme.getToken(currentToken.type);
151155
final TextAttribute attribute = getTokenTextAttribute(token);
152156
if (lastAttribute.equals(attribute)) {
153-
length += getTokenLength(tokenStartIndex, nextToken, lineIndex, doc);
157+
length += getTokenLength(tokenStartIndex, nextToken, lineLength);
154158
firstToken = false;
155159
} else {
156160
if (!firstToken) {
@@ -160,7 +164,7 @@ else if (isTokenAfterRegion(currentToken, startLineOffset, damageRegion)) {
160164
lastToken = token;
161165
lastAttribute = attribute;
162166
lastStart = tokenStartIndex + startLineOffset;
163-
length = getTokenLength(tokenStartIndex, nextToken, lineIndex, doc);
167+
length = getTokenLength(tokenStartIndex, nextToken, lineLength);
164168
}
165169
}
166170
}
@@ -173,8 +177,9 @@ else if (isTokenAfterRegion(currentToken, startLineOffset, damageRegion)) {
173177
// These exceptions can be thrown if there is a delay running the tokenizer thread
174178
// and the tokens become out of sync with the document line data.
175179
// As this is an expected state, only log them if tracing is enabled.
176-
if (TMUIPlugin.isLogTraceEnabled())
180+
if (TMUIPlugin.isLogTraceEnabled()) {
177181
TMUIPlugin.logError(ex);
182+
}
178183
} catch (final Exception ex) {
179184
error = ex;
180185
TMUIPlugin.logError(ex);
@@ -210,16 +215,16 @@ private void addStyleRange(final TextPresentation presentation, final int offset
210215
* Initialize foreground, background color, current line highlight from the current theme.
211216
*/
212217
private void applyThemeToViewer() {
213-
this.isViewerStyleColorsInitialized = false;
214-
this.isViewerHighlightColorInitialized = false;
218+
isViewerStyleColorsInitialized = false;
219+
isViewerHighlightColorInitialized = false;
215220
applyThemeToViewerIfNeeded();
216221
}
217222

218223
/**
219224
* Initialize foreground, background color, current line highlight from the current theme if needed.
220225
*/
221226
private void applyThemeToViewerIfNeeded() {
222-
if (!isViewerStyleColorsInitialized && this.theme instanceof final ITheme theTheme) {
227+
if (!isViewerStyleColorsInitialized && theme instanceof final ITheme theTheme) {
223228
final StyledText styledText = viewer.getTextWidget();
224229
theTheme.initializeViewerColors(styledText);
225230
isViewerStyleColorsInitialized = true;
@@ -269,10 +274,9 @@ private boolean isTokenBeforeRegion(final TMToken token, final int startLineOffs
269274
return token.startIndex + startLineOffset < damage.getOffset();
270275
}
271276

272-
private int getTokenLength(final int tokenStartIndex, final @Nullable TMToken nextToken, final int line, final IDocument doc)
273-
throws BadLocationException {
277+
private int getTokenLength(final int tokenStartIndex, final @Nullable TMToken nextToken, final int lineLength) {
274278
return nextToken == null
275-
? doc.getLineLength(line) - tokenStartIndex
279+
? lineLength - tokenStartIndex
276280
: nextToken.startIndex - tokenStartIndex;
277281
}
278282

0 commit comments

Comments
 (0)