Skip to content
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

Analyze completion site re: trailing parens #680

Open
wants to merge 17 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
3 changes: 2 additions & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
},
"windows": {
"program": "ark.exe"
}
},
"sourceLanguages": ["rust"]
Comment on lines +17 to +18
Copy link
Member Author

Choose a reason for hiding this comment

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

I think this is a neutral-to-positive move for us.

},
{
"type": "lldb",
Expand Down
1 change: 1 addition & 0 deletions crates/ark/src/lsp/completions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//

mod completion_item;
mod parameter_hints;
mod provide;
mod resolve;
mod sources;
Expand Down
85 changes: 65 additions & 20 deletions crates/ark/src/lsp/completions/completion_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ use tower_lsp::lsp_types::Range;
use tower_lsp::lsp_types::TextEdit;
use tree_sitter::Node;

use crate::lsp::completions::parameter_hints::ParameterHints;
use crate::lsp::completions::types::CompletionData;
use crate::lsp::completions::types::PromiseStrategy;
use crate::lsp::document_context::DocumentContext;
Expand Down Expand Up @@ -169,6 +170,7 @@ pub(super) unsafe fn completion_item_from_package(
pub(super) fn completion_item_from_function(
name: &str,
package: Option<&str>,
parameter_hints: ParameterHints,
) -> Result<CompletionItem> {
let label = format!("{}", name);
let mut item = completion_item(label, CompletionData::Function {
Expand All @@ -182,17 +184,23 @@ pub(super) fn completion_item_from_function(
item.label_details = Some(label_details);

let insert_text = sym_quote_invalid(name);
item.insert_text_format = Some(InsertTextFormat::SNIPPET);
item.insert_text = Some(format!("{insert_text}($0)"));

// provide parameter completions after completing function
item.command = Some(Command {
title: "Trigger Parameter Hints".to_string(),
command: "editor.action.triggerParameterHints".to_string(),
..Default::default()
});
if parameter_hints.is_enabled() {
item.insert_text_format = Some(InsertTextFormat::SNIPPET);
item.insert_text = Some(format!("{insert_text}($0)"));

return Ok(item);
// provide parameter completions after completing function
item.command = Some(Command {
title: "Trigger Parameter Hints".to_string(),
command: "editor.action.triggerParameterHints".to_string(),
..Default::default()
});
} else {
item.insert_text_format = Some(InsertTextFormat::PLAIN_TEXT);
item.insert_text = Some(insert_text);
}

Ok(item)
}

fn item_details(package: Option<&str>) -> CompletionItemLabelDetails {
Expand Down Expand Up @@ -245,9 +253,17 @@ pub(super) unsafe fn completion_item_from_object(
envir: SEXP,
package: Option<&str>,
promise_strategy: PromiseStrategy,
parameter_hints: ParameterHints,
) -> Result<CompletionItem> {
if r_typeof(object) == PROMSXP {
return completion_item_from_promise(name, object, envir, package, promise_strategy);
return completion_item_from_promise(
name,
object,
envir,
package,
promise_strategy,
parameter_hints,
);
}

// TODO: For some functions (e.g. S4 generics?) the help file might be
Expand All @@ -256,7 +272,7 @@ pub(super) unsafe fn completion_item_from_object(
// In other words, when creating a completion item for these functions,
// we should also figure out where we can receive the help from.
if Rf_isFunction(object) != 0 {
return completion_item_from_function(name, package);
return completion_item_from_function(name, package, parameter_hints);
}

let mut item = completion_item(name, CompletionData::Object {
Expand Down Expand Up @@ -287,12 +303,20 @@ pub(super) unsafe fn completion_item_from_promise(
envir: SEXP,
package: Option<&str>,
promise_strategy: PromiseStrategy,
parameter_hints: ParameterHints,
) -> Result<CompletionItem> {
if r_promise_is_forced(object) {
// Promise has already been evaluated before.
// Generate completion item from underlying value.
let object = PRVALUE(object);
return completion_item_from_object(name, object, envir, package, promise_strategy);
return completion_item_from_object(
name,
object,
envir,
package,
promise_strategy,
parameter_hints,
);
}

if promise_strategy == PromiseStrategy::Force && r_promise_is_lazy_load_binding(object) {
Expand All @@ -302,7 +326,14 @@ pub(super) unsafe fn completion_item_from_promise(
// important for functions, where we also set a `CompletionItem::command()`
// to display function signature help after the completion.
let object = r_promise_force_with_rollback(object)?;
return completion_item_from_object(name, object.sexp, envir, package, promise_strategy);
return completion_item_from_object(
name,
object.sexp,
envir,
package,
promise_strategy,
parameter_hints,
);
}

// Otherwise we never want to force promises, so we return a fairly
Expand Down Expand Up @@ -342,19 +373,28 @@ pub(super) unsafe fn completion_item_from_namespace(
name: &str,
namespace: SEXP,
package: &str,
parameter_hints: ParameterHints,
) -> Result<CompletionItem> {
// First, look in the namespace itself.
if let Some(item) =
completion_item_from_symbol(name, namespace, Some(package), PromiseStrategy::Force)
{
if let Some(item) = completion_item_from_symbol(
name,
namespace,
Some(package),
PromiseStrategy::Force,
parameter_hints,
) {
return item;
}

// Otherwise, try the imports environment.
let imports = ENCLOS(namespace);
if let Some(item) =
completion_item_from_symbol(name, imports, Some(package), PromiseStrategy::Force)
{
if let Some(item) = completion_item_from_symbol(
name,
imports,
Some(package),
PromiseStrategy::Force,
parameter_hints,
) {
return item;
}

Expand All @@ -376,7 +416,10 @@ pub(super) unsafe fn completion_item_from_lazydata(
// long time to load.
let promise_strategy = PromiseStrategy::Simple;

match completion_item_from_symbol(name, env, Some(package), promise_strategy) {
// Lazydata objects are never functions, so this doesn't really matter
let parameter_hints = ParameterHints::Enabled;

match completion_item_from_symbol(name, env, Some(package), promise_strategy, parameter_hints) {
Some(item) => item,
None => {
// Should be impossible, but we'll be extra safe
Expand All @@ -390,6 +433,7 @@ pub(super) unsafe fn completion_item_from_symbol(
envir: SEXP,
package: Option<&str>,
promise_strategy: PromiseStrategy,
parameter_hints: ParameterHints,
) -> Option<Result<CompletionItem>> {
let symbol = r_symbol!(name);

Expand Down Expand Up @@ -422,6 +466,7 @@ pub(super) unsafe fn completion_item_from_symbol(
envir,
package,
promise_strategy,
parameter_hints,
))
}

Expand Down
Loading
Loading