Skip to content

Eliminate the snippet source #782

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

Merged
merged 9 commits into from
Apr 29, 2025
181 changes: 0 additions & 181 deletions crates/ark/resources/snippets/r.code-snippets

This file was deleted.

43 changes: 27 additions & 16 deletions crates/ark/src/lsp/completions/sources/composite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ mod document;
mod keyword;
pub(crate) mod pipe;
mod search_path;
mod snippets;
mod subset;
mod workspace;

Expand All @@ -27,6 +26,23 @@ use crate::lsp::completions::sources::CompletionSource;
use crate::treesitter::NodeType;
use crate::treesitter::NodeTypeExt;

#[derive(Clone, Hash, PartialEq, Eq)]
struct CompletionItemKey {
label: String,
kind_str: String,
}

impl CompletionItemKey {
fn new(item: &CompletionItem) -> Self {
Self {
label: item.label.clone(),
kind_str: item
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can't just use the completion item kind here, without dealing with a hash implementation for that enum. So I decided to just use a string representation, which is convenient for logging anyway.

.kind
.map_or_else(|| "Text".to_string(), |k| format!("{:?}", k)),
}
}
}

// Locally useful data structure for tracking completions and their source
#[derive(Clone, Default)]
struct CompletionItemWithSource {
Expand Down Expand Up @@ -60,12 +76,6 @@ pub(crate) fn get_completions(
if is_identifier_like(completion_context.document_context.node) {
push_completions(keyword::KeywordSource, completion_context, &mut completions)?;

push_completions(
snippets::SnippetSource,
completion_context,
&mut completions,
)?;

push_completions(
search_path::SearchPathSource,
completion_context,
Expand Down Expand Up @@ -94,7 +104,7 @@ pub(crate) fn get_completions(
fn push_completions<S>(
source: S,
completion_context: &CompletionContext,
completions: &mut HashMap<String, CompletionItemWithSource>,
completions: &mut HashMap<CompletionItemKey, CompletionItemWithSource>,
) -> anyhow::Result<()>
where
S: CompletionSource,
Expand All @@ -103,15 +113,17 @@ where

if let Some(source_completions) = collect_completions(source, completion_context)? {
for item in source_completions {
if let Some(existing) = completions.get(&item.label) {
let key = CompletionItemKey::new(&item);
if let Some(existing) = completions.get(&key) {
log::trace!(
"Completion with label '{}' already exists (first contributed by source: {}, now also from: {})",
item.label,
"Completion with label '{}' and kind '{:?}' already exists (first contributed by source: {}, now also from: {})",
key.label,
key.kind_str,
existing.source,
source_name
);
} else {
completions.insert(item.label.clone(), CompletionItemWithSource {
completions.insert(key, CompletionItemWithSource {
item,
source: source_name.to_string(),
});
Expand All @@ -124,7 +136,7 @@ where

/// Produce plain old CompletionItems and sort them
fn finalize_completions(
completions: HashMap<String, CompletionItemWithSource>,
completions: HashMap<CompletionItemKey, CompletionItemWithSource>,
) -> Vec<CompletionItem> {
let mut items: Vec<CompletionItem> = completions
.into_values()
Expand Down Expand Up @@ -183,8 +195,7 @@ fn is_identifier_like(x: Node) -> bool {
// non-`identifier` kinds. However, we do still want to provide completions
// here, especially in two cases:
// - `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`.
// - completions of certain reserved words from the keyword source
if matches!(x.node_type(), NodeType::Anonymous(kind) if matches!(kind.as_str(), "if" | "for" | "while"))
{
return true;
Expand All @@ -208,7 +219,7 @@ mod tests {
fn test_completions_on_anonymous_node_keywords() {
r_task(|| {
// `if`, `for`, and `while` in particular are both tree-sitter
// anonymous nodes and snippet keywords, so they need to look like
// anonymous nodes and keywords, so they need to look like
// identifiers that we provide completions for
for keyword in ["if", "for", "while"] {
let point = Point { row: 0, column: 0 };
Expand Down
Loading