From 269bd66c7fc09a8f1dae2583ae544925ddde72be Mon Sep 17 00:00:00 2001 From: sebthom Date: Fri, 16 Aug 2024 19:10:39 +0200 Subject: [PATCH] refact: address sonar findings --- .../core/internal/rule/RegExpSourceList.java | 2 +- .../core/internal/theme/ThemeTrieElement.java | 9 ++-- .../core/model/TMTokenizationSupport.java | 14 +++--- .../eclipse/tm4e/markdown/marked/Lexer.java | 6 ++- .../ui/text/TMPresentationReconciler.java | 48 +++++++++---------- 5 files changed, 38 insertions(+), 41 deletions(-) diff --git a/org.eclipse.tm4e.core/src/main/java/org/eclipse/tm4e/core/internal/rule/RegExpSourceList.java b/org.eclipse.tm4e.core/src/main/java/org/eclipse/tm4e/core/internal/rule/RegExpSourceList.java index 16ea2e5d4..2e783fee0 100644 --- a/org.eclipse.tm4e.core/src/main/java/org/eclipse/tm4e/core/internal/rule/RegExpSourceList.java +++ b/org.eclipse.tm4e.core/src/main/java/org/eclipse/tm4e/core/internal/rule/RegExpSourceList.java @@ -33,7 +33,7 @@ final class RegExpSourceList { private boolean hasAnchors; private @Nullable CompiledRule cached; - private @Nullable final CompiledRule[][] anchorCache = new CompiledRule[2][2]; + private final @Nullable CompiledRule[][] anchorCache = new CompiledRule[2][2]; private void disposeCache() { cached = null; diff --git a/org.eclipse.tm4e.core/src/main/java/org/eclipse/tm4e/core/internal/theme/ThemeTrieElement.java b/org.eclipse.tm4e.core/src/main/java/org/eclipse/tm4e/core/internal/theme/ThemeTrieElement.java index 235371157..d03dbd9bf 100644 --- a/org.eclipse.tm4e.core/src/main/java/org/eclipse/tm4e/core/internal/theme/ThemeTrieElement.java +++ b/org.eclipse.tm4e.core/src/main/java/org/eclipse/tm4e/core/internal/theme/ThemeTrieElement.java @@ -11,7 +11,7 @@ */ package org.eclipse.tm4e.core.internal.theme; -import static org.eclipse.tm4e.core.internal.utils.StringUtils.strArrCmp; +import static org.eclipse.tm4e.core.internal.utils.StringUtils.*; import java.util.ArrayList; import java.util.HashMap; @@ -150,11 +150,8 @@ public void insert(final int scopeDepth, final String scope, @Nullable final Lis tail = scope.substring(dotIndex + 1); } - ThemeTrieElement child = this._children.get(head); - if (child == null) { - child = new ThemeTrieElement(this._mainRule.clone(), ThemeTrieElementRule.cloneArr(this._rulesWithParentScopes)); - this._children.put(head, child); - } + final ThemeTrieElement child = this._children.computeIfAbsent(head, + key -> new ThemeTrieElement(this._mainRule.clone(), ThemeTrieElementRule.cloneArr(this._rulesWithParentScopes))); child.insert(scopeDepth + 1, tail, parentScopes, fontStyle, foreground, background); } diff --git a/org.eclipse.tm4e.core/src/main/java/org/eclipse/tm4e/core/model/TMTokenizationSupport.java b/org.eclipse.tm4e.core/src/main/java/org/eclipse/tm4e/core/model/TMTokenizationSupport.java index c417842f1..a34648c0d 100644 --- a/org.eclipse.tm4e.core/src/main/java/org/eclipse/tm4e/core/model/TMTokenizationSupport.java +++ b/org.eclipse.tm4e.core/src/main/java/org/eclipse/tm4e/core/model/TMTokenizationSupport.java @@ -16,7 +16,7 @@ */ package org.eclipse.tm4e.core.model; -import static org.eclipse.tm4e.core.internal.utils.NullSafetyHelper.castNonNull; +import static org.eclipse.tm4e.core.internal.utils.NullSafetyHelper.*; import java.time.Duration; import java.util.ArrayList; @@ -198,13 +198,11 @@ private Integer[] getTokenIds(final String scope) { tokens = new @NonNull Integer[tmpTokens.length]; for (int i = 0; i < tmpTokens.length; i++) { final String token = tmpTokens[i]; - Integer tokenId = this.tokenToTokenId.get(token); - if (tokenId == null) { - tokenId = ++this.lastAssignedTokenId; - this.tokenToTokenId.put(token, tokenId); - this.tokenIdToToken.add(token); - } - tokens[i] = tokenId; + tokens[i] = this.tokenToTokenId.computeIfAbsent(token, _token -> { + final var tokenId = ++this.lastAssignedTokenId; + this.tokenIdToToken.add(_token); + return tokenId; + }); } this.scopeToTokenIds.put(scope, tokens); diff --git a/org.eclipse.tm4e.markdown/src/main/java/org/eclipse/tm4e/markdown/marked/Lexer.java b/org.eclipse.tm4e.markdown/src/main/java/org/eclipse/tm4e/markdown/marked/Lexer.java index b01bde8dc..1209d37db 100644 --- a/org.eclipse.tm4e.markdown/src/main/java/org/eclipse/tm4e/markdown/marked/Lexer.java +++ b/org.eclipse.tm4e.markdown/src/main/java/org/eclipse/tm4e/markdown/marked/Lexer.java @@ -48,8 +48,10 @@ public static Tokens lex(final String src, @Nullable final Options options) { } private Tokens lex(String src) { - src = src.replaceAll("\r\n|\r", "\n").replaceAll("\t", " ").replaceAll("\u00a0", " ").replaceAll("\u2424", - "\n"); + src = src.replaceAll("\r\n|\r", "\n") // + .replace("\t", " ") // + .replace("\u00a0", " ") // + .replace("\u2424", "\n"); return this.token(src, true); } diff --git a/org.eclipse.tm4e.ui/src/main/java/org/eclipse/tm4e/ui/text/TMPresentationReconciler.java b/org.eclipse.tm4e.ui/src/main/java/org/eclipse/tm4e/ui/text/TMPresentationReconciler.java index c27484fb0..ee22b227c 100644 --- a/org.eclipse.tm4e.ui/src/main/java/org/eclipse/tm4e/ui/text/TMPresentationReconciler.java +++ b/org.eclipse.tm4e.ui/src/main/java/org/eclipse/tm4e/ui/text/TMPresentationReconciler.java @@ -304,34 +304,34 @@ IRegion getRegionOfTextEvent(final TextEvent event) { ? viewerExt5.widgetRange2ModelRange(new Region(event.getOffset(), length)) : new Region(event.getOffset() + viewer.getVisibleRegion().getOffset(), length); } - } - /** - * Finds a grammar for the given document. - */ - private @Nullable IGrammar findGrammar(final IDocument doc) { - final IGrammar currentGrammar = isForcedGrammar ? this.grammar : null; - if (currentGrammar != null) - return currentGrammar; - - final ContentTypeInfo info = ContentTypeHelper.findContentTypes(doc); - if (info == null) - return null; - - final IContentType[] contentTypes = info.getContentTypes(); - // try to determine the grammar based on the content types - IGrammar grammar = TMEclipseRegistryPlugin.getGrammarRegistryManager().getGrammarFor(contentTypes); - if (grammar == null) { - // try to determine the grammar based on the file type - final String fileName = info.getFileName(); - if (fileName.indexOf('.') > -1) { - final String fileExtension = new Path(fileName).getFileExtension(); - if (fileExtension != null) { - grammar = TMEclipseRegistryPlugin.getGrammarRegistryManager().getGrammarForFileExtension(fileExtension); + /** + * Finds a grammar for the given document. + */ + private @Nullable IGrammar findGrammar(final IDocument doc) { + final IGrammar currentGrammar = isForcedGrammar ? TMPresentationReconciler.this.grammar : null; + if (currentGrammar != null) + return currentGrammar; + + final ContentTypeInfo info = ContentTypeHelper.findContentTypes(doc); + if (info == null) + return null; + + final IContentType[] contentTypes = info.getContentTypes(); + // try to determine the grammar based on the content types + IGrammar grammar = TMEclipseRegistryPlugin.getGrammarRegistryManager().getGrammarFor(contentTypes); + if (grammar == null) { + // try to determine the grammar based on the file type + final String fileName = info.getFileName(); + if (fileName.indexOf('.') > -1) { + final String fileExtension = new Path(fileName).getFileExtension(); + if (fileExtension != null) { + grammar = TMEclipseRegistryPlugin.getGrammarRegistryManager().getGrammarForFileExtension(fileExtension); + } } } + return grammar; } - return grammar; } public @Nullable IGrammar getGrammar() {