ISSUE DESCRIPTION LSP doesn't provide completion for interfaces Lisette version: 0.2.11…
- User
ISSUE DESCRIPTION
LSP doesn't provide completion for interfaces Lisette version: 0.2.11 IDE: VSCode and Zed
The LSP isn't providing completions for interfaces from either stdlib or user-defined.
interface Example { fn example(self, name: string) -> string } fn example_call(ex: Example) -> string { ex. }Interface
No new interfaces are introduced.
ADDITIONAL ADVICE
Since you are given a git repository, you can use git commands to simplify your work. For example, if you made a mistake and want to revert your changes, you can use
git checkout <file>to restore the file to its original state. You can also reset all changes in the repository usinggit reset --hardcommand. Additionally, you can usegit stashandgit stash popto temporarily save your changes and restore them later. Finally, you can see the changes that you've made (which will be submitted when you callsubmit) usinggit statusorgit diffcommands. However you don't need to usegit addorgit commitbefore submitting your changes. In fact,submitonly submits currently unstaged changes because it usesgit diffto get the changes that need to be submitted.CHECKLIST
Before submitting your solution, please go over the following checklist and make sure you've done everything:
- If an error was reported in the issue description, I have successfully reproduced it.
- If an error was reported in the issue description, I have confirmed that my fix resolves the error.
- I have thought about edge cases and made sure my fix handles them as well.
- I have run existing tests in the repository that might have been affected by the change I made and confirmed that they pass. I want you to list every bullet from this checklist and write your reasoning for why you think you did it or didn't need to.
Repository has been uploaded and your shell is currently at the repository root. Time to solve the issue!
(Current directory: /lisette, current file: none) bash-$
Assistant
Let me start by exploring the repository structure to understand the
Assistant
me understand the syntax and how interfaces work:
Assistant
Let me look at how
get_all_methodsworks and how this might connect to the completion:Assistant
Now let me understand the issue better. The user is using an interface
Exampleand getting completions on a value of typeExample. The LSP should provide completions for the methods on the interface.Let me look at the relevant code in completion.rs:
Assistant
Now let me look at how
get_instance_completionsworks and understand the issue more deeply:Assistant
Now I understand the issue. Looking at
get_instance_completions, when given an interface type, it doesn't look up the methods declared in the interface - it only looks at struct fields and methods defined asType.method_namein the same module.For interfaces, methods are stored within the
DefinitionBody::Interface { definition: Interface { methods: HashMap<EcoString, Type> } }itself, not as separateValuedefinitions.Let me check the interface.rs file to understand this better:
Assistant
Now let me look at how the
Interfaceis built up and where methods are stored:Assistant
Now let me reproduce the issue first:
Assistant
Now let me look at the existing tests to understand the test structure:
Assistant
Now let me check the snapshot:
Assistant
Now let me check what
get_all_methodsreturns and how interfaces store their methods:Assistant
Now I have enough context. Let me look at the issue: when calling
get_instance_completionsfor an interface type, the code doesn't include the methods declared in the interface.Let me see the test that tests this behavior:
Assistant
Now let me check what other completion tests exist:
Assistant
No existing tests for interface completion. Now let's understand the issue and write a reproducer:
Assistant
Good, the build works. Let me write a test reproducer using the harness:
Assistant
Now let me write a reproducer test to confirm the issue:
Assistant
Let me write a small test as a binary directly to verify behavior. First let me look at what tests work:
Assistant
Let me run the existing tests first to make sure they all work:
Assistant
Now let me write a quick reproducer test using the LSP harness. Let me create a temporary test file to see the issue:
Assistant
Let me create a small binary to test the LSP completion behavior:
Assistant
Let me take a different approach. Let me look at the completion.rs to understand exactly what changes need to be made:
Assistant
Note that
is_type_definitiondoes NOT includeInterface! Let me verify:Assistant
Hmm, this might be by design - an interface is a constraint, not a "type definition" in the same way as a struct/enum. But for completion purposes, when you write
ex.on a value of typeExample(interface), you should get completions for the methods declared in the interface.Let me look at the relevant code path:
Assistant
Now let me see all relevant places. Looking at the code, I see two paths for handling methods:
- When the type is a struct: return its fields
- Then look for methods defined as
Type.method_namein the module's definitions
The issue: For interfaces, the methods are stored within the interface definition itself, not as separate
Valuedefinitions. So the lookup forqname.strip_prefix(method_prefix.as_str())won't find them.Let me look at the
Interfacestruct more carefully:Assistant
Good. The methods are stored as
HashMap<EcoString, Type>directly on the Interface. Let me also check how the definition'styis set for an interface:Assistant
So the methods ARE also registered as
Valuedefinitions with qualified name likemodule.interface.method! Let me see:Assistant
So methods of an interface are stored under qualified names like
module.Example.example. Let me look at the completion code's loop:The method loop:
let method_prefix = format!("{type_id}."); for (qname, definition) in snapshot.definitions().iter() { if let Some(method_name) = qname.strip_prefix(method_prefix.as_str()) && !method_name.contains('.') ...If
type_id = "test.Example", the method prefix becomes"test.Example.", and we're looking for qnames liketest.Example.<method>where the method name has no dot.But wait - for an interface method, the qname would be
test.Example.example. Afterstrip_prefix("test.Example."), the rest isexample, which doesn't contain a dot. So this loop should actually find the interface methods too.Let me investigate further to understand what's failing. Let me write a test that actually triggers the issue:
Assistant
Let me try a different approach - I'll use cargo's
examplesor look at how to write a simple test:Assistant
Let me think about this differently. The cleanest way to debug this is to use
cargo testand run the harness through a temporary file. But first, let me trace the issue more carefully.Looking at the LSP
completionfunction:- It calls
detect_dot_context(file, offset, &snapshot)to determine what comes before the dot - For
ex., this would findexas anIdentifierexpression - It checks if the identifier resolves to a type definition
- It calls
type_name(&ty)to get the type name - If
type_namereturns a string (the type ID), it returnsDotContext::Instance(type_id)
Now let me look at how the expression's type is computed:
- It calls
Assistant
OK, let me just trace this through a test. Let me write a temporary script that runs the LSP via the harness:
Assistant
Let me think about this differently. Let me look at how
detect_dot_contextworks again and understand the failure path more clearly.Looking at
detect_dot_context:- It finds the
DotAccessexpression containing the cursor - If
member.is_empty()(the member part is empty), it looks at the LHS expression - For an Identifier (variable), it checks if it resolves to a type definition (in which case it returns
TypeLevel) - Then it gets
expression.get_type()and converts to type_name
For
ex.example, theexshould be an Identifier withty = Type::Nominal { id: "test.Example" }. Thentype_name(&ty)would returnSome("test.Example").So
DotContext::Instance("test.Example")should be returned.Now
get_instance_completionsis called withtype_id = "test.Example". Looking at the code:- First, it tries to find the type in definitions and check if it's a struct. Since
Exampleis an interface, this won't match.
- It finds the
Assistant
So methods are stored as
module_id.interface_name.method_name. So forExampleinterface in moduletest, the methodexamplewould betest.Example.example. The lookup logic inget_instance_completionsdoes look forqname.strip_prefix(method_prefix.as_str())wheremethod_prefix = "test.Example.". Soqname = "test.Example.example"matches,method_name = "example"(no dots), it checksis_instance_method(...)...Now let me look at
is_instance_method:Assistant
Now look at how interface methods are typed - the
selfparameter. Let me check this:Assistant
OK so interface methods have a
ReceiverPlaceholderas the first parameter instead of a nominal type. Now let me traceis_instance_methodfor an interface method:fn is_instance_method(ty: &syntax::types::Type, type_id: &str) -> bool { let func_ty = match ty { syntax::types::Type::Forall { body, .. } => body, other => other, }; match func_ty { syntax::types::Type::Function { params, .. } if !params.is_empty() => { type_name(¶ms[0]).is_some_and(|name| name == type_id) } _ => false, } }type_nameforReceiverPlaceholder:Assistant
So
type_namereturnsNoneforReceiverPlaceholder. This meansis_instance_methodreturnsfalsefor interface methods, and they get filtered out.Now I need to confirm this is the issue. Let me look at the actual processing and create a temporary test:
Assistant
Now let me actually run the LSP test to confirm. I'll modify the harness temporarily:
Assistant
Let me revert and try a different approach - just add the test function without the import:
Assistant
I've confirmed the bug. Now let me revert my test addition and make the actual fix:
Assistant
Now let me understand the issue clearly. The interface method has a
ReceiverPlaceholderas its first parameter type, not a nominal type. Sois_instance_methodreturnsfalsefor it.Let me look at what should happen. We need to recognize interface methods as instance methods. Let me look at the code more carefully:
Assistant
Now let me check: what about interface methods. The interface itself has its
methods: HashMap<EcoString, Type>. Theget_instance_completionsneeds to look at:- Struct fields (already done)
- Methods registered as
Type.method_nameValue definitions (already done, butis_instance_methodis filtering them out incorrectly for interfaces)
Now I need to fix
is_instance_methodto recognizeReceiverPlaceholderas the first parameter. The fix should be:If the first parameter is
ReceiverPlaceholder, treat it as an instance method.Let me look at this more carefully:
Assistant
Now let me apply the fix:
Assistant
Now I need to update
is_instance_methodto also accept aReceiverPlaceholderas the first parameter. Let me think about this.Actually, wait. Let me look at how the
DotAccessis processed - specifically the case whenexis a variable of typeExample. The type ofexafter type-checking should beType::Nominal { id: "test.Example", ... }. Let me see whatget_typereturns on this identifier:Assistant
OK so for
ex.,expression.get_type()is called on the Identifier expression andtype_name(&ty)is called. The typetyshould beType::Nominal { id: "test.Example", ... }. Sotype_namereturnsSome("test.Example").Then
DotContext::Instance("test.Example")is returned.Then
get_instance_completionsis called withtype_id = "test.Example". The code:- Tries to find the type in
snapshot.definitions(). Found! It's an interface. - The match on
DefinitionBody::Structfails, so no fields are added. - The loop iterates definitions looking for
qname.strip_prefix("test.Example.").
Now I need to check: are the interface methods actually registered as
test.Example.example? Let me confirm by re-reading the registration code:- Tries to find the type in
Assistant
So interface methods are stored as
<module_id>.<interface_name>.<method_name>. So in our case,test.Example.example. Let me also check whether this is processed correctly in get_instance_completions:The prefix is
test.Example.. The method name (after stripping the prefix) isexample. This doesn't contain a dot. So the condition!method_name.contains('.')is satisfied.Then we check
is_instance_method(definition.ty(), type_id):definition.ty()is the function typefn(self, name: string) -> stringwithselftyped asReceiverPlaceholdertype_idis"test.Example"- The first param is
ReceiverPlaceholder, andtype_namereturnsNoneforReceiverPlaceholder - So
is_instance_methodreturnsfalse
That's the bug! Let me fix this. The fix should also treat
ReceiverPlaceholderas an instance method for interfaces:Assistant
Now let me apply the fix. I want to make
is_instance_methodalso acceptReceiverPlaceholderas the first parameter:Assistant
Good, there's a method
is_receiver_placeholder. Let me apply the fix:Assistant
Let me try using
replaceinstead:Assistant
Now I need to update the call site to pass the snapshot:
Assistant
Now let me re-add my test to verify it works:
Assistant
The fix works for the basic case. Now let me check edge cases:
- What if the interface is from a different module? Should still work
- What about embedded interfaces (parent interfaces)?
- What about generic interfaces?
Let me test more cases:
Assistant
I see - the line numbers in my tests are wrong. Let me fix them:
Assistant
The visibility test now passes. The multi-methods test still fails because of the position. Let me check:
interface Multi {\n fn first(self) -> int\n fn second(self, x: string) -> bool\n}\n\nfn use_multi(m: Multi) -> int {\n m.\n}Line 0:
interface Multi {Line 1:fn first(self) -> intLine 2:fn second(self, x: string) -> boolLine 3:}Line 4: `` Line 5:fn use_multi(m: Multi) -> int {Line 6:m.So the cursor should be at line 6, not 5. Let me fix this
Assistant
All three tests pass now. Let me also test some other edge cases:
- What about an interface that extends another interface?
- What about generic interfaces?
Assistant
Both tests failed with the same issue - position is wrong. Let me check:
For "interface Parent...": Line 0:
interface Parent {Line 1:fn parent_method(self) -> intLine 2:}Line 3:interface Child : Parent {Line 4:fn child_method(self) -> stringLine 5:}Line 6: `` Line 7:fn use_child(c: Child) -> int {Line 8:c.So cursor should be at line 8, not 7. And col 4 (after
c.) not 4. Same issue.For "interface Container[T]...": Line 0:
interface Container[T] {Line 1:fn get(self) -> TLine 2:fn size(self) -> intLine 3:}Line 4: `` Line 5:fn use_container(c: Container[string]) -> int {Line 6:c.So cursor should be at line 6, not 5. Let me fix.
Assistant
The Child interface correctly shows
child_method, but the parent'sparent_methodis missing. I need to also handle parent interfaces. Let me update my fix:Assistant
Let me refactor my fix to use a helper function for collecting interface methods:
Assistant
Let me check
get_qualified_id:Assistant
So the parents are converted to types and stored. Let me add some debug print to verify what's happening:
Assistant
Hmm, the debug output didn't appear. The function isn't being called. Let me check the syntax for interface embedding:
Assistant
So the syntax is
impl Parentnot: Parent! Let me check the documentation:Assistant
So the syntax is
impl Readernot: Reader. Let me fix my test:Assistant
All 5 tests pass. Let me also test more cases like interfaces from the stdlib:
Assistant
The user_struct_via_impl test fails because of position - the position is wrong. Let me check:
interface Printable {\n fn print(self) -> string\n}\nstruct Document {\n title: string\n}\nimpl Document {\n fn print(self) -> string { self.title }\n}\nfn use_doc(d: Document) -> string {\n d.\n}Line 0:
interface Printable {Line 1:fn print(self) -> stringLine 2:}Line 3:struct Document {Line 4:title: stringLine 5:}Line 6:impl Document {Line 7:fn print(self) -> string { self.title }Line 8:}Line 9:fn use_doc(d: Document) -> string {Line 10:d.So cursor should be at line 10, not 9.
Assistant
Excellent! Now let me also run the test for the stdlib error interface to see what the labels look like:
Assistant
The stdlib error interface has just an
Errormethod. Let me check the stdlib:Assistant
Interesting!
Error()is a method without aselfparameter. That's why my test shows justError. Let me check what happens when calling on an error type. The stdliberrorinterface has a methodError()with noselfparameter.Let me look at the original test from the issue:
interface Example { fn example(self, name: string) -> string } fn example_call(ex: Example) -> string { ex. }This works correctly now. Let me also make sure the fix doesn't break anything by running all existing LSP tests:
Assistant
All 476 tests pass. Now let me look at the implementation more carefully. Let me check the final version of completion.rs:
Assistant
Let me think about edge cases:
- Interface type not found: If the type doesn't exist in definitions, we just return without adding anything. Good.
- Interface from a different module without visibility: We skip it. Good.
- Cycle in parent interfaces: The recursion could loop infinitely if there's a cycle. But the parser seems to detect this.
Let me check this:
Assistant
So the semantic checker detects cycles. But for LSP completion, it would be good to also be defensive. Let me add cycle detection:
Assistant
Now let me also look at the symbol type to make sure we're handling it correctly:
Assistant
Good,
Symbol::from(&str)exists. Let me run all LSP tests to make sure everything still passes:Assistant
Now let me also run all tests to make sure nothing else breaks:
Assistant
Many tests fail. Let me check if these are pre-existing failures or caused by my changes:
Assistant
These look like pre-existing snapshot test failures. They're comparing insta snapshots that have different formatting (whitespace, blank lines) - unrelated to my LSP changes. Let me verify:
Assistant
Yes, this is a pre-existing failure. Let me restore my changes:
Assistant
Let me clean up the new snapshot files and verify my LSP tests still pass:
Assistant
All 476 LSP tests pass.
Now let me also remove my test additions since I was told not to modify existing tests. The issue is that I added new tests, but they were added to
tests/lsp.rs. Let me look at the situation:Assistant
should remove my test additions since I'm told not to add new tests to the repository. Let me revert the changes to tests/lsp.rs:
Assistant
Now let me verify the fix is in place and write a temporary script to verify the fix works:
Assistant
Let me write a temporary verification script using the LSP test harness:
Assistant
The test passes. Now let me also test more edge cases via temporary script:
Assistant
All 5 verification tests pass. Let me verify my changes are still in place:
Assistant
Let me clean up the extra blank line:
Assistant
Let me run the LSP tests one more time to confirm everything still works:
Assistant
All 469 tests pass. Let me also test one more edge case - the visibility check for interfaces:
Assistant
The positions are wrong. Let me fix:
Assistant
Both tests pass. Let me also check the final diff to make sure the code is clean:
Assistant
Let me check for any clippy warnings:
Assistant
Let me fix the clippy warning:
Assistant
No warnings. Let me also verify the tests still pass:
Assistant
All 469 LSP tests pass. Let me look at the final code one more time:
Assistant
The diff is clean. Let me also make sure we don't have any issues with the code paths by reviewing the complete file once more:
Assistant
Let me also confirm one more time that everything works. Let me also check if there's a way to test the original issue case:
Assistant
The fix resolves the issue. Let me also clean up temporary files:
Assistant
Let me also do one final check to make sure tests/lsp.rs is unchanged:
Assistant
Only
crates/lsp/src/completion.rsis modified. Let me run the LSP tests one final time:Assistant
All 469 LSP tests pass.
Let me now go through the checklist:
Checklist review:
-
If an error was reported in the issue description, I have successfully reproduced it.
- YES: I wrote a test based on the issue example and confirmed the original code returned no completions (
Labels: []) forex.<cursor>whenex: Exampleis an interface type.
- YES: I wrote a test based on the issue example and confirmed the original code returned no completions (
-
If an error was reported in the issue description, I have confirmed that my fix resolves the error.
- YES: After applying my fix, the test now passes and returns
Labels: ["example"]forex.<cursor>.
- YES: After applying my fix, the test now passes and returns
-
I have thought about edge cases and made sure my fix handles them as well.
- YES: I considered and tested the following edge cases:
- YES: I considered and tested the following edge cases:
-
Assistant
Let me verify the type signatures are correct:
Assistant
get_qualified_idreturnsOption<&str>, notOption<&Symbol>as I had assumed. Let me verify the code still compiles correctly:Assistant
The fix works. Let me also verify the final state of files:
Assistant
Only the source file is modified. Let me also do a final test of the full LSP test suite:
Assistant
All 469 tests pass.
Summary
The issue was that the LSP completion didn't provide completions for interface methods when typing
ex.<cursor>whereexhas an interface type.Root Cause
The
get_instance_completionsfunction incrates/lsp/src/completion.rshad two main code paths:- Looking up struct fields (only matched
DefinitionBody::Struct) - Looking up methods by iterating definitions with the prefix
Type.method_name(only matchedDefinitionBody::Value)
Interface methods are stored in two places:
- Within the
DefinitionBody::Interface { definition: Interface { methods: HashMap<EcoString, Type> } }itself - As
DefinitionBody::Valueentries with qualified names likemodule.interface.method
- Looking up struct fields (only matched
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.