Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

TabCompleterImpl.java 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 TabCompleterImpl implements 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 TabCompleterImpl}.
  44. *
  45. * @param configManager The manager to read config settings from.
  46. */
  47. public TabCompleterImpl(final AggregateConfigProvider configManager) {
  48. this.parent = null;
  49. this.configManager = configManager;
  50. }
  51. /**
  52. * Creates a new instance of {@link TabCompleterImpl}.
  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 TabCompleterImpl(
  58. final AggregateConfigProvider configManager,
  59. @Nullable final TabCompleter parent) {
  60. this.parent = parent;
  61. this.configManager = configManager;
  62. }
  63. @Override
  64. public List<String> complete(final String partial, @Nullable final AdditionalTabTargets additionals) {
  65. final List<String> result = new ArrayList<>();
  66. final boolean caseSensitive = configManager.getOptionBool("tabcompletion", "casesensitive");
  67. final boolean allowEmpty = configManager.getOptionBool("tabcompletion", "allowempty");
  68. if (partial.isEmpty() && !allowEmpty) {
  69. return result;
  70. }
  71. final Multimap<TabCompletionType, String> targets = ArrayListMultimap.create(entries);
  72. if (additionals != null) {
  73. targets.putAll(TabCompletionType.ADDITIONAL, additionals);
  74. }
  75. targets.keys().stream()
  76. // Filter out keys that aren't allowed by the additional argument (if present)
  77. .filter(k -> additionals == null || additionals.shouldInclude(k))
  78. // Select all values for the valid keys
  79. .flatMap(k -> targets.get(k).stream())
  80. // Filter out values that don't case sensitively match, if case sensitivity is on
  81. .filter(v -> !caseSensitive || v.startsWith(partial))
  82. // Filter out values that don't case INsensitively match, if case sensitivity is off
  83. .filter(v -> caseSensitive || v.toLowerCase().startsWith(partial.toLowerCase()))
  84. // Filter out duplicates
  85. .distinct()
  86. // Add them all to the result
  87. .forEach(result::add);
  88. if (parent != null) {
  89. if (additionals != null) {
  90. additionals.clear();
  91. }
  92. parent.complete(partial, additionals).stream()
  93. .filter(match -> !result.contains(match))
  94. .forEach(result::add);
  95. }
  96. return result;
  97. }
  98. @Override
  99. public void addEntry(final TabCompletionType type, final String entry) {
  100. entries.put(type, entry);
  101. }
  102. @Override
  103. public void addEntries(final TabCompletionType type, final Iterable<String> newEntries) {
  104. if (newEntries == null) {
  105. return;
  106. }
  107. for (String entry : newEntries) {
  108. addEntry(type, entry);
  109. }
  110. }
  111. @Override
  112. public void removeEntry(final TabCompletionType type, final String entry) {
  113. entries.remove(type, entry);
  114. }
  115. @Override
  116. public void clear() {
  117. entries.clear();
  118. }
  119. @Override
  120. public void clear(final TabCompletionType type) {
  121. entries.removeAll(type);
  122. }
  123. }