Skip to content

Commit

Permalink
refact: fixing a few sonarlint findings
Browse files Browse the repository at this point in the history
  • Loading branch information
sebthom committed Jan 11, 2024
1 parent 854d55a commit 50518ae
Show file tree
Hide file tree
Showing 16 changed files with 49 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ public List<String> getScopes() {
@Override
public boolean equals(final Object obj) {
return obj instanceof final RawToken other //
? Objects.equals(this.value, other.value) && Objects.equals(this.scopes, other.scopes)
: false;
&& Objects.equals(this.value, other.value) //
&& Objects.equals(this.scopes, other.scopes);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public String toString() {
private final ModelTokensChangedEvent.Listeners listeners = new ModelTokensChangedEvent.Listeners();

/** The background thread performing async line tokenizations */
private @Nullable volatile TokenizerThread tokenizerThread;
private volatile @Nullable TokenizerThread tokenizerThread;
private volatile boolean tokenizerThreadHasWork;
private TMTokenizationSupport tokenizer = lazyNonNull();

Expand Down Expand Up @@ -188,10 +188,8 @@ private void setAllTokensAreValid() {

/**
* revalidates tokens of lines starting at {@link #firstLineToRevalidate} until all lines are processed or new {@link Edit} arrive.
*
* @throws InterruptedException
*/
private void revalidateTokens() throws InterruptedException {
private void revalidateTokens() {
final int startLineIndex = firstLineToRevalidate;
final int startLineNumber = startLineIndex + 1;
if (DEBUG_LOGGING)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class TMParserTest {
@SuppressWarnings("null")
private void validateCaptures(final RawGrammar grammar) {
assertNotNull(grammar.getPatterns());
assertEquals(grammar.getPatterns().size(), 1);
assertEquals(1, grammar.getPatterns().size());
final var pattern = grammar.getPatterns().iterator().next();
assertEquals("THE_PATTERN", pattern.getName());
assertEquals("BEGIN_PATTERN", pattern.getBegin());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ private static String normalizeIndentationFromWhitespace(final String str, final

final var result = new StringBuilder();
if (!insertSpaces) {
final long tabsCnt = Math.round(Math.floor(spacesCnt / tabSize));
final long tabsCnt = spacesCnt / tabSize;
spacesCnt = spacesCnt % tabSize;
for (int i = 0; i < tabsCnt; i++) {
result.append('\t');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@
* @see <a href="https://github.com/microsoft/vscode/blob/main/src/vs/editor/test/common/modes/supports/onEnter.test.ts">
* https://github.com/microsoft/vscode/blob/main/src/vs/editor/test/common/modes/supports/onEnter.test.ts</a>
*/
public class OnEnterSupportTest {
class OnEnterSupportTest {

@Test
public void testUseBrackets() {
void testUseBrackets() {

class Support extends OnEnterSupport {
Support() {
Expand Down Expand Up @@ -74,7 +74,7 @@ void testIndentAction(final String beforeText, final String afterText, final Ind
}

@Test
public void testRegExpRules() {
void testRegExpRules() {

class Support extends OnEnterSupport {
Support() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import org.eclipse.tm4e.languageconfiguration.internal.model.LanguageConfiguration;
import org.junit.jupiter.api.Test;

public class ParsingTest {
class ParsingTest {

@Nullable
private LanguageConfiguration loadLanguageConfiguration(final String path) throws IOException {
Expand All @@ -37,13 +37,13 @@ private LanguageConfiguration loadLanguageConfiguration(final String path) throw
}

@Test
public void testCanLoadPhpLanguageConfig() throws Exception {
void testCanLoadPhpLanguageConfig() throws Exception {
final var languageConfiguration = loadLanguageConfiguration("/php-language-configuration.json");
assertNotNull(languageConfiguration);
final var comments = languageConfiguration.getComments();
assertNotNull(comments);
assertNotNull(comments.blockComment);
assertEquals(comments.lineComment, "//");
assertEquals("//", comments.lineComment);
assertNotNull(languageConfiguration.getBrackets());
assertNotNull(languageConfiguration.getAutoClosingPairs());
assertEquals(";:.,=}])>` \n\t", languageConfiguration.getAutoCloseBefore());
Expand All @@ -54,13 +54,13 @@ public void testCanLoadPhpLanguageConfig() throws Exception {
}

@Test
public void testCanLoadRustLanguageConfig() throws Exception {
void testCanLoadRustLanguageConfig() throws Exception {
final var languageConfiguration = loadLanguageConfiguration("/rust-language-configuration.json");
assertNotNull(languageConfiguration);
final var comments = languageConfiguration.getComments();
assertNotNull(comments);
assertNotNull(comments.blockComment);
assertEquals(comments.lineComment, "//");
assertEquals("//", comments.lineComment);
assertNotNull(languageConfiguration.getBrackets());
assertNotNull(languageConfiguration.getAutoClosingPairs());
assertNull(languageConfiguration.getAutoCloseBefore());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ public class BlockRules {
+ "|var|samp|kbd|sub|sup|i|b|u|mark|ruby|rt|rp|bdi|bdo"
+ "|span|br|wbr|ins|del|img)\\b)\\w+(?!:\\/|[^\\w\\s@]*@)\\b";

public static BlockRules normal = normal();
public static BlockRules gfm = gfm();
public static BlockRules tables = tables();
public static final BlockRules normal = normal();
public static final BlockRules gfm = gfm();
public static final BlockRules tables = tables();

public final RegExp newline;
public final RegExp code;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,7 @@ private static InlineRules inline() {
}

private static InlineRules normal() {
final InlineRules normal = inline();
return normal;
return inline();
}

private static InlineRules pedantic() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ class RegistryTest {
@Test
void testGrammarRegistered() {
final IContentType contentType = Platform.getContentTypeManager().getContentType("org.eclipse.tm4e.ui.tests.testContentType");
final IGrammar grammar = TMEclipseRegistryPlugin.getGrammarRegistryManager().getGrammarFor(new IContentType[] { contentType });
final IGrammar grammar = TMEclipseRegistryPlugin.getGrammarRegistryManager().getGrammarFor(contentType);
Assertions.assertNotNull(grammar);
}

@Test
void testThemeAppliesToSubtypes() {
final IContentType contentType = Platform.getContentTypeManager().getContentType("org.eclipse.tm4e.ui.tests.testContentType.child");
final IGrammar grammar = TMEclipseRegistryPlugin.getGrammarRegistryManager().getGrammarFor(new IContentType[] { contentType });
final IGrammar grammar = TMEclipseRegistryPlugin.getGrammarRegistryManager().getGrammarFor(contentType);
Assertions.assertNotNull(grammar);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ void tearDown() throws InterruptedException {
}

@Test
void colorizeTypescript() throws Exception {
void colorizeTypescript() {
editor = new TMEditor(grammar, theme, "let a = '';\nlet b = 10;\nlet c = true;");

final List<ICommand> commands = editor.execute();
Expand Down Expand Up @@ -89,7 +89,7 @@ void colorizeTypescript() throws Exception {
}

@Test
void colorizeTypescriptWithInvalidate1() throws Exception {
void colorizeTypescriptWithInvalidate1() {
editor = new TMEditor(grammar, theme, "let a = '';\nlet b = 10;\nlet c = true;");
editor.invalidateTextPresentation(0, 3);

Expand Down Expand Up @@ -135,7 +135,7 @@ void colorizeTypescriptWithInvalidate1() throws Exception {
}

@Test
void colorizeTypescriptWithInvalidate2() throws Exception {
void colorizeTypescriptWithInvalidate2() {
editor = new TMEditor(grammar, theme, "let a = '';\nlet b = 10;\nlet c = true;");
editor.invalidateTextPresentation(0, 2);

Expand Down Expand Up @@ -181,7 +181,7 @@ void colorizeTypescriptWithInvalidate2() throws Exception {
}

@Test
void colorizeTypescriptWithInvalidate3() throws Exception {
void colorizeTypescriptWithInvalidate3() {
editor = new TMEditor(grammar, theme, "let a = '';\nlet b = 10;\nlet c = true;");
editor.invalidateTextPresentation(1, 2);

Expand Down Expand Up @@ -227,7 +227,7 @@ void colorizeTypescriptWithInvalidate3() throws Exception {
}

@Test
void colorizeTypescriptWithInvalidate4() throws Exception {
void colorizeTypescriptWithInvalidate4() {
editor = new TMEditor(grammar, theme, "let a = '';\nlet b = 10;\nlet c = true;");
editor.invalidateTextPresentation(1, 1);

Expand Down Expand Up @@ -274,7 +274,7 @@ void colorizeTypescriptWithInvalidate4() throws Exception {

@Disabled
@Test
void colorizeTypescriptWithInvalidate8() throws Exception {
void colorizeTypescriptWithInvalidate8() {
editor = new TMEditor(grammar, theme, "let a = '';\nlet b = 10;\nlet c = true;");
editor.invalidateTextPresentation(1, 8);

Expand Down Expand Up @@ -320,7 +320,7 @@ void colorizeTypescriptWithInvalidate8() throws Exception {
}

@Test
void colorizeTypescriptWithInvalidateAndSeveralLines() throws Exception {
void colorizeTypescriptWithInvalidateAndSeveralLines() {
editor = new TMEditor(grammar, theme, "a\r\n\r\nb");
editor.invalidateTextPresentation(0, 6);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ void executeCommandSync(final Command command) {
wait(command);
} catch (final InterruptedException e) {
e.printStackTrace();
Thread.currentThread().interrupt();
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public List<ICommand> execute() {
final var commands = this.commands;
final var done = new AtomicBoolean(false);
new Thread(() -> {
commands.forEach(c -> collector.executeCommandSync(c));
commands.forEach(collector::executeCommandSync);
done.set(true);
}, "Commands Executor").start();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.tests.harness.util.DisplayHelper;

public class TestUtils {
public final class TestUtils {

public static IEditorDescriptor assertHasGenericEditor() {
final var genericEditorDescr = PlatformUI.getWorkbench().getEditorRegistry()
Expand All @@ -53,7 +53,7 @@ public static void assertNoTM4EThreadsRunning() throws InterruptedException {
final var tm4eThread = tm4eThreads.entrySet().iterator().next();
final var ex = new IllegalStateException("Thread " + tm4eThread.getKey() + " is still busy");
ex.setStackTrace(tm4eThread.getValue());
ex.printStackTrace(System.out);
ex.printStackTrace();

fail("TM4E threads still running:\n" + tm4eThreads.keySet().stream()
.map(t -> " - " + t + " " + t.getClass().getName()).collect(Collectors.joining("\n")));
Expand Down Expand Up @@ -94,4 +94,7 @@ protected boolean condition() {
}
}.waitForCondition(display, timeout_ms);
}

private TestUtils() {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@
*/
package org.eclipse.tm4e.ui.internal.snippets;

import static org.eclipse.tm4e.core.internal.utils.NullSafetyHelper.castNullable;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.core.runtime.IConfigurationElement;
Expand Down Expand Up @@ -52,7 +54,7 @@ private static synchronized ISnippetManager createInstance() {
return manager;
}

private final Map<String, @Nullable Collection<ISnippet>> snippets = new HashMap<>();
private final Map<String, List<ISnippet>> snippets = new HashMap<>();

private SnippetManager() {
}
Expand All @@ -77,17 +79,12 @@ private void loadGrammarsFromExtensionPoints() {

private void registerSnippet(final Snippet snippet) {
final String scopeName = snippet.getScopeName();
Collection<ISnippet> snippets = this.snippets.get(scopeName);
if (snippets == null) {
snippets = new ArrayList<>();
this.snippets.put(scopeName, snippets);
}
snippets.add(snippet);
snippets.computeIfAbsent(scopeName, scopeName_ -> new ArrayList<>()).add(snippet);
}

@Override
public ISnippet[] getSnippets(final String scopeName) {
final Collection<ISnippet> snippets = this.snippets.get(scopeName);
final List<ISnippet> snippets = castNullable(this.snippets.get(scopeName));
return snippets != null ? snippets.toArray(ISnippet[]::new) : EMPTY_SNIPPETS;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,8 @@ public void save() throws BackingStoreException {
json.addProperty("path", theme.getPath());
json.addProperty("dark", theme.isDark());
return json;
}).collect(JsonArray::new, (final JsonArray array, final JsonObject object) -> array.add(object),
(r, r1) -> {
})
}).collect(JsonArray::new, JsonArray::add, (r, r1) -> {
})
.toString());

// Save Theme associations in the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,18 +111,15 @@ public class TMPresentationReconciler implements IPresentationReconciler {
return null;
}

private final ModelTokensChangedEvent.Listener modelsTokensChangedListener = new ModelTokensChangedEvent.Listener() {
@Override
public void onModelTokensChanged(final ModelTokensChangedEvent event) {
final var colorizer = TMPresentationReconciler.this.colorizer;
if (colorizer != null) {
final Control control = colorizer.getTextViewer().getTextWidget();
if (control != null) {
control.getDisplay().asyncExec(() -> colorizer.colorize(event));
}
private final ModelTokensChangedEvent.Listener modelsTokensChangedListener = (ModelTokensChangedEvent event) -> {
final var colorizer = TMPresentationReconciler.this.colorizer;
if (colorizer != null) {
final Control control = colorizer.getTextViewer().getTextWidget();
if (control != null) {
control.getDisplay().asyncExec(() -> colorizer.colorize(event));
}
MarkerUtils.updateTextMarkers(event);
}
MarkerUtils.updateTextMarkers(event);
};

/**
Expand Down

0 comments on commit 50518ae

Please sign in to comment.