top | item 33053883

(no title)

ljw1004 | 3 years ago

My job over the past several years has been to work on IDE integrations for a language (Hack, Flow). Prior to that I worked on C#/VB "Roslyn" system at Microsoft, which is entirely about exposing the internals of the compiler to plugins.

Autocomplete isn't really helped by the AST. Here instead is how it works:

(1) Query the backend "at the specified line+col in the file, what context are we in?"

(2) The context might be a qualified identifier, e.g. "foo.|" or "foo.ba|" or "MyFoo::|", in which case the context will have to tell you the type of the thing to the left of the qualifier. You then make a second request to the backend to ask for a list of members of that type. (This requires you have some common language with the backend for referring to types).

(3) Or, the context might be an unqualified identifier, e.g. "|" or "fo|". In this case you have to assemble a list of all possible identifiers in this position. (You might also want to assemble keywords here, but it's hardly necessary). This list will include all local variables; for this, you can use the AST so long as the semantics of your language aren't too tricky about what variables are in scope, or you might query the backend for them. The list will also include every single top-level symbol in your entire project. (Even in languages which require import or #include statements, you still want to list top-level identifiers that haven't yet been imported, because that's what a nice IDE experience is -- you help the user type this and then help them auto-insert the import).

This fact means that autocomplete has to be powered by the build system, has to know about all files in the project. Also, for large projects, there might be countless top-level identifiers across the project, maybe in the tens of thousands, so autocomplete needs some serious pre-compute assistance.

So how would one achieve any part of the above with an AST? The AST solely helps with part of the first step, allowing you to identify which AST node you're in. Then you still have to query the backend using the exact same API as above to learn the type of the qualifier to the left of the current AST node. Then you still have to query the backend to find all members of that type. And you still have to query the backend for its build-system knowledge of every type in the file.

In other words, the AST has barely helped you at all in your quest to implement autocomplete.

discuss

order