@@ -3,8 +3,10 @@ import type {
33 SqlNamespaceCatalogSearchOutcome ,
44} from "./namespace-catalog-coordinator.js" ;
55import type {
6+ SqlCanonicalNamespacePath ,
67 SqlNamespaceCatalogResolvedContainer ,
78 SqlNamespaceContainerRole ,
9+ SqlNamespacePathComponent ,
810 SqlNamespaceQuerySite ,
911} from "./namespace-catalog-types.js" ;
1012import 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
7480function 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(
158162function 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