@fc-components/monaco-editor 0.1.27 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -3,7 +3,7 @@ import MonacoEditor from 'react-monaco-editor';
3
3
  import * as monaco from 'monaco-editor';
4
4
  import { languages, Range, editor, Selection, KeyMod, KeyCode, MarkerSeverity } from 'monaco-editor';
5
5
  import { promLanguageDefinition } from 'monaco-promql';
6
- import { parser, LabelName, UnquotedLabelMatcher, LabelMatchers, VectorSelector, PromQL, Identifier, FunctionCallBody, StringLiteral, BinaryExpr, NumberDurationLiteralInDurationContext, MatrixSelector, GroupingLabels, AggregateModifier, AggregateExpr, MatchOp, EqlSingle, EqlRegex, Neq, NeqRegex } from '@fc-components/lezer-metricsql';
6
+ import { parser, LabelName, UnquotedLabelMatcher, LabelMatchers, VectorSelector, PromQL, Identifier, FunctionCallBody, StringLiteral, BinaryExpr, NumberDurationLiteralInDurationContext, MatrixSelector, GroupingLabels, WithExpr, WithAssignment, AggregateModifier, AggregateExpr, MatchOp, EqlSingle, EqlRegex, Neq, NeqRegex } from '@fc-components/lezer-metricsql';
7
7
  import { v4 } from 'uuid';
8
8
  import { css } from '@emotion/css';
9
9
 
