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 10.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /*
  2. * Copyright (c) 2006-2011 DMDirc Developers
  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.WritableFrameContainer;
  24. import com.dmdirc.commandparser.CommandArguments;
  25. import com.dmdirc.commandparser.CommandInfo;
  26. import com.dmdirc.commandparser.CommandManager;
  27. import com.dmdirc.commandparser.CommandType;
  28. import com.dmdirc.commandparser.commands.Command;
  29. import com.dmdirc.commandparser.commands.IntelligentCommand;
  30. import com.dmdirc.commandparser.commands.IntelligentCommand.IntelligentCommandContext;
  31. import com.dmdirc.config.IdentityManager;
  32. import com.dmdirc.util.collections.MapList;
  33. import java.util.Arrays;
  34. import java.util.List;
  35. import java.util.Locale;
  36. import java.util.Map;
  37. /**
  38. * The tab completer handles a user's request to tab complete some word.
  39. */
  40. public class TabCompleter {
  41. /**
  42. * The parent TabCompleter. Results from parents are merged with results
  43. * from this completer.
  44. */
  45. private TabCompleter parent;
  46. /**
  47. * The entries in this completer.
  48. */
  49. private final MapList<TabCompletionType, String> entries
  50. = new MapList<TabCompletionType, String>();
  51. /** Creates a new instance of TabCompleter. */
  52. public TabCompleter() {
  53. //Do nothing.
  54. }
  55. /**
  56. * Creates a new instance of TabCompleter, with a designated parent.
  57. * @param newParent The parent TabCompleter, which is checked for matches if
  58. * local ones fail
  59. */
  60. public TabCompleter(final TabCompleter newParent) {
  61. this.parent = newParent;
  62. }
  63. /**
  64. * Attempts to complete the partial string.
  65. *
  66. * @param partial The string to tab complete
  67. * @param additionals A list of additional strings to use
  68. * @return A TabCompleterResult containing any matches found
  69. */
  70. public TabCompleterResult complete(final String partial,
  71. final AdditionalTabTargets additionals) {
  72. final TabCompleterResult result = new TabCompleterResult();
  73. final MapList<TabCompletionType, String> targets
  74. = new MapList<TabCompletionType, String>(entries);
  75. final boolean caseSensitive = IdentityManager.getGlobalConfig()
  76. .getOptionBool("tabcompletion", "casesensitive");
  77. final boolean allowEmpty = IdentityManager.getGlobalConfig()
  78. .getOptionBool("tabcompletion", "allowempty");
  79. if (partial.isEmpty() && !allowEmpty) {
  80. return result;
  81. }
  82. if (additionals != null) {
  83. targets.safeGet(TabCompletionType.ADDITIONAL).addAll(additionals);
  84. }
  85. for (Map.Entry<TabCompletionType, List<String>> typeEntry : targets.entrySet()) {
  86. if (additionals != null && !additionals.shouldInclude(typeEntry.getKey())) {
  87. // If we're not including this type, skip to the next.
  88. continue;
  89. }
  90. for (String entry : typeEntry.getValue()) {
  91. // Skip over duplicates
  92. if (result.hasResult(entry)) {
  93. continue;
  94. }
  95. if (caseSensitive && entry.startsWith(partial)) {
  96. result.addResult(entry);
  97. } else if (!caseSensitive && entry.toLowerCase(Locale.getDefault())
  98. .startsWith(partial.toLowerCase(Locale.getDefault()))) {
  99. result.addResult(entry);
  100. }
  101. }
  102. }
  103. if (parent != null) {
  104. if (additionals != null) {
  105. additionals.clear();
  106. }
  107. result.merge(parent.complete(partial, additionals));
  108. }
  109. return result;
  110. }
  111. /**
  112. * Adds a new entry to this tab completer's list.
  113. *
  114. * @param type The type of the entry that's being added
  115. * @param entry The new entry to be added
  116. */
  117. public void addEntry(final TabCompletionType type, final String entry) {
  118. entries.add(type, entry);
  119. if (type == TabCompletionType.COMMAND && entry.length() > 1
  120. && entry.charAt(0) == CommandManager.getCommandManager().getCommandChar()
  121. && entry.charAt(1) != CommandManager.getCommandManager().getSilenceChar()) {
  122. // If we're adding a command name that doesn't include the silence
  123. // character, also add a version with the silence char
  124. addEntry(type, entry.substring(0, 1) + CommandManager.getCommandManager().getSilenceChar()
  125. + entry.substring(1));
  126. }
  127. }
  128. /**
  129. * Adds multiple new entries to this tab completer's list.
  130. *
  131. * @param type The type of the entries that're being added
  132. * @param newEntries Entries to be added
  133. */
  134. public void addEntries(final TabCompletionType type, final List<String> newEntries) {
  135. if (newEntries == null) {
  136. return;
  137. }
  138. for (String entry : newEntries) {
  139. addEntry(type, entry);
  140. }
  141. }
  142. /**
  143. * Removes a specified entry from this tab completer's list.
  144. *
  145. * @param type The type of the entry that should be removed
  146. * @param entry The entry to be removed
  147. */
  148. public void removeEntry(final TabCompletionType type, final String entry) {
  149. entries.remove(type, entry);
  150. }
  151. /**
  152. * Replaces the current entries with the new list.
  153. *
  154. * @param type The type of entry which should be replaced
  155. * @param newEntries the new entries to use
  156. */
  157. public void replaceEntries(final TabCompletionType type, final List<String> newEntries) {
  158. entries.clear(type);
  159. entries.add(type, newEntries);
  160. }
  161. /**
  162. * Clears all entries in this tab completer.
  163. */
  164. public void clear() {
  165. entries.clear();
  166. }
  167. /**
  168. * Clears all entries of the specified type in this tab completer.
  169. *
  170. * @param type The type of entry to clear
  171. */
  172. public void clear(final TabCompletionType type) {
  173. entries.clear(type);
  174. }
  175. /**
  176. * Retrieves intelligent results for a deferred command.
  177. *
  178. * @param arg The argument number that is being requested
  179. * @param context Intelligent tab completion context
  180. * @param offset The number of arguments our command used before deferring
  181. * to this method
  182. * @return Additional tab targets for the text, or null if none are available
  183. */
  184. public static AdditionalTabTargets getIntelligentResults(final int arg,
  185. final IntelligentCommandContext context, final int offset) {
  186. if (arg == offset) {
  187. final AdditionalTabTargets targets = new AdditionalTabTargets().excludeAll();
  188. targets.include(TabCompletionType.COMMAND);
  189. return targets;
  190. } else {
  191. return getIntelligentResults(context.getWindow(),
  192. new CommandArguments(context.getPreviousArgs().subList(offset,
  193. context.getPreviousArgs().size())), context.getPartial());
  194. }
  195. }
  196. /**
  197. * Retrieves the intelligent results for the command and its arguments
  198. * formed from args.
  199. *
  200. * @param window The input window the results are required for
  201. * @param args The input arguments
  202. * @param partial The partially-typed word being completed (if any)
  203. * @return Additional tab targets for the text, or null if none are available
  204. * @since 0.6.4
  205. */
  206. private static AdditionalTabTargets getIntelligentResults(
  207. final WritableFrameContainer window,
  208. final CommandArguments args, final String partial) {
  209. if (!args.isCommand()) {
  210. return null;
  211. }
  212. final Map.Entry<CommandInfo, Command> command
  213. = CommandManager.getCommandManager().getCommand(args.getCommandName());
  214. AdditionalTabTargets targets = null;
  215. if (command != null) {
  216. if (command.getValue() instanceof IntelligentCommand) {
  217. targets = ((IntelligentCommand) command.getValue())
  218. .getSuggestions(args.getArguments().length,
  219. new IntelligentCommandContext(window,
  220. Arrays.asList(args.getArguments()), partial));
  221. }
  222. if (command.getKey().getType().equals(CommandType.TYPE_CHANNEL)) {
  223. if (targets == null) {
  224. targets = new AdditionalTabTargets();
  225. }
  226. targets.include(TabCompletionType.CHANNEL);
  227. }
  228. }
  229. return targets;
  230. }
  231. /**
  232. * Handles potentially intelligent tab completion.
  233. *
  234. * @param window The input window the results are required for
  235. * @param text The text that is being completed
  236. * @param partial The partially-typed word being completed (if any)
  237. * @return Additional tab targets for the text, or null if none are available
  238. * @since 0.6.4
  239. */
  240. public static AdditionalTabTargets getIntelligentResults(
  241. final WritableFrameContainer window, final String text,
  242. final String partial) {
  243. return getIntelligentResults(window, new CommandArguments(text), partial);
  244. }
  245. }