@@ -19,16 +19,21 @@ export type SQLCompletion = {
1919 type ?: string ;
2020} ;
2121
22+ // Characters that form SQL identifiers in our editor: word chars, dots,
23+ // single quotes, brackets, $, {, }, and : — to support expressions like
24+ // `ResourceAttributes['service.name']`, `$__dateFilter`, `{name:Type}`.
25+ const IDENTIFIER_CHAR = "[\\w.'[\\]${}:]" ;
26+ const IDENTIFIER_BEFORE = new RegExp ( `${ IDENTIFIER_CHAR } +` ) ;
27+ const IDENTIFIER_AFTER = new RegExp ( `^${ IDENTIFIER_CHAR } +` ) ;
28+ const IDENTIFIER_VALID_FOR = new RegExp ( `^${ IDENTIFIER_CHAR } *$` ) ;
29+
2230/**
2331 * Creates a custom CodeMirror completion source for SQL identifiers (column names, table
2432 * names, functions, etc.) that inserts them verbatim, without quoting.
2533 */
2634export function createIdentifierCompletionSource ( completions : Completion [ ] ) {
2735 return ( context : CompletionContext ) => {
28- // Match word characters, dots, single quotes, brackets, $, {, }, and :
29- // to support identifiers like `ResourceAttributes['service.name']`,
30- // macros like `$__dateFilter`, and query params like `{name:Type}`
31- const prefix = context . matchBefore ( / [ \w . ' [ \] $ { } : ] + / ) ;
36+ const prefix = context . matchBefore ( IDENTIFIER_BEFORE ) ;
3237 if ( ! prefix && ! context . explicit ) return null ;
3338
3439 // Suppress suggestions after AS keyword since the user is typing a custom alias
@@ -37,10 +42,18 @@ export function createIdentifierCompletionSource(completions: Completion[]) {
3742 . trimEnd ( ) ;
3843 if ( / \b A S $ / i. test ( textBefore ) ) return null ;
3944
45+ // Look forward from cursor to include trailing identifier characters
46+ // (e.g. the `']` in `ResourceAttributes['host.']`) so accepting a
47+ // suggestion replaces the entire identifier, not just up to the cursor.
48+ const docText = context . state . doc . sliceString ( context . pos ) ;
49+ const suffix = docText . match ( IDENTIFIER_AFTER ) ;
50+ const to = suffix ? context . pos + suffix [ 0 ] . length : context . pos ;
51+
4052 return {
4153 from : prefix ?. from ?? context . pos ,
54+ to,
4255 options : completions ,
43- validFor : / ^ [ \w . ' [ \] $ { } : ] * $ / ,
56+ validFor : IDENTIFIER_VALID_FOR ,
4457 } ;
4558 } ;
4659}
0 commit comments