What is Intellisense?

Many modern editors have features to make life easier when working code, markup, and other types of structured text. Collectively, Microsoft calls these features “Intellisense” and they include features such as quick-info and parameter-help tooltips, completions, go-to-definition, and many more.

VSCode intellisense in action

Many of us use it every day, but have you ever wondered what’s going on behind the scenes to make it happen?

First stop: types of Intellisense

Intellisense features can be roughly divided into 2 categories:

Contextual Intellisense

Global intellisense

Next stop: Compilers

Most compilers have 4 stages:

Compilers: a worked example (simplified)

Lexical analysis

The lexer turns a stream of characters into discrete tokens:

Before: _count == 5

After:

Syntactic analysis

The parser turns a stream of tokens into an AST.

Before:

After:


Semantic analysis

The compiler refines and interprets the AST.

Before:

After:

Compiler vs. Language Service

The Compiler’s job

The Language service’s job

So what does a language service actually DO?

Here’s some source text:

raw

Let’s put on our language-service goggles! What does the language service see?

Tokens

Break up the text into tokens such as:

raw

Syntax

Parse the tokens into a syntax tree:

raw

Semantic Model

How is this useful?

Ask the language service what’s on line 2:

Element(Name=Element2,Prefix=,Namespace=) Attribute(Name=Attribute1,Prefix=,Namespace=,Value=Value1)

Ask the language service what completions can be offered in the whitespace before Attribute1:

Containing element is Element2, which has 2 possible attributes (Attribute1 and Attribute2); Attribute1 is already present, so:

1 completion: Attribute2=""