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

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