gosrc

package
v0.0.0-...-603fb30 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 24, 2026 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Overview

Package gosrc extracts template text from Go source files.

A template may be authored in a `//sqletch:query` const instead of a .sql file (docs/design/13-go-source-input.md). Extraction is purely syntactic — go/parser only, never go/types — so the target package need not compile; it may reference generated symbols that do not exist yet.

The unit handed back is not a substring but a *view*: a copy of the whole file with everything outside one template literal blanked and the tail truncated. Byte offsets and line numbers therefore match the real file exactly, which is what lets every downstream phase — the scanner, the source map, diagnostics, the LSP's UTF-16 conversion — work unmodified on Go-authored templates.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsGoSource

func IsGoSource(path string) bool

IsGoSource reports whether path is read as Go source rather than as a template file. This is the only dispatch: `queries:` globs carry both forms.

func Views

func Views(path string, src []byte, yield func(view []byte, start int)) []diagnostics.Diagnostic

Views calls yield once per marked template literal, in source order, with an offset-preserving view of the file and the byte offset at which that literal's content begins, and returns diagnostics for markers that do not name an extractable template.

start is where the scanner should begin: the view's bytes before it are blank trivia (§view), so scanning is equivalent whether it starts at 0 or at start, but starting at start avoids re-lexing — and re-copying, as one whitespace token's Text — the blank prefix once per literal, which would be O(literals × len(src)) quadratic CPU (and hang the LSP on open through cli.scanSource). The caller passes it to template.Scanner.ScanFileFrom.

The []byte handed to yield is a single backing buffer reused across every view — it is only valid until yield returns. This keeps total memory O(len(src)) instead of O(literals × len(src)): a file with thousands of marked consts would otherwise allocate a full file-length prefix copy per const and exhaust memory. Reuse is sound because the scanner copies out everything it retains — skeleton and body texts are string copies and spans are byte offsets, never slices into the source — so nothing observes the buffer after yield returns.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL