Skip to content

Commit 8c0f729

Browse files
committed
Add the file watcher and its test back
Signed-off-by: Siddhi Agrawal <[email protected]>
1 parent 543b906 commit 8c0f729

File tree

2 files changed

+84
-6
lines changed

2 files changed

+84
-6
lines changed

kclvm/tools/src/LSP/src/state.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -208,15 +208,15 @@ impl LanguageServerState {
208208
match e.kind {
209209
notify::EventKind::Modify(kind) => {
210210
if let notify::event::ModifyKind::Data(data_change) = kind {
211-
if let notify::event::DataChange::Content = data_change {
211+
if let notify::event::DataChange::Any = data_change {
212212
let paths = e.paths;
213213
let kcl_config_file: Vec<PathBuf> =
214214
filter_kcl_config_file(&paths);
215215
if !kcl_config_file.is_empty() {
216216
// TODO: wait for fix `kcl mod metadata` to read only. Otherwise it will lead to an infinite loop
217-
// return Some(Event::FileWatcher(
218-
// FileWatcherEvent::ChangedConfigFile(kcl_config_file),
219-
// ));
217+
return Some(Event::FileWatcher(
218+
FileWatcherEvent::ChangedConfigFile(kcl_config_file),
219+
));
220220
}
221221
}
222222
}

kclvm/tools/src/LSP/src/tests.rs

Lines changed: 80 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1527,9 +1527,87 @@ fn complete_import_external_file_e2e_test() {
15271527
}
15281528
}
15291529

1530+
#[test]
1531+
fn mod_file_watcher_modify_test() {
1532+
let path = PathBuf::from(".")
1533+
.join("src")
1534+
.join("test_data")
1535+
.join("watcher")
1536+
.join("modify")
1537+
.canonicalize()
1538+
.unwrap();
1539+
1540+
let mod_file_path = path.join("kcl.mod");
1541+
let main_path = path.join("main.k");
1542+
1543+
let mod_src_bac = std::fs::read_to_string(mod_file_path.clone()).unwrap();
1544+
let main_src = std::fs::read_to_string(main_path.clone()).unwrap();
1545+
1546+
let initialize_params = InitializeParams {
1547+
workspace_folders: Some(vec![WorkspaceFolder {
1548+
uri: Url::from_file_path(path.clone()).unwrap(),
1549+
name: "test".to_string(),
1550+
}]),
1551+
..Default::default()
1552+
};
1553+
let server = Project {}.server(initialize_params);
1554+
1555+
// Mock open file
1556+
server.notification::<lsp_types::notification::DidOpenTextDocument>(
1557+
lsp_types::DidOpenTextDocumentParams {
1558+
text_document: TextDocumentItem {
1559+
uri: Url::from_file_path(main_path.clone()).unwrap(),
1560+
language_id: "KCL".to_string(),
1561+
version: 0,
1562+
text: main_src,
1563+
},
1564+
},
1565+
);
1566+
1567+
// Simulate modifying the kcl.mod file
1568+
std::fs::write(&mod_file_path, "[package]\nname = \"add\"\nedition = \"v0.9.0\"\nversion = \"0.0.1\"\n\n[dependencies]\nhelloworld = \"0.1.4\"\n").unwrap();
1569+
1570+
// wait for download dependence
1571+
wait_async!(5000);
1572+
1573+
let id = server.next_request_id.get();
1574+
server.next_request_id.set(id.wrapping_add(1));
1575+
1576+
let r: Request = Request::new(
1577+
id.into(),
1578+
"textDocument/hover".to_string(),
1579+
HoverParams {
1580+
text_document_position_params: TextDocumentPositionParams {
1581+
text_document: TextDocumentIdentifier {
1582+
uri: Url::from_file_path(main_path).unwrap(),
1583+
},
1584+
position: Position::new(0, 8),
1585+
},
1586+
work_done_progress_params: Default::default(),
1587+
},
1588+
);
1589+
1590+
// Send request and wait for it's response
1591+
let res = server.send_and_receive(r);
1592+
1593+
std::fs::write(mod_file_path, mod_src_bac).unwrap();
1594+
assert_eq!(
1595+
res.result.unwrap(),
1596+
to_json(Hover {
1597+
contents: HoverContents::Scalar(MarkedString::LanguageString(
1598+
lsp_types::LanguageString {
1599+
language: "KCL".to_owned(),
1600+
value: "helloworld: ".to_string(),
1601+
}
1602+
)),
1603+
range: None
1604+
})
1605+
.unwrap()
1606+
)
1607+
}
1608+
15301609
// TODO: wait for fix `kcl mod metadata` to read only. Otherwise it will lead to an infinite loop
1531-
#[allow(dead_code)]
1532-
// #[test]
1610+
#[test]
15331611
fn mod_file_watcher_test() {
15341612
let path = PathBuf::from(".")
15351613
.join("src")

0 commit comments

Comments
 (0)