Skip to content

Commit

Permalink
fix: completion does not work in type decl files
Browse files Browse the repository at this point in the history
  • Loading branch information
mtshiba committed Dec 15, 2023
1 parent f44909a commit a10d9e8
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 23 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
33 changes: 13 additions & 20 deletions crates/els/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -751,13 +751,13 @@ impl<Checker: BuildRunnable, Parser: Parsable> Server<Checker, Parser> {
uri: &NormalizedUrl,
) -> Option<MappedRwLockReadGuard<ModuleContext>> {
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.
Expand All @@ -771,7 +771,7 @@ impl<Checker: BuildRunnable, Parser: Parsable> Server<Checker, Parser> {

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)))
}
Expand All @@ -789,15 +789,15 @@ impl<Checker: BuildRunnable, Parser: Parsable> Server<Checker, Parser> {

pub(crate) fn get_visitor(&self, uri: &NormalizedUrl) -> Option<HIRVisitor> {
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()))
}

pub(crate) fn get_searcher(&self, uri: &NormalizedUrl, kind: ExprKind) -> Option<HIRVisitor> {
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(
Expand Down Expand Up @@ -833,20 +833,14 @@ impl<Checker: BuildRunnable, Parser: Parsable> Server<Checker, Parser> {

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);
}
Expand Down Expand Up @@ -917,7 +911,6 @@ impl<Checker: BuildRunnable, Parser: Parsable> Server<Checker, Parser> {

pub(crate) fn get_builtin_module(&self) -> Option<&Context> {
self.shared
.mod_cache
.raw_ref_ctx(Path::new("<builtins>"))
.map(|mc| &mc.context)
}
Expand All @@ -934,19 +927,19 @@ impl<Checker: BuildRunnable, Parser: Parsable> Server<Checker, Parser> {

pub fn remove_module_entry(&mut self, uri: &NormalizedUrl) -> Option<ModuleEntry> {
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<MappedRwLockReadGuard<HIR>> {
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()
Expand All @@ -955,17 +948,17 @@ impl<Checker: BuildRunnable, Parser: Parsable> Server<Checker, Parser> {

pub fn steal_entry(&self, uri: &NormalizedUrl) -> Option<ModuleEntry> {
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<MappedRwLockReadGuard<Module>> {
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()
Expand Down
49 changes: 47 additions & 2 deletions crates/erg_compiler/module/global.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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<ModuleEntry> {
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<MappedRwLockReadGuard<ModuleEntry>> {
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<Item = &ModuleEntry> {
self.mod_cache
.raw_values()
.chain(self.py_mod_cache.raw_values())
}

pub fn raw_path_and_modules(&self) -> impl Iterator<Item = (&NormalizedPathBuf, &ModuleEntry)> {
self.mod_cache
.raw_iter()
.chain(self.py_mod_cache.raw_iter())
}
}

0 comments on commit a10d9e8

Please sign in to comment.