Skip to content

Commit b215705

Browse files
committed
Fix line split logic
- Fix issues where shows patsh were not correctly truncated - Relates #556
1 parent 7866c96 commit b215705

File tree

4 files changed

+196
-21
lines changed

4 files changed

+196
-21
lines changed

spring-shell-core/src/main/java/org/springframework/shell/style/PartsTextRenderer.java

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -44,37 +44,53 @@ public String toString(PartsText value, String formatString, Locale locale) {
4444
int width = values.width;
4545
int max = width - prefix;
4646
List<PartText> parts = value.getParts();
47+
4748
for (int i = 0; i < parts.size(); i++) {
48-
PartText pt = parts.get(i);
49-
String text;
50-
boolean doBreak = false;
49+
PartText current = parts.get(i);
50+
PartText next = i + 1 < parts.size() ? parts.get(i + 1) : null;
5151

52-
int newLen = len + pt.getText().length();
52+
boolean doBreak = false;
53+
String text = current.getText();
54+
int currentLen = len + text.length();
55+
int nextLen = next != null ? currentLen + next.getText().length() : -1;
5356

54-
// if current would take over max length
55-
if (newLen > max) {
56-
int l = max - len - dots;
57-
text = String.format(locale, "%1." + l + "s.." , pt.getText());
57+
if (currentLen > max - dots && nextLen > 0) {
58+
int l = max - len;
59+
int diff = l - text.length();
60+
if (diff == 1) {
61+
text = text.substring(0, text.length() - 1) + "..";
62+
}
63+
else if (diff == 0) {
64+
text = String.format(locale, "%1." + (text.length() - 2) + "s.." , text);
65+
}
66+
else {
67+
text = String.format(locale, "%1." + (l - dots) + "s.." , text);
68+
}
5869
doBreak = true;
5970
}
60-
// if next would take over max length
61-
else if (i + 1 < parts.size() && newLen + parts.get(i + 1).getText().length() > max) {
62-
int l = max - len - dots;
63-
text = String.format(locale, "%1." + l + "s.." , pt.getText());
71+
else if (currentLen == max - dots) {
72+
text = text + "..";
6473
doBreak = true;
6574
}
66-
// we're fine as is
67-
else {
68-
text = pt.getText();
75+
else if (currentLen > max) {
76+
int l = max - len - dots;
77+
if (l == 0) {
78+
text = "..";
79+
}
80+
else {
81+
text = String.format(locale, "%1." + l + "s.." , text);
82+
}
83+
doBreak = true;
6984
}
70-
String tag = pt.isMatch() ? values.matchStyle : values.textStyle;
85+
86+
String tag = current.isMatch() ? values.matchStyle : values.textStyle;
7187
buf.append(String.format("@{%s %s}", themeResolver.resolveStyleTag(tag), text));
72-
len += pt.getText().length();
88+
len += text.length();
89+
7390
if (doBreak) {
7491
break;
7592
}
7693
}
77-
7894
return buf.toString();
7995
}
8096

spring-shell-core/src/test/java/org/springframework/shell/component/PathSearchTests.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,20 @@ static Stream<Arguments> testOfNameMatchParts() {
7676
Arrays.asList(
7777
PartText.of("01234567", false),
7878
PartText.of("8", true),
79-
PartText.of("9", true)))
79+
PartText.of("9", true))),
80+
Arguments.of("spring-shell-core/build/test-results/test/TEST-org.springframework.shell.support.search.FuzzyMatchV2SearchMatchAlgorithmTests.xml", new int[] { 13, 33, 59, 67, 73 },
81+
Arrays.asList(
82+
PartText.of("spring-shell-", false),
83+
PartText.of("c", true),
84+
PartText.of("ore/build/test-resu", false),
85+
PartText.of("l", true),
86+
PartText.of("ts/test/TEST-org.springfr", false),
87+
PartText.of("a", true),
88+
PartText.of("mework.", false),
89+
PartText.of("s", true),
90+
PartText.of("hell.", false),
91+
PartText.of("s", true),
92+
PartText.of("upport.search.FuzzyMatchV2SearchMatchAlgorithmTests.xml", false)))
8093
);
8194
}
8295

spring-shell-core/src/test/java/org/springframework/shell/style/PartsTextRendererTests.java

