Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

TabCompleterImpl.java 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. * Copyright (c) 2006-2017 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
  5. * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
  6. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
  7. * permit persons to whom the Software is furnished to do so, subject to the following conditions:
  8. *
  9. * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
  10. * Software.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  13. * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
  14. * OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  15. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  16. */
  17. package com.dmdirc.ui.input;
  18. import com.dmdirc.config.provider.AggregateConfigProvider;
  19. import com.google.common.collect.ArrayListMultimap;
  20. import com.google.common.collect.Multimap;
  21. import javax.annotation.Nullable;
  22. import java.util.ArrayList;
  23. import java.util.List;
  24. /**
  25. * The tab completer handles a user's request to tab complete some word.
  26. */
  27. public class TabCompleterImpl implements TabCompleter {
  28. /**
  29. * The parent TabCompleter. Results from parents are merged with results from this completer.
  30. */
  31. @Nullable
  32. private final TabCompleter parent;
  33. /** The config manager to use for reading settings. */
  34. private final AggregateConfigProvider configManager;
  35. /** The entries in this completer. */
  36. private final Multimap<TabCompletionType, String> entries = ArrayListMultimap.create();
  37. /**
  38. * Creates a new instance of {@link TabCompleterImpl}.
  39. *
  40. * @param configManager The manager to read config settings from.
  41. */
  42. public TabCompleterImpl(final AggregateConfigProvider configManager) {
  43. this.parent = null;
  44. this.configManager = configManager;
  45. }
  46. /**
  47. * Creates a new instance of {@link TabCompleterImpl}.
  48. *
  49. * @param configManager The manager to read config settings from.
  50. * @param parent The parent tab completer to inherit completions from.
  51. */
  52. public TabCompleterImpl(
  53. final AggregateConfigProvider configManager,
  54. @Nullable final TabCompleter parent) {
  55. this.parent = parent;
  56. this.configManager = configManager;
  57. }
  58. @Override
  59. public List<String> complete(final String partial, @Nullable final AdditionalTabTargets additionals) {
  60. final List<String> result = new ArrayList<>();
  61. final boolean caseSensitive = configManager.getOptionBool("tabcompletion", "casesensitive");
  62. final boolean allowEmpty = configManager.getOptionBool("tabcompletion", "allowempty");
  63. if (partial.isEmpty() && !allowEmpty) {
  64. return result;
  65. }
  66. final Multimap<TabCompletionType, String> targets = ArrayListMultimap.create(entries);
  67. if (additionals != null) {
  68. targets.putAll(TabCompletionType.ADDITIONAL, additionals);
  69. }
  70. targets.keys().stream()
  71. // Filter out keys that aren't allowed by the additional argument (if present)
  72. .filter(k -> additionals == null || additionals.shouldInclude(k))
  73. // Select all values for the valid keys
  74. .flatMap(k -> targets.get(k).stream())
  75. // Filter out values that don't case sensitively match, if case sensitivity is on
  76. .filter(v -> !caseSensitive || v.startsWith(partial))
  77. // Filter out values that don't case INsensitively match, if case sensitivity is off
  78. .filter(v -> caseSensitive || v.toLowerCase().startsWith(partial.toLowerCase()))
  79. // Filter out duplicates
  80. .distinct()
  81. // Add them all to the result
  82. .forEach(result::add);
  83. if (parent != null) {
  84. if (additionals != null) {
  85. additionals.clear();
  86. }
  87. parent.complete(partial, additionals).stream()
  88. .filter(match -> !result.contains(match))
  89. .forEach(result::add);
  90. }
  91. return result;
  92. }
  93. @Override
  94. public void addEntry(final TabCompletionType type, final String entry) {
  95. entries.put(type, entry);
  96. }
  97. @Override
  98. public void addEntries(final TabCompletionType type, final Iterable<String> newEntries) {
  99. if (newEntries == null) {
  100. return;
  101. }
  102. for (String entry : newEntries) {
  103. addEntry(type, entry);
  104. }
  105. }
  106. @Override
  107. public void removeEntry(final TabCompletionType type, final String entry) {
  108. entries.remove(type, entry);
  109. }
  110. @Override
  111. public void clear() {
  112. entries.clear();
  113. }
  114. @Override
  115. public void clear(final TabCompletionType type) {
  116. entries.removeAll(type);
  117. }
  118. }