php-language-server icon indicating copy to clipboard operation
php-language-server copied to clipboard

Feature request: transform dot to -> after variables

Open NSExceptional opened this issue 4 years ago β€’ 5 comments

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. |

NSExceptional avatar Jul 22 '20 17:07 NSExceptional

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..

pjio avatar Jul 23 '20 06:07 pjio

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.

NSExceptional avatar Jul 23 '20 14:07 NSExceptional

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.

NSExceptional avatar Jul 23 '20 17:07 NSExceptional

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 ;-)

pjio avatar Jul 24 '20 14:07 pjio

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

NSExceptional avatar Jul 24 '20 15:07 NSExceptional