1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*
 * Copyright (c) 2016 Boucher, Antoni <bouanto@zoho.com>
 *
 * 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.
 */

use std::collections::HashMap;
use std::marker::PhantomData;

use mg_settings::{EnumMetaData, SettingCompletion};

use completion::{Completer, CompletionResult};

/// A command completer.
pub struct CommandCompleter<T: Clone> {
    metadata: Vec<(String, String)>,
    _phantom: PhantomData<T>,
}

impl<T: Clone + EnumMetaData> CommandCompleter<T> {
    #[allow(unknown_lints, new_without_default_derive)]
    /// Create a new command completer.
    pub fn new() -> CommandCompleter<T> {
        let mut data: Vec<_> =
            T::get_metadata().iter()
                .filter(|&(_, metadata)| !metadata.completion_hidden)
                .map(|(setting_name, metadata)| (setting_name.clone(), metadata.help_text.clone()))
                .collect();
        data.push(("map".to_string(), "Create a new key binding".to_string()));
        data.push(("set".to_string(), "Change the value of a setting".to_string()));
        data.push(("unmap".to_string(), "Delete a key binding".to_string()));
        data.sort();
        CommandCompleter {
            metadata: data,
            _phantom: PhantomData,
        }
    }
}

impl<T: Clone> Completer for CommandCompleter<T> {
    fn completions(&mut self, input: &str) -> Vec<CompletionResult> {
        self.metadata.iter()
            .filter(|&&(ref command, ref help)|
                    command.to_lowercase().contains(&input) ||
                    help.to_lowercase().contains(&input))
            .map(|&(ref col1, ref col2)| CompletionResult::new(&[col1, col2]))
            .collect()
    }
}

/// A nop completer.
pub struct NoCompleter {
}

impl NoCompleter {
    #[allow(unknown_lints, new_without_default_derive)]
    /// Create a new nop completer.
    pub fn new() -> Self {
        NoCompleter {
        }
    }
}

impl Completer for NoCompleter {
    fn complete_result(&self, _value: &str) -> String {
        String::new()
    }

    fn completions(&mut self, _input: &str) -> Vec<CompletionResult> {
        vec![]
    }
}

/// A setting completer.
pub struct SettingCompleter<T> {
    selected_name: Option<String>,
    setting_names: Vec<(String, String)>,
    setting_values: HashMap<String, Vec<String>>,
    _phantom: PhantomData<T>,
}

impl<T: EnumMetaData + SettingCompletion> SettingCompleter<T> {
    #[allow(unknown_lints, new_without_default_derive)]
    /// Create a new setting completer.
    pub fn new() -> Self {
        let mut data: Vec<_> =
            T::get_metadata().iter()
                .filter(|&(_, metadata)| !metadata.completion_hidden)
                .map(|(setting_name, metadata)| (setting_name.clone(), metadata.help_text.clone()))
                .collect();
        data.sort();
        SettingCompleter {
            selected_name: None,
            setting_names: data,
            setting_values: T::get_value_completions(),
            _phantom: PhantomData,
        }
    }
}

impl<T> Completer for SettingCompleter<T> {
    fn complete_result(&self, value: &str) -> String {
        if let Some(ref name) = self.selected_name {
            format!("set {} = {}", name, value)
        }
        else {
            format!("set {} =", value)
        }
    }

    fn completions(&mut self, input: &str) -> Vec<CompletionResult> {
        if input.contains("= ") {
            let mut iter = input.split_whitespace();
            if let Some(name) = iter.next() {
                if let Some(values) = self.setting_values.get(name) {
                    iter.next(); // Skip the equal token.
                    let input_value = iter.next().unwrap_or_default();
                    self.selected_name = Some(name.to_string());
                    return values.iter()
                        .filter(|value| value.contains(input_value))
                        .map(|value| CompletionResult::new(&[value, &String::new()]))
                        .collect();
                }
            }
            vec![]
        }
        else {
            let input = input.trim();
            self.selected_name = None;
            self.setting_names.iter()
                .filter(|&&(ref setting, ref help)|
                        setting.to_lowercase().contains(input) ||
                        help.to_lowercase().contains(input))
                .map(|&(ref col1, ref col2)| CompletionResult::new(&[col1, col2]))
                .collect()
        }
    }
}