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.

TabCompleterUtils.java 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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.interfaces.CommandController;
  30. import java.util.Arrays;
  31. import java.util.Map;
  32. import javax.annotation.Nullable;
  33. import javax.inject.Inject;
  34. import javax.inject.Singleton;
  35. /**
  36. * Utilities relating to {@link TabCompleter}s.
  37. */
  38. @Singleton
  39. public class TabCompleterUtils {
  40. private final CommandController commandController;
  41. @Inject
  42. public TabCompleterUtils(final CommandController commandController) {
  43. this.commandController = commandController;
  44. }
  45. /**
  46. * Retrieves intelligent results for a deferred command.
  47. *
  48. * @param arg The argument number that is being requested
  49. * @param context Intelligent tab completion context
  50. * @param offset The number of arguments our command used before deferring to this method
  51. *
  52. * @return Additional tab targets for the text, or null if none are available
  53. */
  54. @Nullable
  55. public AdditionalTabTargets getIntelligentResults(final int arg,
  56. final IntelligentCommand.IntelligentCommandContext context, final int offset) {
  57. if (arg == offset) {
  58. final AdditionalTabTargets targets = new AdditionalTabTargets().excludeAll();
  59. targets.include(TabCompletionType.COMMAND);
  60. return targets;
  61. } else {
  62. return getIntelligentResults(context.getWindow(),
  63. new CommandArguments(commandController,
  64. context.getPreviousArgs().subList(offset,
  65. context.getPreviousArgs().size())), context.getPartial());
  66. }
  67. }
  68. /**
  69. * Retrieves the intelligent results for the command and its arguments formed from args.
  70. *
  71. * @param window The input window the results are required for
  72. * @param args The input arguments
  73. * @param partial The partially-typed word being completed (if any)
  74. *
  75. * @return Additional tab targets for the text, or null if none are available
  76. *
  77. * @since 0.6.4
  78. */
  79. @Nullable
  80. private AdditionalTabTargets getIntelligentResults(
  81. final FrameContainer window,
  82. final CommandArguments args, final String partial) {
  83. if (!args.isCommand()) {
  84. return null;
  85. }
  86. final Map.Entry<CommandInfo, Command> command =
  87. commandController.getCommand(args.getCommandName());
  88. AdditionalTabTargets targets = null;
  89. if (command != null) {
  90. if (command.getValue() instanceof IntelligentCommand) {
  91. targets = ((IntelligentCommand) command.getValue())
  92. .getSuggestions(args.getArguments().length,
  93. new IntelligentCommand.IntelligentCommandContext(window,
  94. Arrays.asList(args.getArguments()), partial));
  95. }
  96. if (command.getKey().getType() == CommandType.TYPE_CHANNEL) {
  97. if (targets == null) {
  98. targets = new AdditionalTabTargets();
  99. }
  100. targets.include(TabCompletionType.CHANNEL);
  101. }
  102. }
  103. return targets;
  104. }
  105. /**
  106. * Handles potentially intelligent tab completion.
  107. *
  108. * @param window The input window the results are required for
  109. * @param text The text that is being completed
  110. * @param partial The partially-typed word being completed (if any)
  111. *
  112. * @return Additional tab targets for the text, or null if none are available
  113. *
  114. * @since 0.6.4
  115. */
  116. @Nullable
  117. public AdditionalTabTargets getIntelligentResults(
  118. final FrameContainer window, final CommandController commandController,
  119. final String text, final String partial) {
  120. return getIntelligentResults(window,
  121. new CommandArguments(commandController, text), partial);
  122. }
  123. }