1

I've been wondering for a while: how do autocompletions work?

Example: In PhpStorm whenever I use a class and type -> it shows me all properties and methods of that class. It also auto completes namespaces and even functions inside libraries such as jQuery.

Does it run some sort of regex on the files, or does it parse them somehow?

3
  • Hi g3mini, does your issue is solved? If you think artspd's reply is helpful for your question, you can mark it as answer, so other community members who have the same or similar issue can easier search and confirm this information:) Jan 30, 2017 at 2:49
  • You don't say... I've only just gotten the time to fully read it =)
    – JordyvD
    Jan 30, 2017 at 12:12
  • Sorry for any inappropriate, it is just a reminder :) Feb 1, 2017 at 2:20

1 Answer 1

6

PhpStorm developer here. I'd like to go through some basics in case it may be helpful for those who want to implement their own plugin.

First of all, a code has to be broken into tokens using a lexer. Then AST (an abstract syntax tree) and PSI (a program structure interface) are built using a parser. PhpStorm has its own implementations of the lexer and the parser. This is how a PSI tree looks like for a simple class.

PSI tree

When you type in an editor or explicitly invoke a completion action (Ctrl+Space) a number of completion contributors are invoked. They're intended to return a list of suggestions based on a cursor's position.

Let's consider a case when completion is invoked inside a field reference.

Field reference

PhpStorm knows that at the current position all class members can be suggested. It starts obtaining a class reference (the $class variable in our case) and determining its type. If a variable resolves to a class its type is a class' FQN (fully qualified name).

To obtain methods and fields of a class its PSI element is needed. A special index is used to map an FQN to an appropriate PhpClass tree element. Indices are initially built when a project is opened for the first time and updated for each modified file.

PhpStorm collects all members from the PSI element (including parent's ones), then from its traits. They're filtered depending on a current context (e.g. access scope) and an already typed name's part (f).

Code completion

Suggestions are shown in a list which is sorted by how good element's name matches, its type, position and so on. The list rearranges when you type.

When you press Enter to insert an element PhpStorm invokes one more handler. It knows how to properly insert the element into a code. For instance, it can add parentheses for a method or import a class reference. In our case, it's enough to put brackets and place a cursor just after them because the method has no parameters.

Result

That's basically it. It worth mention that the IntelliJ IDEA platform allows a plugin to provide an implementation for each step described above. Thus completion can be improved or extended for some particular framework or language.

1
  • 1
    Thank you very much for this detailed and very helpful explanation! :D
    – JordyvD
    Jan 30, 2017 at 12:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.