Skip to content

Commit 1e4c1a1

Browse files
committed
refact: address sonar findings
1 parent 4392e1d commit 1e4c1a1

6 files changed

Lines changed: 42 additions & 47 deletions

File tree

org.eclipse.tm4e.core/src/main/java/org/eclipse/tm4e/core/internal/rule/RegExpSourceList.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ final class RegExpSourceList {
3333
private boolean hasAnchors;
3434

3535
private @Nullable CompiledRule cached;
36-
private @Nullable final CompiledRule[][] anchorCache = new CompiledRule[2][2];
36+
private final @Nullable CompiledRule[][] anchorCache = new CompiledRule[2][2];
3737

3838
private void disposeCache() {
3939
cached = null;

org.eclipse.tm4e.core/src/main/java/org/eclipse/tm4e/core/internal/theme/ThemeTrieElement.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
*/
1212
package org.eclipse.tm4e.core.internal.theme;
1313

14-
import static org.eclipse.tm4e.core.internal.utils.StringUtils.strArrCmp;
14+
import static org.eclipse.tm4e.core.internal.utils.StringUtils.*;
1515

1616
import java.util.ArrayList;
1717
import java.util.HashMap;
@@ -150,11 +150,8 @@ public void insert(final int scopeDepth, final String scope, @Nullable final Lis
150150
tail = scope.substring(dotIndex + 1);
151151
}
152152

153-
ThemeTrieElement child = this._children.get(head);
154-
if (child == null) {
155-
child = new ThemeTrieElement(this._mainRule.clone(), ThemeTrieElementRule.cloneArr(this._rulesWithParentScopes));
156-
this._children.put(head, child);
157-
}
153+
final ThemeTrieElement child = this._children.computeIfAbsent(head,
154+
key -> new ThemeTrieElement(this._mainRule.clone(), ThemeTrieElementRule.cloneArr(this._rulesWithParentScopes)));
158155

159156
child.insert(scopeDepth + 1, tail, parentScopes, fontStyle, foreground, background);
160157
}

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

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
*/
1717
package org.eclipse.tm4e.core.model;
1818

19-
import static org.eclipse.tm4e.core.internal.utils.NullSafetyHelper.castNonNull;
19+
import static org.eclipse.tm4e.core.internal.utils.NullSafetyHelper.*;
2020

2121
import java.time.Duration;
2222
import java.util.ArrayList;
@@ -198,13 +198,11 @@ private Integer[] getTokenIds(final String scope) {
198198
tokens = new @NonNull Integer[tmpTokens.length];
199199
for (int i = 0; i < tmpTokens.length; i++) {
200200
final String token = tmpTokens[i];
201-
Integer tokenId = this.tokenToTokenId.get(token);
202-
if (tokenId == null) {
203-
tokenId = ++this.lastAssignedTokenId;
204-
this.tokenToTokenId.put(token, tokenId);
205-
this.tokenIdToToken.add(token);
206-
}
207-
tokens[i] = tokenId;
201+
tokens[i] = this.tokenToTokenId.computeIfAbsent(token, _token -> {
202+
final var tokenId = ++this.lastAssignedTokenId;
203+
this.tokenIdToToken.add(_token);
204+
return tokenId;
205+
});
208206
}
209207

210208
this.scopeToTokenIds.put(scope, tokens);

org.eclipse.tm4e.languageconfiguration/src/main/java/org/eclipse/tm4e/languageconfiguration/internal/ToggleLineCommentHandler.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -453,14 +453,12 @@ private static ITextSelection expandTextSelectionToSurroundingBlockComment(final
453453
final int selectionStartLine = document.getLineOfOffset(textSelectionStart);
454454
final int selectionEndLine = document.getLineOfOffset(textSelectionEnd);
455455
final int[] lineRange = { -1, -1 };
456-
Set<Integer> lines = computeLines(new TextSelection(textSelectionStart, textSelectionEnd - textSelectionStart),
457-
document);
458-
lines = lines.stream().filter(l -> l >= selectionStartLine && l <= selectionEndLine && !TextUtils.isBlankLine(document, l))
459-
.map(l -> {
456+
computeLines(new TextSelection(textSelectionStart, textSelectionEnd - textSelectionStart), document).stream()
457+
.filter(l -> l >= selectionStartLine && l <= selectionEndLine && !TextUtils.isBlankLine(document, l))
458+
.forEach(l -> {
460459
lineRange[0] = lineRange[0] == -1 || lineRange[0] > l ? l : lineRange[0];
461460
lineRange[1] = lineRange[1] < l ? l : lineRange[1];
462-
return l;
463-
}).collect(Collectors.toSet());
461+
});
464462

465463
final Set<ITypedRegion> comments = getBlockCommentParts(document, textSelectionStart,
466464
textSelectionEnd - textSelectionStart, open, close);

org.eclipse.tm4e.markdown/src/main/java/org/eclipse/tm4e/markdown/marked/Lexer.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,10 @@ public static Tokens lex(final String src, @Nullable final Options options) {
4848
}
4949

5050
private Tokens lex(String src) {
51-
src = src.replaceAll("\r\n|\r", "\n").replaceAll("\t", " ").replaceAll("\u00a0", " ").replaceAll("\u2424",
52-
"\n");
51+
src = src.replaceAll("\r\n|\r", "\n") //
52+
.replace("\t", " ") //
53+
.replace("\u00a0", " ") //
54+
.replace("\u2424", "\n");
5355
return this.token(src, true);
5456
}
5557

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

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -304,34 +304,34 @@ IRegion getRegionOfTextEvent(final TextEvent event) {
304304
? viewerExt5.widgetRange2ModelRange(new Region(event.getOffset(), length))
305305
: new Region(event.getOffset() + viewer.getVisibleRegion().getOffset(), length);
306306
}
307-
}
308307

309-
/**
310-
* Finds a grammar for the given document.
311-
*/
312-
private @Nullable IGrammar findGrammar(final IDocument doc) {
313-
final IGrammar currentGrammar = isForcedGrammar ? this.grammar : null;
314-
if (currentGrammar != null)
315-
return currentGrammar;
316-
317-
final ContentTypeInfo info = ContentTypeHelper.findContentTypes(doc);
318-
if (info == null)
319-
return null;
320-
321-
final IContentType[] contentTypes = info.getContentTypes();
322-
// try to determine the grammar based on the content types
323-
IGrammar grammar = TMEclipseRegistryPlugin.getGrammarRegistryManager().getGrammarFor(contentTypes);
324-
if (grammar == null) {
325-
// try to determine the grammar based on the file type
326-
final String fileName = info.getFileName();
327-
if (fileName.indexOf('.') > -1) {
328-
final String fileExtension = new Path(fileName).getFileExtension();
329-
if (fileExtension != null) {
330-
grammar = TMEclipseRegistryPlugin.getGrammarRegistryManager().getGrammarForFileExtension(fileExtension);
308+
/**
309+
* Finds a grammar for the given document.
310+
*/
311+
private @Nullable IGrammar findGrammar(final IDocument doc) {
312+
final IGrammar currentGrammar = isForcedGrammar ? TMPresentationReconciler.this.grammar : null;
313+
if (currentGrammar != null)
314+
return currentGrammar;
315+
316+
final ContentTypeInfo info = ContentTypeHelper.findContentTypes(doc);
317+
if (info == null)
318+
return null;
319+
320+
final IContentType[] contentTypes = info.getContentTypes();
321+
// try to determine the grammar based on the content types
322+
IGrammar grammar = TMEclipseRegistryPlugin.getGrammarRegistryManager().getGrammarFor(contentTypes);
323+
if (grammar == null) {
324+
// try to determine the grammar based on the file type
325+
final String fileName = info.getFileName();
326+
if (fileName.indexOf('.') > -1) {
327+
final String fileExtension = new Path(fileName).getFileExtension();
328+
if (fileExtension != null) {
329+
grammar = TMEclipseRegistryPlugin.getGrammarRegistryManager().getGrammarForFileExtension(fileExtension);
330+
}
331331
}
332332
}
333+
return grammar;
333334
}
334-
return grammar;
335335
}
336336

337337
public @Nullable IGrammar getGrammar() {

0 commit comments

Comments
 (0)