Lines changed: 143 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,149 @@ static Stream<Arguments> test() {
8888
PartText.of("3456", true),
8989
PartText.of("789", false)
9090
),
91-
"012345..")
91+
"012345.."),
92+
Arguments.of(
93+
"width:12,prefix:0,textStyle:style-item-selector,matchStyle:style-level-warn",
94+
PartsText.of(
95+
PartText.of("a", false),
96+
PartText.of("b", true),
97+
PartText.of("cd", false),
98+
PartText.of("efg", true),
99+
PartText.of("h", false),
100+
PartText.of("i", true),
101+
PartText.of("jkl", true)
102+
),
103+
"abcdefghijkl"),
104+
Arguments.of(
105+
"width:11,prefix:0,textStyle:style-item-selector,matchStyle:style-level-warn",
106+
PartsText.of(
107+
PartText.of("a", false),
108+
PartText.of("b", true),
109+
PartText.of("cd", false),
110+
PartText.of("efg", true),
111+
PartText.of("h", false),
112+
PartText.of("i", true),
113+
PartText.of("jkl", true)
114+
),
115+
"abcdefghi.."),
116+
Arguments.of(
117+
"width:10,prefix:0,textStyle:style-item-selector,matchStyle:style-level-warn",
118+
PartsText.of(
119+
PartText.of("a", false),
120+
PartText.of("b", true),
121+
PartText.of("cd", false),
122+
PartText.of("efg", true),
123+
PartText.of("h", false),
124+
PartText.of("i", true),
125+
PartText.of("jkl", true)
126+
),
127+
"abcdefgh.."),
128+
Arguments.of(
129+
"width:9,prefix:0,textStyle:style-item-selector,matchStyle:style-level-warn",
130+
PartsText.of(
131+
PartText.of("a", false),
132+
PartText.of("b", true),
133+
PartText.of("cd", false),
134+
PartText.of("efg", true),
135+
PartText.of("h", false),
136+
PartText.of("i", true),
137+
PartText.of("jkl", true)
138+
),
139+
"abcdefg.."),
140+
Arguments.of(
141+
"width:8,prefix:0,textStyle:style-item-selector,matchStyle:style-level-warn",
142+
PartsText.of(
143+
PartText.of("a", false),
144+
PartText.of("b", true),
145+
PartText.of("cd", false),
146+
PartText.of("efg", true),
147+
PartText.of("h", false),
148+
PartText.of("i", true),
149+
PartText.of("jkl", true)
150+
),
151+
"abcdef.."),
152+
Arguments.of(
153+
"width:7,prefix:0,textStyle:style-item-selector,matchStyle:style-level-warn",
154+
PartsText.of(
155+
PartText.of("a", false),
156+
PartText.of("b", true),
157+
PartText.of("cd", false),
158+
PartText.of("efg", true),
159+
PartText.of("h", false),
160+
PartText.of("i", true),
161+
PartText.of("jkl", true)
162+
),
163+
"abcde.."),
164+
Arguments.of(
165+
"width:6,prefix:0,textStyle:style-item-selector,matchStyle:style-level-warn",
166+
PartsText.of(
167+
PartText.of("a", false),
168+
PartText.of("b", true),
169+
PartText.of("cd", false),
170+
PartText.of("efg", true),
171+
PartText.of("h", false),
172+
PartText.of("i", true),
173+
PartText.of("jkl", true)
174+
),
175+
"abcd.."),
176+
Arguments.of(
177+
"width:5,prefix:0,textStyle:style-item-selector,matchStyle:style-level-warn",
178+
PartsText.of(
179+
PartText.of("a", false),
180+
PartText.of("b", true),
181+
PartText.of("cd", false),
182+
PartText.of("efg", true),
183+
PartText.of("h", false),
184+
PartText.of("i", true),
185+
PartText.of("jkl", true)
186+
),
187+
"abc.."),
188+
Arguments.of(
189+
"width:4,prefix:0,textStyle:style-item-selector,matchStyle:style-level-warn",
190+
PartsText.of(
191+
PartText.of("a", false),
192+
PartText.of("b", true),
193+
PartText.of("cd", false),
194+
PartText.of("efg", true),
195+
PartText.of("h", false),
196+
PartText.of("i", true),
197+
PartText.of("jkl", true)
198+
),
199+
"ab.."),
200+
Arguments.of(
201+
"width:3,prefix:0,textStyle:style-item-selector,matchStyle:style-level-warn",
202+
PartsText.of(
203+
PartText.of("a", false),
204+
PartText.of("b", true),
205+
PartText.of("cd", false),
206+
PartText.of("efg", true),
207+
PartText.of("h", false),
208+
PartText.of("i", true),
209+
PartText.of("jkl", true)
210+
),
211+
"a.."),
212+
Arguments.of(
213+
"width:3,prefix:0,textStyle:style-item-selector,matchStyle:style-level-warn",
214+
PartsText.of(
215+
PartText.of("abcdefg", false),
216+
PartText.of("hijklmn", true)
217+
),
218+
"a.."),
219+
Arguments.of(
220+
"width:126,prefix:0,textStyle:style-item-selector,matchStyle:style-level-warn",
221+
PartsText.of(
222+
PartText.of("e2e/spring-shell-e2e-tests/node_modules/@babel/plugin-syntax-types", false),
223+
PartText.of("c", true),
224+
PartText.of("ript/test/fixtures/disa", false),
225+
PartText.of("l", true),
226+
PartText.of("low-jsx-ambiguity/type-parameter-un", false),
227+
PartText.of("a", true),
228+
PartText.of("mbiguou", false),
229+
PartText.of("s", true),
230+
PartText.of("/output.j", false),
231+
PartText.of("s", true)
232+
),
233+
"e2e/spring-shell-e2e-tests/node_modules/@babel/plugin-syntax-typescript/test/fixtures/disallow-jsx-ambiguity/type-parameter-..")
92234
);
93235
}
94236

spring-shell-core/src/test/java/org/springframework/shell/support/search/FuzzyMatchV2SearchMatchAlgorithmTests.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,11 @@ static Stream<Arguments> testFuzzyMatchV2Extra() {
146146
Arguments.of(false, true, "/tmp/test/suomi/O.txt", "oo", false, 12, 17, new int[] { 12, 16 },
147147
SCORE_MATCH * 2 + BONUS_BOUNDARY_DELIMITER + SCORE_GAP_START + SCORE_GAP_EXTENSION * 2),
148148
Arguments.of(false, true, "/tmp/test/suomi/Ö.txt", "oo", false, 12, 17, new int[] { 12, 16 },
149-
SCORE_MATCH * 2 + BONUS_BOUNDARY_DELIMITER + SCORE_GAP_START + SCORE_GAP_EXTENSION * 2)
149+
SCORE_MATCH * 2 + BONUS_BOUNDARY_DELIMITER + SCORE_GAP_START + SCORE_GAP_EXTENSION * 2),
150+
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 },
151+
48),
152+
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 },
153+
28)
150154
);
151155
}
152156

0 commit comments

Comments
 (0)