Skip to content

Commit

Permalink
feat(YQL): improve variable symbol value types
Browse files Browse the repository at this point in the history
  • Loading branch information
Raubzeug committed Dec 25, 2024
1 parent b8037da commit 467e2c3
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 9 deletions.
8 changes: 4 additions & 4 deletions src/autocomplete/databases/yql/yql-autocomplete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import {
ISymbolTableVisitor,
ProcessVisitedRulesResult,
} from '../../shared/autocomplete-types';
import {ColumnAliasSymbol, TableSymbol} from '../../shared/symbol-table.js';
import {ColumnAliasSymbol, ExtendedVariable, TableSymbol} from '../../shared/symbol-table.js';
import {TableQueryPosition} from '../../shared/tables';
import {isStartingToWriteRule} from '../../shared/cursor.js';
import {shouldSuggestTemplates} from '../../shared/query.js';
Expand Down Expand Up @@ -165,7 +165,7 @@ class YQLVariableSymbolTableVisitor extends YQLSymbolTableVisitor {
const variable = getVariable(index);
if (variable) {
this.symbolTable.addNewSymbolOfType(
c3.VariableSymbol,
ExtendedVariable,
this.scope,
variable,
variableValue,
Expand All @@ -188,7 +188,7 @@ class YQLVariableSymbolTableVisitor extends YQLSymbolTableVisitor {
if (variable) {
const value = context.literal_value()?.getText();

this.symbolTable.addNewSymbolOfType(c3.VariableSymbol, this.scope, variable, value);
this.symbolTable.addNewSymbolOfType(ExtendedVariable, this.scope, variable, value);
}
} catch (error) {
if (!(error instanceof c3.DuplicateSymbolError)) {
Expand Down Expand Up @@ -228,7 +228,7 @@ class YQLVariableSymbolTableVisitor extends YQLSymbolTableVisitor {
const variable = context.bind_parameter()?.an_id_or_type()?.getText();
if (variable) {
this.symbolTable.addNewSymbolOfType(
c3.VariableSymbol,
ExtendedVariable,
this.scope,
variable,
undefined,
Expand Down
2 changes: 1 addition & 1 deletion src/autocomplete/shared/autocomplete-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export interface ColumnAliasSuggestion {

export interface VariableSuggestion {
name: string;
value: unknown;
value?: {columns?: string[]};
}

export type LexerConstructor<T> = new (input: CharStream) => T;
Expand Down
4 changes: 4 additions & 0 deletions src/autocomplete/shared/symbol-table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ export class TableSymbol extends c3.TypedSymbol {
}
}

export class ExtendedVariable extends c3.VariableSymbol {
value: {columns?: string[]} | undefined;
}

export function getUniqueTableSuggestions(symbols: TableSymbol[] = []): Table[] {
const suggestionsMap = symbols.reduce(
(acc, table) => {
Expand Down
8 changes: 4 additions & 4 deletions src/autocomplete/shared/variables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
SymbolTableVisitor,
VariableSuggestion,
} from './autocomplete-types';
import {getScope} from './symbol-table';
import {ExtendedVariable, getScope} from './symbol-table';
import {computeTokenContext} from './compute-token-position';

export function getVariableSuggestions<L extends LexerType, P extends ParserType>(
Expand Down Expand Up @@ -37,16 +37,16 @@ export function getVariableSuggestions<L extends LexerType, P extends ParserType

function suggestVariables(symbolTable: c3.SymbolTable, context: ParseTree): VariableSuggestion[] {
const scope = getScope(context, symbolTable);
let symbols: c3.VariableSymbol[] = [];
let symbols: ExtendedVariable[] = [];

// Local scope
if (scope instanceof c3.ScopedSymbol) {
symbols = scope.getNestedSymbolsOfTypeSync(c3.VariableSymbol);
symbols = scope.getNestedSymbolsOfTypeSync(ExtendedVariable);

// Global scope
} else if (symbolTable) {
symbols = symbolTable
.getNestedSymbolsOfTypeSync(c3.VariableSymbol)
.getNestedSymbolsOfTypeSync(ExtendedVariable)
// If symbol's parent has context it means it is local scoped, so no need to suggest it in global scope
.filter((symbol) => !symbol.parent?.context);
}
Expand Down

0 comments on commit 467e2c3

Please sign in to comment.