Skip to content

Commit

Permalink
refact: address sonar findings
Browse files Browse the repository at this point in the history
  • Loading branch information
sebthom committed Aug 16, 2024
1 parent 4392e1d commit 1e4c1a1
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -453,14 +453,12 @@ private static ITextSelection expandTextSelectionToSurroundingBlockComment(final
final int selectionStartLine = document.getLineOfOffset(textSelectionStart);
final int selectionEndLine = document.getLineOfOffset(textSelectionEnd);
final int[] lineRange = { -1, -1 };
Set<Integer> lines = computeLines(new TextSelection(textSelectionStart, textSelectionEnd - textSelectionStart),
document);
lines = lines.stream().filter(l -> l >= selectionStartLine && l <= selectionEndLine && !TextUtils.isBlankLine(document, l))
.map(l -> {
computeLines(new TextSelection(textSelectionStart, textSelectionEnd - textSelectionStart), document).stream()
.filter(l -> l >= selectionStartLine && l <= selectionEndLine && !TextUtils.isBlankLine(document, l))
.forEach(l -> {
lineRange[0] = lineRange[0] == -1 || lineRange[0] > l ? l : lineRange[0];
lineRange[1] = lineRange[1] < l ? l : lineRange[1];
return l;
}).collect(Collectors.toSet());
});

final Set<ITypedRegion> comments = getBlockCommentParts(document, textSelectionStart,
textSelectionEnd - textSelectionStart, open, close);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit 1e4c1a1

Please sign in to comment.