From a10d9e80ec8d8b88c1d2d197622eba4673392b23 Mon Sep 17 00:00:00 2001 From: Shunsuke Shibayama Date: Sat, 16 Dec 2023 00:01:46 +0900 Subject: [PATCH] fix: completion does not work in type decl files --- .github/workflows/main.yml | 2 +- crates/els/server.rs | 33 ++++++++----------- crates/erg_compiler/module/global.rs | 49 ++++++++++++++++++++++++++-- 3 files changed, 61 insertions(+), 23 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 92fb55fcf..aaade7479 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -47,7 +47,7 @@ jobs: python-version: ${{ matrix.python-version }} - uses: Swatinem/rust-cache@v2 - run: rustup update stable - - run: cargo test --features backtrace --all --verbose -- --include-ignored --nocapture --exclude els + - run: cargo test --features backtrace --all --exclude els --verbose -- --include-ignored --nocapture # FIXME: Currently, when running ELS tests in the GitHub actions environment, # the tests fail probabilistically on Windows and MacOS. # This is likely due to a delay in processing timing. diff --git a/crates/els/server.rs b/crates/els/server.rs index 41524219e..f5ecc3512 100644 --- a/crates/els/server.rs +++ b/crates/els/server.rs @@ -751,13 +751,13 @@ impl Server { uri: &NormalizedUrl, ) -> Option> { let path = uri.to_file_path().ok()?; - let ent = self.shared.mod_cache.get(&path)?; + let ent = self.shared.get_module(&path)?; Some(MappedRwLockReadGuard::map(ent, |ent| &ent.module)) } pub(crate) fn raw_get_mod_ctx(&self, uri: &NormalizedUrl) -> Option<&ModuleContext> { let path = uri.to_file_path().ok()?; - self.shared.mod_cache.raw_ref_ctx(&path) + self.shared.raw_ref_ctx(&path) } /// TODO: Reuse cache. @@ -771,7 +771,7 @@ impl Server { pub(crate) fn steal_lowerer(&mut self, uri: &NormalizedUrl) -> Option<(ASTLowerer, IRs)> { let path = uri.to_file_path().ok()?; - let module = self.shared.mod_cache.remove(&path)?; + let module = self.shared.remove_module(&path)?; let lowerer = ASTLowerer::new_with_ctx(module.module); Some((lowerer, IRs::new(module.id, module.ast, module.hir))) } @@ -789,7 +789,7 @@ impl Server { pub(crate) fn get_visitor(&self, uri: &NormalizedUrl) -> Option { let path = uri.to_file_path().ok()?; - let ent = self.shared.mod_cache.get(&path)?; + let ent = self.shared.get_module(&path)?; ent.hir.as_ref()?; let hir = MappedRwLockReadGuard::map(ent, |ent| ent.hir.as_ref().unwrap()); Some(HIRVisitor::new(hir, &self.file_cache, uri.clone())) @@ -797,7 +797,7 @@ impl Server { pub(crate) fn get_searcher(&self, uri: &NormalizedUrl, kind: ExprKind) -> Option { let path = uri.to_file_path().ok()?; - let ent = self.shared.mod_cache.get(&path)?; + let ent = self.shared.get_module(&path)?; ent.hir.as_ref()?; let hir = MappedRwLockReadGuard::map(ent, |ent| ent.hir.as_ref().unwrap()); Some(HIRVisitor::new_searcher( @@ -833,20 +833,14 @@ impl Server { pub(crate) fn _get_all_ctxs(&self) -> Vec<&ModuleContext> { let mut ctxs = vec![]; - ctxs.extend(self.shared.mod_cache.raw_values().map(|ent| &ent.module)); - ctxs.extend(self.shared.py_mod_cache.raw_values().map(|ent| &ent.module)); + ctxs.extend(self.shared.raw_modules().map(|ent| &ent.module)); ctxs } pub(crate) fn get_workspace_ctxs(&self) -> Vec<&Context> { let project_root = project_root_of(&self.home).unwrap_or(self.home.clone()); let mut ctxs = vec![]; - for (path, ent) in self - .shared - .mod_cache - .raw_iter() - .chain(self.shared.py_mod_cache.raw_iter()) - { + for (path, ent) in self.shared.raw_path_and_modules() { if path.starts_with(&project_root) { ctxs.push(&ent.module.context); } @@ -917,7 +911,6 @@ impl Server { pub(crate) fn get_builtin_module(&self) -> Option<&Context> { self.shared - .mod_cache .raw_ref_ctx(Path::new("")) .map(|mc| &mc.context) } @@ -934,19 +927,19 @@ impl Server { pub fn remove_module_entry(&mut self, uri: &NormalizedUrl) -> Option { let path = uri.to_file_path().ok()?; - self.shared.mod_cache.remove(&path) + self.shared.remove_module(&path) } pub fn insert_module_entry(&mut self, uri: NormalizedUrl, entry: ModuleEntry) { let Ok(path) = uri.to_file_path() else { return; }; - self.shared.mod_cache.insert(path.into(), entry); + self.shared.insert_module(path.into(), entry); } pub fn get_hir(&self, uri: &NormalizedUrl) -> Option> { let path = uri.to_file_path().ok()?; - let ent = self.shared.mod_cache.get(&path)?; + let ent = self.shared.get_module(&path)?; ent.hir.as_ref()?; Some(MappedRwLockReadGuard::map(ent, |ent| { ent.hir.as_ref().unwrap() @@ -955,17 +948,17 @@ impl Server { pub fn steal_entry(&self, uri: &NormalizedUrl) -> Option { let path = uri.to_file_path().ok()?; - self.shared.mod_cache.remove(&path) + self.shared.remove_module(&path) } pub fn restore_entry(&self, uri: NormalizedUrl, entry: ModuleEntry) { let path = uri.to_file_path().unwrap(); - self.shared.mod_cache.insert(path.into(), entry); + self.shared.insert_module(path.into(), entry); } pub fn get_ast(&self, uri: &NormalizedUrl) -> Option> { let path = uri.to_file_path().ok()?; - let ent = self.shared.mod_cache.get(&path)?; + let ent = self.shared.get_module(&path)?; ent.ast.as_ref()?; Some(MappedRwLockReadGuard::map(ent, |ent| { ent.ast.as_ref().unwrap() diff --git a/crates/erg_compiler/module/global.rs b/crates/erg_compiler/module/global.rs index 406e13b8c..eb8743b98 100644 --- a/crates/erg_compiler/module/global.rs +++ b/crates/erg_compiler/module/global.rs @@ -1,9 +1,10 @@ use erg_common::config::ErgConfig; use erg_common::pathutil::NormalizedPathBuf; +use erg_common::shared::MappedRwLockReadGuard; -use crate::context::Context; +use crate::context::{Context, ModuleContext}; -use super::cache::SharedModuleCache; +use super::cache::{ModuleEntry, SharedModuleCache}; use super::errors::{SharedCompileErrors, SharedCompileWarnings}; use super::graph::SharedModuleGraph; use super::impls::SharedTraitImpls; @@ -94,4 +95,48 @@ impl SharedCompilerResource { self.graph.rename_path(old, new.clone()); self.promises.rename(old, new); } + + pub fn insert_module(&self, path: NormalizedPathBuf, entry: ModuleEntry) { + if path.to_string_lossy().ends_with("d.er") { + self.py_mod_cache.insert(path, entry); + } else { + self.mod_cache.insert(path, entry); + } + } + + pub fn remove_module(&self, path: &std::path::Path) -> Option { + if path.to_string_lossy().ends_with("d.er") { + self.py_mod_cache.remove(path) + } else { + self.mod_cache.remove(path) + } + } + + pub fn get_module(&self, path: &std::path::Path) -> Option> { + if path.to_string_lossy().ends_with("d.er") { + self.py_mod_cache.get(path) + } else { + self.mod_cache.get(path) + } + } + + pub fn raw_ref_ctx(&self, path: &std::path::Path) -> Option<&ModuleContext> { + if path.to_string_lossy().ends_with("d.er") { + self.py_mod_cache.raw_ref_ctx(path) + } else { + self.mod_cache.raw_ref_ctx(path) + } + } + + pub fn raw_modules(&self) -> impl Iterator { + self.mod_cache + .raw_values() + .chain(self.py_mod_cache.raw_values()) + } + + pub fn raw_path_and_modules(&self) -> impl Iterator { + self.mod_cache + .raw_iter() + .chain(self.py_mod_cache.raw_iter()) + } }