Skip to content

Commit c22309a

Browse files
committed
test: add incremental test
1 parent 7c1c07d commit c22309a

File tree

7 files changed

+61
-7
lines changed

7 files changed

+61
-7
lines changed

src/ast/test.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -676,3 +676,25 @@ fn test_fmt() {
676676
let text_edit = test_lsp::<PLFormat>(&Database::default(), None, ActionType::LspFmt, testfile);
677677
debug_assert!(text_edit[0].is_empty());
678678
}
679+
680+
#[test]
681+
fn test_lsp_incremental() {
682+
let raw_db = Database::default().enable_logging();
683+
let db = &raw_db;
684+
let docs = MemDocs::default();
685+
let input = MemDocsInput::new(
686+
db,
687+
Arc::new(Mutex::new(RefCell::new(docs))),
688+
"test/lsp_incremental/main.pi".to_string(),
689+
Default::default(),
690+
ActionType::Diagnostic,
691+
None,
692+
None,
693+
);
694+
compile_dry(db, input).unwrap();
695+
let l = raw_db.take_logs();
696+
assert_eq!(l.len(), 64);
697+
compile_dry(db, input).unwrap();
698+
let ll = raw_db.take_logs();
699+
assert_eq!(ll.len(), 0);
700+
}

src/db.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,25 @@ impl Db for Database {
5757
self.ref_str.lock().unwrap().get_mut().clone()
5858
}
5959
}
60+
61+
impl Database {
62+
/// Enable logging of each salsa event.
63+
#[cfg(test)]
64+
pub fn enable_logging(self) -> Self {
65+
assert!(self.logs.is_none());
66+
Self {
67+
storage: self.storage,
68+
logs: Some(Default::default()),
69+
ref_str: self.ref_str,
70+
}
71+
}
72+
73+
#[cfg(test)]
74+
pub fn take_logs(&self) -> Vec<String> {
75+
if let Some(logs) = &self.logs {
76+
std::mem::take(&mut *logs.lock().unwrap())
77+
} else {
78+
panic!("logs not enabled");
79+
}
80+
}
81+
}

src/lsp/lspserver.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
//! - goto definition
77
//! - find references
88
use std::{
9-
cell::RefCell,
9+
cell::{Cell, RefCell},
1010
error::Error,
1111
sync::{Arc, Mutex},
1212
thread::available_parallelism,
@@ -137,7 +137,7 @@ fn main_loop(
137137
None,
138138
None,
139139
);
140-
let mut tokens = FxHashMap::default();
140+
let last_tokens = Cell::new(Default::default());
141141
let mut completions: Vec<Vec<lsp_types::CompletionItem>> = vec![];
142142

143143
log::info!("starting main loop");
@@ -221,7 +221,7 @@ fn main_loop(
221221
})
222222
.on::<SemanticTokensFullRequest, _>(|id, params| {
223223
let uri = url_to_path(params.text_document.uri);
224-
docin.set_file(&mut db).to(uri.clone());
224+
docin.set_file(&mut db).to(uri);
225225
docin.set_action(&mut db).to(ActionType::SemanticTokensFull);
226226
docin
227227
.set_params(&mut db)
@@ -231,15 +231,15 @@ fn main_loop(
231231
if newtokens.is_empty() {
232232
newtokens.push(SemanticTokens::default());
233233
}
234-
_ = tokens.insert(uri, newtokens[0].clone());
234+
last_tokens.replace(newtokens[0].clone());
235235
let sender = connection.sender.clone();
236236
pool.execute(move || {
237237
send_semantic_tokens(&sender, id, newtokens[0].clone());
238238
});
239239
})
240240
.on::<SemanticTokensFullDeltaRequest, _>(|id, params| {
241241
let uri = url_to_path(params.text_document.uri);
242-
docin.set_file(&mut db).to(uri.clone());
242+
docin.set_file(&mut db).to(uri);
243243
docin.set_action(&mut db).to(ActionType::SemanticTokensFull);
244244
docin
245245
.set_params(&mut db)
@@ -249,8 +249,9 @@ fn main_loop(
249249
if newtokens.is_empty() {
250250
newtokens.push(SemanticTokens::default());
251251
}
252-
let old = tokens.insert(uri, newtokens[0].clone());
253-
let delta = diff_tokens(&old.unwrap().data, &newtokens[0].data);
252+
253+
let old = last_tokens.replace(newtokens[0].clone());
254+
let delta = diff_tokens(&old.data, &newtokens[0].data);
254255
let sender = connection.sender.clone();
255256
pool.execute(move || {
256257
send_semantic_tokens_edit(

test/lsp_incremental/Kagari.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
entry = "main.pi"
2+
project = "lsp_incremental"

test/lsp_incremental/main.pi

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
use std::io;
2+
use lsp_incremental::module1;
3+
fn main() i64 {
4+
io::print_s("Hello World\n");
5+
return 0;
6+
}

test/lsp_incremental/module1.pi

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
use lsp_incremental::module2;

test/lsp_incremental/module2.pi

Whitespace-only changes.

0 commit comments

Comments
 (0)