Provide code completion items for field member expressions
Review Request #118465 - Created June 2, 2014 and submitted
Information | |
---|---|
Denis Steckelmacher | |
kdev-qmljs | |
master | |
Reviewers | |
kdevelop | |
This patch adds support for field member expressions ("base.identifier") to the QML/JS code-completion. When "something." is typed by the user, KDevelop displays the list of symbols accessible from the inner context of something. "something" can be any expression recognized by ExpressionVisitor, so this will continue to work when ExpressionVisitor will be able to recognize "table["test"][repository.getIndice()].". The patch is inspired from how Python handles such completion. The idea is to try to parse as much as possible of the line being edited, in order to have the most complete AST possible. When this is done, ExpressionVisitor is given the current context and visits this partial AST. The declaration it returns (if any) is used to get the code-completion items. The only thing that doesn't quite please me in this patch is that I parse the current line several times (each time with one character removed from the left). It means that if the user is editing a very long line (say 200 characters), QmlJS::Document::parse will be called up to 200 times! Is there another mean to detect which part of the current line contains a valid expression? Do you think that it would be worth it to complexify the code by trying to skip more than one character each time (for instance, by skipping directly to the next dot or bracket).
Manual testing shows that everything work as expected. For instance, the completions shown at the cursor (|) in this snippet are all the completions valid in button: Label { id: foo bar: "baz" Button { id: button property int my_property; } onTest: { console.log(button.|); // my_property listed } }
In principle I like it, but I don't think the "remove characters one by one" approach is a particularily good one. Can you maybe use the QMLJS lexer to lex the remainder of the line, and then have some simple logic to decide which tokens form a complete expression?
Review request changed
Change Summary:
Use the QMLJS lexer to skip more than one character at a time when a sub-expression cannot be parsed, and ensure that a fresh QmlJS::Document is used for each parse (this is inefficient but there are asserts in QmlJS::Document that require that).
Diff: |
Revision 2 (+64 -2) |
---|
Nice work getting this done so quickly, and certainly better than the previous solution. What I actually meant however was something more clever, similar to how Python does it: you can know from the tokens whether the expression can be parsed or not (for example by going backwards to the next unmatched parenthesis or quotation mark) with fairly little code. Do you think that'd make sense here? It would avoid the repeated trial-and-error parsing.
Review request changed
Change Summary:
Use un-balanced braces to find where a valid sub-expression could start in a partial expression.
Diff: |
Revision 3 (+76 -2) |
---|
I'm not sure if this is all which is needed, but it's certainly a very good start which will be easy to extend now. Thanks!
-
codecompletion/context.cpp (Diff revision 3) -
What you actually want to do is not necessarily parse the largest possible expression, but the one which is relevant for your completion context. Which usually happens to be the largest one. ;)
This review has been submitted with commit acd20fadac0e415a047d950e9e329476caba1a26 by Denis Steckelmacher to branch master.