@@ -6,11 +6,7 @@ import {
66 isPromqlSavedChartConfig ,
77 isRawSqlSavedChartConfig ,
88} from '@hyperdx/common-utils/dist/guards' ;
9- import {
10- MACRO_SUGGESTIONS ,
11- MacroSuggestion ,
12- VARIABLE_MACRO_SUGGESTIONS ,
13- } from '@hyperdx/common-utils/dist/macros' ;
9+ import { MACRO_SUGGESTIONS } from '@hyperdx/common-utils/dist/macros' ;
1410import { QUERY_PARAMS_BY_DISPLAY_TYPE } from '@hyperdx/common-utils/dist/rawSqlParams' ;
1511import {
1612 BuilderSavedChartConfig ,
@@ -30,76 +26,16 @@ import {
3026 SourceKind ,
3127 TSource ,
3228} from '@hyperdx/common-utils/dist/types' ;
33- import {
34- substituteVariables ,
35- VARIABLE_FORMATS ,
36- VariableFormat ,
37- } from '@hyperdx/common-utils/dist/variables' ;
3829
3930import { getStoredLanguage } from '@/components/SearchInput' ;
4031import { type SQLCompletion } from '@/components/SQLEditor/utils' ;
32+ import {
33+ buildVariableCompletions ,
34+ toMacroCompletion ,
35+ } from '@/components/SQLEditor/variableCompletions' ;
4136
4237import { ChartEditorFormState } from './types' ;
4338
44- /** What each `${name:format}` renders, for the completion's help text. */
45- const VARIABLE_FORMAT_DESCRIPTIONS : Record < VariableFormat , string > = {
46- sqlstring : "Quoted and comma-separated, escaped for SQL. e.g. 'a', 'b', 'c'" ,
47- csv : 'Comma-separated and unquoted. Not SQL-escaped. e.g. a,b,c' ,
48- regex : 'A regex alternation. Regex escaped. e.g. (a|b|c)' ,
49- lucene : 'An OR of quoted terms, for Lucene inputs. e.g. ("a" OR "b" OR "c")' ,
50- } ;
51-
52- /** What `snippet` expands to against the variable's current selection. */
53- function describeVariableExpansion (
54- snippet : string ,
55- variable : ChartVariable ,
56- ) : string | undefined {
57- let expansion : string ;
58- try {
59- expansion = substituteVariables ( snippet , [ variable ] ) ;
60- } catch {
61- return undefined ;
62- }
63- return `Expands to: ${ expansion === '' ? '(empty string)' : expansion } ` ;
64- }
65-
66- /**
67- * Completion help built as markup rather than a string, so `footnote` sits on
68- * its own line.
69- *
70- * CodeMirror turns a string `info` into a single text node, where a `\n` is
71- * subject to the inherited `white-space` and generally collapses to a space.
72- * A `Node` is rendered as given, so the break is structural.
73- */
74- function completionInfo ( description : string , footnote : string ) {
75- return ( ) => {
76- const dom = document . createElement ( 'div' ) ;
77-
78- const main = document . createElement ( 'div' ) ;
79- main . textContent = description ;
80- dom . appendChild ( main ) ;
81-
82- const sub = document . createElement ( 'div' ) ;
83- sub . className = 'cm-completionInfo-footnote' ;
84- sub . textContent = footnote ;
85- dom . appendChild ( sub ) ;
86-
87- return dom ;
88- } ;
89- }
90-
91- const toMacroCompletion = ( {
92- name,
93- minArgs,
94- description,
95- } : MacroSuggestion ) : SQLCompletion => ( {
96- label : `$__${ name } ` ,
97- apply : minArgs > 0 ? `$__${ name } (` : `$__${ name } ` ,
98- detail : 'macro' ,
99- info : description ,
100- type : 'function' ,
101- } ) ;
102-
10339/**
10440 * Autocomplete entries offered on top of columns/keywords in the raw SQL
10541 * editor: query params, macros, and variables (when available).
@@ -124,80 +60,10 @@ export function buildRawSqlCompletions({
12460 } ) ,
12561 ) ;
12662
127- const macroCompletions = MACRO_SUGGESTIONS . map ( toMacroCompletion ) ;
128-
129- if ( ! variables ?. length ) {
130- return [ ...paramCompletions , ...macroCompletions ] ;
131- }
132-
133- const variableMacroCompletions =
134- VARIABLE_MACRO_SUGGESTIONS . map ( toMacroCompletion ) ;
135-
136- const variableCompletions = variables . flatMap ( ( variable ) : SQLCompletion [ ] => {
137- const { name } = variable ;
138-
139- /** A static description and an expansion preview given the current variable selections */
140- const help = ( snippet : string | undefined , description : string ) => {
141- const expansion =
142- snippet == null
143- ? undefined
144- : describeVariableExpansion ( snippet , variable ) ;
145- return expansion ? completionInfo ( description , expansion ) : description ;
146- } ;
147-
148- return [
149- ...( variable . expression
150- ? [
151- {
152- label : `$__filter(${ name } )` ,
153- apply : `$__filter(${ name } )` ,
154- detail : 'variable filter' ,
155- info : help (
156- `$__filter(${ name } )` ,
157- `Filters by the ${ name } variable using its defined expression. Matches every row when no values are selected for the variable.` ,
158- ) ,
159- type : 'function' ,
160- } ,
161- ]
162- : [ ] ) ,
163- {
164- label : `$${ name } ` ,
165- apply : `$${ name } ` ,
166- detail : 'variable' ,
167- info : help (
168- `$${ name } ` ,
169- `The selected values of ${ name } , in the default sqlstring format. Has no valid empty state — prefer $__filter(<expression>, ${ name } ).` ,
170- ) ,
171- type : 'variable' ,
172- } ,
173- {
174- label : `\${${ name } }` ,
175- apply : `\${${ name } }` ,
176- detail : 'variable' ,
177- info : help (
178- `\${${ name } }` ,
179- `The same as $${ name } , but delimited — use it when the reference runs into following word characters, as in \${${ name } }_total.` ,
180- ) ,
181- type : 'variable' ,
182- } ,
183- ...VARIABLE_FORMATS . map ( ( format ) : SQLCompletion => {
184- const reference = `\${${ name } :${ format } }` ;
185- return {
186- label : reference ,
187- apply : reference ,
188- detail : 'variable' ,
189- info : help ( reference , VARIABLE_FORMAT_DESCRIPTIONS [ format ] ) ,
190- type : 'variable' ,
191- } ;
192- } ) ,
193- ] ;
194- } ) ;
195-
19663 return [
19764 ...paramCompletions ,
198- ...macroCompletions ,
199- ...variableMacroCompletions ,
200- ...variableCompletions ,
65+ ...MACRO_SUGGESTIONS . map ( toMacroCompletion ) ,
66+ ...buildVariableCompletions ( variables ) ,
20167 ] ;
20268}
20369
0 commit comments