Skip to content

Commit e9382bf

Browse files
Do not include excluded files even when the client opens them
This require a pretty big modification, because this is a new kind of file: exists - but ignore it.
1 parent ac6b054 commit e9382bf

File tree

11 files changed

+216
-121
lines changed

11 files changed

+216
-121
lines changed

crates/load-cargo/src/lib.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,9 @@ pub fn load_workspace(
9494
let contents = loader.load_sync(path);
9595
let path = vfs::VfsPath::from(path.to_path_buf());
9696
vfs.set_file_contents(path.clone(), contents);
97-
vfs.file_id(&path)
97+
vfs.file_id(&path).and_then(|(file_id, excluded)| {
98+
(excluded == vfs::FileExcluded::No).then_some(file_id)
99+
})
98100
},
99101
extra_env,
100102
);

crates/rust-analyzer/src/config.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1909,10 +1909,14 @@ impl Config {
19091909
}
19101910
_ => FilesWatcher::Server,
19111911
},
1912-
exclude: self.files_exclude().iter().map(|it| self.root_path.join(it)).collect(),
1912+
exclude: self.excluded().collect(),
19131913
}
19141914
}
19151915

1916+
pub fn excluded(&self) -> impl Iterator<Item = AbsPathBuf> + use<'_> {
1917+
self.files_exclude().iter().map(|it| self.root_path.join(it))
1918+
}
1919+
19161920
pub fn notifications(&self) -> NotificationsConfig {
19171921
NotificationsConfig {
19181922
cargo_toml_not_found: self.notifications_cargoTomlNotFound().to_owned(),

crates/rust-analyzer/src/global_state.rs

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -650,15 +650,17 @@ impl GlobalStateSnapshot {
650650
RwLockReadGuard::map(self.vfs.read(), |(it, _)| it)
651651
}
652652

653-
pub(crate) fn url_to_file_id(&self, url: &Url) -> anyhow::Result<FileId> {
653+
/// Returns `None` if the file was excluded.
654+
pub(crate) fn url_to_file_id(&self, url: &Url) -> anyhow::Result<Option<FileId>> {
654655
url_to_file_id(&self.vfs_read(), url)
655656
}
656657

657658
pub(crate) fn file_id_to_url(&self, id: FileId) -> Url {
658659
file_id_to_url(&self.vfs_read(), id)
659660
}
660661

661-
pub(crate) fn vfs_path_to_file_id(&self, vfs_path: &VfsPath) -> anyhow::Result<FileId> {
662+
/// Returns `None` if the file was excluded.
663+
pub(crate) fn vfs_path_to_file_id(&self, vfs_path: &VfsPath) -> anyhow::Result<Option<FileId>> {
662664
vfs_path_to_file_id(&self.vfs_read(), vfs_path)
663665
}
664666

@@ -750,14 +752,21 @@ pub(crate) fn file_id_to_url(vfs: &vfs::Vfs, id: FileId) -> Url {
750752
url_from_abs_path(path)
751753
}
752754

753-
pub(crate) fn url_to_file_id(vfs: &vfs::Vfs, url: &Url) -> anyhow::Result<FileId> {
755+
/// Returns `None` if the file was excluded.
756+
pub(crate) fn url_to_file_id(vfs: &vfs::Vfs, url: &Url) -> anyhow::Result<Option<FileId>> {
754757
let path = from_proto::vfs_path(url)?;
755-
let res = vfs.file_id(&path).ok_or_else(|| anyhow::format_err!("file not found: {path}"))?;
756-
Ok(res)
758+
vfs_path_to_file_id(vfs, &path)
757759
}
758760

759-
pub(crate) fn vfs_path_to_file_id(vfs: &vfs::Vfs, vfs_path: &VfsPath) -> anyhow::Result<FileId> {
760-
let res =
761+
/// Returns `None` if the file was excluded.
762+
pub(crate) fn vfs_path_to_file_id(
763+
vfs: &vfs::Vfs,
764+
vfs_path: &VfsPath,
765+
) -> anyhow::Result<Option<FileId>> {
766+
let (file_id, excluded) =
761767
vfs.file_id(vfs_path).ok_or_else(|| anyhow::format_err!("file not found: {vfs_path}"))?;
762-
Ok(res)
768+
match excluded {
769+
vfs::FileExcluded::Yes => Ok(None),
770+
vfs::FileExcluded::No => Ok(Some(file_id)),
771+
}
763772
}

crates/rust-analyzer/src/handlers/notification.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ use crate::{
2222
mem_docs::DocumentData,
2323
reload,
2424
target_spec::TargetSpec,
25+
try_default,
2526
};
2627

2728
pub(crate) fn handle_cancel(state: &mut GlobalState, params: CancelParams) -> anyhow::Result<()> {
@@ -74,6 +75,14 @@ pub(crate) fn handle_did_open_text_document(
7475
tracing::error!("duplicate DidOpenTextDocument: {}", path);
7576
}
7677

78+
if let Some(abs_path) = path.as_path() {
79+
if state.config.excluded().any(|excluded| abs_path.starts_with(&excluded)) {
80+
tracing::trace!("opened excluded file {abs_path}");
81+
state.vfs.write().0.insert_excluded_file(path);
82+
return Ok(());
83+
}
84+
}
85+
7786
let contents = params.text_document.text.into_bytes();
7887
state.vfs.write().0.set_file_contents(path, Some(contents));
7988
if state.config.discover_workspace_config().is_some() {
@@ -127,7 +136,7 @@ pub(crate) fn handle_did_close_text_document(
127136
tracing::error!("orphan DidCloseTextDocument: {}", path);
128137
}
129138

130-
if let Some(file_id) = state.vfs.read().0.file_id(&path) {
139+
if let Some((file_id, vfs::FileExcluded::No)) = state.vfs.read().0.file_id(&path) {
131140
state.diagnostics.clear_native_for(file_id);
132141
}
133142

@@ -146,7 +155,7 @@ pub(crate) fn handle_did_save_text_document(
146155
) -> anyhow::Result<()> {
147156
if let Ok(vfs_path) = from_proto::vfs_path(&params.text_document.uri) {
148157
let snap = state.snapshot();
149-
let file_id = snap.vfs_path_to_file_id(&vfs_path)?;
158+
let file_id = try_default!(snap.vfs_path_to_file_id(&vfs_path)?);
150159
let sr = snap.analysis.source_root_id(file_id)?;
151160

152161
if state.config.script_rebuild_on_save(Some(sr)) && state.build_deps_changed {
@@ -290,7 +299,7 @@ fn run_flycheck(state: &mut GlobalState, vfs_path: VfsPath) -> bool {
290299
let _p = tracing::info_span!("run_flycheck").entered();
291300

292301
let file_id = state.vfs.read().0.file_id(&vfs_path);
293-
if let Some(file_id) = file_id {
302+
if let Some((file_id, vfs::FileExcluded::No)) = file_id {
294303
let world = state.snapshot();
295304
let invocation_strategy_once = state.config.flycheck(None).invocation_strategy_once();
296305
let may_flycheck_workspace = state.config.flycheck_workspace(None);

0 commit comments

Comments
 (0)