This document describes the code completion system that provides IntelliSense suggestions for identifiers, members, keywords, imports, and other code elements. For information about signature help (parameter hints), see Signature Help. For refactoring operations, see Refactorings.
The code completion system is responsible for generating contextually-appropriate suggestions when a user types code in an editor. The system analyzes the cursor position, determines the syntactic and semantic context, gathers candidate symbols from local scopes and imported modules, filters and ranks them based on relevance, and returns completion entries to the editor.
The main entry point is getCompletionsAtPosition src/services/services.ts1330 which returns a CompletionInfo src/services/types.ts741-755 object containing completion entries. Clients can then request additional details for specific entries via getCompletionEntryDetails src/services/services.ts1332
The completion system is implemented primarily in the language service layer and relies on the type checker for semantic information.
Sources: src/services/services.ts1330-1340 src/services/completions.ts1-100
The getCompletionsAtPosition function in services.ts calls into the Completions namespace in completions.ts:
Sources: src/services/completions.ts150-400
The getCompletionData src/services/completions.ts1210 function determines what kind of completion is appropriate based on the cursor position. It examines the AST node at the position and surrounding context using findPrecedingToken src/compiler/utilities.ts6012
| Context Type | Description | Example |
|---|---|---|
StringLiteral | Inside string literals for paths, JSX attributes | import * from "█" |
MemberAccess | After dot or bracket operator | obj.█ |
GlobalsCompletion | Unqualified identifier position | const x = █ |
TypeOnlyLocation | Type annotation position | const x: █ |
JsDocTagContext | Inside JSDoc comment | /** @param █ */ |
JsxContext | JSX tag names or attributes | <Component █ /> |
Sources: src/services/completions.ts1210-1800 src/services/stringCompletions.ts14
Once the context is determined, the system collects candidate symbols from various sources:
Sources: src/services/completions.ts2500-3500 src/services/exportInfoMap.ts12
For member access expressions (object.█), the system:
checker.getTypeAtLocation src/compiler/checker.ts3815checker.getPropertiesOfType src/compiler/checker.ts31110checker.isSymbolAccessible src/compiler/checker.ts28540Sources: src/services/completions.ts2000-2800
The auto-import system provides completions for symbols available from other modules.
Key Data Structures:
ExportInfoMap: Maps symbol names to their export locations src/services/exportInfoMap.ts12CompletionEntryDataAutoImport: Contains module specifier and position src/services/types.ts717-724IncompleteCompletionsCache: Caches partial results src/server/editorServices.ts68Sources: src/services/completions.ts4000-5000 src/compiler/moduleSpecifiers.ts1-100
The StringCompletions src/services/stringCompletions.ts14 namespace handles completions for import paths and other string contexts.
The system offers:
getFileSystemCompletions src/services/stringCompletions.ts310node_modules/.tsconfig.json.Sources: src/services/stringCompletions.ts14-500
The system filters and ranks completion candidates to present the most relevant suggestions first.
Completions are ranked using a sortText field. SortText src/services/completions.ts44-55 enumeration defines the priority levels:
LocalDeclarationPriority: "0"LocationPriority: "1"OptionalMember: "2"MemberDeclaredBySpreadAssignment: "3"SuggestedClassMembers: "4"GlobalsOrKeywords: "5"AutoImportSuggestions: "6"JavascriptIdentifiers: "7"Sources: src/services/completions.ts44-70
getCompletionEntryDetails src/services/services.ts1332 provides:
symbol.getDocumentationComment src/compiler/checker.ts5430typeChecker.typeToString src/compiler/checker.ts4890importFixes.getImportCodeActions src/services/codefixes/importFixes.ts120Sources: src/services/completions.ts6500-7500
getJSDocTagCompletions src/services/completions.ts2850getJSXCompletions src/services/completions.ts3200 providing attribute and tag name suggestions based on the intrinsic or component type.Sources: src/services/completions.ts2800-3800
completionInfo command tests/baselines/reference/api/typescript.d.ts59Sources: src/compiler/checker.ts117 src/server/protocol.ts1-100
Refresh this wiki