{"version":3,"file":"codemirrorUtils.mjs","sources":["../../../../src/components/DataLinks/codemirrorUtils.ts"],"sourcesContent":["import { type Extension } from '@codemirror/state';\nimport { Decoration, type DecorationSet, EditorView, ViewPlugin, type ViewUpdate } from '@codemirror/view';\n\nimport { DataLinkBuiltInVars, type GrafanaTheme2, VariableOrigin, type VariableSuggestion } from '@grafana/data';\n\nimport { type CodeMirrorCompletionContext, type CodeMirrorCompletionResult } from '../CodeMirror/types';\nimport { createVariableCompletionSource } from '../CodeMirror/variableCompletion';\n\n// Matches a complete `${...}` variable reference anywhere in the document.\nconst VARIABLE_PATTERN = /\\$\\{[^}]+\\}/g;\n\nexport type DataLinkInterpolationMode = 'url' | 'text';\n\nconst VARIABLE_CLASS = 'cm-variable';\n\n/**\n * Theme contribution for the DataLink variable token. Layered on top of the\n * inline input theme; targets only `.cm-variable`, so it does not compete with\n * the base content color.\n */\nexport function createDataLinkTheme(theme: GrafanaTheme2): Extension {\n  return EditorView.theme({\n    [`.${VARIABLE_CLASS}`]: {\n      color: theme.colors.success.text,\n      fontWeight: `${theme.typography.fontWeightMedium}`,\n    },\n  });\n}\n\n/**\n * Syntax highlighter for `${...}` variable patterns in data link URLs. Mirrors\n * the old Prism `builtInVariable` grammar. Uses a ViewPlugin (not a\n * MatchDecorator) so the global regex's `lastIndex` is reset on every rebuild,\n * avoiding stale state.\n */\nexport function createDataLinkHighlighter(): Extension {\n  const decoration = Decoration.mark({ class: VARIABLE_CLASS });\n\n  const build = (view: EditorView): DecorationSet => {\n    const text = view.state.doc.toString();\n    const ranges: Array<ReturnType<typeof decoration.range>> = [];\n\n    VARIABLE_PATTERN.lastIndex = 0;\n    let match;\n    while ((match = VARIABLE_PATTERN.exec(text)) !== null) {\n      ranges.push(decoration.range(match.index, match.index + match[0].length));\n    }\n\n    return Decoration.set(ranges);\n  };\n\n  return ViewPlugin.fromClass(\n    class {\n      decorations: DecorationSet;\n\n      constructor(view: EditorView) {\n        this.decorations = build(view);\n      }\n\n      update(update: ViewUpdate) {\n        if (update.docChanged || update.viewportChanged) {\n          this.decorations = build(update.view);\n        }\n      }\n    },\n    { decorations: (v) => v.decorations }\n  );\n}\n\nfunction getApplyText(suggestion: VariableSuggestion, mode: DataLinkInterpolationMode): string {\n  if (\n    mode === 'text' ||\n    suggestion.origin !== VariableOrigin.Template ||\n    suggestion.value === DataLinkBuiltInVars.includeVars\n  ) {\n    return `\\${${suggestion.value}}`;\n  }\n  return `\\${${suggestion.value}:queryparam}`;\n}\n\n/**\n * Autocompletion source for data link variables, triggered by `$` — and, in\n * `'url'` mode, also by `=`, the query-param separator (`?key=`), which is\n * preserved rather than replaced.\n *\n * Pass `{ mode: 'text' }` for plain-text fields such as a link title: `=` no\n * longer triggers and template variables are formatted as `${var}` without the\n * URL-only `:queryparam` encoding.\n */\nexport function dataLinkAutocompletion(\n  suggestions: VariableSuggestion[],\n  options: { mode?: DataLinkInterpolationMode } = {}\n): (context: CodeMirrorCompletionContext) => CodeMirrorCompletionResult | null {\n  const { mode = 'url' } = options;\n\n  return createVariableCompletionSource(suggestions, {\n    separators: mode === 'url' ? ['='] : [],\n    getInsertText: (suggestion) => getApplyText(suggestion, mode),\n  });\n}\n"],"names":[],"mappings":";;;;;AASA,MAAM,gBAAA,GAAmB,cAAA;AAIzB,MAAM,cAAA,GAAiB,aAAA;AAOhB,SAAS,oBAAoB,KAAA,EAAiC;AACnE,EAAA,OAAO,WAAW,KAAA,CAAM;AAAA,IACtB,CAAC,CAAA,CAAA,EAAI,cAAc,CAAA,CAAE,GAAG;AAAA,MACtB,KAAA,EAAO,KAAA,CAAM,MAAA,CAAO,OAAA,CAAQ,IAAA;AAAA,MAC5B,UAAA,EAAY,CAAA,EAAG,KAAA,CAAM,UAAA,CAAW,gBAAgB,CAAA;AAAA;AAClD,GACD,CAAA;AACH;AAQO,SAAS,yBAAA,GAAuC;AACrD,EAAA,MAAM,aAAa,UAAA,CAAW,IAAA,CAAK,EAAE,KAAA,EAAO,gBAAgB,CAAA;AA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