Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

1.1 KB
Raw Blame
import { type QueryRange } from 'lib/monaco/multiQueryUtils'

import { analyzeInnermostSelect } from './hogqlParserWorkerManager'

/**
 * Find the innermost SELECT subquery at the given cursor offset within a query.
 * Returns null if the cursor is only in the outermost SELECT (no nesting).
 *
 * @param query - The query text to parse (a single top-level query, already split by semicolons)
 * @param cursorOffset - Cursor position in the full editor text
 * @param queryStartOffset - Where this query starts in the full editor text
 */
export async function findInnermostSelectAtOffset(
    query: string,
    cursorOffset: number,
    queryStartOffset: number
): Promise<QueryRange | null> {
    try {
        const range = await analyzeInnermostSelect(query, cursorOffset - queryStartOffset)
        if (!range) {
            return null
        }

        return {
            query: query.slice(range.start, range.end),
            start: queryStartOffset + range.start,
            end: queryStartOffset + range.end,
        }
    } catch {
        return null
    }
}