Skip to content

Commit 8f60454

Browse files
committed
refact: cleanup TMModel
1 parent 6935c1b commit 8f60454

1 file changed

Lines changed: 26 additions & 21 deletions

File tree

  • org.eclipse.tm4e.core/src/main/java/org/eclipse/tm4e/core/model

org.eclipse.tm4e.core/src/main/java/org/eclipse/tm4e/core/model/TMModel.java

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,9 @@ public void run() {
164164
// wait up to 50ms for the next edit, so that edits made in fast succession (e.g. by a formatter) are applied
165165
// in one go before the token revalidation loop happens
166166
final var edit = edits.poll(50, TimeUnit.MILLISECONDS);
167-
if (edit == null)
167+
if (edit == null) {
168168
break;
169+
}
169170
applyEdit(edit);
170171
}
171172
}
@@ -195,8 +196,9 @@ private void setAllTokensAreValid() {
195196
private void revalidateTokens() {
196197
final int startLineIndex = firstLineToRevalidate;
197198
final int startLineNumber = startLineIndex + 1;
198-
if (DEBUG_LOGGING)
199+
if (DEBUG_LOGGING) {
199200
logDebug("(%d)", startLineNumber);
201+
}
200202

201203
long startTime = System.currentTimeMillis();
202204
var changedRanges = new ArrayList<Range>();
@@ -209,13 +211,8 @@ private void revalidateTokens() {
209211
// iterate over all lines from startLineIndex to end of file to check if (re)tokenization is required
210212
for (currLineIndex = startLineIndex; currLineIndex < linesCount; currLineIndex++) {
211213

212-
// check if TokenizerThread is still running
213-
if (isInterrupted()) {
214-
break;
215-
}
216-
217-
// check if new edits are queued -> if so, abort current tokenization loop
218-
if (!edits.isEmpty()) {
214+
// check if TokenizerThread is still running and no new edits are queued
215+
if (isInterrupted() || !edits.isEmpty()) {
219216
break;
220217
}
221218

@@ -231,19 +228,22 @@ private void revalidateTokens() {
231228
if (prevLineTokens != null) {
232229
if (currLineTokens.tokens != null && currLineTokens.startState.equals(prevLineTokens.endState)) {
233230
// has matching start and has tokens ==> is up to date
234-
if (DEBUG_LOGGING)
231+
if (DEBUG_LOGGING) {
235232
logDebug("(%d) >> DONE - tokens of line %d are up-to-date", startLineNumber, currLineNumber);
233+
}
236234
firstLineToRevalidate = currLineIndex + 1;
237235
prevLineTokens = currLineTokens;
238236
continue;
239237
}
240-
if (prevLineTokens.endState != null)
238+
if (prevLineTokens.endState != null) {
241239
currLineTokens.startState = prevLineTokens.endState;
240+
}
242241
}
243242

244243
// (re)tokenize the line
245-
if (DEBUG_LOGGING)
244+
if (DEBUG_LOGGING) {
246245
logDebug("(%d) >> tokenizing line %d...", startLineNumber, currLineNumber);
246+
}
247247
TokenizationResult r;
248248
try {
249249
final String lineText = getLineText(currLineIndex);
@@ -276,8 +276,9 @@ private void revalidateTokens() {
276276

277277
// if MAX_TIME_PER_MULTI_LINE_VALIDATIONS reached, notify listeners about line changes
278278
if (System.currentTimeMillis() - startTime >= MAX_TIME_PER_MULTI_LINE_VALIDATIONS) {
279-
if (DEBUG_LOGGING)
279+
if (DEBUG_LOGGING) {
280280
logDebug("(%d) >> changedRanges: %s", startLineNumber, changedRanges);
281+
}
281282
listeners.dispatchEvent(changedRanges, TMModel.this);
282283
changedRanges = new ArrayList<>();
283284
prevRange = null;
@@ -286,20 +287,23 @@ private void revalidateTokens() {
286287
}
287288

288289
// notify listeners about remaining line changes
289-
if (DEBUG_LOGGING)
290+
if (DEBUG_LOGGING) {
290291
logDebug("(%d) >> changedRanges: %s", startLineNumber, changedRanges);
292+
}
291293
listeners.dispatchEvent(changedRanges, TMModel.this);
292294

293295
setAllTokensAreValid();
294296
}
295297

296298
private void applyEdit(final Edit edit) {
297-
if (DEBUG_LOGGING)
299+
if (DEBUG_LOGGING) {
298300
logDebug("(%s)", edit);
301+
}
299302

300303
final var lineIndex = edit.lineIndex;
301-
if (isAllTokensAreValid() || lineIndex < firstLineToRevalidate)
304+
if (isAllTokensAreValid() || lineIndex < firstLineToRevalidate) {
302305
firstLineToRevalidate = lineIndex;
306+
}
303307

304308
// check if single line update
305309
if (edit.replacedCount == 1 && edit.replacementCount == 1) {
@@ -397,8 +401,9 @@ public void onLinesReplaced(final int lineIndex, final int replacedLinesCount, f
397401
if (replacedLinesCount == 0 && replacementLinesCount == 0)
398402
return;
399403

400-
if (DEBUG_LOGGING)
404+
if (DEBUG_LOGGING) {
401405
logDebug("(%d, -%d, +%d)", lineIndex + 1, replacedLinesCount, replacementLinesCount);
406+
}
402407

403408
edits.add(new Edit(lineIndex, replacedLinesCount, replacementLinesCount));
404409
}
@@ -430,22 +435,22 @@ public void dispose() {
430435

431436
private synchronized void startTokenizerThread() {
432437
if (grammar != null && listeners.isNotEmpty()) {
433-
var thread = this.tokenizerThread;
438+
var thread = tokenizerThread;
434439
if (thread == null || !thread.isAlive() || thread.isInterrupted()) {
435-
thread = this.tokenizerThread = new TokenizerThread();
440+
thread = tokenizerThread = new TokenizerThread();
436441
thread.start();
437442
}
438443
}
439444
}
440445

441446
/** Interrupt the thread if running. */
442447
private synchronized void stopTokenizerThread() {
443-
final var thread = this.tokenizerThread;
448+
final var thread = tokenizerThread;
444449
if (thread == null)
445450
return;
446451

447452
thread.interrupt();
448-
this.tokenizerThread = null;
453+
tokenizerThread = null;
449454
}
450455

451456
@Override

0 commit comments

Comments
 (0)