Skip to content

Commit c363c97

Browse files
committed
Started working on utf-16 index translation
1 parent dbb5be0 commit c363c97

File tree

4 files changed

+105
-35
lines changed

4 files changed

+105
-35
lines changed

src/components/language_server/src/lib.rs

Lines changed: 76 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,24 @@
11
mod document;
2+
mod file_uri;
23
mod log;
34
mod message;
45
mod methods;
56

6-
use crate::message::{Message, Response};
7+
use crate::{
8+
file_uri::FileUri,
9+
message::{Message, Response},
10+
};
711
use daemon::connect_to_daemon;
812
pub(crate) use document::*;
913
use fingerprint::COMPILER_BUILT_AT;
1014
use ipc_message::{IpcMessage, IpcMessageId, IpcRequest, IpcResponse};
1115
pub(crate) use log::*;
1216
use lsp_types::{
13-
CompletionItem, CompletionList, CompletionResponse, DidChangeTextDocumentParams, Uri,
17+
CompletionItem, CompletionList, CompletionResponse, DidChangeTextDocumentParams,
18+
InitializeParams, Uri,
1419
};
1520
use pin_project_lite::pin_project;
21+
use serde::Deserialize;
1622
use smol::{
1723
io::{AsyncWriteExt, WriteHalf},
1824
net::TcpStream,
@@ -22,8 +28,12 @@ use std::{
2228
io::{BufReader, BufWriter, Stdin, Stdout, Write},
2329
pin::pin,
2430
process::ExitCode,
31+
sync::Arc,
32+
};
33+
use text_edit::{
34+
DocumentChange, TextEditLineColumnUtf16, TextPosition, TextPositionLineColumnUtf16,
35+
TextRangeLineColumnUtf16,
2536
};
26-
use text_edit::TextPosition;
2737
use transport::{read_message_raw_async, write_message_raw_async};
2838

2939
pin_project! {
@@ -137,7 +147,6 @@ pub async fn start() -> ExitCode {
137147
)
138148
.await;
139149
}
140-
IpcResponse::Changed => todo!(),
141150
IpcResponse::Saved => todo!(),
142151
IpcResponse::Completion(items) => {
143152
Server::send_to_editor(
@@ -234,6 +243,8 @@ pub async fn start() -> ExitCode {
234243
))
235244
.unwrap();
236245

246+
let init_params = InitializeParams::deserialize(request.params);
247+
237248
write_message_raw_async(server.as_mut().project().daemon, &data)
238249
.await
239250
.unwrap();
@@ -270,33 +281,68 @@ pub async fn start() -> ExitCode {
270281
};
271282
}
272283
Message::Response(_) => (),
273-
Message::Notification(notif) => match notif.method.as_str() {
274-
"initialized" => (),
275-
"textDocument/didChange" => {
276-
let params =
277-
serde_json::from_value::<DidChangeTextDocumentParams>(notif.params)
278-
.unwrap();
279-
280-
dbg!(params);
281-
282-
/*
283-
notification.params
284-
285-
write_message_raw_async(server.as_mut().project().daemon, &data)
286-
.await
287-
.unwrap();
288-
server.as_mut().project().daemon.flush().await.unwrap();
289-
*/
290-
}
291-
"exit" => {
292-
return if server.did_shutdown {
293-
ExitCode::SUCCESS
294-
} else {
295-
ExitCode::FAILURE
296-
};
284+
Message::Notification(notif) => {
285+
match notif.method.as_str() {
286+
"initialized" => (),
287+
"textDocument/didChange" => {
288+
let params =
289+
serde_json::from_value::<DidChangeTextDocumentParams>(notif.params)
290+
.unwrap();
291+
292+
if let Some("file") = params
293+
.text_document
294+
.uri
295+
.scheme()
296+
.map(|scheme| scheme.as_str())
297+
{
298+
let document_changes =
299+
params.content_changes.into_iter().map(|change| {
300+
if let Some(range) = change.range {
301+
assert!(range.start <= range.end, "Invalid range");
302+
303+
DocumentChange::IncrementalUtf16(TextEditLineColumnUtf16 {
304+
range: TextRangeLineColumnUtf16 {
305+
start: TextPositionLineColumnUtf16 {
306+
line_utf16: range.start.line,
307+
column_utf16: range.start.character,
308+
},
309+
end: TextPositionLineColumnUtf16 {
310+
line_utf16: range.end.line,
311+
column_utf16: range.end.character,
312+
},
313+
},
314+
replace_with: Arc::from(change.text),
315+
})
316+
} else {
317+
DocumentChange::Full(Arc::from(change.text))
318+
}
319+
});
320+
321+
eprintln!(
322+
"{:?} {:?}",
323+
&params.text_document.uri.to_file_path(),
324+
&document_changes
325+
);
326+
}
327+
328+
/*
329+
notification.params
330+
write_message_raw_async(server.as_mut().project().daemon, &data)
331+
.await
332+
.unwrap();
333+
server.as_mut().project().daemon.flush().await.unwrap();
334+
*/
335+
}
336+
"exit" => {
337+
return if server.did_shutdown {
338+
ExitCode::SUCCESS
339+
} else {
340+
ExitCode::FAILURE
341+
};
342+
}
343+
_ => (),
297344
}
298-
_ => (),
299-
},
345+
}
300346
}
301347
}
302348

