Skip to content

Commit 4f28d1c

Browse files
authored
Merge pull request #2965 from idodeclare/feature/better_help
Improve configuration help
2 parents 3e6137d + 6c593e2 commit 4f28d1c

File tree

5 files changed

+166
-39
lines changed

5 files changed

+166
-39
lines changed

opengrok-indexer/src/main/java/org/opengrok/indexer/analysis/AnalyzerGuruHelp.java

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,13 @@
1818
*/
1919

2020
/*
21-
* Copyright (c) 2017-2018, Chris Fraire <[email protected]>.
21+
* Copyright (c) 2017-2019, Chris Fraire <[email protected]>.
2222
*/
2323

2424
package org.opengrok.indexer.analysis;
2525

26+
import org.opengrok.indexer.util.StringUtils;
27+
2628
import java.util.ArrayList;
2729
import java.util.List;
2830
import java.util.Locale;
@@ -141,7 +143,7 @@ private static String[] splitLines(String str, int width) {
141143
int llen = 0;
142144
int i = 0;
143145
while (i < str.length()) {
144-
int wlen = nextTestWhitespaceLength(false, str, i);
146+
int wlen = StringUtils.whitespaceOrControlLength(str, i, false);
145147
if (wlen > 0) {
146148
String word = str.substring(i, i + wlen);
147149
if (llen < 1) {
@@ -160,7 +162,7 @@ private static String[] splitLines(String str, int width) {
160162
i += wlen;
161163
}
162164

163-
int slen = nextTestWhitespaceLength(true, str, i);
165+
int slen = StringUtils.whitespaceOrControlLength(str, i, true);
164166
i += slen;
165167
}
166168
if (b.length() > 0) {
@@ -171,20 +173,6 @@ private static String[] splitLines(String str, int width) {
171173
return res.stream().toArray(String[]::new);
172174
}
173175

174-
private static int nextTestWhitespaceLength(boolean match, String str,
175-
int off) {
176-
int i = 0;
177-
while (off + i < str.length()) {
178-
int cp = Character.codePointAt(str, off + i);
179-
if ((Character.isWhitespace(cp) || Character.isISOControl(cp)) !=
180-
match) {
181-
return i;
182-
}
183-
i += Character.charCount(cp);
184-
}
185-
return str.length() - off;
186-
}
187-
188176
private static List<MappedFactory> byKey(
189177
Map<String, AnalyzerFactory> mapped) {
190178

opengrok-indexer/src/main/java/org/opengrok/indexer/configuration/ConfigurationHelp.java

Lines changed: 99 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*/
1919

2020
/*
21-
* Copyright (c) 2018, Chris Fraire <[email protected]>.
21+
* Copyright (c) 2018-2019, Chris Fraire <[email protected]>.
2222
*/
2323

2424
package org.opengrok.indexer.configuration;
@@ -41,13 +41,16 @@
4141
import org.opengrok.indexer.history.RepositoryInfo;
4242
import org.opengrok.indexer.index.Filter;
4343
import org.opengrok.indexer.index.IgnoredNames;
44+
import org.opengrok.indexer.util.StringUtils;
4445

4546
/**
4647
* Represents a utility class to present some user-readable help regarding
4748
* {@link Configuration}.
4849
*/
4950
public class ConfigurationHelp {
5051

52+
private static final String XML_COMMENT_START = " <!-- ";
53+
5154
private ConfigurationHelp() {
5255
}
5356

@@ -64,6 +67,8 @@ public static String getSamples()
6467
Class klass = conf.getClass();
6568

6669
StringBuilder b = new StringBuilder();
70+
LinesBuilder h = new LinesBuilder();
71+
6772
b.append("Configuration examples:\n");
6873
b.append("\n");
6974

@@ -96,12 +101,16 @@ public static String getSamples()
96101
"(?sx)^<\\?xml.*Configuration\\d*\">\\n", "");
97102
sample = sample.replaceFirst("</object>\\n</java>", "");
98103

99-
b.append(" <!-- Sample for ");
100-
b.append(mthd.getName());
101-
b.append(". Default is: ");
102-
b.append(defaultValue);
103-
b.append(" -->");
104-
b.append("\n");
104+
h.clear();
105+
h.append(XML_COMMENT_START);
106+
h.append("Sample for ");
107+
h.append(mthd.getName());
108+
h.append(". Default is ");
109+
h.appendWords(defaultValue);
110+
h.appendWords(" -->");
111+
h.appendLine();
112+
113+
b.append(h);
105114
b.append(sample);
106115
}
107116
return b.toString();
@@ -119,10 +128,7 @@ private static List<Method> getSetters(Class klass) {
119128
res.add(mth);
120129
}
121130
}
122-
res.sort((o1, o2) -> {
123-
int cmp = o1.getName().compareToIgnoreCase(o2.getName());
124-
return cmp;
125-
});
131+
res.sort((o1, o2) -> o1.getName().compareToIgnoreCase(o2.getName()));
126132
return res;
127133
}
128134

@@ -182,7 +188,7 @@ private static Object getSampleValue(Method setter, Object defaultValue) {
182188
}
183189
return null;
184190
} else if (paramType == SuggesterConfig.class) {
185-
return new SuggesterConfig();
191+
return SuggesterConfig.getForHelp();
186192
} else {
187193
throw new UnsupportedOperationException("getSampleValue() for " +
188194
paramType + ", " + genType);
@@ -195,12 +201,11 @@ private static Object getSampleListValue(Type genType) {
195201
}
196202
ParameterizedType genParamType = (ParameterizedType) genType;
197203
Type actType = genParamType.getActualTypeArguments()[0];
198-
Object res = null;
199204

200205
if (actType != RepositoryInfo.class) {
201206
throw new UnsupportedOperationException("Not supported yet for " + actType);
202207
}
203-
return res;
208+
return null;
204209
}
205210

206211
private static Object getSampleMapValue(Type genType) {
@@ -211,7 +216,7 @@ private static Object getSampleMapValue(Type genType) {
211216
Type[] actualTypeArguments = genParamType.getActualTypeArguments();
212217
Type actType0 = actualTypeArguments[0];
213218
Type actType1 = actualTypeArguments[1];
214-
Object res = null;
219+
Object res;
215220

216221
if (actType0 == String.class) {
217222
if (actType1 == String.class) {
@@ -240,7 +245,7 @@ private static Object getSampleSetValue(Type genType) {
240245
}
241246
ParameterizedType genParamType = (ParameterizedType) genType;
242247
Type actType = genParamType.getActualTypeArguments()[0];
243-
Object res = null;
248+
Object res;
244249

245250
if (actType == String.class) {
246251
Set<String> strset = new HashSet<>();
@@ -286,6 +291,19 @@ private static Object getDefaultValue(Class<?> klass, Method setter,
286291
}
287292
}
288293

294+
// Return a text override for some objects.
295+
switch (gname) {
296+
case "getSuggesterConfig":
297+
return "as below but with Boolean opposites, non-zeroes decremented by 1, null " +
298+
"for allowed-projects, and also including \"full\" in allowed-fields";
299+
case "getPluginStack":
300+
return "an empty stack";
301+
case "getIncludedNames":
302+
return "an empty filter";
303+
case "getIgnoredNames":
304+
return "OpenGrok's standard set of ignored files and directories";
305+
}
306+
289307
try {
290308
return getter.invoke(cinst);
291309
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
@@ -301,4 +319,69 @@ private static boolean isDeprecated(Method mth) {
301319
}
302320
return false;
303321
}
322+
323+
private static class LinesBuilder {
324+
static final int LINE_LENGTH = 80;
325+
326+
final StringBuilder b = new StringBuilder();
327+
int length;
328+
329+
void clear() {
330+
b.setLength(0);
331+
length = 0;
332+
}
333+
334+
void append(String value) {
335+
b.append(value);
336+
length += value.length();
337+
}
338+
339+
void appendWords(Object value) {
340+
if (value == null) {
341+
appendWords("null");
342+
} else {
343+
appendWords(value.toString());
344+
}
345+
}
346+
347+
void appendWords(String value) {
348+
if (length > LINE_LENGTH) {
349+
appendLine();
350+
}
351+
352+
int i = 0;
353+
while (true) {
354+
int spaceLen = StringUtils.whitespaceOrControlLength(value, i, true);
355+
int wordLen = StringUtils.whitespaceOrControlLength(value, i + spaceLen, false);
356+
357+
if (wordLen < 1) {
358+
break;
359+
}
360+
361+
String word = value.substring(i + spaceLen, i + spaceLen + wordLen);
362+
if (length + spaceLen + wordLen > LINE_LENGTH) {
363+
appendLine();
364+
for (int j = 0; j < XML_COMMENT_START.length(); ++j) {
365+
append(" ");
366+
}
367+
append(word);
368+
} else {
369+
append(value.substring(i, i + spaceLen));
370+
append(word);
371+
}
372+
373+
i += spaceLen + wordLen;
374+
}
375+
}
376+
377+
void appendLine() {
378+
b.append("\n");
379+
length = 0;
380+
}
381+
382+
@Override
383+
public String toString() {
384+
return b.toString();
385+
}
386+
}
304387
}

opengrok-indexer/src/main/java/org/opengrok/indexer/configuration/SuggesterConfig.java

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
/*
2121
* Copyright (c) 2018 Oracle and/or its affiliates. All rights reserved.
22+
* Portions Copyright (c) 2019, Chris Fraire <[email protected]>.
2223
*/
2324
package org.opengrok.indexer.configuration;
2425

@@ -41,7 +42,7 @@ public class SuggesterConfig {
4142
public static final boolean ENABLED_DEFAULT = true;
4243
public static final int MAX_RESULTS_DEFAULT = 10;
4344
public static final int MIN_CHARS_DEFAULT = 0;
44-
public static final int MAX_PROJECTS_DEFAULT = Integer.MAX_VALUE;
45+
public static final int MAX_PROJECTS_DEFAULT = Short.MAX_VALUE;
4546
public static final boolean ALLOW_COMPLEX_QUERIES_DEFAULT = true;
4647
public static final boolean ALLOW_MOST_POPULAR_DEFAULT = true;
4748
public static final boolean SHOW_SCORES_DEFAULT = false;
@@ -335,4 +336,33 @@ public int hashCode() {
335336
buildTerminationTime, rebuildThreadPoolSizeInNcpuPercent);
336337
}
337338

339+
/**
340+
* Gets an instance version suitable for helper documentation by shifting
341+
* most default properties slightly.
342+
*/
343+
static SuggesterConfig getForHelp() {
344+
SuggesterConfig res = new SuggesterConfig();
345+
res.setEnabled(!res.isEnabled());
346+
res.setMaxResults(1 + res.getMaxResults());
347+
res.setMinChars(1 + res.getMinChars());
348+
res.setAllowedProjects(new HashSet<>(Arrays.asList("project-1", "project-2")));
349+
res.setMaxProjects(1 + res.getMaxProjects());
350+
res.setAllowedFields(getAllowedFieldsForHelp(res.getAllowedFields()));
351+
res.setAllowComplexQueries(!res.isAllowComplexQueries());
352+
res.setAllowMostPopular(!res.isAllowMostPopular());
353+
res.setShowScores(!res.isShowScores());
354+
res.setShowProjects(!res.isShowProjects());
355+
res.setShowTime(!res.isShowTime());
356+
res.setTimeThreshold(1 + res.getTimeThreshold());
357+
res.setRebuildCronConfig("1 0 * * *");
358+
res.setBuildTerminationTime(1 + res.getBuildTerminationTime());
359+
res.setRebuildThreadPoolSizeInNcpuPercent(1 + res.getRebuildThreadPoolSizeInNcpuPercent());
360+
return res;
361+
}
362+
363+
private static HashSet<String> getAllowedFieldsForHelp(Set<String> allowedFields) {
364+
HashSet<String> res = new HashSet<>(allowedFields);
365+
res.remove(QueryBuilder.FULL);
366+
return res;
367+
}
338368
}

opengrok-indexer/src/main/java/org/opengrok/indexer/index/IgnoredNames.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
/*
2121
* Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.
22+
* Portions Copyright (c) 2019, Chris Fraire <[email protected]>.
2223
*/
2324
package org.opengrok.indexer.index;
2425

@@ -34,6 +35,8 @@
3435
*/
3536
public class IgnoredNames implements Serializable {
3637
private static final long serialVersionUID = 1L;
38+
private static final String FILE_PREFIX = "f:";
39+
private static final String DIR_PREFIX = "d:";
3740

3841
private IgnoredFiles ignoredFiles;
3942
private IgnoredDirs ignoredDirs;
@@ -58,10 +61,10 @@ public void setItems(List<String> item) {
5861
}
5962

6063
public void add(String pattern) {
61-
if (pattern.startsWith("f:")) {
62-
ignoredFiles.add(pattern.substring(2));
63-
} else if (pattern.startsWith("d:")) {
64-
ignoredDirs.add(pattern.substring(2));
64+
if (pattern.startsWith(FILE_PREFIX)) {
65+
ignoredFiles.add(pattern.substring(FILE_PREFIX.length()));
66+
} else if (pattern.startsWith(DIR_PREFIX)) {
67+
ignoredDirs.add(pattern.substring(DIR_PREFIX.length()));
6568
} else {
6669
// backward compatibility
6770
ignoredFiles.add(pattern);

opengrok-indexer/src/main/java/org/opengrok/indexer/util/StringUtils.java

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
/*
2121
* Copyright (c) 2008, 2018, Oracle and/or its affiliates. All rights reserved.
22-
* Portions Copyright (c) 2017-2018, Chris Fraire <[email protected]>.
22+
* Portions Copyright (c) 2017-2019, Chris Fraire <[email protected]>.
2323
*/
2424

2525
package org.opengrok.indexer.util;
@@ -299,4 +299,27 @@ public static int countPushback(String capture, Pattern pat) {
299299
}
300300
return 0;
301301
}
302+
303+
/**
304+
* Determine the length of the next whitespace- or ISO control
305+
* character-related sequence within a string.
306+
* @param str a defined instance
307+
* @param off the starting offset within {@code str}
308+
* @param shouldMatch a value indicating whether to match all contiguous
309+
* whitespace or ISO control characters ({@code true}) or
310+
* all contiguous non-whitespace and non-control
311+
* characters ({@code false}) starting at {@code off}
312+
* @return a length greater than or equal to zero
313+
*/
314+
public static int whitespaceOrControlLength(String str, int off, boolean shouldMatch) {
315+
int i = 0;
316+
while (off + i < str.length()) {
317+
int cp = Character.codePointAt(str, off + i);
318+
if ((Character.isWhitespace(cp) || Character.isISOControl(cp)) != shouldMatch) {
319+
return i;
320+
}
321+
i += Character.charCount(cp);
322+
}
323+
return str.length() - off;
324+
}
302325
}

0 commit comments

Comments
 (0)