-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.rs
More file actions
379 lines (322 loc) · 12.7 KB
/
Copy pathmain.rs
File metadata and controls
379 lines (322 loc) · 12.7 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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
pub mod goto_definition;
pub mod helpers;
pub mod hover;
pub mod semantic_token;
use dashmap::DashMap;
use glicol::EngineError;
use goto_definition::goto_definition;
use pest::error::LineColLocation;
use ropey::{Rope, RopeSlice};
use semantic_token::{Highlighter, LEGEND_TYPE};
use std::borrow::Cow;
use tokio::sync::Mutex;
use tower_lsp::jsonrpc::Result;
use tower_lsp::lsp_types::*;
use tower_lsp::{Client, LanguageServer, LspService, Server};
use tree_sitter::{InputEdit, Parser, Point, Tree};
struct Backend {
client: Client,
parser: Mutex<Parser>,
documents: DashMap<Url, (Tree, Rope, Mutex<Highlighter>)>,
}
#[tower_lsp::async_trait]
impl LanguageServer for Backend {
async fn initialize(&self, _: InitializeParams) -> Result<InitializeResult> {
Ok(InitializeResult {
server_info: None,
capabilities: ServerCapabilities {
text_document_sync: Some(TextDocumentSyncCapability::Kind(
TextDocumentSyncKind::INCREMENTAL,
)),
definition_provider: Some(OneOf::Left(true)),
hover_provider: Some(HoverProviderCapability::Simple(true)),
semantic_tokens_provider: Some(
SemanticTokensServerCapabilities::SemanticTokensRegistrationOptions(
SemanticTokensRegistrationOptions {
text_document_registration_options: {
TextDocumentRegistrationOptions {
document_selector: Some(vec![DocumentFilter {
language: Some("glicol".to_string()),
scheme: Some("file".to_string()),
pattern: None,
}]),
}
},
semantic_tokens_options: SemanticTokensOptions {
work_done_progress_options: WorkDoneProgressOptions::default(),
legend: SemanticTokensLegend {
token_types: LEGEND_TYPE.into(),
token_modifiers: vec![],
},
range: Some(true),
full: Some(SemanticTokensFullOptions::Bool(true)),
},
static_registration_options: StaticRegistrationOptions::default(),
},
),
),
..ServerCapabilities::default()
},
})
}
async fn initialized(&self, _: InitializedParams) {
self.client
.log_message(MessageType::INFO, "glicol lsp server initialized!")
.await;
}
async fn shutdown(&self) -> Result<()> {
Ok(())
}
async fn did_open(&self, params: DidOpenTextDocumentParams) {
let mut parser = self.parser.lock().await;
let new_tree = parser.parse(¶ms.text_document.text, None);
if let Some(new_tree) = new_tree {
self.documents.insert(
params.text_document.uri,
(
new_tree,
Rope::from_str(¶ms.text_document.text),
Mutex::new(Default::default()),
),
);
}
}
async fn did_change(&self, params: DidChangeTextDocumentParams) {
let mut parser = self.parser.lock().await;
let data = self.documents.get_mut(¶ms.text_document.uri);
if let Some(mut data) = data {
let (tree, rope, _) = data.value_mut();
for change in params.content_changes {
if let Some(range) = change.range {
let mut start = rope.line_to_char(range.start.line as usize);
start += range.start.character as usize;
let mut end = rope.line_to_char(range.end.line as usize);
end += range.end.character as usize;
let old_end_byte = rope.char_to_byte(end);
let new_end_char = start + change.text.len();
let new_end_byte = rope.char_to_byte(new_end_char);
rope.remove(start..end);
rope.insert(start, &change.text);
let new_end_line = rope.char_to_line(start + change.text.len());
tree.edit(&InputEdit {
start_byte: rope.char_to_byte(start),
old_end_byte,
new_end_byte,
start_position: Point {
row: range.start.line as usize,
column: range.start.character as usize,
},
old_end_position: Point {
row: range.end.line as usize,
column: range.end.character as usize,
},
new_end_position: Point {
row: new_end_line,
column: new_end_char - rope.line_to_char(new_end_line),
},
});
}
let new_tree = parser.parse(format!("{}", rope), Some(tree));
*tree = new_tree.unwrap();
self.client
.log_message(MessageType::INFO, change.text)
.await;
}
self.diagnostics(
¶ms.text_document.uri,
rope.byte_slice(..),
params.text_document.version,
)
.await;
}
}
async fn did_save(&self, _: DidSaveTextDocumentParams) {}
async fn did_close(&self, params: DidCloseTextDocumentParams) {
self.documents.remove(¶ms.text_document.uri);
}
async fn goto_definition(
&self,
params: GotoDefinitionParams,
) -> Result<Option<GotoDefinitionResponse>> {
let data = self
.documents
.get(¶ms.text_document_position_params.text_document.uri);
let data = if let Some(data) = data {
data
} else {
return Ok(None);
};
let (tree, rope, _) = data.value();
goto_definition(
tree,
rope.byte_slice(..),
params.text_document_position_params.position.line as usize,
params.text_document_position_params.position.character as usize,
)
.map(|byte_range| {
let byte_to_position = |byte| {
let start_line = rope.byte_to_line(byte);
let start_line_char = rope.line_to_char(start_line);
let start_char = rope.byte_to_char(byte) - start_line_char;
Position {
line: start_line as u32,
character: start_char as u32,
}
};
GotoDefinitionResponse::Scalar(Location {
uri: params.text_document_position_params.text_document.uri,
range: Range {
start: byte_to_position(byte_range.start),
end: byte_to_position(byte_range.end),
},
})
})
.map(Ok)
.transpose()
}
async fn hover(&self, params: HoverParams) -> Result<Option<Hover>> {
let data = self
.documents
.get(¶ms.text_document_position_params.text_document.uri);
let data = if let Some(data) = data {
data
} else {
return Ok(None);
};
let (tree, rope, _) = data.value();
Ok(hover::hover(
tree,
rope.byte_slice(..),
params.text_document_position_params.position.line as usize,
params.text_document_position_params.position.character as usize,
)
.map(|raw| Hover {
contents: HoverContents::Scalar(MarkedString::String(raw)),
range: None,
}))
}
async fn semantic_tokens_full(
&self,
params: SemanticTokensParams,
) -> Result<Option<SemanticTokensResult>> {
let data = self.documents.get(¶ms.text_document.uri);
let data = if let Some(data) = data {
data
} else {
return Ok(None);
};
let (_tree, rope, highlighter) = data.value();
let data = highlighter
.lock()
.await
.semantic_tokens(rope.byte_slice(..));
Ok(Some(SemanticTokensResult::Tokens(SemanticTokens {
result_id: None,
data,
})))
}
async fn semantic_tokens_range(
&self,
params: SemanticTokensRangeParams,
) -> Result<Option<SemanticTokensRangeResult>> {
let data = self.documents.get(¶ms.text_document.uri);
let data = if let Some(data) = data {
data
} else {
return Ok(None);
};
let (_tree, rope, highlighter) = data.value();
let data = highlighter
.lock()
.await
.semantic_tokens(rope.byte_slice(..));
Ok(Some(SemanticTokensRangeResult::Tokens(SemanticTokens {
result_id: None,
data,
})))
}
}
const BLOCK_SIZE: usize = 128;
impl Backend {
async fn diagnostics<'a>(&self, uri: &Url, rope_slice: RopeSlice<'a>, version: i32) {
let parse_result = match std::panic::catch_unwind(|| {
// TODO: keep the Engine around
let mut engine = glicol::Engine::<BLOCK_SIZE>::new();
let code: Cow<str> = rope_slice.into();
engine.update_with_code(&code);
engine.parse()
}) {
Ok(no_panic) => no_panic,
Err(_panic) => {
self.client
.publish_diagnostics(uri.clone(), vec![], Some(version))
.await;
return;
}
};
if let Err(error) = parse_result {
match error {
EngineError::ParsingError(error) => {
let (start, end) = match &error.line_col {
LineColLocation::Pos((line, col)) => {
let pos = Position {
line: *line as u32 - 1,
character: *col as u32 - 1,
};
(pos, pos)
}
LineColLocation::Span((line1, col1), (line2, col2)) => (
Position {
line: *line1 as u32 - 1,
character: *col1 as u32 - 1,
},
Position {
line: *line2 as u32 - 1,
character: *col2 as u32 - 1,
},
),
};
self.client
.publish_diagnostics(
uri.clone(),
vec![Diagnostic {
range: Range { start, end },
severity: Some(DiagnosticSeverity::ERROR),
code: Some(NumberOrString::String(error.line().to_string())),
code_description: None,
source: Some("glicol engine".to_string()),
message: error.variant.message().to_string(),
related_information: None,
tags: None,
data: None,
}],
Some(version),
)
.await;
}
EngineError::NonExistReference(_) => log::error!("unimplemented"),
EngineError::NonExsitSample(_) => log::error!("unimplemented"),
}
} else {
self.client
.publish_diagnostics(uri.clone(), vec![], Some(version))
.await;
}
}
}
#[tokio::main]
async fn main() {
env_logger::init();
let stdin = tokio::io::stdin();
let stdout = tokio::io::stdout();
let mut parser = Parser::new();
parser
.set_language(tree_sitter_glicol::language())
.expect("Error loading grammar");
let (service, socket) = LspService::new(|client| Backend {
client,
parser: Mutex::new(parser),
documents: DashMap::new(),
});
Server::new(stdin, stdout, socket).serve(service).await;
}