This commit is contained in:
Janne Valkealahti
2022-10-22 06:17:24 +01:00
parent 092a169fce
commit 46505e8b72
2 changed files with 12 additions and 12 deletions

View File

@@ -235,7 +235,7 @@ public class PathSearch extends AbstractTextComponent<Path, PathSearchContext> {
if (!StringUtils.hasText(text)) {
text = ".";
}
PartsText partsText = PathSearchContext.ofNameMatchPartsx(text, positions);
PartsText partsText = PathSearchContext.ofPositions(text, positions);
PathViewItem item = new PathViewItem(scoredPath.getPath(), partsText, false);
return item;
})
@@ -438,13 +438,13 @@ public class PathSearch extends AbstractTextComponent<Path, PathSearchContext> {
* @param positions the positions array, expected to be ordered and no duplicates
* @return parts text
*/
public static PartsText ofNameMatchPartsx(String text, int[] positions) {
public static PartsText ofPositions(String text, int[] positions) {
List<PartText> parts = new ArrayList<>();
if (positions.length == 0) {
parts.addAll(nameMatchPartsx(text, -1));
parts.addAll(ofPosition(text, -1));
}
else if (positions.length == 1 && positions[0] == text.length()) {
parts.addAll(nameMatchPartsx(text, text.length() - 1));
parts.addAll(ofPosition(text, text.length() - 1));
}
else {
int sidx = 0;
@@ -453,22 +453,22 @@ public class PathSearch extends AbstractTextComponent<Path, PathSearchContext> {
eidx = positions[i];
if (sidx < text.length()) {
String partText = text.substring(sidx, eidx + 1);
parts.addAll(nameMatchPartsx(partText, eidx - sidx));
parts.addAll(ofPosition(partText, eidx - sidx));
}
else {
parts.addAll(nameMatchPartsx(String.valueOf(text.charAt(text.length() - 1)), 0));
parts.addAll(ofPosition(String.valueOf(text.charAt(text.length() - 1)), 0));
}
sidx = eidx + 1;
}
if (sidx < text.length()) {
String partText = text.substring(sidx, text.length());
parts.addAll(nameMatchPartsx(partText, -1));
parts.addAll(ofPosition(partText, -1));
}
}
return PartsText.of(parts);
}
static List<PartText> nameMatchPartsx(String text, int position) {
static List<PartText> ofPosition(String text, int position) {
List<PartText> parts = new ArrayList<>();
if (position < 0) {
parts.add(PartText.of(text, false));

View File

@@ -31,7 +31,7 @@ import static org.assertj.core.api.Assertions.assertThat;
public class PathSearchTests {
static Stream<Arguments> testOfNameMatchParts() {
static Stream<Arguments> testParts() {
return Stream.of(
Arguments.of("0", new int[] { 0 },
Arrays.asList(PartText.of("0", true))),
@@ -95,9 +95,9 @@ public class PathSearchTests {
@ParameterizedTest
@MethodSource
void testOfNameMatchParts(String text, int[] positions, List<PartText> parts) {
PartsText ofNameMatchPartsx = PathSearchContext.ofNameMatchPartsx(text, positions);
List<PartText> res = ofNameMatchPartsx.getParts();
void testParts(String text, int[] positions, List<PartText> parts) {
PartsText partsText = PathSearchContext.ofPositions(text, positions);
List<PartText> res = partsText.getParts();
assertThat(res).hasSize(parts.size());
for (int i = 0; i < parts.size(); i++) {
assertThat(res.get(i).getText()).isEqualTo(parts.get(i).getText());