# Deep Dive: `@volar/source-map` & `@volar/code-gen` > [Docs Index](README.md) • [Repo README](../README.md) • [Plugin Authoring](plugin-authoring.md) • [Performance Guide](performance-and-debugging.md) When you author language plugins, you must translate source text into generated code without losing the ability to map diagnostics, hovers, and refactors back to the original file. Volar provides two foundational packages for this job: - `@volar/source-map`: low-level mapping utilities used everywhere Volar needs to relate two coordinate spaces (source ↔ generated). - `@volar/code-gen`: higher-level builder that emits text and source maps in lockstep, making it painless to describe complex virtual files. This guide explains every exported API, flag, and workflow, with exhaustive examples and best practices. ## `@volar/source-map` Basics ```ts import { SourceMap, Mapping, CodeInformation } from '@volar/source-map'; const map = new SourceMap(); map.add({ data: { verification: true, navigation: true }, sourceRange: [srcStart, srcEnd], mappedRange: [genStart, genEnd], }); const mapped = map.getMappedRange([srcStart, srcEnd]); const reverse = map.getSourceRange([genStart, genEnd]); ``` ### Mapping Fields - `sourceRange`: `[startOffset, endOffset]` in the original document. - `mappedRange`: `[startOffset, endOffset]` in the generated document. - `data`: a `CodeInformation` object controlling feature visibility: - `verification`: allow diagnostics + code actions. - `completion`: allow completions; ` { isAdditional: true }` to mark extra suggestions (imports, etc.). - `semantic`: enable hover/inlay semantics. - `navigation`: rename/reference/definition support. - `structure`: outlines/folding. - `format`: formatting support. Use the `SourceMap` methods: - `getMappedRange(range, filter?)` – yields generated segments for a source range. - `getSourceRange(range, filter?)` – inverse lookup. - `forEach()` – iterate all mappings. ### Best Practices 1. **Minimal coverage** – map only the user-authored slices; synthetic scaffolding should either be unmapped or flagged with `verification: false`. 2. **Feature-specific filters** – when computing rename ranges, pass a filter to `getMappedRange` that checks `data.navigation`. 3. **Chunk by content type** – create separate mappings for template vs script vs style segments so downstream plugins can enable/disable features per chunk. ## `@volar/code-gen` Workflow `@volar/code-gen` builds on the source-map APIs and gives you a friendly builder for emitting text + mappings simultaneously. ```ts import { CodeGen } from '@volar/code-gen'; const codeGen = new CodeGen(); codeGen.addText('const components = '); codeGen.addCode( 'getComponents()', { start: templateOffset, end: templateOffset + 13 }, { verification: true, navigation: true, }, ); codeGen.addText(';\n'); const text = codeGen.getText(); const mappings = codeGen.getMappings(); const embeddedFiles = codeGen.getEmbeddedFiles(); ``` ### Core Methods - `addText(text: string)` – append raw generated text (no mapping). - `addCode(generatedText, sourceRange, codeInfo)` – append text and register a mapping back to the source. - `addMapping(mapping)` – manually push a `Mapping`. - `addMapping2` (advanced) – register multi-segment mappings. - `addExtraMapping` – for completion-only or formatting-only segments. - `getText()` – returns the generated string. - `getMappings()` – returns an array of `Mapping`. - `getLength()` – current length of generated text. - `getEmbeddedFiles()` – nested `CodeGen` instances for embedded content (templates within scripts, etc.). - `getTextDocument(uri, languageId)` – convenience for building a `TextDocument` from the generated content. ### Embedded Files Use `codeGen.addEmbeddedFile(file)` when your language plugin produces nested virtual documents (e.g., a `