Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: lsp crash caused by vfs loader #778

Merged
merged 2 commits into from
Oct 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions kclvm/tools/src/LSP/src/notification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ impl LanguageServerState {
if let Some(id) = self.vfs.read().file_id(&path.clone().into()) {
self.opened_files.remove(&id);
}
self.vfs_handle.invalidate(path);
self.loader.handle.invalidate(path);

Ok(())
}
Expand All @@ -127,7 +127,7 @@ impl LanguageServerState {
) -> anyhow::Result<()> {
for change in params.changes {
let path = from_lsp::abs_path(&change.uri)?;
self.vfs_handle.invalidate(path);
self.loader.handle.invalidate(path);
}
Ok(())
}
Expand Down
21 changes: 14 additions & 7 deletions kclvm/tools/src/LSP/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ use lsp_types::{
};
use parking_lot::RwLock;
use ra_ap_vfs::{FileId, Vfs};
use ra_ap_vfs_notify::NotifyHandle;
use std::collections::HashMap;
use std::{sync::Arc, time::Instant};

Expand All @@ -34,6 +33,11 @@ pub(crate) enum Event {
Lsp(lsp_server::Message),
}

pub(crate) struct Handle<H, C> {
pub(crate) handle: H,
pub(crate) _receiver: C,
}

/// State for the language server
pub(crate) struct LanguageServerState {
/// Channel to send language server messages to the client
Expand Down Expand Up @@ -67,7 +71,7 @@ pub(crate) struct LanguageServerState {
pub opened_files: IndexSet<FileId>,

/// The VFS loader
pub vfs_handle: Box<dyn ra_ap_vfs::loader::Handle>,
pub loader: Handle<Box<dyn ra_ap_vfs::loader::Handle>, Receiver<ra_ap_vfs::loader::Message>>,

/// The word index map
pub word_index_map: HashMap<Url, HashMap<String, Vec<Location>>>,
Expand Down Expand Up @@ -95,10 +99,13 @@ impl LanguageServerState {
) -> Self {
let (task_sender, task_receiver) = unbounded::<Task>();

let (vfs_sender, receiver) = unbounded::<ra_ap_vfs::loader::Message>();
let handle: NotifyHandle =
ra_ap_vfs::loader::Handle::spawn(Box::new(move |msg| vfs_sender.send(msg).unwrap()));
let handle = Box::new(handle) as Box<dyn ra_ap_vfs::loader::Handle>;
let loader = {
let (sender, _receiver) = unbounded::<ra_ap_vfs::loader::Message>();
let handle: ra_ap_vfs_notify::NotifyHandle =
ra_ap_vfs::loader::Handle::spawn(Box::new(move |msg| sender.send(msg).unwrap()));
let handle = Box::new(handle) as Box<dyn ra_ap_vfs::loader::Handle>;
Handle { handle, _receiver }
};

// build word index for all the workspace folders
// todo: async
Expand Down Expand Up @@ -127,8 +134,8 @@ impl LanguageServerState {
shutdown_requested: false,
analysis: Analysis::default(),
opened_files: IndexSet::new(),
vfs_handle: handle,
word_index_map,
loader,
}
}

Expand Down
45 changes: 45 additions & 0 deletions kclvm/tools/src/LSP/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,51 @@ fn notification_test() {
}
}

#[test]
fn close_file_test() {
let root = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
let mut path = root.clone();

path.push("src/test_data/diagnostics.k");

let path = path.to_str().unwrap();
let src = std::fs::read_to_string(path.clone()).unwrap();
let server = Project {}.server(InitializeParams::default());

// Mock open file
server.notification::<lsp_types::notification::DidOpenTextDocument>(
lsp_types::DidOpenTextDocumentParams {
text_document: TextDocumentItem {
uri: Url::from_file_path(path).unwrap(),
language_id: "KCL".to_string(),
version: 0,
text: src.clone(),
},
},
);

// Mock close file
server.notification::<lsp_types::notification::DidCloseTextDocument>(
lsp_types::DidCloseTextDocumentParams {
text_document: TextDocumentIdentifier {
uri: Url::from_file_path(path).unwrap(),
},
},
);

// Mock reopen file
server.notification::<lsp_types::notification::DidOpenTextDocument>(
lsp_types::DidOpenTextDocumentParams {
text_document: TextDocumentItem {
uri: Url::from_file_path(path).unwrap(),
language_id: "KCL".to_string(),
version: 0,
text: src,
},
},
);
}

#[test]
fn goto_def_test() {
let root = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
Expand Down
Loading