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
//! Performs name-based text completion using a `GlobalScope`.

use scope::{GlobalScope, MasterScope};

/// Returns a sorted list of possible name completions for the given prefix.
///
/// Returns `None` if no possible completions exist.
pub fn complete_name(word: &str, scope: &GlobalScope)
        -> Option<Vec<String>> {
    let mut results = Vec::new();

    for name in MasterScope::names() {
        scope.with_name(name, |name| {
            if name.starts_with(word) {
                results.push(name.to_owned());
            }
        });
    }

    scope.with_values(|values| {
        for &(name, _) in values {
            scope.with_name(name, |name| {
                if name.starts_with(word) {
                    results.push(name.to_owned());
                }
            });
        }
    });

    scope.with_macros(|macros| {
        for &(name, _) in macros {
            scope.with_name(name, |name| {
                if name.starts_with(word) {
                    results.push(name.to_owned());
                }
            });
        }
    });

    if results.is_empty() {
        None
    } else {
        results.sort();
        Some(results)
    }
}