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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*
  2. * Copyright (c) 2006-2015 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.interfaces.config.AggregateConfigProvider;
  24. import com.google.common.collect.ArrayListMultimap;
  25. import com.google.common.collect.Multimap;
  26. import javax.annotation.Nullable;
  27. /**
  28. * The tab completer handles a user's request to tab complete some word.
  29. */
  30. public class TabCompleter {
  31. /**
  32. * The parent TabCompleter. Results from parents are merged with results from this completer.
  33. */
  34. @Nullable
  35. private final TabCompleter parent;
  36. /** The config manager to use for reading settings. */
  37. private final AggregateConfigProvider configManager;
  38. /** The entries in this completer. */
  39. private final Multimap<TabCompletionType, String> entries = ArrayListMultimap.create();
  40. /**
  41. * Creates a new instance of {@link TabCompleter}.
  42. *
  43. * @param configManager The manager to read config settings from.
  44. */
  45. public TabCompleter(final AggregateConfigProvider configManager) {
  46. this.parent = null;
  47. this.configManager = configManager;
  48. }
  49. /**
  50. * Creates a new instance of {@link TabCompleter}.
  51. *
  52. * @param configManager The manager to read config settings from.
  53. * @param parent The parent tab completer to inherit completions from.
  54. */
  55. public TabCompleter(
  56. final AggregateConfigProvider configManager,
  57. @Nullable final TabCompleter parent) {
  58. this.parent = parent;
  59. this.configManager = configManager;
  60. }
  61. /**
  62. * Attempts to complete the partial string.
  63. *
  64. * @param partial The string to tab complete
  65. * @param additionals A list of additional strings to use
  66. *
  67. * @return A TabCompleterResult containing any matches found
  68. */
  69. public TabCompletionMatches complete(final String partial,
  70. @Nullable final AdditionalTabTargets additionals) {
  71. final TabCompletionMatches result = new TabCompletionMatches();
  72. final boolean caseSensitive = configManager.getOptionBool("tabcompletion", "casesensitive");
  73. final boolean allowEmpty = configManager.getOptionBool("tabcompletion", "allowempty");
  74. if (partial.isEmpty() && !allowEmpty) {
  75. return result;
  76. }
  77. final Multimap<TabCompletionType, String> targets = ArrayListMultimap.create(entries);
  78. if (additionals != null) {
  79. targets.putAll(TabCompletionType.ADDITIONAL, additionals);
  80. }
  81. targets.keys().stream()
  82. // Filter out keys that aren't allowed by the additional argument (if present)
  83. .filter(k -> additionals == null || additionals.shouldInclude(k))
  84. // Select all values for the valid keys
  85. .flatMap(k -> targets.get(k).stream())
  86. // Filter out values that don't case sensitively match, if case sensitivity is on
  87. .filter(v -> !caseSensitive || v.startsWith(partial))
  88. // Filter out values that don't case INsensitively match, if case sensitivity is off
  89. .filter(v -> caseSensitive || v.toLowerCase().startsWith(partial.toLowerCase()))
  90. // Filter out duplicates
  91. .distinct()
  92. // Add them all to the result
  93. .forEach(result::addResult);
  94. if (parent != null) {
  95. if (additionals != null) {
  96. additionals.clear();
  97. }
  98. result.merge(parent.complete(partial, additionals));
  99. }
  100. return result;
  101. }
  102. /**
  103. * Adds a new entry to this tab completer's list.
  104. *
  105. * @param type The type of the entry that's being added
  106. * @param entry The new entry to be added
  107. */
  108. public void addEntry(final TabCompletionType type, final String entry) {
  109. entries.put(type, entry);
  110. }
  111. /**
  112. * Adds multiple new entries to this tab completer's list.
  113. *
  114. * @param type The type of the entries that're being added
  115. * @param newEntries Entries to be added
  116. */
  117. public void addEntries(final TabCompletionType type, final Iterable<String> newEntries) {
  118. if (newEntries == null) {
  119. return;
  120. }
  121. for (String entry : newEntries) {
  122. addEntry(type, entry);
  123. }
  124. }
  125. /**
  126. * Removes a specified entry from this tab completer's list.
  127. *
  128. * @param type The type of the entry that should be removed
  129. * @param entry The entry to be removed
  130. */
  131. public void removeEntry(final TabCompletionType type, final String entry) {
  132. entries.remove(type, entry);
  133. }
  134. /**
  135. * Clears all entries in this tab completer.
  136. */
  137. public void clear() {
  138. entries.clear();
  139. }
  140. /**
  141. * Clears all entries of the specified type in this tab completer.
  142. *
  143. * @param type The type of entry to clear
  144. */
  145. public void clear(final TabCompletionType type) {
  146. entries.removeAll(type);
  147. }
  148. }