use std::path::PathBuf;
use lsp_types::{CreateFilesParams, DeleteFilesParams, RenameFilesParams, Uri};
use crate::lsp::documents::reload_open_documents_referenced_files;
use crate::lsp::global_state::GlobalState;
use crate::lsp::uri_ext::UriExt;
fn op_uri_to_path(uri: &str) -> Option<PathBuf> {
uri.parse::<Uri>()
.ok()
.and_then(|uri| uri.to_file_path().map(|p| p.into_owned()))
}
pub(crate) fn did_create_files(gs: &mut GlobalState, params: CreateFilesParams) {
for file in ¶ms.files {
if let Some(path) = op_uri_to_path(&file.uri) {
gs.salsa.intern_file(Some(path));
}
}
reload_open_documents_referenced_files(gs);
gs.arm_settle();
}
pub(crate) fn did_delete_files(gs: &mut GlobalState, params: DeleteFilesParams) {
for file in ¶ms.files {
if let Ok(uri) = file.uri.parse::<Uri>() {
gs.diagnostics
.drop_uri(&uri, &gs.sender, gs.supports_pull_diagnostics);
}
if let Some(path) = op_uri_to_path(&file.uri) {
gs.salsa.evict_file_text(&path);
gs.salsa.intern_file(Some(path));
}
}
reload_open_documents_referenced_files(gs);
gs.arm_settle();
}
pub(crate) fn did_rename_files(gs: &mut GlobalState, params: RenameFilesParams) {
for rename in ¶ms.files {
if let Ok(old_uri) = rename.old_uri.parse::<Uri>() {
gs.diagnostics
.drop_uri(&old_uri, &gs.sender, gs.supports_pull_diagnostics);
}
if let Some(old_path) = op_uri_to_path(&rename.old_uri) {
gs.salsa.evict_file_text(&old_path);
gs.salsa.intern_file(Some(old_path));
}
if let Some(new_path) = op_uri_to_path(&rename.new_uri) {
gs.salsa.intern_file(Some(new_path));
}
}
reload_open_documents_referenced_files(gs);
gs.arm_settle();
}