import { type ILexingError, IToken, TokenType } from "chevrotain"; /** * A table reference found in the SQL query */ export interface TableRef { /** The table name */ table: string; /** Optional alias */ alias?: string; } /** * Result from content assist analysis */ export interface ContentAssistResult { /** Token types that are syntactically valid at the cursor position */ nextTokenTypes: TokenType[]; /** Tables/aliases found in the query (for column suggestions) */ tablesInScope: TableRef[]; /** Columns from CTEs, keyed by CTE name (lowercase) */ cteColumns: Record; /** The tokens before the cursor */ tokensBefore: IToken[]; /** Whether the cursor is in the middle of a word (partial token being typed) */ isMidWord: boolean; /** Any lexer errors */ lexErrors: ILexingError[]; /** * When the cursor is after a qualified reference (e.g., "t1." or "trades."), * this contains the qualifier name (e.g., "t1" or "trades"). The provider * should resolve this against tablesInScope aliases/names to filter columns. */ qualifiedTableRef?: string; /** Whether the grammar context expects column names (expression/columnRef positions) */ suggestColumns: boolean; /** Whether the grammar context expects table names (tableName positions, or expression context) */ suggestTables: boolean; /** Whether scalar functions are valid at this position (any expression context) */ suggestScalarFunctions: boolean; /** Whether aggregate functions are valid at this position (SELECT/ORDER BY/HAVING) */ suggestAggregateFunctions: boolean; /** Whether window functions are valid at this position (SELECT/ORDER BY) */ suggestWindowFunctions: boolean; /** Whether table-valued functions are valid at this position (FROM/JOIN) */ suggestTableValuedFunctions: boolean; /** * Bare column names (lowercase) referenced before the cursor in expression * context. Used by the provider to boost tables containing all these columns. */ referencedColumns: Set; /** Whether the cursor is inside a WHERE clause expression */ isConditionContext: boolean; /** * Concrete keywords the grammar accepts only through the generic `identifier` * sub-rule (so the word stays non-reserved) and therefore never surface via * the normal `IdentifierKeyword` path. Re-injected per rule context so * multi-word `GRANT`/`REVOKE`, `CONVERT PARTITION` and `SHOW CREATE DATABASE` * clauses can be autocompleted. See `contextKeywordSuggestions`. */ contextKeywords: string[]; } /** * Position categories used to drive context-aware suggestion emission. * * - "newName" — name being defined (CREATE TABLE ): suppress * all schema/function suggestions. * - "expression" — expression position where aggregates and windows * are syntactically valid: SELECT items, ORDER BY. * - "restrictedExpression"— any expression where aggregates and windows are * NOT valid: WHERE / GROUP BY / JOIN ON predicates, * UPDATE SET RHS, INSERT VALUES rows, DECLARE * assignment RHS. * - "columnReference" — bare column-name reference with no surrounding * expression context: ALTER TABLE … DROP COLUMN * , ALTER TABLE … RENAME COLUMN . * Columns suggested; functions never valid here. * - "tableSource" — FROM/JOIN positions: tables + table-valued fns. * - "tableName" — DROP TABLE / INSERT INTO / UPDATE / TRUNCATE * TABLE / RENAME TABLE: tables only, no functions. * - "numeric" — LIMIT / OFFSET: numeric literal expected; nothing * useful to suggest from schema or functions. */ export type PositionKind = "newName" | "expression" | "restrictedExpression" | "columnReference" | "tableSource" | "tableName" | "numeric"; /** * Set of category flags that the suggestion-builder consults to decide which * function and schema buckets to emit. */ export interface CategoryFlags { suggestColumns: boolean; suggestTables: boolean; suggestScalarFunctions: boolean; suggestAggregateFunctions: boolean; suggestWindowFunctions: boolean; suggestTableValuedFunctions: boolean; } /** * Extract bare column names referenced in expression context from a token list. * * Scans the tokens and collects identifier names that are likely column * references, excluding: * - Qualified identifiers (followed by a Dot token — table/alias qualifiers) * - Middle segments of multi-part names (preceded AND followed by a Dot) * - Known table names and aliases (matched against tableAndAliasSet) * - Function calls (followed by a left-parenthesis token) * * @param tokens - Tokens to scan * @param tableAndAliasSet - Lowercase table names and aliases already in scope * (built from tablesInScope by the caller). Identifiers matching any of these * are excluded because they are table/alias references, not column names. * * Returns a Set of lowercase column names for efficient lookup. */ export declare function extractReferencedColumns(tokens: IToken[], tableAndAliasSet: Set): Set; /** * Get content assist suggestions for a SQL string at a given cursor position * * @param fullSql - The complete SQL string * @param cursorOffset - The cursor position (0-indexed character offset) * @returns Content assist result with next valid tokens and tables in scope */ export declare function getContentAssist(fullSql: string, cursorOffset: number): ContentAssistResult; /** * Simplified version that just returns next valid token names */ export declare function getNextValidTokens(sql: string): string[]; /** * Check if a token type is expected at the current position */ export declare function isTokenExpected(sql: string, tokenName: string): boolean;