Incomplete AST for completion?

Hamish Rodda rodda at kde.org
Tue Mar 31 01:45:19 UTC 2009


On Tuesday 31 March 2009 10:25:15 Milian Wolff wrote:
> Hi guys!
>
> Would it be possible to use the AST when building the completion items? I
> really dislike that we have to do so much string operations for evaluating
> the type of completion. Yet the problem I see is that completion should
> still (somewhat) work when the document is invalid.
>
> So coul incomplete AST exist? Could we use these in the completion context?
>
> Background:
> Look at the following PHP snippets, assume that your cursor is at "..." and
> you request the completion list. In all of these cases the uppercase
> identifiers have to be filtered from the result:
>
> should only show classes but not ASDF itself:
> class ASDF extends ...
>
> should only show interfaces but neither FOO nor BAR:
> class asdf implements FOO, BAR, ...
>
> should only show interfaces but neither FOO nor BAR nor ASDF:
> interface FOO extends BAR, ASDF, ...
>
> There are possibly far more examples like these. Assuming I'd have an
> incomplete AST up to the position of the cursor, couldn't I then easily
> traverse the tree to get whatever I need? Furthermore: Wouldn't I easily
> know what kind of stuff the user is trying to write (compare with the
> "expected FOO BAR" messages our parsers emit) - that kind of info would be
> very handy for evaluation the list of completion items!
>
> Any idea if this is at all possible?

I thought about using the lexer to tokenise the current context up to the 
completion point, to make the completion context creation easier than doing 
many string comparisons, but to actually create an ast is going to require 
that the code is well-formed, and when you're doing code completion, that's 
usually not the case.  So perhaps you could just try lexing the text, work 
back to some common tokens (eg ";" ")" "}" for c++), and then working forwards 
to determine the completion context.

Cheers,
Hamish.




More information about the KDevelop-devel mailing list