UNPKG

rawsql-ts

Version:

[beta]High-performance SQL parser and AST analyzer written in TypeScript. Provides fast parsing and advanced transformation capabilities.

115 lines (114 loc) 4.89 kB
import { IntelliSenseContext } from './CursorContextAnalyzer'; import { ScopeInfo } from './ScopeResolver'; import { ParseToPositionOptions, PositionParseResult } from './PositionAwareParser'; import { QueryCollection } from './MultiQuerySplitter'; import { LineColumn } from './LexemeCursor'; /** * Convenience API for SQL IntelliSense integration * * Provides simplified, high-level functions that combine the functionality * of the various position-aware parsing components for easy integration * with Monaco Editor and other code editors. * * @example * ```typescript * import { parseToPosition, getCursorContext, resolveScope, splitQueries } from 'rawsql-ts'; * * // Parse incomplete SQL with error recovery * const sql = "SELECT u.name FROM users u WHERE u."; * const parseResult = parseToPosition(sql, sql.length, { errorRecovery: true }); * * // Get cursor context for completion suggestions * const context = getCursorContext(sql, sql.length); * console.log(context.isAfterDot); // true * console.log(context.precedingIdentifier); // "u" * * // Get scope information for table/column completion * const scope = resolveScope(sql, sql.length); * console.log(scope.availableTables); // [{ name: 'users', alias: 'u' }] * * // Handle multi-query editor * const multiSQL = "SELECT 1; SELECT 2;"; * const queries = splitQueries(multiSQL); * const activeQuery = queries.getActive(12); // Get query at position * ``` */ /** * Parse SQL up to cursor position with error recovery * * Combines position-aware parsing with error recovery to handle incomplete SQL * that users are actively typing. Ideal for providing IntelliSense in editors. * * @param sql - SQL text to parse * @param cursorPosition - Cursor position (character offset or line/column) * @param options - Parsing options including error recovery settings * @returns Parse result with position-specific information */ export declare function parseToPosition(sql: string, cursorPosition: number | LineColumn, options?: ParseToPositionOptions): PositionParseResult; /** * Analyze cursor context for IntelliSense completion suggestions * * Determines what type of completions should be offered at the cursor position * based on SQL syntax context (SELECT clause, WHERE condition, etc.). * * @param sql - SQL text to analyze * @param cursorPosition - Cursor position (character offset or line/column) * @returns Cursor context information for completion logic */ export declare function getCursorContext(sql: string, cursorPosition: number | LineColumn): IntelliSenseContext; /** * Resolve scope information at cursor position * * Provides comprehensive information about available tables, CTEs, and columns * at the specified cursor position for intelligent completion suggestions. * * @param sql - SQL text to analyze * @param cursorPosition - Cursor position (character offset or line/column) * @returns Complete scope information including available tables and columns */ export declare function resolveScope(sql: string, cursorPosition: number | LineColumn): ScopeInfo; /** * Split multi-query SQL text into individual queries * * Handles SQL editors that contain multiple statements separated by semicolons. * Properly handles string literals and comments containing semicolons. * * @param sql - Multi-query SQL text * @returns Collection of individual queries with position information */ export declare function splitQueries(sql: string): QueryCollection; /** * Get IntelliSense information for a cursor position in multi-query context * * Combines query splitting, context analysis, and scope resolution to provide * complete IntelliSense information for a cursor position in multi-query SQL. * * @param sql - Multi-query SQL text * @param cursorPosition - Cursor position * @param options - Parsing options * @returns Complete IntelliSense information or undefined if position is invalid */ export declare function getIntelliSenseInfo(sql: string, cursorPosition: number | LineColumn, options?: ParseToPositionOptions): { context: IntelliSenseContext; scope: ScopeInfo; parseResult: PositionParseResult; currentQuery: string; relativePosition: number; } | undefined; /** * Get completion suggestions based on cursor context and scope * * Uses the new IntelliSense interface to provide targeted completion suggestions. * This function leverages the suggestion-based design to efficiently determine * what completions should be offered. * * @param sql - SQL text * @param cursorPosition - Cursor position * @returns Array of completion suggestions with context information */ export declare function getCompletionSuggestions(sql: string, cursorPosition: number | LineColumn): Array<{ type: 'keyword' | 'table' | 'column' | 'cte' | 'function'; value: string; detail?: string; documentation?: string; }>;