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.1KB

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