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

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