

Ask HN: How does code completion (as in IDEs) work on a technical level? - cweagans

Conceptually, I understand the parts, but I&#x27;m having trouble translating that to working code. I&#x27;ve been doing some research and came across Jedi (https:&#x2F;&#x2F;github.com&#x2F;davidhalter&#x2F;jedi), which is an autocomplete library for Python.<p>Is there some guide somewhere that walks through the process of building something like Jedi for some other language (even an imaginary one)?<p>While it&#x27;s tangential to this particular discussion, my goal (eventually) is to learn what I need to know to be able to build something like Jedi for PHP. There&#x27;s only a handful of options for PHP code completion, and none of them provide the level of intelligence of a proprietary IDE like PhpStorm. That needs to change, IMO.
======
bjourne
Start small. Make a function that takes a string which is part of an English
word and returns a list of completions for that word. If on Linux, use the
word list in `/usr/share/dict/words`.

The next step up is to improve on that function. F.e. if the string is "disc"
a good candidate probably is "discussion" but not "discus" unless that word
previously occurs in the document.

Then continue from there. Coding is learnt by doing not by reading guides.

~~~
cweagans
Well, yeah. I mean, that's straightforward enough, but what people frequently
leave out is what "improve on that function" entails, especially when you
start getting into really weird codebases.

Like I said, I understand the overall components - I need a lexer/parser to
parse the code into a usable data structure that I can then examine to pull
out information about that code. I also need some way for that parser to not
puke if the syntax isn't perfect (for instance, if I'm typing in a line of
code, that line of code isn't going to be syntactically perfect).

I've been a developer for a number of years now. I know how to learn stuff.
The problem is that this particular problem is so nebulous and infrequently
solved in a publicly viewable way that I don't even know what I don't know,
hence the idea of looking at a guide for some simple, imaginary language. If I
can see the basics without all of the eventual code rot and cruft, I can go
from there. A sample implementation in pretty much any language would also be
adequate - I can do without the prose.

------
solomatov
Take a look at IntelliJ IDEA CE source. It's an open source project. Also,
read this description of how to implement a language support in IntelliJ:
[https://confluence.jetbrains.com/display/IDEADEV/Developing+...](https://confluence.jetbrains.com/display/IDEADEV/Developing+Custom+Language+Plugins+for+IntelliJ+IDEA)

