Skip to content

Links, colors and more ​

Each of these features adds one part of the protocol. It only asks when the service has the optional method and a server of the document answers it.

language.links (DocumentLinksFeature) holds the links a server finds (textDocument/documentLink), such as import paths or web addresses in comments. They refresh 500 ms after the last edit. Mod+click follows a link before going to a definition, and holding Mod draws the link under the pointer. The feature resolves a link without a target first (documentLink/resolve).

A file target, or a document of a server's scheme, opens through the host's openPlace at the one-based line and column of its fragment: #L12, #12 or #L12,4. A web address goes to openExternal(url), or a new browser window without it. all(), linkAt(position), follow(link) and refresh() serve a host that lists links itself.

Document colors ​

language.colors (DocumentColorsFeature) draws a swatch before each color a server finds (textDocument/documentColor). Pressing it lists the server's notations of the color (textDocument/colorPresentation), so rgb(255 0 0) can become #f00, plus a row for the platform's color picker. A new color keeps the old notation when the server offers it. configure({ enabled: false }) removes the swatches; presentations(color) and apply(presentation, color) serve a host with its own picker.

The swatch is the swatch field of an EditorInlayHint: a CSS color drawn as a 12-pixel square, with its label for assistive technology. setInlayHints(hints, owner?) keeps each owner's hints apart, so swatches and server hints coexist.

Linked editing ​

language.linkedEditing (LinkedEditingFeature) asks which ranges change together once the caret rests 100 ms (textDocument/linkedEditingRange), such as a start and end tag name. Typing in one updates the others in the same undo step. An edit outside the ranges, text the server's wordPattern refuses, or a second caret ends the link. configure({ enabled: false }) turns it off.

Inline completions ​

language.inlineCompletion (InlineCompletionFeature) owns the editor's ghost text: it draws one suggestion at a time after the caret, takes it with Tab, steps through alternatives with Alt+] and Alt+[, and puts it away with Escape, an edit or a move of the caret. Suggestions come from sources. The language servers' (textDocument/inlineCompletion) are one, off until configure({ enabled: true }), since a host may draw its own. It asks 300 ms after typing stops.

An app adds a source with addSource(source, priority). An InlineSuggestionSource says whether it is enabled and automatic (asked when typing stops, and not only when a person asks), answers suggest(request) with InlineSuggestions, and may hear accepted. The InlineSuggestionRequest holds the caret, the triggerKind and a signal that aborts when the text changes, the caret moves away or the suggestion is put away. The suggestInline key asks the enabled source of the highest priority; triggerSource(source) asks one from the app's own command. A suggestion holds what it inserts, what the ghost shows, an optional accessory drawn after its first line (the function it returns runs once when the editor takes that element away), and words: true when it may be taken a word at a time with the acceptGhostWord key (firstWord says which).

Tab takes a suggestion a person asked for before a suggestion list or the pick list, and after them when it came by itself. Running snippets and templates come after both.

Format on type ​

language.formatOnType lets the language server tidy code as a person types, through textDocument/onTypeFormatting: a } moved to its block's indentation, the line after Enter, a ; that closes a statement.

It needs the service's optional onTypeFormatting (LanguageService) and a server with documentOnTypeFormattingProvider; supported says so. The trigger characters are the server's firstTriggerCharacter and moreTriggerCharacter, listed by triggerCharacters().

It is off by default. A host turns it on with language.formatOnType.configure(true) when the person chooses it. configure(false) turns it off and cancels a pending request. isEnabled reads the state.

After a typed trigger character, it sends the character as ch and the editor's tabSize and insertSpaces. Only when all of these hold:

  • The change is typed (source is 'input', or marked typed as Enter is). Undo, redo, host commands, reloads, completions, multi-character pastes and IME commits of several characters do not ask.
  • The editor has one caret, is writable and has no snippet session.

The answer applies only if the text and caret are unchanged; a later edit cancels the request. Overlapping edits, empty answers and failures change nothing. The edits join the undo step of the typed character (editor.applyEdits(edits, { mergeUndo: true })), so one undo removes both.

Saving ​

language.prepareSave(reason?) sends textDocument/willSave and applies what textDocument/willSaveWaitUntil returns, such as sorted imports. It skips a server that takes longer than a second. Call it, write the file, then let the service send didSave. reason is 1 for a manual save, 2 after a delay and 3 on focus loss.

Formatting a selection ​

language.codeActions.formatSelection() formats every selection, through textDocument/rangesFormatting when offered and textDocument/rangeFormatting otherwise, or the whole document without a selection. formatsSelection says whether a server formats ranges. The context menu lists it while something is selected.

The project ​

project.pullWorkspaceProblems(uri) asks the server of uri for the problems of every file it knows (workspace/diagnostic) and puts them in project.problems. It returns false when no server offers workspace diagnostics. The host decides when to pull, such as after a save.

project.readText(uri) reads a document of a server-owned scheme, such as a decompiled class, through textDocumentContent, so peek and the call hierarchy can show it. A server's window/showDocument goes to openPlace or openExternal, and its messages to onServerMessage.

Licenses and third-party notices are listed in each package.