Skip to content

Commit 6cfc926

Browse files
committed
Import testthat in test files
1 parent 06d0c94 commit 6cfc926

File tree

3 files changed

+35
-1
lines changed

3 files changed

+35
-1
lines changed

crates/ark/src/lsp/diagnostics.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,22 @@ pub(crate) fn generate_diagnostics(doc: Document, state: WorldState) -> Vec<Diag
182182
}
183183
}
184184

185+
// Simple workaround to include testthat exports in test files. I think the
186+
// general principle would be that (a) files in `tests/testthat/` include
187+
// `testthat.R` as a preamble (note that people modify that file e.g. to add
188+
// more `library()` calls), and (b) all helper files are included in a
189+
// test-specific workspace (which is effectively the case currently as we
190+
// don't special-case how workspace inclusion works for packages). We might
191+
// want to provide a mechanism for test packages to declare this sort of
192+
// test files setup.
193+
if doc.testthat {
194+
if let Some(pkg) = state.library.get("testthat") {
195+
for export in &pkg.namespace.exports {
196+
context.workspace_symbols.insert(export.clone());
197+
}
198+
}
199+
}
200+
185201
// Add per-environment session symbols
186202
for scope in state.console_scopes.iter() {
187203
for name in scope.iter() {

crates/ark/src/lsp/documents.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ pub struct Document {
4848

4949
// Configuration of the document, such as indentation settings.
5050
pub config: DocumentConfig,
51+
52+
/// Whether the document is a testthat file. This property should not be
53+
/// here, it's just a short term stopgap.
54+
pub testthat: bool,
5155
}
5256

5357
impl std::fmt::Debug for Document {
@@ -80,6 +84,7 @@ impl Document {
8084
version,
8185
ast,
8286
config: Default::default(),
87+
testthat: false,
8388
}
8489
}
8590

crates/ark/src/lsp/main_loop.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
use std::collections::HashMap;
99
use std::future;
10+
use std::path::Path;
1011
use std::path::PathBuf;
1112
use std::pin::Pin;
1213
use std::sync::atomic::AtomicBool;
@@ -806,7 +807,19 @@ async fn process_diagnostics_batch(batch: Vec<RefreshDiagnosticsTask>) {
806807
let _span = tracing::info_span!("diagnostics_refresh", uri = %uri).entered();
807808

808809
if let Some(document) = state.documents.get(&uri) {
809-
let diagnostics = generate_diagnostics(document.clone(), state.clone());
810+
// Special case testthat-specific behaviour. This is a simple
811+
// stopgap approach that has some false positives (e.g. when we
812+
// work on testthat itself the flag will always be true), but
813+
// that shouldn't have much practical impact.
814+
let mut doc = document.clone();
815+
if Path::new(uri.path())
816+
.components()
817+
.any(|c| c.as_os_str() == "testthat")
818+
{
819+
doc.testthat = true;
820+
};
821+
822+
let diagnostics = generate_diagnostics(doc, state.clone());
810823
Some(RefreshDiagnosticsResult {
811824
uri,
812825
diagnostics,

0 commit comments

Comments
 (0)