Skip to content

Commit 8a9e4f9

Browse files
committed
fix: remove _ctx and add tokenIndex into position
1 parent ab1da92 commit 8a9e4f9

File tree

3 files changed

+53
-55
lines changed

3 files changed

+53
-55
lines changed

src/parser/common/entityCollector.ts

Lines changed: 4 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ export interface StmtContext {
3535
readonly parentStmt: StmtContext | null;
3636
readonly isContainCaret?: boolean;
3737
readonly scopeDepth: number;
38-
readonly _ctx: ParserRuleContext;
3938
readonly text: string;
4039
}
4140

@@ -58,7 +57,6 @@ export function toStmtContext(
5857
isContainCaret,
5958
text: stmtText,
6059
scopeDepth: type === StmtContextType.COMMON_STMT ? 0 : (parentStmt?.scopeDepth ?? 0) + 1,
61-
_ctx: ctx,
6260
};
6361
}
6462

@@ -106,8 +104,6 @@ export interface BaseEntityContext {
106104
isAccessible: boolean;
107105
/** Entities related to this entity */
108106
relatedEntities: EntityContext[] | null;
109-
/** The parser rule context for this entity, it will be deleted after the entity is collected because of json serialization */
110-
_ctx?: ParserRuleContext;
111107
/** Comment attribute for this entity */
112108
[AttrName.comment]: WordRange | null;
113109
/** Alias attribute for this entity */
@@ -225,7 +221,6 @@ export function toEntityContext(
225221
position,
226222
belongStmt,
227223
declareType: metaInfo?.declareType,
228-
_ctx: ctx,
229224
[AttrName.comment]: null,
230225
};
231226
switch (entityInfo.entityContextType) {
@@ -328,10 +323,10 @@ function findAttributeChildren(
328323
* @returns true if entity is contained within rangeEntity's range
329324
*/
330325
function isEntityInScope(entity: EntityContext, rangeEntity: EntityContext): boolean {
331-
const entityStart = entity._ctx?.start?.tokenIndex;
332-
const entityStop = entity._ctx?.stop?.tokenIndex;
333-
const rangeStart = rangeEntity._ctx?.start?.tokenIndex;
334-
const rangeStop = rangeEntity._ctx?.stop?.tokenIndex;
326+
const entityStart = entity.position.startTokenIndex;
327+
const entityStop = entity.position.endTokenIndex;
328+
const rangeStart = rangeEntity.position.startTokenIndex;
329+
const rangeStop = rangeEntity.position.endTokenIndex;
335330

336331
return (
337332
entityStart != null &&
@@ -408,55 +403,12 @@ export abstract class EntityCollector {
408403

409404
exitProgram() {
410405
const entities = Array.from(this._entitiesSet);
411-
this.removeCtxAttr();
412406
if (this._caretTokenIndex !== -1) {
413407
this.attachAccessibleToEntities(entities);
414408
}
415409
this._entityStack.clear();
416410
}
417411

418-
/**
419-
* Remove _ctx property to avoid circular references during JSON serialization
420-
*/
421-
private removeCtxAttr() {
422-
const entities = Array.from(this._entitiesSet);
423-
// Use WeakSet to track processed objects and avoid infinite recursion from circular references
424-
const processed = new WeakSet();
425-
426-
const removeCtx = (obj: any) => {
427-
if (!obj || typeof obj !== 'object' || processed.has(obj)) {
428-
return;
429-
}
430-
processed.add(obj);
431-
432-
if ('_ctx' in obj) {
433-
obj._ctx = undefined;
434-
}
435-
436-
if (obj.belongStmt) {
437-
removeCtx(obj.belongStmt);
438-
}
439-
440-
if (obj.rootStmt) {
441-
removeCtx(obj.rootStmt);
442-
}
443-
444-
if (obj.parentStmt) {
445-
removeCtx(obj.parentStmt);
446-
}
447-
448-
if (obj.relatedEntities && Array.isArray(obj.relatedEntities)) {
449-
obj.relatedEntities.forEach(removeCtx);
450-
}
451-
452-
if (obj.columns && Array.isArray(obj.columns)) {
453-
obj.columns.forEach(removeCtx);
454-
}
455-
};
456-
457-
entities.forEach(removeCtx);
458-
}
459-
460412
/**
461413
* Determines if the caret is inside a derived table subquery
462414
* For example, in: SELECT id FROM t1, (SELECT name from t2) as t3

src/parser/common/textAndWord.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,13 @@ export interface WordPosition {
1111
readonly startColumn: number;
1212
/** end at ..n + 1 */
1313
readonly endColumn: number;
14+
/** start at 0 */
15+
readonly startTokenIndex: number;
16+
/** end at ..n-1 */
17+
readonly endTokenIndex: number;
1418
}
1519

16-
export interface WordRange extends WordPosition {
20+
export interface WordRange extends Omit<WordPosition, 'startTokenIndex' | 'endTokenIndex'> {
1721
/** content of word */
1822
readonly text: string;
1923
}
@@ -31,6 +35,10 @@ export interface TextPosition {
3135
readonly startColumn: number;
3236
/** end at ..n + 1 */
3337
readonly endColumn: number;
38+
/** start at 0 */
39+
readonly startTokenIndex: number;
40+
/** end at ..n-1 */
41+
readonly endTokenIndex: number;
3442
}
3543

3644
export interface TextSlice extends TextPosition {
@@ -75,6 +83,8 @@ export function ctxToText(
7583
endIndex,
7684
startColumn: ctx.start.column + 1,
7785
endColumn: ctx.stop.column + 1 + (ctx.stop.text?.length ?? 0),
86+
startTokenIndex: ctx.start.tokenIndex,
87+
endTokenIndex: ctx.stop.tokenIndex,
7888
};
7989
}
8090

src/parser/postgresql/postgreEntityCollector.ts

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ import {
3131
type ViewNameCreateContext,
3232
TargetListContext,
3333
SelectNoParensContext,
34+
XmlTableContext,
35+
FuncTableContext,
36+
SelectWithParensContext,
3437
} from '../../lib/postgresql/PostgreSqlParser';
3538
import type { PostgreSqlParserListener } from '../../lib/postgresql/PostgreSqlParserListener';
3639
import {
@@ -69,7 +72,40 @@ export class PostgreSqlEntityCollector extends EntityCollector implements Postgr
6972
);
7073
}
7174

72-
exitExpressionTable(ctx: ExpressionTableContext) {
75+
exitXmlTable(ctx: XmlTableContext) {
76+
this.pushEntity(
77+
ctx,
78+
EntityContextType.TABLE,
79+
[
80+
{
81+
attrName: AttrName.alias,
82+
endContextList: [ExpressionTableContext.name],
83+
},
84+
],
85+
{
86+
declareType: TableDeclareType.EXPRESSION,
87+
}
88+
);
89+
}
90+
91+
exitFuncTable(ctx: FuncTableContext) {
92+
this.pushEntity(
93+
ctx,
94+
EntityContextType.TABLE,
95+
[
96+
{
97+
attrName: AttrName.alias,
98+
endContextList: [ExpressionTableContext.name],
99+
},
100+
],
101+
{
102+
declareType: TableDeclareType.EXPRESSION,
103+
}
104+
);
105+
}
106+
107+
exitSelectWithParens(ctx: SelectWithParensContext) {
108+
if (!isChildContextOf(ctx, PostgreSqlParser.RULE_expressionTable)) return;
73109
this.pushEntity(
74110
ctx,
75111
EntityContextType.TABLE,
@@ -119,7 +155,7 @@ export class PostgreSqlEntityCollector extends EntityCollector implements Postgr
119155
]);
120156
}
121157

122-
exitTarget_list(ctx: TargetListContext) {
158+
exitTargetList(ctx: TargetListContext) {
123159
if (!isChildContextOf(ctx, PostgreSqlParser.RULE_simpleSelect)) return;
124160
this.pushEntity(ctx, EntityContextType.QUERY_RESULT);
125161
}

0 commit comments

Comments
 (0)