Skip to content

Be willing to complete in the face of emptiness #772

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 30 additions & 7 deletions crates/ark/src/lsp/completions/sources/composite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ use std::collections::HashMap;
use stdext::*;
use tower_lsp::lsp_types::CompletionItem;
use tower_lsp::lsp_types::CompletionItemKind;
use tree_sitter::Node;

use crate::lsp::completions::completion_context::CompletionContext;
use crate::lsp::completions::sources::collect_completions;
Expand Down Expand Up @@ -57,7 +56,7 @@ pub(crate) fn get_completions(

// For the rest of the general completions, we require an identifier to
// begin showing anything.
if is_identifier_like(completion_context.document_context.node) {
if is_identifier_like(completion_context) {
push_completions(keyword::KeywordSource, completion_context, &mut completions)?;

push_completions(
Expand Down Expand Up @@ -172,8 +171,10 @@ fn sort_completions(completions: &mut Vec<CompletionItem>) {
}
}

fn is_identifier_like(x: Node) -> bool {
if x.is_identifier() {
fn is_identifier_like(completion_context: &CompletionContext) -> bool {
let node = completion_context.document_context.node;

if node.is_identifier() {
// Obvious case
return true;
}
Expand All @@ -185,12 +186,29 @@ fn is_identifier_like(x: Node) -> bool {
// - `for<tab>` should provide completions for things like `forcats`
// - `for<tab>` should provide snippet completions for the `for` snippet
// The keywords here come from matching snippets in `r.code-snippets`.
if matches!(x.node_type(), NodeType::Anonymous(kind) if matches!(kind.as_str(), "if" | "for" | "while"))
if matches!(node.node_type(), NodeType::Anonymous(kind) if matches!(kind.as_str(), "if" | "for" | "while"))
{
return true;
}

return false;
// Consider when the user asks for completions with no existing
// text-to-complete, such as at the R prompt in the Console or in an empty R
// file.
// Gesture-wise, a Positron user could do this with Ctrl + Space, which
// invokes the command editor.action.triggerSuggest.
// The nominal completion node in these cases is basically degenerate, i.e.
// it's just the root node of the AST.
// In this case, we should just provide "all" completions, for some
// reasonable definition of "all".
// TODO: Handle the related case of asking for completions on an empty line
// of a non-empty R file. In this case, we will have latched on to some
// neighboring node, but perhaps should not. The text extracted from this
// node is usually not an identifier and this function will return false.
if node.node_type() == NodeType::Program {
return true;
}

false
}

#[cfg(test)]
Expand All @@ -206,6 +224,9 @@ mod tests {

#[test]
fn test_completions_on_anonymous_node_keywords() {
use crate::lsp::completions::completion_context::CompletionContext;
use crate::lsp::state::WorldState;

r_task(|| {
// `if`, `for`, and `while` in particular are both tree-sitter
// anonymous nodes and snippet keywords, so they need to look like
Expand All @@ -214,7 +235,9 @@ mod tests {
let point = Point { row: 0, column: 0 };
let document = Document::new(keyword, None);
let context = DocumentContext::new(&document, point, None);
assert!(is_identifier_like(context.node));
let state = WorldState::default();
let completion_context = CompletionContext::new(&context, &state);
assert!(is_identifier_like(&completion_context));
assert_eq!(
context.node.node_type(),
NodeType::Anonymous(keyword.to_string())
Expand Down