src/components/language_server/src/methods/mod.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@ pub mod text_document;
33
use crate::{Server, message::Request};
44
use lsp_types::{
55
CompletionOptions, DiagnosticOptions, DiagnosticServerCapabilities, InitializeResult,
6-
ServerCapabilities, ServerInfo, TextDocumentSyncCapability, TextDocumentSyncKind,
7-
WorkDoneProgressOptions,
6+
PositionEncodingKind, ServerCapabilities, ServerInfo, TextDocumentSyncCapability,
7+
TextDocumentSyncKind, WorkDoneProgressOptions,
88
request::{Initialize, Request as LspRequest, Shutdown},
99
};
1010

1111
pub fn initialize() -> <Initialize as LspRequest>::Result {
1212
return InitializeResult {
1313
capabilities: ServerCapabilities {
14+
position_encoding: Some(PositionEncodingKind::UTF32),
1415
completion_provider: Some(CompletionOptions {
1516
resolve_provider: None,
1617
trigger_characters: None,

src/representations/ipc_message/src/lib.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::{
44
path::PathBuf,
55
sync::Arc,
66
};
7-
use text_edit::{TextEdit, TextPosition};
7+
use text_edit::{DocumentChange, TextEdit, TextPosition};
88
use vfs::Canonical;
99

1010
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
@@ -40,7 +40,6 @@ pub enum IpcRequest {
4040
pub enum IpcResponse {
4141
Initialized,
4242
ShuttingDown,
43-
Changed,
4443
Saved,
4544
Completion(Vec<String>),
4645
Diagnostics(Vec<(String, TextPosition)>),
@@ -50,7 +49,7 @@ pub enum IpcResponse {
5049
pub enum IpcNotification {
5150
Exit,
5251
DidOpen(IpcFile),
53-
DidChange(IpcFile, Vec<TextEdit>),
52+
DidChange(IpcFile, Vec<DocumentChange>),
5453
DidSave(IpcFile),
5554
}
5655

src/representations/text_edit/src/lib.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,32 @@ impl std::ops::AddAssign<TextLength> for TextPosition {
123123
}
124124
}
125125

126+
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
127+
pub enum DocumentChange {
128+
IncrementalUtf16(TextEditLineColumnUtf16),
129+
Full(Arc<str>),
130+
}
131+
126132
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
127133
pub struct TextEdit {
128134
pub range: TextRange,
129135
pub replace_with: Arc<str>,
130136
}
137+
138+
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
139+
pub struct TextEditLineColumnUtf16 {
140+
pub range: TextRangeLineColumnUtf16,
141+
pub replace_with: Arc<str>,
142+
}
143+
144+
#[derive(Copy, Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
145+
pub struct TextRangeLineColumnUtf16 {
146+
pub start: TextPositionLineColumnUtf16,
147+
pub end: TextPositionLineColumnUtf16,
148+
}
149+
150+
#[derive(Copy, Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
151+
pub struct TextPositionLineColumnUtf16 {
152+
pub line_utf16: u32,
153+
pub column_utf16: u32,
154+
}

0 commit comments

Comments
 (0)