php-language-server
php-language-server copied to clipboard
Feature request: transform dot to -> after variables
Background
VS Code's CPP tools extension recently added a feature Visual Studio itself has long had for CXX development:
the ability to automatically "fix" member/dot access (variable.member
) on pointers and change it to a dereference/arrow operator (variable->member
)
Request
I would love the same for PHP. When I start typing this:
$person.| // cursor β |
I would like intellisense to start suggesting things as if I had typed this:
$person->|
βββββββββββββββ
βname β
βββββββββββββββ€
βage β
βββββββββββββββ€
βsalary β
βββββββββββββββ
and once I select an intellisense suggestion, it should change this:
$person.selectedSuggestion|
to this:
$person->selectedSuggestion|
I love PHP, but I hate the arrow operator. This would be a huge QOL improvement for me, especially if it would work on function calls, class instantiations, subscripting results, etc.
Potential issues
PHP uses the dot to concatenate strings. I don't personally see this being a problem as long as it doesn't change .
to ->
unless you select an intellisense suggestion. Suggestions also shouldn't appear if you put a space right after the dot, as in
$myString. |
Looks like a client side issue to me. I assume if the server changed to this behavior, dozens of language clients would have to be modified to handle the changed response. If a plugin on the client side just converts $foo.
to $foo->
and then invokes the server, it should work for everyone..
I assume if the server changed to this behavior, dozens of language clients would have to be modified to handle the changed response
I'm not sure what you mean by this, can you elaborate? Does the LSP not provide a way to modify existing text behind the cursor when a suggestion is chosen?
I would assume it does, since the VS Code CPP tools extension works exactly as I've described, and it's a language server.
After some digging, there's no LSP limitation. The VS Code CPP Tools extension does exactly what I suggested.
Relevant LSP section: https://microsoft.github.io/language-server-protocol/specification#textDocument_completion
The CompletionItem
object has a property called additionalTextEdits
that allows you to make changes to the document behind the cursor.
You're right about this! I wasn't aware how much the protocol allows to control text edits on the client.
The member access logic is handled here: https://github.com/felixfbecker/php-language-server/blob/master/src/CompletionProvider.php#L240
The MemberAccessExpression
is part of Microsofts Tolerant PHP Parser.
For $this.
the node Microsoft\PhpParser\Node\Expression\BinaryExpression
would have to be handled...
Maybe it's possible... but I'm just a regular user with some time to play around ;-)
Well for pointing me in the right direction, I can take a look and try to make a PR π
I may need some background on how to debug this thing though, the readme section wasn't quite clear enough for someone who has never made a VS Code extension