How do “lang-servers” work within VScode? How can an extension figure out an entirely custom codebase and its functions and give me definitions on just right click?

473 views

It is crazy useful and ive no idea how it works.

Server makes me think it is a web service but it works even with like completely local code that is entirely custom and stuff.

All i know is i tend to need to compile the codebase once before it gets the new function definitions.

In: 5

18 Answers

Anonymous 0 Comments

The language server is a separate process from the main VS Code process, whose only job is to be fed code and spit out parsed results.

The fact that it is separated in this way is where the “server” part comes from. It’s not a *web* server–it lives entirely on your PC–but it does kind of act like a web server in some ways. Namely, it sits around idling until something asks it to do something, and returns a response once it gets a request. That’s all a “server” technically is.

When you open VS Code, it starts up the lang server on the side. When you open a file, VS Code passes the contents of the file to the lang server, which chews on it for a little bit. When the lang server is done, VS Code gets back a response containing what each piece of code is. VS Code uses that response to decide what colors certain chunks of code should be drawn, what tooltips they should get, whether there are any errors or warnings to complain about, the whole shebang.

The reason the lang server is set up as a separate server instead of just being a part of VS Code proper is for several reasons.

For one, that code crunching can take some time for very large files. If the lang server is going to have to spend a good chunk of time doing its thing, you don’t want the rest of VS Code to lock up and wait for it to finish, you want it to still be usable while it thinks. Separating the lang server out allows this to be possible.

Second, if the lang server is linked to VS Code over a standardized communication protocol, then the actual lang server becomes a black box, where we don’t care what goes on inside. As long as we can talk to it, what it actually does doesn’t matter. So we could, if we wanted, replace the whole lang server with a completely different one, as long as the new one “speaks” the same protocol. This allows us to support multiple programming languages just by replacing the lang server.

Lastly, VS Code uses lang servers that are not proprietary to VS Code. They are open, standalone projects that anyone can use (within license restrictions, which tend to be quite permissive). Since the lang server is basically its own little island, you can thus drop it into any project that knows how to speak to it and it will “just work”.

As to how the lang server itself actually does its thing, that can be quite complex. The thing you will want to research here is “abstract syntax trees” (or “ASTs” for short). The lang server will read incoming code and use a set of rules to build an AST. It is then the resultant AST that VS Code gets back and uses to do things like syntax highlighting, tooltips, error detection, etc.

You are viewing 1 out of 18 answers, click here to view all answers.