1515
1616import java .io .IOException ;
1717import java .io .InputStream ;
18+ import java .util .ArrayList ;
1819import java .util .Arrays ;
1920import java .util .Collections ;
2021import java .util .Comparator ;
@@ -44,7 +45,7 @@ public class DocsCommand implements CommandExecutor {
4445 * The parameters that indicate also searching internal packages and the core docs.
4546 */
4647 private static final Set <String > includeAllParams = new HashSet <>(Arrays .asList ("all" , "a" ));
47-
48+
4849 /**
4950 * Executes the {@code !docs} command.
5051 *
@@ -108,7 +109,8 @@ private void populateMethods(DiscordApi api, EmbedBuilder embed, String searchSt
108109 .thenCompose (JavadocParser ::getMethods )
109110 : CompletableFuture .completedFuture (Collections .emptySet ());
110111
111- Map <String , List <JavadocMethod >> methods = apiMethods .thenCombine (coreMethods , this ::unionOf ).join ().stream ()
112+ Map <String , List <JavadocMethod >> methodsByClass = apiMethods
113+ .thenCombine (coreMethods , this ::unionOf ).join ().stream ()
112114 .filter (method -> method .getFullName ().toLowerCase ().contains (searchString .toLowerCase ()))
113115 .filter (method -> {
114116 String packageName = method .getPackageName ();
@@ -118,41 +120,51 @@ private void populateMethods(DiscordApi api, EmbedBuilder embed, String searchSt
118120 .collect (Collectors .groupingBy (JavadocMethod ::getClassName ));
119121
120122
121- if (methods .isEmpty ()) {
123+ if (methodsByClass .isEmpty ()) {
122124 embed .setTitle ("Methods" );
123125 embed .setDescription ("No matching methods found!" );
124126 return ;
125127 }
126128
127- int totalTextCount = 0 ;
128- int classCounter = 0 ;
129- for (Map .Entry <String , List <JavadocMethod >> entry : methods .entrySet ()) {
130- StringBuilder strBuilder = new StringBuilder ();
131- int methodCounter = 0 ;
132- for (JavadocMethod method : entry .getValue ()) {
133- if (strBuilder .length () > 800 ) { // To prevent hitting the maximum field size
134- strBuilder .append ("• " ).append (entry .getValue ().size () - methodCounter ).append (" more ..." );
135- break ;
136- }
137- strBuilder .append ("• [" )
129+ int totalTextCount = 25 ; // the maximum tracker string length
130+ List <Map .Entry <String , List <JavadocMethod >>> entries = new ArrayList <>(methodsByClass .entrySet ());
131+ int classesAmount = entries .size ();
132+ for (int classIndex = 0 ; classIndex < classesAmount ; classIndex ++) {
133+ Map .Entry <String , List <JavadocMethod >> entry = entries .get (classIndex );
134+ List <JavadocMethod > methods = entry .getValue ();
135+ StringBuilder methodsBuilder = new StringBuilder ();
136+ int methodsAmount = methods .size ();
137+ for (int methodIndex = 0 ; methodIndex < methodsAmount ; methodIndex ++) {
138+ JavadocMethod method = methods .get (methodIndex );
139+ StringBuilder methodBuilder = new StringBuilder ()
140+ .append ("• [" )
138141 .append (method .getShortenedName ())
139142 .append ("](" )
140143 .append (method .getFullUrl ())
141144 .append (")\n " );
142- methodCounter ++;
145+ int nextMoreSize = methodIndex == (methodsAmount - 1 )
146+ ? 0
147+ : 11 + (int ) (Math .log10 (methodsAmount - methodIndex - 1 ) + 1 );
148+ if ((methodsBuilder .length () + methodBuilder .length () + nextMoreSize ) <= 1024 ) {
149+ methodsBuilder .append (methodBuilder );
150+ } else {
151+ methodsBuilder .append ("• " ).append (methodsAmount - methodIndex ).append (" more ..." );
152+ break ;
153+ }
143154 }
144- embed .addField (entry .getKey (), strBuilder .toString ());
145- totalTextCount += entry .getKey ().length () + strBuilder .length ();
146- classCounter ++;
147- if (totalTextCount > 5000 ) { // To prevent hitting the maximum embed size
155+ int nextMoreSize = classIndex == (classesAmount - 1 )
156+ ? 0
157+ : 57 + (int ) (Math .log10 (classesAmount - classIndex - 1 ) + 1 );
158+ String className = entry .getKey ();
159+ if ((totalTextCount + className .length () + methodsBuilder .length () + nextMoreSize ) <= 6000 ) {
160+ embed .addField (className , methodsBuilder .toString ());
161+ totalTextCount += className .length () + methodsBuilder .length ();
162+ } else {
163+ embed .addField (String .format ("And **%d** more classes ..." , classesAmount - classIndex ),
164+ "Maybe try a less generic search?" );
148165 break ;
149166 }
150167 }
151-
152- if (methods .size () - classCounter > 0 ) {
153- embed .addField ("And **" + (methods .size () - classCounter ) + "** more classes ..." ,
154- "Maybe try a less generic search?" );
155- }
156168 }
157169
158170 /**
0 commit comments