Php-language-server: Automatically add use statement

Created on 9 Jan 2017  路  28Comments  路  Source: felixfbecker/php-language-server

When adding a class in the code, it would be great if the use statement was automatically written into the top of the file. Thanks for considering!

feature

Most helpful comment

Please don't +1 issues. It clutters the thread without adding value to the discussion and spams maintainers with notifications. Use GitHub reactions to upvote features.
image

All 28 comments

I would love to have this, and it is not even hard that hard to implement.

Glad to hear it wouldn't be difficult. I hope you'll forgive me for not contributing beyond the request 0:-)

:+1:

Please don't +1 issues. It clutters the thread without adding value to the discussion and spams maintainers with notifications. Use GitHub reactions to upvote features.
image

@felixfbecker Is there any protocol I can use?

yeah, you can return additionalTextEdits from textDocument/completion

@felixfbecker How to get the positon of the first php start tag after the namespace definition? I don't have any (serious) idea. Should I set a variable in PhpDocument with the place to enter while parsing the thing?

Theres a getClosestNode() in helpers which you can use to find the closest namespace Node, then you can easily get the first statement inside it.

Beautiful. I thought I need to implement this functionality myself.

Ah, there is a bug. getClosestNode will return void instead of null. Short pull request comes...

Functions that return nothing return null.

This does not. That's the bug.

function foo() {
}

$f = foo();

var_dump($f); // NULL

It's better to return that from the code, isn't it?

It makes not difference

okay.

How is the PHP start tag called in php-parser? I'd looked in the files, but I couldn't find anything related.
Node\Stmt\Namespace_ is for Namespaces
??? is for the PHP start tag?

There is no PHP start tag, only InlineHtml

But I can't rely on that, can I? I need to find the first "save" location where I can insert the use statement. At the moment I'm searching for a fallback when there is no namespace statement.

before the first class definition?

yes, and maybe behind the declare node(implemented). But I need a fallback.

The fallback would just be at the root? Which is the $stmts array

But then it may be possible to get a use statement right after inline html?

@jens1o how is it going?
do you need help?
Where to contribute?

Hmm, I didn't continued, since I have school again and I'm writing lots of exams... So yeah, I need some help. In the evening, I'll push my tries, maybe you can contribute..

@jens1o where can we find the branch?

On my old pc. Dammit. I'm again in exam phase, I'm sorry. Even though, I would have to write it again because of the new parser. Before I can do that, I would like to push forward foreach support.

Was this page helpful?
0 / 5 - 0 ratings