Skip to content

Commit

Permalink
Move tests to parameter_hints.rs
Browse files Browse the repository at this point in the history
It seems obvious to me that we are testing a feature that "officially" lives here now
  • Loading branch information
DavisVaughan committed Jan 31, 2025
1 parent 6f7e012 commit ec931de
Show file tree
Hide file tree
Showing 2 changed files with 189 additions and 188 deletions.
189 changes: 189 additions & 0 deletions crates/ark/src/lsp/completions/parameter_hints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,192 @@ fn skip_namespace_operator(node: Node) -> Node {
node
}
}

#[cfg(test)]
mod tests {
use tower_lsp::lsp_types::CompletionItem;
use tower_lsp::lsp_types::InsertTextFormat;
use tree_sitter::Point;

use crate::lsp::completions::provide_completions;
use crate::lsp::document_context::DocumentContext;
use crate::lsp::documents::Document;
use crate::lsp::state::WorldState;
use crate::r_task;

fn point_from_cursor(text: &str) -> (String, Point) {
let cursor_pos = text.find('@').unwrap();
let text = text.replace('@', "");
(text, Point::new(0, cursor_pos))
}

fn find_completion(completions: &[CompletionItem], label: &str) -> CompletionItem {
completions
.iter()
.find(|item| item.label == label)
.unwrap()
.clone()
}

#[test]
fn test_completions_dont_add_parentheses_inside_special_functions_naked() {
r_task(|| {
let (text, point) = point_from_cursor("debug(enc@)");
let document = Document::new(text.as_str(), None);
let context = DocumentContext::new(&document, point, None);
let completions = provide_completions(&context, &WorldState::default()).unwrap();
let completion = find_completion(&completions, "enc2native");

// (1) correct string (no trailing parens)
// (2) plain text, not a snippet with a placeholder for the cursor
// (3) no extra command to trigger parameter hints
assert_eq!(completion.insert_text.unwrap(), String::from("enc2native"));
assert_eq!(
completion.insert_text_format.unwrap(),
InsertTextFormat::PLAIN_TEXT
);
assert!(completion.command.is_none());
})
}

#[test]
fn test_completions_dont_add_parentheses_inside_special_functions_double_colon() {
r_task(|| {
let state = WorldState::default();

let (text, point) = point_from_cursor("debug(base::ab@)");
let document = Document::new(text.as_str(), None);
let context = DocumentContext::new(&document, point, None);
let completions = provide_completions(&context, &state).unwrap();
let completion = find_completion(&completions, "abs");

assert_eq!(completion.insert_text.unwrap(), String::from("abs"));
assert_eq!(
completion.insert_text_format.unwrap(),
InsertTextFormat::PLAIN_TEXT
);
assert!(completion.command.is_none());

// User hasn't typed any namespace name yet, but we show them a completion list
// here and they pick from it, so it's a common case
let (text, point) = point_from_cursor("debug(base::@)");
let document = Document::new(text.as_str(), None);
let context = DocumentContext::new(&document, point, None);
let completions = provide_completions(&context, &state).unwrap();
let completion = find_completion(&completions, "abs");

assert_eq!(completion.insert_text.unwrap(), String::from("abs"));
assert_eq!(
completion.insert_text_format.unwrap(),
InsertTextFormat::PLAIN_TEXT
);
assert!(completion.command.is_none());
})
}

#[test]
fn test_completions_dont_add_parentheses_inside_special_functions_triple_colon() {
r_task(|| {
let (text, point) = point_from_cursor("debug(utils:::.get@)");
let document = Document::new(text.as_str(), None);
let context = DocumentContext::new(&document, point, None);
let completions = provide_completions(&context, &WorldState::default()).unwrap();
let completion = find_completion(&completions, ".getHelpFile");

assert_eq!(
completion.insert_text.unwrap(),
String::from(".getHelpFile")
);
assert_eq!(
completion.insert_text_format.unwrap(),
InsertTextFormat::PLAIN_TEXT
);
assert!(completion.command.is_none());
})
}

#[test]
fn test_completions_dont_add_parentheses_for_help_operator_naked() {
r_task(|| {
let state = WorldState::default();

// Unary help
let (text, point) = point_from_cursor("?enc@");
let document = Document::new(text.as_str(), None);
let context = DocumentContext::new(&document, point, None);
let completions = provide_completions(&context, &state).unwrap();
let completion = find_completion(&completions, "enc2native");

assert_eq!(completion.insert_text.unwrap(), String::from("enc2native"));
assert_eq!(
completion.insert_text_format.unwrap(),
InsertTextFormat::PLAIN_TEXT
);
assert!(completion.command.is_none());

// Binary help
let (text, point) = point_from_cursor("method?enc@");
let document = Document::new(text.as_str(), None);
let context = DocumentContext::new(&document, point, None);
let completions = provide_completions(&context, &state).unwrap();
let completion = find_completion(&completions, "enc2native");

assert_eq!(completion.insert_text.unwrap(), String::from("enc2native"));
assert_eq!(
completion.insert_text_format.unwrap(),
InsertTextFormat::PLAIN_TEXT
);
assert!(completion.command.is_none());
})
}

#[test]
fn test_completions_dont_add_parentheses_for_help_operator_double_colon() {
r_task(|| {
let state = WorldState::default();

// Unary help
let (text, point) = point_from_cursor("?base::enc@");
let document = Document::new(text.as_str(), None);
let context = DocumentContext::new(&document, point, None);
let completions = provide_completions(&context, &state).unwrap();
let completion = find_completion(&completions, "enc2native");

assert_eq!(completion.insert_text.unwrap(), String::from("enc2native"));
assert_eq!(
completion.insert_text_format.unwrap(),
InsertTextFormat::PLAIN_TEXT
);
assert!(completion.command.is_none());

// Binary help
let (text, point) = point_from_cursor("method?base::enc@");
let document = Document::new(text.as_str(), None);
let context = DocumentContext::new(&document, point, None);
let completions = provide_completions(&context, &state).unwrap();
let completion = find_completion(&completions, "enc2native");

assert_eq!(completion.insert_text.unwrap(), String::from("enc2native"));
assert_eq!(
completion.insert_text_format.unwrap(),
InsertTextFormat::PLAIN_TEXT
);
assert!(completion.command.is_none());

// User hasn't typed any namespace name yet, but we show them a completion list
// here and they pick from it, so it's a common case
let (text, point) = point_from_cursor("?base::@");
let document = Document::new(text.as_str(), None);
let context = DocumentContext::new(&document, point, None);
let completions = provide_completions(&context, &state).unwrap();
let completion = find_completion(&completions, "enc2native");

assert_eq!(completion.insert_text.unwrap(), String::from("enc2native"));
assert_eq!(
completion.insert_text_format.unwrap(),
InsertTextFormat::PLAIN_TEXT
);
assert!(completion.command.is_none());
})
}
}
188 changes: 0 additions & 188 deletions crates/ark/src/lsp/completions/provide.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,191 +33,3 @@ pub(crate) fn provide_completions(
// document, the current workspace, and any call related arguments
completions_from_composite_sources(context, state, parameter_hints)
}

