-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Name nodes for property names etc #322
Comments
Preliminary implementation: https://github.com/nikic/PHP-Parser/compare/identifiersAsNodes If the I'm using a new node type here, because I'd like to keep a distinction between namespaced and non-namespaced names. Many of the methods on I've kept the contents of One case that still uses strings are built-in type annotations like |
I agree, good point. What do you think about a What do you think about making the |
In most cases it is the complete name though, so something like
Not totally sold on this yet, though not sure why... I suspect this may have weird interactions with name creation and modification. E.g. if you run the name resolver, names will have some parts "located" in the namespace-declaration (or a use-clause) and some parts "located" at the actual location of the name. That seems weird to me, but maybe it's okay. Relatedly, if you do something like |
I've now changed I think that while we're at it, we should also take a look at other cases where we currently represent something as plain strings without a wrapping node. In particular, we have a number of places where variable names are presented as simple strings without a wrapping
The following two syntactically appear as variables, but really aren't. I'm not totally sure how these should be presented. Would it be inappropriate to use
|
It took a while, but I get what you mean. But even at the moment, the
I would assume
What about
Definitely! In fact, I actually need these all to do proper go-to-definition. Would you then do an
I would say ket's do it in every case where the offset information would be different. If the
Yes, that will finally enable differentiating the class part and the variable part. But I guess |
Because in most cases it's not going to be a part of a name. I can see the argument that for
In these cases I would use a
PropertyProperty can also contain an initializer expression, in which case the Identifier would encompass a smaller range. As to what the Identifier would contain here: It should not include the
Making this a |
I've pushed a few more changes:
I'm not really happy with point two. I don't want to use |
I agree, it is more of a variable declaration and therefor a |
Probably time to move forward with this. In combination with #41, which based on the work here implements some major changes to make format-preserving code transformations possible, I will probably just fork version 4.0 now, so I have more freedom in changing the AST without adding options for all the things. |
In version 4.0 Identifier nodes are now always used (no longer behind an option), so I'm considering this to be resolved. |
Currently there is no way to get the offset information of the name of a
PropertyFetch
orMethodCall
. Assume having code like this:$obj
has two methods,whatever
andwhatever2
.The user's cursor is behind
what
, I will suggest both methods.First issue is the node under the cursor will resolve to the whole
MethodCall
node. Therefor I cannot find out what prefix the user has already typed (what
) and cannot filter by that. If the method name was a node, I could just get the substring of the method name between the start offset and the cursor position offset.Second issue is when the user selects
whatever2
, I need to tell the editor to replace the method name (and only the method name) withwhatever2
. But since it is not a node, it does not have offset information, and all I can do is insert it directly likeor solve it with a smart algorithm that "guesses" by the source code string how to insert it without overlapping parts.
Other places where this issue occurs is in find-all-references, where a method reference will always highlight the whole
MethodCall
node instead of just the method node.Also the other way around, clicking anywhere except the variable node inside the
MethodCall
(even the->
operator) will resolve to the definition of the method, not just the name.The same problem is also true for classes.
extends
andimplements
contain properName
nodes, but the class name itself is just a string. Invoking find-all-references should only work on the class name, but in reality you can click anywhere in the class, even whitespace, to get the references of the class.I would propose to make all string "names" a
Name
node so we can always get offset information.The node should probably not be a
Node\Name\FullyQualified
node with parts, but just a simpleNode\Name
node that wraps the string and exposes the offset information like any other node.This is also an argument in favor of having a
parts
sub-node array forFullyQualifiedName
:would would ideally be
Where each part has its own offset information, but the whole
Name\FullyQualified
node still just joins the parts with a backslash when calling__toString()
. That would make it much easier to provide completion for namespaces, like when you dowhere the cursor is behind
Other
, the node would resolve to theNode\Name
node withOtherNamespace
value. I can then do suggestions filtered by theOther
prefix and when the user selects one, I can tell the editor to replace only theOtherNamespace
node. Currently I can only replace the wholeNode\Name\FullyQualified
name node, or I have to do my own custom string parsing (searching for the next backslash) to find out what needs to be replaced.The last proposal is kind of optional, because as I just said it is possible to accomplish this, but I just wanted for you to consider this because it is a counter-argument to removing the
parts
array.The text was updated successfully, but these errors were encountered: