File Source: BashStyle.java
/*
P/P * Method: com.dmdirc.addons.tabcompletion_bash.BashStyle__static_init
*/
1 /*
2 * Copyright (c) 2006-2009 Chris Smith, Shane Mc Cormack, Gregory Holmes
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
23 package com.dmdirc.addons.tabcompletion_bash;
24
25 import com.dmdirc.ui.input.AdditionalTabTargets;
26 import com.dmdirc.ui.input.TabCompleter;
27 import com.dmdirc.ui.input.TabCompleterResult;
28 import com.dmdirc.ui.input.tabstyles.TabCompletionResult;
29 import com.dmdirc.ui.input.tabstyles.TabCompletionStyle;
30 import com.dmdirc.ui.interfaces.InputWindow;
31
32 import java.awt.Toolkit;
33
34 public class BashStyle implements TabCompletionStyle {
35
36 /** The last position the user tab-completed at. */
37 private int lastPosition = -1;
38
39 /** The number of times the user has tab-completed the same position. */
40 private int tabCount = 0;
41
42 /** The last word that was tab completed. */
43 private String lastWord = "";
44
45 /** The tab completer that we use. */
46 protected final TabCompleter tabCompleter;
47
48 /** The input window that we use. */
49 protected final InputWindow window;
50
51 /**
52 * Creates a new Bash-style tab completer.
53 *
54 * @param completer The tab completer this style is for
55 * @param window The window this tab style is for
56 */
/*
P/P * Method: void com.dmdirc.addons.tabcompletion_bash.BashStyle(TabCompleter, InputWindow)
*
* Postconditions:
* this.lastPosition == -1
* this.lastWord == &""
* this.tabCompleter == completer
* init'ed(this.tabCompleter)
* this.tabCount == 0
* this.window == window
* init'ed(this.window)
*/
57 public BashStyle(final TabCompleter completer, final InputWindow window) {
58 this.tabCompleter = completer;
59 this.window = window;
60 }
61
62 /** {@inheritDoc} */
63 @Override
64 public TabCompletionResult getResult(final String original, final int start,
65 final int end, final AdditionalTabTargets additional) {
/*
P/P * Method: TabCompletionResult getResult(String, int, int, AdditionalTabTargets)
*
* Preconditions:
* init'ed(this.lastPosition)
* original != null
* this.tabCompleter != null
* (soft) init'ed(this.lastWord)
* (soft) this.tabCount <= 232-2
* (soft) this.window != null
*
* Presumptions:
* com.dmdirc.ui.input.TabCompleter:complete(...)@67 != null
* com.dmdirc.ui.input.TabCompleterResult:getBestSubstring(...)@92 != null
* com.dmdirc.ui.input.TabCompleterResult:getResults(...)@84 != null
* java.awt.Toolkit:getDefaultToolkit(...)@78 != null
* java.util.List:get(...)@84 != null
* ...
*
* Postconditions:
* java.lang.String:substring(...)._tainted == original._tainted
* init'ed(java.lang.String:substring(...)._tainted)
* return_value in Addr_Set{null,&new TabCompletionResult(getResult#4),&new TabCompletionResult(getResult#1)}
* this.lastPosition == One-of{old this.lastPosition, start}
* init'ed(this.lastPosition)
* this.lastWord == One-of{old this.lastWord, &java.lang.String:substring(...)}
* init'ed(this.lastWord)
* this.tabCount == One-of{old this.tabCount + 1, 1}
* this.tabCount >= -231+1
* new TabCompletionResult(getResult#1) num objects <= 1
* ...
*
* Test Vectors:
* this.lastPosition - start: {-6_442_450_943..-1, 1..6_442_450_943}, {0}
* com.dmdirc.ui.input.TabCompleterResult:getResultCount(...)@77: {-231..-1, 1..232-1}, {0}
* com.dmdirc.ui.input.TabCompleterResult:getResultCount(...)@81: {-231..0, 2..232-1}, {1}
* java.lang.String:equals(...)@69: {0}, {1}
* java.lang.String:equalsIgnoreCase(...)@93: {0}, {1}
*/
66 final String word = original.substring(start, end);
67 final TabCompleterResult res = tabCompleter.complete(word, additional);
68
69 if (start == lastPosition && word.equals(lastWord)) {
70 tabCount++;
71 } else {
72 lastPosition = start;
73 lastWord = word;
74 tabCount = 1;
75 }
76
77 if (res.getResultCount() == 0) {
78 Toolkit.getDefaultToolkit().beep();
79
80 return null;
81 } else if (res.getResultCount() == 1) {
82 // One result, just replace it
83
84 final String result = res.getResults().get(0);
85
86 return new TabCompletionResult(
87 original.substring(0, start) + result + original.substring(end),
88 start + result.length());
89 } else {
90 // Multiple results
91
92 final String sub = res.getBestSubstring();
93 if (sub.equalsIgnoreCase(word) && tabCount >= 2) {
94 window.addLine("tabCompletion", res.toString());
95
96 return null;
97 } else {
98 return new TabCompletionResult(
99 original.substring(0, start) + sub + original.substring(end),
100 start + sub.length());
101 }
102 }
103 }
104
105 }
SofCheck Inspector Build Version : 2.17854
| BashStyle.java |
2009-Jun-25 01:54:24 |
| BashStyle.class |
2009-Sep-02 17:04:15 |