From bc4e90bb6905bf235dbbe67f8902c4cbe4fbf721 Mon Sep 17 00:00:00 2001 From: Serhat Yenican Date: Mon, 27 May 2024 17:11:02 +0200 Subject: [PATCH] SLCORE-819 Add underscore char to the split pattern for better binding suggestion --- .../core/BindingCandidatesFinder.java | 3 +- .../sonarlint/core/TextSearchIndex.java | 12 +++++-- .../sonarlint/core/TextSearchIndexTest.java | 34 +++++++++++++++++++ 3 files changed, 45 insertions(+), 4 deletions(-) create mode 100644 backend/core/src/test/java/org/sonarsource/sonarlint/core/TextSearchIndexTest.java diff --git a/backend/core/src/main/java/org/sonarsource/sonarlint/core/BindingCandidatesFinder.java b/backend/core/src/main/java/org/sonarsource/sonarlint/core/BindingCandidatesFinder.java index 9df2739ff3..5ed6cc7998 100644 --- a/backend/core/src/main/java/org/sonarsource/sonarlint/core/BindingCandidatesFinder.java +++ b/backend/core/src/main/java/org/sonarsource/sonarlint/core/BindingCandidatesFinder.java @@ -40,6 +40,7 @@ public class BindingCandidatesFinder { private static final SonarLintLogger LOG = SonarLintLogger.get(); + private static final String SPLIT_PATTERN = "[\\W_]+"; private final ConfigurationRepository configRepository; private final BindingClueProvider bindingClueProvider; private final SonarProjectsCache sonarProjectsCache; @@ -106,7 +107,7 @@ private boolean isConfigScopeNameCloseEnoughToSonarProject(String configScopeNam LOG.debug("Unable to find SonarProject with key '{}' on connection '{}' in the cache", projectKey, connectionId); return false; } - TextSearchIndex index = new TextSearchIndex<>(); + TextSearchIndex index = new TextSearchIndex<>(SPLIT_PATTERN); var p = sonarProjectOpt.get(); index.index(p, p.getKey() + " " + p.getName()); var searchResult = index.search(configScopeName); diff --git a/backend/core/src/main/java/org/sonarsource/sonarlint/core/TextSearchIndex.java b/backend/core/src/main/java/org/sonarsource/sonarlint/core/TextSearchIndex.java index 159fc2035a..6764f2f311 100644 --- a/backend/core/src/main/java/org/sonarsource/sonarlint/core/TextSearchIndex.java +++ b/backend/core/src/main/java/org/sonarsource/sonarlint/core/TextSearchIndex.java @@ -45,11 +45,17 @@ * Performance of search: O(log N) on the number of indexed terms + O(N) on the number of results */ public class TextSearchIndex { - private static final String SPLIT_PATTERN = "\\W"; + private static final String DEFAULT_SPLIT_PATTERN = "\\W"; + private final String splitPattern; private TreeMap> termToObj; private Map objToWordFrequency; public TextSearchIndex() { + this(DEFAULT_SPLIT_PATTERN); + } + + public TextSearchIndex(String splitPattern) { + this.splitPattern = splitPattern; clear(); } @@ -180,8 +186,8 @@ private void addToDictionary(String token, int tokenIndex, T obj) { entries.add(new DictEntry(obj, tokenIndex)); } - private static List tokenize(String text) { - var split = text.split(SPLIT_PATTERN); + private List tokenize(String text) { + var split = text.split(splitPattern); List terms = new ArrayList<>(split.length); for (String s : split) { diff --git a/backend/core/src/test/java/org/sonarsource/sonarlint/core/TextSearchIndexTest.java b/backend/core/src/test/java/org/sonarsource/sonarlint/core/TextSearchIndexTest.java new file mode 100644 index 0000000000..3b28b31385 --- /dev/null +++ b/backend/core/src/test/java/org/sonarsource/sonarlint/core/TextSearchIndexTest.java @@ -0,0 +1,34 @@ +/* + * SonarLint Core - Implementation + * Copyright (C) 2016-2024 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonarsource.sonarlint.core; + +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +class TextSearchIndexTest { + @Test + void splits_strings_based_on_provided_split_pattern() { + TextSearchIndex index = new TextSearchIndex<>("[\\W_]+"); + index.index("text", "a-b-c_d"); + var searchResult = index.search("d"); + assertThat(searchResult).isNotEmpty(); + } +} \ No newline at end of file