Skip to content

Commit

Permalink
fix: pseudo method completion
Browse files Browse the repository at this point in the history
  • Loading branch information
mtshiba committed Jan 4, 2025
1 parent a511a24 commit c7178c0
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 15 deletions.
42 changes: 28 additions & 14 deletions crates/els/completion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ impl_u8_enum! { CompletionOrder; i32;
Normal = 1000000,
Builtin = 1,
OtherNamespace = 2,
PseudoMethod = 16,
Escaped = 32,
DoubleEscaped = 64,
}
Expand Down Expand Up @@ -172,6 +173,8 @@ impl<'b> CompletionOrderSetter<'b> {
orders.push(CompletionOrder::DoubleEscaped);
} else if self.label.starts_with('_') {
orders.push(CompletionOrder::Escaped);
} else if self.label.starts_with("Function::") {
orders.push(CompletionOrder::PseudoMethod);
}
if self.kind.is_builtin() {
orders.push(CompletionOrder::Builtin);
Expand Down Expand Up @@ -663,6 +666,7 @@ impl<Checker: BuildRunnable, Parser: Parsable> Server<Checker, Parser> {
)
.set(&mut item);
item.kind = Some(comp_item_kind(&ty, Mutability::Immutable));
self.set_pseudo_method_comp(&uri, pos, &comp_kind, &mut item)?;
already_appeared.insert(item.label.clone());
result.push(item);
}
Expand Down Expand Up @@ -728,20 +732,7 @@ impl<Checker: BuildRunnable, Parser: Parsable> Server<Checker, Parser> {
.set(&mut item);
item.kind = Some(comp_item_kind(&vi.t, vi.muty));
item.data = Some(Value::String(vi.def_loc.to_string()));
// s.`Function::map` => map(s)
if comp_kind.should_be_method() && item.label.starts_with("Function::") {
let receiver = self.get_receiver(&uri, pos)?;
if let Some(mut range) = receiver.as_ref().and_then(|expr| loc_to_range(expr.loc()))
{
// FIXME:
let s_receiver = self.file_cache.get_ranged(&uri, range)?.unwrap_or_default();
range.end.character += 1;
let name = item.label.trim_start_matches("Function::");
let remove = TextEdit::new(range, "".to_string());
item.insert_text = Some(format!("{name}({s_receiver})"));
item.additional_text_edits = Some(vec![remove]);
}
}
self.set_pseudo_method_comp(&uri, pos, &comp_kind, &mut item)?;
already_appeared.insert(item.label.clone());
result.push(item);
}
Expand All @@ -759,6 +750,29 @@ impl<Checker: BuildRunnable, Parser: Parsable> Server<Checker, Parser> {
Ok(Some(CompletionResponse::Array(result)))
}

// s.`Function::map` => map(s)
fn set_pseudo_method_comp(
&self,
uri: &NormalizedUrl,
pos: Position,
comp_kind: &CompletionKind,
item: &mut CompletionItem,
) -> ELSResult<()> {
if comp_kind.should_be_method() && item.label.starts_with("Function::") {
let receiver = self.get_receiver(uri, pos)?;
if let Some(mut range) = receiver.as_ref().and_then(|expr| loc_to_range(expr.loc())) {
// FIXME:
let s_receiver = self.file_cache.get_ranged(uri, range)?.unwrap_or_default();
range.end.character += 1;
let name = item.label.trim_start_matches("Function::");
let remove = TextEdit::new(range, "".to_string());
item.insert_text = Some(format!("{name}({s_receiver})"));
item.additional_text_edits = Some(vec![remove]);
}
}
Ok(())
}

fn get_attr_completion_by_name(
&self,
comp_kind: &CompletionKind,
Expand Down
2 changes: 1 addition & 1 deletion crates/erg_compiler/context/initialize/classes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4060,7 +4060,7 @@ impl Context {
/* Set! */
let set_mut_t = poly(MUT_SET, vec![ty_tp(T.clone()), N]);
let mut set_mut_ =
Self::builtin_poly_class(MUT_SET, vec![PS::t_nd(TY_T), PS::named_nd(TY_N, Nat)], 2);
Self::builtin_poly_class(MUT_SET, vec![PS::t_nd(TY_T), PS::default(TY_N, Nat)], 2);
set_mut_.register_superclass(set_t.clone(), &set_);
let mut set_mut_copy = Self::builtin_methods(Some(mono(COPY)), 1);
set_mut_copy.register_py_builtin(
Expand Down

0 comments on commit c7178c0

Please sign in to comment.