#[cfg(test)]
mod tests {
use tower_lsp::lsp_types::InsertTextFormat;
use tree_sitter::Point;

use super::*;
use crate::lsp::document_context::DocumentContext;
use crate::lsp::documents::Document;
use crate::lsp::state::WorldState;
use crate::r_task;

fn point_from_cursor(text: &str) -> (String, Point) {
let cursor_pos = text.find('@').unwrap();
let text = text.replace('@', "");
(text, Point::new(0, cursor_pos))
}

fn find_completion(completions: &[CompletionItem], label: &str) -> CompletionItem {
completions
.iter()
.find(|item| item.label == label)
.unwrap()
.clone()
}

#[test]
fn test_completions_dont_add_parentheses_inside_special_functions_naked() {
r_task(|| {
let (text, point) = point_from_cursor("debug(enc@)");
let document = Document::new(text.as_str(), None);
let context = DocumentContext::new(&document, point, None);
let completions = provide_completions(&context, &WorldState::default()).unwrap();
let completion = find_completion(&completions, "enc2native");

// (1) correct string (no trailing parens)
// (2) plain text, not a snippet with a placeholder for the cursor
// (3) no extra command to trigger parameter hints
assert_eq!(completion.insert_text.unwrap(), String::from("enc2native"));
assert_eq!(
completion.insert_text_format.unwrap(),
InsertTextFormat::PLAIN_TEXT
);
assert!(completion.command.is_none());
})
}

#[test]
fn test_completions_dont_add_parentheses_inside_special_functions_double_colon() {
r_task(|| {
let state = WorldState::default();

let (text, point) = point_from_cursor("debug(base::ab@)");
let document = Document::new(text.as_str(), None);
let context = DocumentContext::new(&document, point, None);
let completions = provide_completions(&context, &state).unwrap();
let completion = find_completion(&completions, "abs");

assert_eq!(completion.insert_text.unwrap(), String::from("abs"));
assert_eq!(
completion.insert_text_format.unwrap(),
InsertTextFormat::PLAIN_TEXT
);
assert!(completion.command.is_none());

// User hasn't typed any namespace name yet, but we show them a completion list
// here and they pick from it, so it's a common case
let (text, point) = point_from_cursor("debug(base::@)");
let document = Document::new(text.as_str(), None);
let context = DocumentContext::new(&document, point, None);
let completions = provide_completions(&context, &state).unwrap();
let completion = find_completion(&completions, "abs");

assert_eq!(completion.insert_text.unwrap(), String::from("abs"));
assert_eq!(
completion.insert_text_format.unwrap(),
InsertTextFormat::PLAIN_TEXT
);
assert!(completion.command.is_none());
})
}

#[test]
fn test_completions_dont_add_parentheses_inside_special_functions_triple_colon() {
r_task(|| {
let (text, point) = point_from_cursor("debug(utils:::.get@)");
let document = Document::new(text.as_str(), None);
let context = DocumentContext::new(&document, point, None);
let completions = provide_completions(&context, &WorldState::default()).unwrap();
let completion = find_completion(&completions, ".getHelpFile");

assert_eq!(
completion.insert_text.unwrap(),
String::from(".getHelpFile")
);
assert_eq!(
completion.insert_text_format.unwrap(),
InsertTextFormat::PLAIN_TEXT
);
assert!(completion.command.is_none());
})
}

#[test]
fn test_completions_dont_add_parentheses_for_help_operator_naked() {
r_task(|| {
let state = WorldState::default();

// Unary help
let (text, point) = point_from_cursor("?enc@");
let document = Document::new(text.as_str(), None);
let context = DocumentContext::new(&document, point, None);
let completions = provide_completions(&context, &state).unwrap();
let completion = find_completion(&completions, "enc2native");

assert_eq!(completion.insert_text.unwrap(), String::from("enc2native"));
assert_eq!(
completion.insert_text_format.unwrap(),
InsertTextFormat::PLAIN_TEXT
);
assert!(completion.command.is_none());

// Binary help
let (text, point) = point_from_cursor("method?enc@");
let document = Document::new(text.as_str(), None);
let context = DocumentContext::new(&document, point, None);
let completions = provide_completions(&context, &state).unwrap();
let completion = find_completion(&completions, "enc2native");

assert_eq!(completion.insert_text.unwrap(), String::from("enc2native"));
assert_eq!(
completion.insert_text_format.unwrap(),
InsertTextFormat::PLAIN_TEXT
);
assert!(completion.command.is_none());
})
}

#[test]
fn test_completions_dont_add_parentheses_for_help_operator_double_colon() {
r_task(|| {
let state = WorldState::default();

// Unary help
let (text, point) = point_from_cursor("?base::enc@");
let document = Document::new(text.as_str(), None);
let context = DocumentContext::new(&document, point, None);
let completions = provide_completions(&context, &state).unwrap();
let completion = find_completion(&completions, "enc2native");

assert_eq!(completion.insert_text.unwrap(), String::from("enc2native"));
assert_eq!(
completion.insert_text_format.unwrap(),
InsertTextFormat::PLAIN_TEXT
);
assert!(completion.command.is_none());

// Binary help
let (text, point) = point_from_cursor("method?base::enc@");
let document = Document::new(text.as_str(), None);
let context = DocumentContext::new(&document, point, None);
let completions = provide_completions(&context, &state).unwrap();
let completion = find_completion(&completions, "enc2native");

assert_eq!(completion.insert_text.unwrap(), String::from("enc2native"));
assert_eq!(
completion.insert_text_format.unwrap(),
InsertTextFormat::PLAIN_TEXT
);
assert!(completion.command.is_none());

// User hasn't typed any namespace name yet, but we show them a completion list
// here and they pick from it, so it's a common case
let (text, point) = point_from_cursor("?base::@");
let document = Document::new(text.as_str(), None);
let context = DocumentContext::new(&document, point, None);
let completions = provide_completions(&context, &state).unwrap();
let completion = find_completion(&completions, "enc2native");

assert_eq!(completion.insert_text.unwrap(), String::from("enc2native"));
assert_eq!(
completion.insert_text_format.unwrap(),
InsertTextFormat::PLAIN_TEXT
);
assert!(completion.command.is_none());
})
}
}

0 comments on commit ec931de

Please sign in to comment.