-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathmain.rs
More file actions
259 lines (209 loc) · 8.47 KB
/
main.rs
File metadata and controls
259 lines (209 loc) · 8.47 KB
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
mod persistence;
use persistence::Persistence;
use tasklist::tasklist;
use std::sync::Arc;
use tokio::sync::Mutex;
use tokio::time::*;
use tower_lsp::jsonrpc::Result;
use tower_lsp::lsp_types::*;
use tower_lsp::{Client, LanguageServer, LspService, Server};
struct Backend {
client: Client,
persistence: Arc<Mutex<Persistence>>,
}
#[tokio::main]
#[quit::main]
async fn main() {
env_logger::init();
let stdin = tokio::io::stdin();
let stdout = tokio::io::stdout();
let persistence = Arc::new(Mutex::new(Persistence::new().unwrap()));
let (service, socket) = LspService::new(|client| Backend {
client,
persistence,
});
Server::new(stdin, stdout, socket).serve(service).await;
}
#[tower_lsp::async_trait]
impl LanguageServer for Backend {
async fn initialize(&self, params: InitializeParams) -> Result<InitializeResult> {
let mut persistence = self.persistence.lock().await;
persistence.initialize(¶ms);
drop(persistence);
tokio::spawn(async move {
#[cfg(not(target_family = "windows"))]
loop {
let editor_process_id = params.process_id.unwrap_or_else(|| quit::with_code(1));
let editor_process_running = psutil::process::processes()
.unwrap()
.into_iter()
.filter_map(|process| process.ok())
.find(|process| process.pid() == editor_process_id);
if let None = editor_process_running {
quit::with_code(1);
}
tokio::time::sleep(Duration::from_secs(60)).await;
}
#[cfg(target_family = "Windows")]
loop {
use tasklist;
let editor_process_id = params.process_id.unwrap_or_else(|| quit::with_code(1));
let editor_process_running = unsafe {
let mut tl = tasklist::Tasklist::new();
tl.any(|process| process.get_pid() == editor_process_id)
};
if !editor_process_running {
quit::with_code(1);
}
tokio::time::sleep(Duration::from_secs(60)).await;
}
});
let background_persistence = Arc::clone(&self.persistence);
tokio::spawn(async move {
loop {
let mut persistence = background_persistence.lock().await;
let _ = persistence.reindex_modified_files();
let _ = persistence.index_included_dirs_once();
let _ = persistence.index_gems_once();
drop(persistence);
tokio::time::sleep(Duration::from_secs(600)).await
}
});
Ok(InitializeResult {
server_info: None,
capabilities: ServerCapabilities {
text_document_sync: Some(TextDocumentSyncCapability::Options(
TextDocumentSyncOptions {
open_close: Some(true),
change: Some(TextDocumentSyncKind::FULL), // todo: incremental
will_save: Some(false),
will_save_wait_until: Some(false),
save: Some(TextDocumentSyncSaveOptions::SaveOptions(SaveOptions {
include_text: Some(true),
})),
},
)),
definition_provider: Some(OneOf::Left(true)),
document_highlight_provider: Some(OneOf::Left(true)),
references_provider: Some(OneOf::Left(true)),
rename_provider: Some(OneOf::Left(true)),
workspace_symbol_provider: Some(OneOf::Left(true)),
..ServerCapabilities::default()
},
})
}
async fn shutdown(&self) -> Result<()> {
Ok(())
}
async fn did_open(&self, params: DidOpenTextDocumentParams) {
let mut persistence = self.persistence.lock().await;
let mut diagnostics: Vec<tower_lsp::lsp_types::Diagnostic> = vec![];
let change_diagnostics =
persistence.diagnostics(¶ms.text_document.text, ¶ms.text_document.uri);
for diagnostic in change_diagnostics {
for unwrapped_diagnostic in diagnostic {
if let Some(finally_diagnostic) = unwrapped_diagnostic {
diagnostics.push(finally_diagnostic.to_owned());
}
}
}
if persistence.report_diagnostics {
self.client
.publish_diagnostics(
params.text_document.uri,
diagnostics,
Some(params.text_document.version),
)
.await;
}
}
async fn did_change(&self, params: DidChangeTextDocumentParams) {
let mut persistence = self.persistence.lock().await;
for content_change in ¶ms.content_changes {
persistence
.reindex_modified_file(
&self.client,
&content_change.text,
¶ms.text_document.uri,
)
.await;
}
}
async fn did_save(&self, params: DidSaveTextDocumentParams) {
let mut persistence = self.persistence.lock().await;
persistence
.reindex_modified_file(
&self.client,
¶ms.text.unwrap(),
¶ms.text_document.uri,
)
.await;
}
async fn did_close(&self, _: DidCloseTextDocumentParams) {
self.client
.log_message(MessageType::INFO, "file closed!")
.await;
}
async fn goto_definition(
&self,
params: GotoDefinitionParams,
) -> Result<Option<GotoDefinitionResponse>> {
let persistence = self.persistence.lock().await;
let definitions = || -> Option<GotoDefinitionResponse> {
let locations = persistence.find_definitions(params.text_document_position_params);
let locations = locations.unwrap();
Some(GotoDefinitionResponse::Array(locations))
}();
Ok(definitions)
}
async fn document_highlight(
&self,
params: DocumentHighlightParams,
) -> Result<Option<Vec<DocumentHighlight>>> {
let persistence = self.persistence.lock().await;
let highlights_response = || -> Option<Vec<DocumentHighlight>> {
let highlights = persistence.find_highlights(params.text_document_position_params);
let highlights = highlights.unwrap();
Some(highlights)
}();
Ok(highlights_response)
}
async fn references(&self, params: ReferenceParams) -> Result<Option<Vec<Location>>> {
let persistence = self.persistence.lock().await;
let text_position = params.clone().text_document_position;
let text_document = ¶ms.text_document_position.text_document;
let locations_response = || -> Option<Vec<Location>> {
let documents = persistence.find_references(text_position).unwrap();
let locations = persistence.documents_to_locations(text_document.uri.path(), documents);
Some(locations)
}();
Ok(locations_response)
}
async fn rename(&self, params: RenameParams) -> Result<Option<WorkspaceEdit>> {
let persistence = self.persistence.lock().await;
let text_position = params.clone().text_document_position;
let text_document = ¶ms.text_document_position.text_document;
let new_name = ¶ms.new_name;
let workspace_edit = || -> Option<WorkspaceEdit> {
let references = persistence.find_references(text_position).unwrap();
let workspace_edit =
persistence.rename_tokens(text_document.uri.path(), references, new_name);
Some(workspace_edit)
}();
Ok(workspace_edit)
}
async fn symbol(
&self,
params: WorkspaceSymbolParams,
) -> Result<Option<Vec<SymbolInformation>>> {
let persistence = self.persistence.lock().await;
let symbol_info_response = || -> Option<Vec<SymbolInformation>> {
let documents = persistence
.find_references_in_workspace(params.query)
.unwrap_or_else(|_| Vec::new());
let symbol_info = persistence.documents_to_symbol_information(documents);
Some(symbol_info)
}();
Ok(symbol_info_response)
}
}