@@ -340,6 +340,7 @@ for (var _i = 0, aggregations_1 = aggregations; _i < aggregations_1.length; _i++
340
340
  // PromQL vector matching + the by and without clauses
341
341
  // (https://prometheus.io/docs/prometheus/latest/querying/operators/#vector-matching)
342
342
  var vectorMatching = ['on', 'ignoring', 'group_right', 'group_left', 'by', 'without'];
343
+ var cteKeywords = ['with'];
343
344
  // Produce a regex matching elements : (elt1|elt2|...)
344
345
  var vectorMatchingRegex = '(' + /*#__PURE__*/vectorMatching.reduce(function (prev, curr) {
345
346
  return prev + '|' + curr;
@@ -351,7 +352,7 @@ var operators = ['+', '-', '*', '/', '%', '^', '==', '!=', '>', '<', '>=', '<=',
351
352
  // (https://prometheus.io/docs/prometheus/latest/querying/basics/#offset-modifier)
352
353
  var offsetModifier = ['offset'];
353
354
  // Merging all the keywords in one list
354
- var keywords = /*#__PURE__*/aggregations.concat(functions).concat(aggregationsOverTime).concat(vectorMatching).concat(offsetModifier);
355
+ var keywords = /*#__PURE__*/aggregations.concat(functions).concat(aggregationsOverTime).concat(vectorMatching).concat(offsetModifier).concat(cteKeywords);
355
356
  // noinspection JSUnusedGlobalSymbols
356
357
  var language = {
357
358
  ignoreCase: false,
@@ -1289,6 +1290,15 @@ var RESOLVERS = [{
1289
1290
  }, {
1290
1291
  path: [GroupingLabels],
1291
1292
  fun: resolveLabelsForGrouping
1293
+ }, {
1294
+ path: [WithExpr],
1295
+ fun: resolveWithExpr
1296
+ }, {
1297
+ path: [WithExpr, PromQL],
1298
+ fun: resolveWithExpr
1299
+ }, {
1300
+ path: [WithAssignment, WithExpr],
1301
+ fun: resolveWithExpr
1292
1302
  }];
1293
1303
  var LABEL_OP_MAP = /*#__PURE__*/new Map([[EqlSingle, '='], [EqlRegex, '=~'], [Neq, '!='], [NeqRegex, '!~']]);
1294
1304
  function getLabelOp(opNode) {
@@ -1461,6 +1471,25 @@ function resolveInFunction() {
1461
1471
  type: 'IN_FUNCTION'
1462
1472
  };
1463
1473
  }
1474
+ function resolveWithExpr(node, text, pos) {
1475
+ // Find the containing WithExpr node (node may be a WithAssignment child)
1476
+ var withExprNode = node.type.id === WithExpr ? node : node.parent;
1477
+ while (withExprNode && withExprNode.type.id !== WithExpr) {
1478
+ withExprNode = withExprNode.parent;
1479
+ }
1480
+ if (!withExprNode) return null;
1481
+ // Only return IN_WITH_BODY when cursor is in the body expression (after the closing ')')
1482
+ var children = getNodeChildren(withExprNode);
1483
+ var closeParen = children.find(function (c) {
1484
+ return getNodeText(c, text) === ')';
1485
+ });
1486
+ if (closeParen && pos > closeParen.to) {
1487
+ return {
1488
+ type: 'IN_WITH_BODY'
1489
+ };
1490
+ }
1491
+ return null;
1492
+ }
1464
1493
  function resolveDurations() {
1465
1494
  return {
1466
1495
  type: 'IN_DURATION'
@@ -1570,6 +1599,25 @@ function getErrorNode(tree, pos) {
1570
1599
  }
1571
1600
  return null;
1572
1601
  }
1602
+ function findMatchingParen(text, openPos) {
1603
+ var depth = 0;
1604
+ for (var i = openPos; i < text.length; i++) {
1605
+ if (text[i] === '(') depth++;
1606
+ if (text[i] === ')') {
1607
+ depth--;
1608
+ if (depth === 0) return i;
1609
+ }
1610
+ }
1611
+ return -1;
1612
+ }
1613
+ function isInWithBody(text, pos) {
1614
+ var withMatch = text.match(/^with\s*\(/i);
1615
+ if (!withMatch) return false;
1616
+ var openParenPos = withMatch[0].length - 1;
1617
+ var closeParenPos = findMatchingParen(text, openParenPos);
1618
+ if (closeParenPos === -1) return false;
1619
+ return pos > closeParenPos;
1620
+ }
1573
1621
  function getSituation(text, pos) {
1574
1622
  // there is a special-case when we are at the start of writing text,
1575
1623
  // so we handle that case first
@@ -1578,6 +1626,12 @@ function getSituation(text, pos) {
1578
1626
  type: 'EMPTY'
1579
1627
  };
1580
1628
  }
1629
+ // text-based check for with body (fallback until grammar supports WithExpr)
1630
+ if (isInWithBody(text, pos)) {
1631
+ return {
1632
+ type: 'IN_WITH_BODY'
1633
+ };
1634
+ }
1581
1635
  /**
1582
1636
  PromQL
1583
1637
  Expr
@@ -1660,12 +1714,19 @@ function _getAllFunctionsAndMetricNamesCompletions() {
1660
1714
  while (1) switch (_context.n) {
1661
1715
  case 0:
1662
1716
  metricNames = getAllMetricNamesCompletions(dataProvider);
1663
- return _context.a(2, [].concat(FUNCTION_COMPLETIONS, metricNames));
1717
+ return _context.a(2, [CTE_KEYWORD_COMPLETION].concat(FUNCTION_COMPLETIONS, metricNames));
1664
1718
  }
1665
1719
  }, _callee);
1666
1720
  }));
1667
1721
  return _getAllFunctionsAndMetricNamesCompletions.apply(this, arguments);
1668
1722
  }
1723
+ var CTE_KEYWORD_COMPLETION = {
1724
+ type: 'FUNCTION',
1725
+ label: 'with',
1726
+ insertText: 'with (',
1727
+ detail: 'with (cte_name = expr, ...) expr',
1728
+ documentation: 'Define Common Table Expressions (CTEs) for use in the query. MetricsQL extension.'
1729
+ };
1669
1730
  var DURATION_COMPLETIONS = /*#__PURE__*/['1m', '5m', '10m', '30m', '1h', '1d'].map(function (text) {
1670
1731
  return {
1671
1732
  type: 'DURATION',
@@ -1907,6 +1968,7 @@ function getCompletions(situation, dataProvider) {
1907
1968
  return Promise.resolve(DURATION_COMPLETIONS);
1908
1969
  case 'IN_FUNCTION':
1909
1970
  return getAllFunctionsAndMetricNamesCompletions(dataProvider);
1971
+ case 'IN_WITH_BODY':
1910
1972
  case 'AT_ROOT':
1911
1973
  {
1912
1974
  return getAllFunctionsAndMetricNamesCompletions(dataProvider);
@@ -1915,7 +1977,7 @@ function getCompletions(situation, dataProvider) {
1915
1977
  {
1916
1978
  var metricNames = getAllMetricNamesCompletions(dataProvider);
1917
1979
  var historyCompletions = getAllHistoryCompletions();
1918
- return Promise.resolve([].concat(historyCompletions, FUNCTION_COMPLETIONS, metricNames));
1980
+ return Promise.resolve([].concat(historyCompletions, [CTE_KEYWORD_COMPLETION], FUNCTION_COMPLETIONS, metricNames));
1919
1981
  }
1920
1982
  case 'IN_LABEL_SELECTOR_NO_LABEL_NAME':
1921
1983
  return getLabelNamesForSelectorCompletions(situation.metricName, situation.hasOperator, situation.otherLabels, dataProvider);