//# 0 errors, 86 messages
//#
/*
    //#BashStyle.java:1:1: class: com.dmdirc.addons.tabcompletion_bash.BashStyle
    //#BashStyle.java:1:1: method: com.dmdirc.addons.tabcompletion_bash.BashStyle.com.dmdirc.addons.tabcompletion_bash.BashStyle__static_init
 * Copyright (c) 2006-2009 Chris Smith, Shane Mc Cormack, Gregory Holmes
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package com.dmdirc.addons.tabcompletion_bash;

import com.dmdirc.ui.input.AdditionalTabTargets;
import com.dmdirc.ui.input.TabCompleter;
import com.dmdirc.ui.input.TabCompleterResult;
import com.dmdirc.ui.input.tabstyles.TabCompletionResult;
import com.dmdirc.ui.input.tabstyles.TabCompletionStyle;
import com.dmdirc.ui.interfaces.InputWindow;

import java.awt.Toolkit;

public class BashStyle implements TabCompletionStyle {
    
    /** The last position the user tab-completed at. */
    private int lastPosition = -1;
    
    /** The number of times the user has tab-completed the same position. */
    private int tabCount = 0;
    
    /** The last word that was tab completed. */
    private String lastWord = "";

    /** The tab completer that we use. */
    protected final TabCompleter tabCompleter;

    /** The input window that we use. */
    protected final InputWindow window;

    /**
     * Creates a new Bash-style tab completer.
     *
     * @param completer The tab completer this style is for
     * @param window The window this tab style is for
     */
    public BashStyle(final TabCompleter completer, final InputWindow window) {
    //#BashStyle.java:57: method: void com.dmdirc.addons.tabcompletion_bash.BashStyle.com.dmdirc.addons.tabcompletion_bash.BashStyle(TabCompleter, InputWindow)
    //#input(void com.dmdirc.addons.tabcompletion_bash.BashStyle(TabCompleter, InputWindow)): completer
    //#input(void com.dmdirc.addons.tabcompletion_bash.BashStyle(TabCompleter, InputWindow)): this
    //#input(void com.dmdirc.addons.tabcompletion_bash.BashStyle(TabCompleter, InputWindow)): window
    //#output(void com.dmdirc.addons.tabcompletion_bash.BashStyle(TabCompleter, InputWindow)): this.lastPosition
    //#output(void com.dmdirc.addons.tabcompletion_bash.BashStyle(TabCompleter, InputWindow)): this.lastWord
    //#output(void com.dmdirc.addons.tabcompletion_bash.BashStyle(TabCompleter, InputWindow)): this.tabCompleter
    //#output(void com.dmdirc.addons.tabcompletion_bash.BashStyle(TabCompleter, InputWindow)): this.tabCount
    //#output(void com.dmdirc.addons.tabcompletion_bash.BashStyle(TabCompleter, InputWindow)): this.window
    //#post(void com.dmdirc.addons.tabcompletion_bash.BashStyle(TabCompleter, InputWindow)): this.lastPosition == -1
    //#post(void com.dmdirc.addons.tabcompletion_bash.BashStyle(TabCompleter, InputWindow)): this.lastWord == &""
    //#post(void com.dmdirc.addons.tabcompletion_bash.BashStyle(TabCompleter, InputWindow)): this.tabCompleter == completer
    //#post(void com.dmdirc.addons.tabcompletion_bash.BashStyle(TabCompleter, InputWindow)): init'ed(this.tabCompleter)
    //#post(void com.dmdirc.addons.tabcompletion_bash.BashStyle(TabCompleter, InputWindow)): this.tabCount == 0
    //#post(void com.dmdirc.addons.tabcompletion_bash.BashStyle(TabCompleter, InputWindow)): this.window == window
    //#post(void com.dmdirc.addons.tabcompletion_bash.BashStyle(TabCompleter, InputWindow)): init'ed(this.window)
        this.tabCompleter = completer;
        this.window = window;
    }
    //#BashStyle.java:60: end of method: void com.dmdirc.addons.tabcompletion_bash.BashStyle.com.dmdirc.addons.tabcompletion_bash.BashStyle(TabCompleter, InputWindow)
    
    /** {@inheritDoc} */
    @Override
    public TabCompletionResult getResult(final String original, final int start,
            final int end, final AdditionalTabTargets additional) {
        final String word = original.substring(start, end);
    //#BashStyle.java:66: method: TabCompletionResult com.dmdirc.addons.tabcompletion_bash.BashStyle.getResult(String, int, int, AdditionalTabTargets)
    //#input(TabCompletionResult getResult(String, int, int, AdditionalTabTargets)): additional
    //#input(TabCompletionResult getResult(String, int, int, AdditionalTabTargets)): end
    //#input(TabCompletionResult getResult(String, int, int, AdditionalTabTargets)): original
    //#input(TabCompletionResult getResult(String, int, int, AdditionalTabTargets)): original._tainted
    //#input(TabCompletionResult getResult(String, int, int, AdditionalTabTargets)): start
    //#input(TabCompletionResult getResult(String, int, int, AdditionalTabTargets)): this
    //#input(TabCompletionResult getResult(String, int, int, AdditionalTabTargets)): this.lastPosition
    //#input(TabCompletionResult getResult(String, int, int, AdditionalTabTargets)): this.lastWord
    //#input(TabCompletionResult getResult(String, int, int, AdditionalTabTargets)): this.tabCompleter
    //#input(TabCompletionResult getResult(String, int, int, AdditionalTabTargets)): this.tabCount
    //#input(TabCompletionResult getResult(String, int, int, AdditionalTabTargets)): this.window
    //#output(TabCompletionResult getResult(String, int, int, AdditionalTabTargets)): java.lang.String:substring(...)._tainted
    //#output(TabCompletionResult getResult(String, int, int, AdditionalTabTargets)): new TabCompletionResult(getResult#1) num objects
    //#output(TabCompletionResult getResult(String, int, int, AdditionalTabTargets)): new TabCompletionResult(getResult#4) num objects
    //#output(TabCompletionResult getResult(String, int, int, AdditionalTabTargets)): return_value
    //#output(TabCompletionResult getResult(String, int, int, AdditionalTabTargets)): this.lastPosition
    //#output(TabCompletionResult getResult(String, int, int, AdditionalTabTargets)): this.lastWord
    //#output(TabCompletionResult getResult(String, int, int, AdditionalTabTargets)): this.tabCount
    //#new obj(TabCompletionResult getResult(String, int, int, AdditionalTabTargets)): java.lang.String:substring(...)
    //#new obj(TabCompletionResult getResult(String, int, int, AdditionalTabTargets)): new TabCompletionResult(getResult#1)
    //#new obj(TabCompletionResult getResult(String, int, int, AdditionalTabTargets)): new TabCompletionResult(getResult#4)
    //#pre[3] (TabCompletionResult getResult(String, int, int, AdditionalTabTargets)): init'ed(this.lastPosition)
    //#pre[7] (TabCompletionResult getResult(String, int, int, AdditionalTabTargets)): original != null
    //#pre[11] (TabCompletionResult getResult(String, int, int, AdditionalTabTargets)): this.tabCompleter != null
    //#pre[5] (TabCompletionResult getResult(String, int, int, AdditionalTabTargets)): (soft) init'ed(this.lastWord)
    //#pre[6] (TabCompletionResult getResult(String, int, int, AdditionalTabTargets)): (soft) this.tabCount <= 4_294_967_294
    //#pre[12] (TabCompletionResult getResult(String, int, int, AdditionalTabTargets)): (soft) this.window != null
    //#presumption(TabCompletionResult getResult(String, int, int, AdditionalTabTargets)): com.dmdirc.ui.input.TabCompleter:complete(...)@67 != null
    //#presumption(TabCompletionResult getResult(String, int, int, AdditionalTabTargets)): com.dmdirc.ui.input.TabCompleterResult:getBestSubstring(...)@92 != null
    //#presumption(TabCompletionResult getResult(String, int, int, AdditionalTabTargets)): com.dmdirc.ui.input.TabCompleterResult:getResults(...)@84 != null
    //#presumption(TabCompletionResult getResult(String, int, int, AdditionalTabTargets)): java.awt.Toolkit:getDefaultToolkit(...)@78 != null
    //#presumption(TabCompletionResult getResult(String, int, int, AdditionalTabTargets)): java.util.List:get(...)@84 != null
    //#presumption(TabCompletionResult getResult(String, int, int, AdditionalTabTargets)): start + java.lang.String:length(...)@86 in {-2_147_483_648..4_294_967_295}
    //#presumption(TabCompletionResult getResult(String, int, int, AdditionalTabTargets)): start + java.lang.String:length(...)@98 in {-2_147_483_648..4_294_967_295}
    //#post(TabCompletionResult getResult(String, int, int, AdditionalTabTargets)): java.lang.String:substring(...)._tainted == original._tainted
    //#post(TabCompletionResult getResult(String, int, int, AdditionalTabTargets)): init'ed(java.lang.String:substring(...)._tainted)
    //#post(TabCompletionResult getResult(String, int, int, AdditionalTabTargets)): return_value in Addr_Set{null,&new TabCompletionResult(getResult#4),&new TabCompletionResult(getResult#1)}
    //#post(TabCompletionResult getResult(String, int, int, AdditionalTabTargets)): this.lastPosition == One-of{old this.lastPosition, start}
    //#post(TabCompletionResult getResult(String, int, int, AdditionalTabTargets)): init'ed(this.lastPosition)
    //#post(TabCompletionResult getResult(String, int, int, AdditionalTabTargets)): this.lastWord == One-of{old this.lastWord, &java.lang.String:substring(...)}
    //#post(TabCompletionResult getResult(String, int, int, AdditionalTabTargets)): init'ed(this.lastWord)
    //#post(TabCompletionResult getResult(String, int, int, AdditionalTabTargets)): this.tabCount == One-of{old this.tabCount + 1, 1}
    //#post(TabCompletionResult getResult(String, int, int, AdditionalTabTargets)): this.tabCount >= -2_147_483_647
    //#post(TabCompletionResult getResult(String, int, int, AdditionalTabTargets)): new TabCompletionResult(getResult#1) num objects <= 1
    //#post(TabCompletionResult getResult(String, int, int, AdditionalTabTargets)): new TabCompletionResult(getResult#4) num objects <= 1
    //#test_vector(TabCompletionResult getResult(String, int, int, AdditionalTabTargets)): this.lastPosition - start: {-6_442_450_943..-1, 1..6_442_450_943}, {0}
    //#test_vector(TabCompletionResult getResult(String, int, int, AdditionalTabTargets)): com.dmdirc.ui.input.TabCompleterResult:getResultCount(...)@77: {-2_147_483_648..-1, 1..4_294_967_295}, {0}
    //#test_vector(TabCompletionResult getResult(String, int, int, AdditionalTabTargets)): com.dmdirc.ui.input.TabCompleterResult:getResultCount(...)@81: {-2_147_483_648..0, 2..4_294_967_295}, {1}
    //#test_vector(TabCompletionResult getResult(String, int, int, AdditionalTabTargets)): java.lang.String:equals(...)@69: {0}, {1}
    //#test_vector(TabCompletionResult getResult(String, int, int, AdditionalTabTargets)): java.lang.String:equalsIgnoreCase(...)@93: {0}, {1}
        final TabCompleterResult res = tabCompleter.complete(word, additional);
    //#BashStyle.java:67: Warning: method not available - call not analyzed
    //#    call on TabCompleterResult com.dmdirc.ui.input.TabCompleter:complete(String, AdditionalTabTargets)
    //#    severity: INFORMATIONAL
    //#    class: com.dmdirc.addons.tabcompletion_bash.BashStyle
    //#    method: TabCompletionResult getResult(String, int, int, AdditionalTabTargets)
    //#    unanalyzed callee: TabCompleterResult com.dmdirc.ui.input.TabCompleter:complete(String, AdditionalTabTargets)
        
        if (start == lastPosition && word.equals(lastWord)) {
            tabCount++;
        } else {
            lastPosition = start;
            lastWord = word;
            tabCount = 1;
        }
        
        if (res.getResultCount() == 0) {
    //#BashStyle.java:77: Warning: method not available - call not analyzed
    //#    call on int com.dmdirc.ui.input.TabCompleterResult:getResultCount()
    //#    severity: INFORMATIONAL
    //#    class: com.dmdirc.addons.tabcompletion_bash.BashStyle
    //#    method: TabCompletionResult getResult(String, int, int, AdditionalTabTargets)
    //#    unanalyzed callee: int com.dmdirc.ui.input.TabCompleterResult:getResultCount()
            Toolkit.getDefaultToolkit().beep();
            
            return null;
        } else if (res.getResultCount() == 1) {
    //#BashStyle.java:81: Warning: method not available - call not analyzed
    //#    call on int com.dmdirc.ui.input.TabCompleterResult:getResultCount()
    //#    severity: INFORMATIONAL
    //#    class: com.dmdirc.addons.tabcompletion_bash.BashStyle
    //#    method: TabCompletionResult getResult(String, int, int, AdditionalTabTargets)
    //#    unanalyzed callee: int com.dmdirc.ui.input.TabCompleterResult:getResultCount()
            // One result, just replace it
            
            final String result = res.getResults().get(0);
    //#BashStyle.java:84: Warning: method not available - call not analyzed
    //#    call on List com.dmdirc.ui.input.TabCompleterResult:getResults()
    //#    severity: INFORMATIONAL
    //#    class: com.dmdirc.addons.tabcompletion_bash.BashStyle
    //#    method: TabCompletionResult getResult(String, int, int, AdditionalTabTargets)
    //#    unanalyzed callee: List com.dmdirc.ui.input.TabCompleterResult:getResults()
            
            return new TabCompletionResult(
    //#BashStyle.java:86: Warning: method not available - call not analyzed
    //#    call on void com.dmdirc.ui.input.tabstyles.TabCompletionResult(String, int)
    //#    severity: INFORMATIONAL
    //#    class: com.dmdirc.addons.tabcompletion_bash.BashStyle
    //#    method: TabCompletionResult getResult(String, int, int, AdditionalTabTargets)
    //#    unanalyzed callee: void com.dmdirc.ui.input.tabstyles.TabCompletionResult(String, int)
                    original.substring(0, start) + result + original.substring(end),
                    start + result.length());
        } else {
            // Multiple results
            
            final String sub = res.getBestSubstring();
    //#BashStyle.java:92: Warning: method not available - call not analyzed
    //#    call on String com.dmdirc.ui.input.TabCompleterResult:getBestSubstring()
    //#    severity: INFORMATIONAL
    //#    class: com.dmdirc.addons.tabcompletion_bash.BashStyle
    //#    method: TabCompletionResult getResult(String, int, int, AdditionalTabTargets)
    //#    unanalyzed callee: String com.dmdirc.ui.input.TabCompleterResult:getBestSubstring()
            if (sub.equalsIgnoreCase(word) && tabCount >= 2) {
                window.addLine("tabCompletion", res.toString());
    //#BashStyle.java:94: Warning: method not available - call not analyzed
    //#    call on String com.dmdirc.ui.input.TabCompleterResult:toString()
    //#    severity: INFORMATIONAL
    //#    class: com.dmdirc.addons.tabcompletion_bash.BashStyle
    //#    method: TabCompletionResult getResult(String, int, int, AdditionalTabTargets)
    //#    unanalyzed callee: String com.dmdirc.ui.input.TabCompleterResult:toString()
    //#BashStyle.java:94: Warning: method not available - call not analyzed
    //#    call on void com.dmdirc.ui.interfaces.InputWindow:addLine(String, Object[])
    //#    severity: INFORMATIONAL
    //#    class: com.dmdirc.addons.tabcompletion_bash.BashStyle
    //#    method: TabCompletionResult getResult(String, int, int, AdditionalTabTargets)
    //#    unanalyzed callee: void com.dmdirc.ui.interfaces.InputWindow:addLine(String, Object[])
                
                return null;
            } else {
                return new TabCompletionResult(
    //#BashStyle.java:98: Warning: method not available - call not analyzed
    //#    call on void com.dmdirc.ui.input.tabstyles.TabCompletionResult(String, int)
    //#    severity: INFORMATIONAL
    //#    class: com.dmdirc.addons.tabcompletion_bash.BashStyle
    //#    method: TabCompletionResult getResult(String, int, int, AdditionalTabTargets)
    //#    unanalyzed callee: void com.dmdirc.ui.input.tabstyles.TabCompletionResult(String, int)
    //#BashStyle.java:98: end of method: TabCompletionResult com.dmdirc.addons.tabcompletion_bash.BashStyle.getResult(String, int, int, AdditionalTabTargets)
                        original.substring(0, start) + sub + original.substring(end),
                        start + sub.length());
            }
        }
    }
    
}    //#output(com.dmdirc.addons.tabcompletion_bash.BashStyle__static_init): __Descendant_Table[com/dmdirc/addons/tabcompletion_bash/BashStyle]
    //#output(com.dmdirc.addons.tabcompletion_bash.BashStyle__static_init): __Dispatch_Table.getResult(Ljava/lang/String;IILcom/dmdirc/ui/input/AdditionalTabTargets;)Lcom/dmdirc/ui/input/tabstyles/TabCompletionResult;
    //#post(com.dmdirc.addons.tabcompletion_bash.BashStyle__static_init): __Descendant_Table[com/dmdirc/addons/tabcompletion_bash/BashStyle] == &__Dispatch_Table
    //#post(com.dmdirc.addons.tabcompletion_bash.BashStyle__static_init): __Dispatch_Table.getResult(Ljava/lang/String;IILcom/dmdirc/ui/input/AdditionalTabTargets;)Lcom/dmdirc/ui/input/tabstyles/TabCompletionResult; == &getResult
    //#BashStyle.java:: end of method: com.dmdirc.addons.tabcompletion_bash.BashStyle.com.dmdirc.addons.tabcompletion_bash.BashStyle__static_init
    //#BashStyle.java:: end of class: com.dmdirc.addons.tabcompletion_bash.BashStyle
