From b215705d49bfb806355c7d2b8521341612965a4e Mon Sep 17 00:00:00 2001 From: Janne Valkealahti Date: Fri, 21 Oct 2022 11:59:23 +0100 Subject: [PATCH] Fix line split logic - Fix issues where shows patsh were not correctly truncated - Relates #556 --- .../shell/style/PartsTextRenderer.java | 52 ++++--- .../shell/component/PathSearchTests.java | 15 +- .../shell/style/PartsTextRendererTests.java | 144 +++++++++++++++++- ...FuzzyMatchV2SearchMatchAlgorithmTests.java | 6 +- 4 files changed, 196 insertions(+), 21 deletions(-) diff --git a/spring-shell-core/src/main/java/org/springframework/shell/style/PartsTextRenderer.java b/spring-shell-core/src/main/java/org/springframework/shell/style/PartsTextRenderer.java index 695cd2fc..54aa19e1 100644 --- a/spring-shell-core/src/main/java/org/springframework/shell/style/PartsTextRenderer.java +++ b/spring-shell-core/src/main/java/org/springframework/shell/style/PartsTextRenderer.java @@ -44,37 +44,53 @@ public class PartsTextRenderer implements AttributeRenderer { int width = values.width; int max = width - prefix; List parts = value.getParts(); + for (int i = 0; i < parts.size(); i++) { - PartText pt = parts.get(i); - String text; + PartText current = parts.get(i); + PartText next = i + 1 < parts.size() ? parts.get(i + 1) : null; + boolean doBreak = false; + String text = current.getText(); + int currentLen = len + text.length(); + int nextLen = next != null ? currentLen + next.getText().length() : -1; - int newLen = len + pt.getText().length(); - - // if current would take over max length - if (newLen > max) { - int l = max - len - dots; - text = String.format(locale, "%1." + l + "s.." , pt.getText()); + if (currentLen > max - dots && nextLen > 0) { + int l = max - len; + int diff = l - text.length(); + if (diff == 1) { + text = text.substring(0, text.length() - 1) + ".."; + } + else if (diff == 0) { + text = String.format(locale, "%1." + (text.length() - 2) + "s.." , text); + } + else { + text = String.format(locale, "%1." + (l - dots) + "s.." , text); + } doBreak = true; } - // if next would take over max length - else if (i + 1 < parts.size() && newLen + parts.get(i + 1).getText().length() > max) { - int l = max - len - dots; - text = String.format(locale, "%1." + l + "s.." , pt.getText()); + else if (currentLen == max - dots) { + text = text + ".."; doBreak = true; } - // we're fine as is - else { - text = pt.getText(); + else if (currentLen > max) { + int l = max - len - dots; + if (l == 0) { + text = ".."; + } + else { + text = String.format(locale, "%1." + l + "s.." , text); + } + doBreak = true; } - String tag = pt.isMatch() ? values.matchStyle : values.textStyle; + + String tag = current.isMatch() ? values.matchStyle : values.textStyle; buf.append(String.format("@{%s %s}", themeResolver.resolveStyleTag(tag), text)); - len += pt.getText().length(); + len += text.length(); + if (doBreak) { break; } } - return buf.toString(); } diff --git a/spring-shell-core/src/test/java/org/springframework/shell/component/PathSearchTests.java b/spring-shell-core/src/test/java/org/springframework/shell/component/PathSearchTests.java index b5bbdf0a..eb37c9d9 100644 --- a/spring-shell-core/src/test/java/org/springframework/shell/component/PathSearchTests.java +++ b/spring-shell-core/src/test/java/org/springframework/shell/component/PathSearchTests.java @@ -76,7 +76,20 @@ public class PathSearchTests { Arrays.asList( PartText.of("01234567", false), PartText.of("8", true), - PartText.of("9", true))) + PartText.of("9", true))), + Arguments.of("spring-shell-core/build/test-results/test/TEST-org.springframework.shell.support.search.FuzzyMatchV2SearchMatchAlgorithmTests.xml", new int[] { 13, 33, 59, 67, 73 }, + Arrays.asList( + PartText.of("spring-shell-", false), + PartText.of("c", true), + PartText.of("ore/build/test-resu", false), + PartText.of("l", true), + PartText.of("ts/test/TEST-org.springfr", false), + PartText.of("a", true), + PartText.of("mework.", false), + PartText.of("s", true), + PartText.of("hell.", false), + PartText.of("s", true), + PartText.of("upport.search.FuzzyMatchV2SearchMatchAlgorithmTests.xml", false))) ); } diff --git a/spring-shell-core/src/test/java/org/springframework/shell/style/PartsTextRendererTests.java b/spring-shell-core/src/test/java/org/springframework/shell/style/PartsTextRendererTests.java index f9f726c5..29c39cbe 100644 --- a/spring-shell-core/src/test/java/org/springframework/shell/style/PartsTextRendererTests.java +++ b/spring-shell-core/src/test/java/org/springframework/shell/style/PartsTextRendererTests.java @@ -88,7 +88,149 @@ class PartsTextRendererTests { PartText.of("3456", true), PartText.of("789", false) ), - "012345..") + "012345.."), + Arguments.of( + "width:12,prefix:0,textStyle:style-item-selector,matchStyle:style-level-warn", + PartsText.of( + PartText.of("a", false), + PartText.of("b", true), + PartText.of("cd", false), + PartText.of("efg", true), + PartText.of("h", false), + PartText.of("i", true), + PartText.of("jkl", true) + ), + "abcdefghijkl"), + Arguments.of( + "width:11,prefix:0,textStyle:style-item-selector,matchStyle:style-level-warn", + PartsText.of( + PartText.of("a", false), + PartText.of("b", true), + PartText.of("cd", false), + PartText.of("efg", true), + PartText.of("h", false), + PartText.of("i", true), + PartText.of("jkl", true) + ), + "abcdefghi.."), + Arguments.of( + "width:10,prefix:0,textStyle:style-item-selector,matchStyle:style-level-warn", + PartsText.of( + PartText.of("a", false), + PartText.of("b", true), + PartText.of("cd", false), + PartText.of("efg", true), + PartText.of("h", false), + PartText.of("i", true), + PartText.of("jkl", true) + ), + "abcdefgh.."), + Arguments.of( + "width:9,prefix:0,textStyle:style-item-selector,matchStyle:style-level-warn", + PartsText.of( + PartText.of("a", false), + PartText.of("b", true), + PartText.of("cd", false), + PartText.of("efg", true), + PartText.of("h", false), + PartText.of("i", true), + PartText.of("jkl", true) + ), + "abcdefg.."), + Arguments.of( + "width:8,prefix:0,textStyle:style-item-selector,matchStyle:style-level-warn", + PartsText.of( + PartText.of("a", false), + PartText.of("b", true), + PartText.of("cd", false), + PartText.of("efg", true), + PartText.of("h", false), + PartText.of("i", true), + PartText.of("jkl", true) + ), + "abcdef.."), + Arguments.of( + "width:7,prefix:0,textStyle:style-item-selector,matchStyle:style-level-warn", + PartsText.of( + PartText.of("a", false), + PartText.of("b", true), + PartText.of("cd", false), + PartText.of("efg", true), + PartText.of("h", false), + PartText.of("i", true), + PartText.of("jkl", true) + ), + "abcde.."), + Arguments.of( + "width:6,prefix:0,textStyle:style-item-selector,matchStyle:style-level-warn", + PartsText.of( + PartText.of("a", false), + PartText.of("b", true), + PartText.of("cd", false), + PartText.of("efg", true), + PartText.of("h", false), + PartText.of("i", true), + PartText.of("jkl", true) + ), + "abcd.."), + Arguments.of( + "width:5,prefix:0,textStyle:style-item-selector,matchStyle:style-level-warn", + PartsText.of( + PartText.of("a", false), + PartText.of("b", true), + PartText.of("cd", false), + PartText.of("efg", true), + PartText.of("h", false), + PartText.of("i", true), + PartText.of("jkl", true) + ), + "abc.."), + Arguments.of( + "width:4,prefix:0,textStyle:style-item-selector,matchStyle:style-level-warn", + PartsText.of( + PartText.of("a", false), + PartText.of("b", true), + PartText.of("cd", false), + PartText.of("efg", true), + PartText.of("h", false), + PartText.of("i", true), + PartText.of("jkl", true) + ), + "ab.."), + Arguments.of( + "width:3,prefix:0,textStyle:style-item-selector,matchStyle:style-level-warn", + PartsText.of( + PartText.of("a", false), + PartText.of("b", true), + PartText.of("cd", false), + PartText.of("efg", true), + PartText.of("h", false), + PartText.of("i", true), + PartText.of("jkl", true) + ), + "a.."), + Arguments.of( + "width:3,prefix:0,textStyle:style-item-selector,matchStyle:style-level-warn", + PartsText.of( + PartText.of("abcdefg", false), + PartText.of("hijklmn", true) + ), + "a.."), + Arguments.of( + "width:126,prefix:0,textStyle:style-item-selector,matchStyle:style-level-warn", + PartsText.of( + PartText.of("e2e/spring-shell-e2e-tests/node_modules/@babel/plugin-syntax-types", false), + PartText.of("c", true), + PartText.of("ript/test/fixtures/disa", false), + PartText.of("l", true), + PartText.of("low-jsx-ambiguity/type-parameter-un", false), + PartText.of("a", true), + PartText.of("mbiguou", false), + PartText.of("s", true), + PartText.of("/output.j", false), + PartText.of("s", true) + ), + "e2e/spring-shell-e2e-tests/node_modules/@babel/plugin-syntax-typescript/test/fixtures/disallow-jsx-ambiguity/type-parameter-..") ); } diff --git a/spring-shell-core/src/test/java/org/springframework/shell/support/search/FuzzyMatchV2SearchMatchAlgorithmTests.java b/spring-shell-core/src/test/java/org/springframework/shell/support/search/FuzzyMatchV2SearchMatchAlgorithmTests.java index bd615c19..0c8959ec 100644 --- a/spring-shell-core/src/test/java/org/springframework/shell/support/search/FuzzyMatchV2SearchMatchAlgorithmTests.java +++ b/spring-shell-core/src/test/java/org/springframework/shell/support/search/FuzzyMatchV2SearchMatchAlgorithmTests.java @@ -146,7 +146,11 @@ class FuzzyMatchV2SearchMatchAlgorithmTests { Arguments.of(false, true, "/tmp/test/suomi/O.txt", "oo", false, 12, 17, new int[] { 12, 16 }, SCORE_MATCH * 2 + BONUS_BOUNDARY_DELIMITER + SCORE_GAP_START + SCORE_GAP_EXTENSION * 2), Arguments.of(false, true, "/tmp/test/suomi/Ö.txt", "oo", false, 12, 17, new int[] { 12, 16 }, - SCORE_MATCH * 2 + BONUS_BOUNDARY_DELIMITER + SCORE_GAP_START + SCORE_GAP_EXTENSION * 2) + SCORE_MATCH * 2 + BONUS_BOUNDARY_DELIMITER + SCORE_GAP_START + SCORE_GAP_EXTENSION * 2), + Arguments.of(false, true, "spring-shell-core/build/test-results/test/TEST-org.springframework.shell.support.search.FuzzyMatchV2SearchMatchAlgorithmTests.xml", "class", false, 13, 74, new int[] { 13, 33, 59, 67, 73 }, + 48), + Arguments.of(false, true, "e2e/spring-shell-e2e-tests/node_modules/@babel/plugin-syntax-typescript/test/fixtures/disallow-jsx-ambiguity/type-parameter-unambiguous/output.js", "class", false, 66, 145, new int[] { 66, 90, 126, 134, 144 }, + 28) ); }