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?


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

Server doesn’t automatically mean web, internet, or even network. A server is just a program that provides a service.

In this case, your code will be sent through a program, which does some processing, and sends some symbols back that vscode can read.

Here’s how it works:

servers don’t need to be exposed to the Internet for it to act as such, it can be bound to a local port, which you do if you want to do local webdev, or communicate via some IPC (inter process) mechanisms

programming languages are fairly rigid in that they are parseable, so the code editor just send your code for every interval via IPC to the “lang servers”, where it will parse your code and send back a detailed response of its structure in some standardised format that your code editor understands, without it actually know how to parse it

Imagine your editor can ask an expert questions about the codebase and help you navigate it that way. Then the editor doesn’t need to know much about each language – all the cool analysis is handled by the language-specific program.

That language expert program is the “language server.” It’s called a server because the editor can throw requests at it exactly the same way you’d ask a backend server for information.

In general, you shouldn’t think of a “server” as something that *has* to run remotely. You have *plenty* of computing power on your workstation to run local servers, and that’s typically how server development is done.

> How can an extension figure out an entirely custom codebase

Literally the same way the compiler can do the same thing. In fact, for a lot of language server implementations, they just use part of the actual compiler (the parser, the lexical, syntax and semantic analyzers), the output of which is [a tree representation]( of all the things in the code, plus the errors the analyzers found.

Essentially, the IDE builds an index of some sort of your code. It basically takes all of your source code and extracts the outline of the code. Function definitions, global variables, etc.

If you have worked with C/C++ you likely know of an old school version of this. The header files. They basically outline your source file with functions and variables. You define the header file and reference it in other source files to both use it and implement it. The IDE builds something similar to this for any language and uses it to give you the definitions.

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

Depending on the language you’re using, it sometimes has to do this. Not all languages are structured in a way that makes this easy to keep up to date on the fly. It’s easier to do in languages like Java and C# because they have a fairly strict structure. Javascript can be a lot harder to deal with.

As for why it’s called a server, it’s because it’s implemented as a separate process. It’s like a server that the IDE is talking to, it’s just running locally on your computer. It’s likely using a loopback interface to communicate using TCP/IP, but it doesn’t have to. They do this for a lot of reasons. But one would be to help keep your IDE responsive while the “server” does a bunch of heavy lifting. Or if one crashes, it doesn’t cause the other to crash.