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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. * Copyright (c) 2006-2013 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.WritableFrameContainer;
  24. import com.dmdirc.ui.input.AdditionalTabTargets;
  25. import com.dmdirc.ui.input.TabCompleter;
  26. import com.dmdirc.ui.input.TabCompleterResult;
  27. import com.dmdirc.ui.input.tabstyles.TabCompletionResult;
  28. import com.dmdirc.ui.input.tabstyles.TabCompletionStyle;
  29. import java.awt.Toolkit;
  30. public class BashStyle implements TabCompletionStyle {
  31. /** The last position the user tab-completed at. */
  32. private int lastPosition = -1;
  33. /** The number of times the user has tab-completed the same position. */
  34. private int tabCount = 0;
  35. /** The last word that was tab completed. */
  36. private String lastWord = "";
  37. /** The tab completer that we use. */
  38. protected final TabCompleter tabCompleter;
  39. /** The input window that we use. */
  40. protected final WritableFrameContainer window;
  41. /**
  42. * Creates a new Bash-style tab completer.
  43. *
  44. * @param completer The tab completer this style is for
  45. * @param window The window this tab style is for
  46. */
  47. public BashStyle(final TabCompleter completer, final WritableFrameContainer window) {
  48. this.tabCompleter = completer;
  49. this.window = window;
  50. }
  51. /** {@inheritDoc} */
  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 TabCompleterResult 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 = res.getBestSubstring();
  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. }