You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TabCompleter.java 9.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /*
  2. * Copyright (c) 2006-2010 Chris Smith, Shane Mc Cormack, Gregory Holmes
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package com.dmdirc.ui.input;
  23. import com.dmdirc.commandparser.CommandArguments;
  24. import com.dmdirc.commandparser.CommandInfo;
  25. import com.dmdirc.commandparser.CommandManager;
  26. import com.dmdirc.commandparser.commands.ChannelCommand;
  27. import com.dmdirc.commandparser.commands.Command;
  28. import com.dmdirc.commandparser.commands.IntelligentCommand;
  29. import com.dmdirc.config.IdentityManager;
  30. import com.dmdirc.util.MapList;
  31. import java.io.Serializable;
  32. import java.util.Arrays;
  33. import java.util.List;
  34. import java.util.Locale;
  35. import java.util.Map;
  36. /**
  37. * The tab completer handles a user's request to tab complete some word.
  38. *
  39. * @author chris
  40. */
  41. public final class TabCompleter implements Serializable {
  42. /**
  43. * A version number for this class. It should be changed whenever the class
  44. * structure is changed (or anything else that would prevent serialized
  45. * objects being unserialized with the new class).
  46. */
  47. private static final long serialVersionUID = 1;
  48. /**
  49. * The parent TabCompleter. Results from parents are merged with results
  50. * from this completer.
  51. */
  52. private TabCompleter parent;
  53. /**
  54. * The entries in this completer.
  55. */
  56. private final MapList<TabCompletionType, String> entries
  57. = new MapList<TabCompletionType, String>();
  58. /** Creates a new instance of TabCompleter. */
  59. public TabCompleter() {
  60. //Do nothing.
  61. }
  62. /**
  63. * Creates a new instance of TabCompleter, with a designated parent.
  64. * @param newParent The parent TabCompleter, which is checked for matches if
  65. * local ones fail
  66. */
  67. public TabCompleter(final TabCompleter newParent) {
  68. this.parent = newParent;
  69. }
  70. /**
  71. * Attempts to complete the partial string.
  72. *
  73. * @param partial The string to tab complete
  74. * @param additionals A list of additional strings to use
  75. * @return A TabCompleterResult containing any matches found
  76. */
  77. public TabCompleterResult complete(final String partial,
  78. final AdditionalTabTargets additionals) {
  79. final TabCompleterResult result = new TabCompleterResult();
  80. final MapList<TabCompletionType, String> targets
  81. = new MapList<TabCompletionType, String>(entries);
  82. final boolean caseSensitive = IdentityManager.getGlobalConfig()
  83. .getOptionBool("tabcompletion", "casesensitive");
  84. final boolean allowEmpty = IdentityManager.getGlobalConfig()
  85. .getOptionBool("tabcompletion", "allowempty");
  86. if (partial.isEmpty() && !allowEmpty) {
  87. return result;
  88. }
  89. if (additionals != null) {
  90. targets.safeGet(TabCompletionType.ADDITIONAL).addAll(additionals);
  91. }
  92. for (Map.Entry<TabCompletionType, List<String>> typeEntry : targets.entrySet()) {
  93. if (additionals != null && !additionals.shouldInclude(typeEntry.getKey())) {
  94. // If we're not including this type, skip to the next.
  95. continue;
  96. }
  97. for (String entry : typeEntry.getValue()) {
  98. // Skip over duplicates
  99. if (result.hasResult(entry)) {
  100. continue;
  101. }
  102. if (caseSensitive && entry.startsWith(partial)) {
  103. result.addResult(entry);
  104. } else if (!caseSensitive && entry.toLowerCase(Locale.getDefault())
  105. .startsWith(partial.toLowerCase(Locale.getDefault()))) {
  106. result.addResult(entry);
  107. }
  108. }
  109. }
  110. if (parent != null) {
  111. if (additionals != null) {
  112. additionals.clear();
  113. }
  114. result.merge(parent.complete(partial, additionals));
  115. }
  116. return result;
  117. }
  118. /**
  119. * Adds a new entry to this tab completer's list.
  120. *
  121. * @param type The type of the entry that's being added
  122. * @param entry The new entry to be added
  123. */
  124. public void addEntry(final TabCompletionType type, final String entry) {
  125. entries.add(type, entry);
  126. if (type == TabCompletionType.COMMAND
  127. && entry.startsWith(String.valueOf(CommandManager.getCommandChar()))
  128. && !entry.startsWith(String.valueOf(CommandManager.getCommandChar())
  129. + CommandManager.getSilenceChar())) {
  130. // If we're adding a command name that doesn't include the silence
  131. // character, also add a version with the silence char
  132. addEntry(type, entry.substring(0, 1) + CommandManager.getSilenceChar()
  133. + entry.substring(1));
  134. }
  135. }
  136. /**
  137. * Adds multiple new entries to this tab completer's list.
  138. *
  139. * @param type The type of the entries that're being added
  140. * @param newEntries Entries to be added
  141. */
  142. public void addEntries(final TabCompletionType type, final List<String> newEntries) {
  143. if (newEntries == null) {
  144. return;
  145. }
  146. for (String entry : newEntries) {
  147. addEntry(type, entry);
  148. }
  149. }
  150. /**
  151. * Removes a specified entry from this tab completer's list.
  152. *
  153. * @param type The type of the entry that should be removed
  154. * @param entry The entry to be removed
  155. */
  156. public void removeEntry(final TabCompletionType type, final String entry) {
  157. entries.remove(type, entry);
  158. }
  159. /**
  160. * Replaces the current entries with the new list.
  161. *
  162. * @param type The type of entry which should be replaced
  163. * @param newEntries the new entries to use
  164. */
  165. public void replaceEntries(final TabCompletionType type, final List<String> newEntries) {
  166. entries.clear(type);
  167. entries.add(type, newEntries);
  168. }
  169. /**
  170. * Clears all entries in this tab completer.
  171. */
  172. public void clear() {
  173. entries.clear();
  174. }
  175. /**
  176. * Clears all entries of the specified type in this tab completer.
  177. *
  178. * @param type The type of entry to clear
  179. */
  180. public void clear(final TabCompletionType type) {
  181. entries.clear(type);
  182. }
  183. /**
  184. * Retrieves intelligent results for a deferred command.
  185. *
  186. * @param arg The argument number that is being requested
  187. * @param previousArgs The full list of previous arguments
  188. * @param offset The number of arguments our command used before deferring
  189. * to this method
  190. * @return Additional tab targets for the text, or null if none are available
  191. */
  192. public static AdditionalTabTargets getIntelligentResults(final int arg,
  193. final List<String> previousArgs, final int offset) {
  194. if (arg == offset) {
  195. final AdditionalTabTargets targets = new AdditionalTabTargets().excludeAll();
  196. targets.include(TabCompletionType.COMMAND);
  197. return targets;
  198. } else {
  199. return getIntelligentResults(
  200. new CommandArguments(previousArgs.subList(offset, previousArgs.size())));
  201. }
  202. }
  203. /**
  204. * Retrieves the intelligent results for the command and its arguments
  205. * formed from args.
  206. *
  207. * @param args The input arguments
  208. * @return Additional tab targets for the text, or null if none are available
  209. */
  210. private static AdditionalTabTargets getIntelligentResults(final CommandArguments args) {
  211. if (!args.isCommand()) {
  212. return null;
  213. }
  214. final Map.Entry<CommandInfo, Command> command
  215. = CommandManager.getCommand(args.getCommandName());
  216. AdditionalTabTargets targets = null;
  217. if (command != null) {
  218. if (command.getValue() instanceof IntelligentCommand) {
  219. targets = ((IntelligentCommand) command.getValue())
  220. .getSuggestions(args.getArguments().length,
  221. Arrays.asList(args.getArgumentsAsString()));
  222. }
  223. if (command.getValue() instanceof ChannelCommand) {
  224. if (targets == null) {
  225. targets = new AdditionalTabTargets();
  226. }
  227. targets.include(TabCompletionType.CHANNEL);
  228. }
  229. }
  230. return targets;
  231. }
  232. /**
  233. * Handles potentially intelligent tab completion.
  234. *
  235. * @param text The text that is being completed
  236. * @return Additional tab targets for the text, or null if none are available
  237. */
  238. public static AdditionalTabTargets getIntelligentResults(final String text) {
  239. return getIntelligentResults(new CommandArguments(text));
  240. }
  241. }