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.

BashStyle.java 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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.addons.tabcompletion_bash;
  23. import com.dmdirc.interfaces.WindowModel;
  24. import com.dmdirc.ui.input.AdditionalTabTargets;
  25. import com.dmdirc.ui.input.TabCompleter;
  26. import com.dmdirc.ui.input.TabCompletionMatches;
  27. import com.dmdirc.ui.input.tabstyles.TabCompletionResult;
  28. import com.dmdirc.ui.input.tabstyles.TabCompletionStyle;
  29. import java.awt.Toolkit;
  30. import java.util.Locale;
  31. public class BashStyle implements TabCompletionStyle {
  32. /** The last position the user tab-completed at. */
  33. private int lastPosition = -1;
  34. /** The number of times the user has tab-completed the same position. */
  35. private int tabCount = 0;
  36. /** The last word that was tab completed. */
  37. private String lastWord = "";
  38. /** The tab completer that we use. */
  39. protected final TabCompleter tabCompleter;
  40. /** The input window that we use. */
  41. protected final WindowModel window;
  42. /**
  43. * Creates a new Bash-style tab completer.
  44. *
  45. * @param completer The tab completer this style is for
  46. * @param window The window this tab style is for
  47. */
  48. public BashStyle(final TabCompleter completer, final WindowModel window) {
  49. this.tabCompleter = completer;
  50. this.window = window;
  51. }
  52. @Override
  53. public TabCompletionResult getResult(final String original, final int start,
  54. final int end, final boolean shiftPressed,
  55. final AdditionalTabTargets additional) {
  56. final String word = original.substring(start, end);
  57. final TabCompletionMatches res = tabCompleter.complete(word, additional);
  58. if (start == lastPosition && word.equals(lastWord)) {
  59. tabCount++;
  60. } else {
  61. lastPosition = start;
  62. lastWord = word;
  63. tabCount = 1;
  64. }
  65. if (res.getResultCount() == 0) {
  66. Toolkit.getDefaultToolkit().beep();
  67. return null;
  68. } else if (res.getResultCount() == 1) {
  69. // One result, just replace it
  70. final String result = res.getResults().get(0);
  71. return new TabCompletionResult(
  72. original.substring(0, start) + result + original.substring(end),
  73. start + result.length());
  74. } else {
  75. // Multiple results
  76. final String sub = getBestSubstring(res);
  77. if (sub.equalsIgnoreCase(word) && tabCount >= 2) {
  78. window.addLine("tabCompletion", res.toString());
  79. return null;
  80. } else {
  81. return new TabCompletionResult(
  82. original.substring(0, start) + sub + original.substring(end),
  83. start + sub.length());
  84. }
  85. }
  86. }
  87. /**
  88. * Returns the longest substring that matches all results.
  89. *
  90. * @return longest possible substring matching all results
  91. */
  92. private String getBestSubstring(final TabCompletionMatches res) {
  93. if (res.getResultCount() == 0) {
  94. return "";
  95. }
  96. final boolean caseSensitive = window.getConfigManager()
  97. .getOptionBool("tabcompletion", "casesensitive");
  98. String substring = res.getResults().get(0);
  99. for (String entry : res.getResults()) {
  100. if (caseSensitive) {
  101. while (!entry.startsWith(substring)) {
  102. substring = substring.substring(0, substring.length() - 1);
  103. }
  104. } else {
  105. while (!entry.toLowerCase(Locale.getDefault()).startsWith(
  106. substring.toLowerCase(Locale.getDefault()))) {
  107. substring = substring.substring(0, substring.length() - 1);
  108. }
  109. }
  110. }
  111. return substring;
  112. }
  113. }