{"modulePath":"github.com/spilchen/sql-ai-tools","version":"v0.1.0","isLatest":true,"isStandardLibrary":false,"goos":"all","goarch":"all","docs":"# package sqlformat\n\nPackage sqlformat wraps the cockroachdb-parser module to provide SQL pretty-printing. The primary entry point is Format, which parses a SQL string and re-emits it in CockroachDB's canonical pretty-printed form using tree.DefaultPrettyCfg.\n\n## Functions\n\n```go\nfunc Format(sql string) (string, error)\n```\nFormat parses sql using the CockroachDB parser and returns the pretty-printed canonical form. Multiple statements in the input are separated by \";\\\\n\" in the output. An empty input returns an empty string with no error. Parse or formatting errors are returned as a Go error; the caller decides how to surface them.\n\n```go\nfunc FormatParsed(stmts statements.Statements) (string, error)\n```\nFormatParsed pretty-prints already-parsed statements. Exposed so callers that already invoked parser.Parse (e.g. to also run version.Inspect on the same AST) can reuse the parsed output rather than reparsing. The only error surface is the pretty-printer itself (cfg.Pretty); parser errors are no longer possible at this layer.\n\n```go\nfunc Highlight(formatted string) string\n```\nHighlight returns formatted with ANSI SGR escape sequences wrapping SQL keywords (bold cyan), string literals (green), and numeric literals (magenta). Identifiers, punctuation, and whitespace are emitted uncolored. The function preserves byte ranges between tokens verbatim, so the visible (de-escaped) output is byte-identical to formatted.\n\nTokenization is performed with scanner.Inspect, which yields token start/end offsets into formatted. If any token comes back as ERROR or as the incomplete-token sentinel (negative ID, documented in scanner.Inspect), Highlight returns formatted unchanged rather than emit a half-colored garbage string. In normal use this fallback never fires because callers feed already-validated SQL, but the defensive check protects against bounds-violating slices if a future caller passes hand-constructed input.\n\nHighlight is intended for terminal output only. JSON envelopes must never contain escape sequences, so callers gate this behind a text-mode + TTY check.\n\n```go\nfunc StripShellPrompts(sql string) string\n```\nStripShellPrompts removes cockroach sql REPL prompt prefixes from pasted input. Two prompt forms are recognized:\n\n\t\u003cuser\u003e@\u003chost-info\u003e\u003e\u003cSP\u003e     // primary prompt (any user)\n\t\u003cpad\u003e-\u003e\u003cSP\u003e                 // continuation prompt (right-aligned)\n\nContinuation lines are stripped only after a primary prompt has already been seen earlier in the input. This guards SQL-only pastes that legitimately contain the JSON field-access operator '-\u003e' at line start (e.g. \"  -\u003e 'key'\") from being mangled. A transcript paste always starts with a primary prompt, so the guard does not interfere with the intended use case.\n\nThe function is a no-op when the input contains no prompts. Lines without a recognized prompt — including blank lines and lines inside multi-line string literals — pass through unchanged.\n\nLimitation: a multi-line string literal whose interior line happens to begin with a primary-prompt-shaped sequence will be incorrectly stripped. This is exceedingly rare in REPL transcripts and not worth the complexity of full lexer-aware line classification at this layer.\n\n## Types\n\n```go\ntype StripResult struct {\n\tStripped string\n\tRemoved  bool\n\t// contains filtered or unexported fields\n}\n```\nStripResult is the value produced by StripShellPromptsWithMap. It pairs the stripped SQL with an offset map that lets callers translate byte offsets in the stripped buffer back to the original input — needed so parser/cluster error positions can be reported in the user's original coordinates after the stripper has shortened earlier lines.\n\nRemoved reports whether any prompt bytes were dropped. When false, Stripped is byte-identical to the function input and Translate is the identity, so handlers can safely treat the no-op case the same as the strip-applied case.\n\nsegments is the per-removal cumulative-delta table consumed by Translate. Each entry records the offset in Stripped where a stretch of \"no further bytes removed\" begins, plus the total number of bytes dropped from the original before that offset. Lookup is a binary search for the largest StrippedStart ≤ query offset, so translation is O(log N) in the number of stripped lines.\n\n```go\nfunc StripShellPromptsWithMap(sql string) StripResult\n```\nStripShellPromptsWithMap behaves like StripShellPrompts but also returns an offset map. Use this when you intend to surface error positions back to the caller, so a \"syntax error at column 30\" in the stripped buffer can be reported as the matching column in the original paste. For pure formatting / display use cases the simpler StripShellPrompts is sufficient.\n\n```go\nfunc (r StripResult) Translate(strippedOffset int) int\n```\nTranslate maps a 0-based byte offset in r.Stripped to the matching 0-based byte offset in the original input. Negative offsets clamp to 0; offsets past len(r.Stripped) clamp to len(original).\n\nTranslation works by binary-searching segments for the largest StrippedStart ≤ strippedOffset and adding its CumDelta. When no segments were produced (no prompts removed), Translate is the identity.\n\n","path":"github.com/spilchen/sql-ai-tools/internal/sqlformat","name":"sqlformat","synopsis":"Package sqlformat wraps the cockroachdb-parser module to provide SQL pretty-printing.","isRedistributable":true}
