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
//! Engines provide source analysis for rust code
use std::path::Path;

/// This module's Error and Result types
mod error;
pub use self::error::{Error, Result};

use ::Config;

/// Provide completions, definitions, and analysis of rust source code
pub trait SemanticEngine : Send + Sync {
    /// Perform any necessary initialization.
    ///
    /// Only needs to be called once when an engine is created.
    fn initialize(&self, config: &Config) -> Result<()>;

    /// Find the definition for the item under the cursor
    fn find_definition(&self, context: &Context) -> Result<Option<Definition>>;

    /// Get a list of completions for the item under the cursor
    fn list_completions(&self, context: &Context) -> Result<Option<Vec<Completion>>>;
}

/// A possible completion for a location
#[derive(Debug)]
pub struct Completion {
    pub text: String,
    pub context: String,
    pub kind: String,
    pub file_path: String,
    pub position: CursorPosition,
}

/// Source file and type information for a found definition
#[derive(Debug)]
pub struct Definition {
    pub position: CursorPosition,
    pub text: String,
    pub text_context: String,
    pub dtype: String,
    pub file_path: String,
}

/// Context for a given operation.
///
/// All operations require a buffer holding the contents of a file, the file's absolute path, and a
/// cursor position to fully specify the request. This object holds all of those items.
#[derive(Debug)]
pub struct Context {
    pub buffers: Vec<Buffer>,
    pub query_cursor: CursorPosition,
    pub query_file: String,
}

impl Context {
    pub fn new<T>(buffers: Vec<Buffer>, position: CursorPosition,
                  file_path: T) -> Context where T: Into<String> {
        Context {
            buffers: buffers,
            query_cursor: position,
            query_file: file_path.into(),
        }
    }

    pub fn query_path<'a>(&'a self) -> &'a Path {
        &Path::new(&self.query_file[..])
    }
}

/// Position of the cursor in a text file
///
/// Similar to a point, it has two coordinates `line` and `col`.
#[derive(Debug)]
pub struct CursorPosition {
    pub line: usize,
    pub col: usize,
}

pub mod racer;
pub use self::racer::Racer;

#[derive(Debug, RustcDecodable, Clone)]
pub struct Buffer {
    pub file_path: String,
    pub contents: String,
}

impl Buffer {
    pub fn path<'a>(&'a self) -> &'a Path {
        &Path::new(&self.file_path[..])
    }
}