Skip to content

Commit 90f6f32

Browse files
committed
Add simple test for goto_definition()
1 parent 3fe33f9 commit 90f6f32

File tree

4 files changed

+71
-3
lines changed

4 files changed

+71
-3
lines changed

crates/ark/src/lsp/definitions.rs

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use crate::lsp::traits::node::NodeExt;
2020
use crate::lsp::traits::rope::RopeExt;
2121
use crate::treesitter::NodeTypeExt;
2222

23-
pub unsafe fn goto_definition<'a>(
23+
pub fn goto_definition<'a>(
2424
document: &'a Document,
2525
params: GotoDefinitionParams,
2626
) -> Result<Option<GotoDefinitionResponse>> {
@@ -75,3 +75,57 @@ pub unsafe fn goto_definition<'a>(
7575
let response = GotoDefinitionResponse::Link(vec![link]);
7676
Ok(Some(response))
7777
}
78+
79+
#[cfg(test)]
80+
mod tests {
81+
use std::path::PathBuf;
82+
83+
use assert_matches::assert_matches;
84+
use tower_lsp::lsp_types;
85+
86+
use super::*;
87+
use crate::lsp::documents::Document;
88+
use crate::lsp::indexer;
89+
90+
#[test]
91+
fn test_goto_definition() {
92+
// Reset the indexer on exit
93+
let _guard = indexer::IndexerGuard;
94+
95+
let code = r#"
96+
foo <- 42
97+
print(foo)
98+
"#;
99+
let doc = Document::new(code, None);
100+
let path = PathBuf::from("/foo/test.R");
101+
102+
indexer::update(&doc, &path).unwrap();
103+
104+
let params = GotoDefinitionParams {
105+
text_document_position_params: lsp_types::TextDocumentPositionParams {
106+
text_document: lsp_types::TextDocumentIdentifier {
107+
uri: Url::from_file_path(&path).unwrap(),
108+
},
109+
position: lsp_types::Position::new(2, 7),
110+
},
111+
work_done_progress_params: Default::default(),
112+
partial_result_params: Default::default(),
113+
};
114+
115+
assert_matches!(
116+
goto_definition(&doc, params).unwrap(),
117+
Some(GotoDefinitionResponse::Link(ref links)) => {
118+
assert!(!links.is_empty());
119+
assert_eq!(links[0].target_uri, Url::from_file_path(&path).unwrap());
120+
121+
assert_eq!(
122+
links[0].target_range,
123+
lsp_types::Range {
124+
start: lsp_types::Position::new(1, 0),
125+
end: lsp_types::Position::new(1, 3),
126+
}
127+
);
128+
}
129+
);
130+
}
131+
}

crates/ark/src/lsp/handlers.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ pub(crate) fn handle_goto_definition(
289289
let document = state.get_document(uri)?;
290290

291291
// build goto definition context
292-
let result = unwrap!(unsafe { goto_definition(&document, params) }, Err(err) => {
292+
let result = unwrap!(goto_definition(&document, params), Err(err) => {
293293
lsp::log_error!("{err:?}");
294294
return Ok(None);
295295
});

crates/ark/src/lsp/indexer.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,18 @@ pub(crate) fn indexer_clear() {
164164
index.clear();
165165
}
166166

167+
/// RAII guard that clears `WORKSPACE_INDEX` when dropped.
168+
/// Useful for ensuring a clean index state in tests.
169+
#[cfg(test)]
170+
pub(crate) struct IndexerGuard;
171+
172+
#[cfg(test)]
173+
impl Drop for IndexerGuard {
174+
fn drop(&mut self) {
175+
indexer_clear();
176+
}
177+
}
178+
167179
fn str_from_path(path: &Path) -> anyhow::Result<&str> {
168180
path.to_str().ok_or(anyhow!(
169181
"Couldn't convert path {} to string",

crates/ark/src/lsp/symbols.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -586,6 +586,7 @@ mod tests {
586586
use crate::lsp::config::LspConfig;
587587
use crate::lsp::config::WorkspaceSymbolsConfig;
588588
use crate::lsp::documents::Document;
589+
use crate::lsp::indexer::IndexerGuard;
589590

590591
fn test_symbol(code: &str) -> Vec<DocumentSymbol> {
591592
let doc = Document::new(code, None);
@@ -900,6 +901,8 @@ a <- function() {
900901
#[test]
901902
fn test_workspace_symbols_include_comment_sections() {
902903
fn run(include_comment_sections: bool) -> Vec<String> {
904+
let _guard = IndexerGuard;
905+
903906
let code = "# Section ----\nfoo <- 1";
904907

905908
let mut config = LspConfig::default();
@@ -921,7 +924,6 @@ a <- function() {
921924
let result = super::symbols(&params, &state).unwrap();
922925
let out = result.into_iter().map(|s| s.name).collect();
923926

924-
indexer::indexer_clear();
925927
out
926928
}
927929

0 commit comments

Comments
 (0)