This repository has been archived by the owner on Jun 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 24
/
completion.rs
98 lines (88 loc) · 2.81 KB
/
completion.rs
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
use iron::mime::Mime;
use iron::prelude::*;
use iron::status;
use serde_json::to_string;
use super::EngineProvider;
use crate::engine::{Buffer, Completion, Context, CursorPosition};
/// Given a location, return a list of possible completions
pub fn list(req: &mut Request) -> IronResult<Response> {
let lcr = match req.get::<::bodyparser::Struct<ListCompletionsRequest>>() {
Ok(Some(s)) => {
trace!("parsed ListCompletionsRequest");
s
}
Ok(None) => {
trace!("failed parsing ListCompletionsRequest");
return Ok(Response::with(status::BadRequest));
}
Err(err) => {
trace!("error while parsing ListCompletionsRequest");
return Err(IronError::new(err, status::InternalServerError));
}
};
let mutex = req.get::<::persistent::Write<EngineProvider>>().unwrap();
let engine = mutex.lock().unwrap_or_else(|e| e.into_inner());
match engine.list_completions(&lcr.context()) {
// 200 OK; found the definition
Ok(Some(completions)) => {
trace!("got a match");
let res = completions
.into_iter()
.map(CompletionResponse::from)
.collect::<Vec<_>>();
let content_type = "application/json".parse::<Mime>().unwrap();
Ok(Response::with((
content_type,
status::Ok,
to_string(&res).unwrap(),
)))
}
// 204 No Content; Everything went ok, but the definition was not found.
Ok(None) => {
trace!("did not find any match");
Ok(Response::with(status::NoContent))
}
// 500 Internal Server Error; Error occurred while searching for the definition
Err(err) => {
trace!("encountered an error");
Err(IronError::new(err, status::InternalServerError))
}
}
}
#[derive(Debug, Deserialize, Clone)]
struct ListCompletionsRequest {
pub buffers: Vec<Buffer>,
pub file_path: String,
pub column: usize,
pub line: usize,
}
impl ListCompletionsRequest {
pub fn context(self) -> Context {
let cursor = CursorPosition {
line: self.line,
col: self.column,
};
Context::new(self.buffers, cursor, self.file_path)
}
}
#[derive(Debug, Serialize)]
struct CompletionResponse {
text: String,
context: String,
kind: String,
file_path: String,
line: usize,
column: usize,
}
impl From<Completion> for CompletionResponse {
fn from(c: Completion) -> CompletionResponse {
CompletionResponse {
text: c.text,
context: c.context,
kind: c.kind,
file_path: c.file_path,
line: c.position.line,
column: c.position.col,
}
}
}