@@ -23,13 +23,17 @@ import {
2323 createCodeMirrorStyleTheme ,
2424 DEFAULT_CODE_MIRROR_BASIC_SETUP ,
2525} from '@/components/SQLEditor/utils' ;
26- import { usePromqlVariableCompletions } from '@/components/SQLEditor/variableCompletions' ;
26+ import {
27+ PROMQL_MACRO_COMPLETIONS ,
28+ usePromqlVariableCompletions ,
29+ } from '@/components/SQLEditor/variableCompletions' ;
2730import {
2831 useVariableValidation ,
2932 VariableIssueIndicator ,
3033 variableValidationState ,
3134} from '@/components/SQLEditor/variableValidation' ;
3235
36+ import { promqlTemplateExtension } from './templateMaskedLanguage' ;
3337import { createVariableCompletionSource } from './variableCompletionSource' ;
3438
3539import styles from '@/components/SQLEditor/SQLInlineEditor.module.scss' ;
@@ -47,6 +51,8 @@ type PromQLEditorProps = {
4751 * modal or other scroll container, which would otherwise clip the popup.
4852 */
4953 parentRef ?: HTMLElement | null ;
54+ /** Suggest `$__interval` and the other chart macros. */
55+ enableMacros ?: boolean ;
5056} ;
5157
5258const MAX_EDITOR_HEIGHT = '150px' ;
@@ -113,30 +119,37 @@ export default function PromQLEditor({
113119 onSubmit,
114120 metricNames,
115121 parentRef,
122+ enableMacros,
116123} : PromQLEditorProps ) {
117124 const { colorScheme } = useMantineColorScheme ( ) ;
118125 const ref = useRef < ReactCodeMirrorRef > ( null ) ;
119126 const compartmentRef = useRef < Compartment > ( new Compartment ( ) ) ;
120127 const [ isFocused , setIsFocused ] = useState ( false ) ;
121128 const variableCompletions = usePromqlVariableCompletions ( ) ;
129+ const templateCompletions = useMemo (
130+ ( ) =>
131+ enableMacros
132+ ? [ ...PROMQL_MACRO_COMPLETIONS , ...variableCompletions ]
133+ : variableCompletions ,
134+ [ enableMacros , variableCompletions ] ,
135+ ) ;
122136 const variableIssues = useVariableValidation ( value , { language : 'promql' } ) ;
123137
124138 const updateAutocomplete = useCallback (
125139 ( viewRef : EditorView ) => {
126140 const override : CompletionSource [ ] = [ ] ;
127141
128- if ( variableCompletions . length > 0 ) {
129- override . push ( createVariableCompletionSource ( variableCompletions ) ) ;
142+ if ( templateCompletions . length > 0 ) {
143+ override . push ( createVariableCompletionSource ( templateCompletions ) ) ;
130144 }
131145
132146 if ( metricNames ?. length ) {
133147 override . push ( debounceAndPruneAutocompleteResults ( metricNames ) ) ;
134148 }
135149
136- // PromQL's own function/keyword completion, re-registered explicitly.
137- // `PromQLExtension.asExtension()` registers it through
138- // `language.data.of({ autocomplete })`, which an `override` array
139- // replaces outright — so without this it is silently lost.
150+ // PromQL's own function/keyword completion. `promqlTemplateExtension`
151+ // installs the language without it, and an `override` array would
152+ // replace a `language.data` registration outright anyway.
140153 override . push ( context => promqlExtension . getComplete ( ) . promQL ( context ) ) ;
141154
142155 viewRef . dispatch ( {
@@ -145,7 +158,7 @@ export default function PromQLEditor({
145158 ) ,
146159 } ) ;
147160 } ,
148- [ metricNames , variableCompletions ] ,
161+ [ metricNames , templateCompletions ] ,
149162 ) ;
150163
151164 useEffect ( ( ) => {
@@ -160,8 +173,10 @@ export default function PromQLEditor({
160173 createCodeMirrorStyleTheme ( MAX_EDITOR_HEIGHT ) ,
161174 EditorView . lineWrapping ,
162175
163- // PromQL syntax highlighting
164- promqlExtension . asExtension ( ) ,
176+ // PromQL syntax highlighting and linting, variable- and macro-aware
177+ promqlTemplateExtension ( promqlExtension , {
178+ enableMacros : ! ! enableMacros ,
179+ } ) ,
165180
166181 // Autocomplete sources (via compartment for hot-swapping)
167182 // eslint-disable-next-line react-hooks/refs
@@ -190,7 +205,7 @@ export default function PromQLEditor({
190205 } ,
191206 ] ) ,
192207 ] ,
193- [ onSubmit , parentRef ] ,
208+ [ onSubmit , parentRef , enableMacros ] ,
194209 ) ;
195210
196211 const onClickCodeMirror = useCallback ( ( ) => {
0 commit comments