Skip to content

Commit 4e613b2

Browse files
committed
refactor(vnext): simplify authenticated completion paths
1 parent e59423c commit 4e613b2

3 files changed

Lines changed: 37 additions & 24 deletions

File tree

src/vnext/__tests__/local-relation-site.test.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { describe, expect, it } from "vitest";
22
import {
3+
analyzeSqlLocalColumnSite,
34
analyzeSqlLocalRelationSite,
45
prepareSqlLocalRelationStatement,
56
type SqlLocalRelationSiteResult,
@@ -495,6 +496,24 @@ describe("local relation-site evidence", () => {
495496
reason: "ambiguous-query-site",
496497
status: "unavailable",
497498
});
499+
expect(
500+
Reflect.apply(analyzeSqlLocalColumnSite, undefined, [
501+
null,
502+
fixture.position,
503+
]),
504+
).toEqual({
505+
reason: "ambiguous-query-site",
506+
status: "unavailable",
507+
});
508+
expect(
509+
analyzeSqlLocalColumnSite(
510+
{ ...preparation.statement },
511+
fixture.position,
512+
),
513+
).toEqual({
514+
reason: "ambiguous-query-site",
515+
status: "unavailable",
516+
});
498517
});
499518

500519
it("is deterministic and freezes every exposed wrapper", () => {

src/vnext/local-relation-site.ts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -183,13 +183,6 @@ export function analyzeSqlLocalRelationSite(
183183
});
184184
}
185185
const relativePosition = position - context.slot.source.from;
186-
if (
187-
!Number.isSafeInteger(relativePosition) ||
188-
relativePosition < 0 ||
189-
relativePosition > context.layout.statementLength
190-
) {
191-
return unavailableSite();
192-
}
193186
return Object.freeze({
194187
local: Object.freeze({
195188
cteVisibility: visibleSqlCtesAt(

src/vnext/namespace-completion.ts

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@ import type {
33
SqlNamespaceCatalogSearchOutcome,
44
} from "./namespace-catalog-coordinator.js";
55
import type {
6+
SqlCanonicalNamespacePath,
67
SqlNamespaceCatalogResolvedContainer,
78
SqlNamespaceContainerRole,
9+
SqlNamespacePathComponent,
810
SqlNamespaceQuerySite,
911
} from "./namespace-catalog-types.js";
1012
import type {
@@ -67,8 +69,12 @@ function compareText(left: string, right: string): number {
6769
return left < right ? -1 : left > right ? 1 : 0;
6870
}
6971

70-
function last<Value>(values: readonly Value[]): Value | null {
71-
return values[values.length - 1] ?? null;
72+
function finalComponent(
73+
path: SqlCanonicalNamespacePath,
74+
): SqlNamespacePathComponent {
75+
let current = path[0];
76+
for (const component of path.slice(1)) current = component;
77+
return current;
7278
}
7379

7480
function pathText(
@@ -83,8 +89,8 @@ function compareContainers(
8389
left: SqlNamespaceCatalogResolvedContainer,
8490
right: SqlNamespaceCatalogResolvedContainer,
8591
): number {
86-
const leftLast = last(left.canonicalPath);
87-
const rightLast = last(right.canonicalPath);
92+
const leftLast = finalComponent(left.canonicalPath);
93+
const rightLast = finalComponent(right.canonicalPath);
8894
return (
8995
(left.matchQuality === right.matchQuality
9096
? 0
@@ -93,11 +99,9 @@ function compareContainers(
9399
: 1) ||
94100
left.canonicalPath.length - right.canonicalPath.length ||
95101
(
96-
leftLast && rightLast
97-
? ROLE_ORDER[leftLast.role] - ROLE_ORDER[rightLast.role]
98-
: 0
102+
ROLE_ORDER[leftLast.role] - ROLE_ORDER[rightLast.role]
99103
) ||
100-
compareText(leftLast?.value ?? "", rightLast?.value ?? "") ||
104+
compareText(leftLast.value, rightLast.value) ||
101105
compareText(pathText(left), pathText(right)) ||
102106
compareText(
103107
left.provenance.containerEntityId,
@@ -158,9 +162,8 @@ function unavailable(
158162
function item(
159163
container: SqlNamespaceCatalogResolvedContainer,
160164
replacementRange: SqlTextRange,
161-
): SqlNamespaceCompletionItem | null {
162-
const component = last(container.canonicalPath);
163-
if (!component) return null;
165+
): SqlNamespaceCompletionItem {
166+
const component = finalComponent(container.canonicalPath);
164167
return Object.freeze({
165168
...(container.detail === undefined
166169
? {}
@@ -228,8 +231,7 @@ export function composeSqlNamespaceCompletion(
228231
const seen = new Set<string>();
229232
const containers: SqlNamespaceCatalogResolvedContainer[] = [];
230233
for (const container of response.containers) {
231-
const component = last(container.canonicalPath);
232-
if (!component) continue;
234+
const component = finalComponent(container.canonicalPath);
233235
let match: ReturnType<SqlNamespacePrefixMatcher>;
234236
try {
235237
match = input.matchPrefix(component, input.prefix);
@@ -251,10 +253,9 @@ export function composeSqlNamespaceCompletion(
251253
containers.push(container);
252254
}
253255
containers.sort(compareContainers);
254-
const items = containers.flatMap((container) => {
255-
const value = item(container, input.replacementRange);
256-
return value ? [value] : [];
257-
});
256+
const items = containers.map((container) =>
257+
item(container, input.replacementRange)
258+
);
258259
return Object.freeze({
259260
source: Object.freeze({
260261
coverage: response.coverage,

0 commit comments

Comments
 (0)