formatter.tsx 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. import {Fragment, useMemo} from 'react';
  2. import {useTheme} from '@emotion/react';
  3. import styled from '@emotion/styled';
  4. import {Tooltip} from 'sentry/components/tooltip';
  5. import {TokenType} from 'sentry/views/metrics/formulaParser/types';
  6. import grammar from './formulaFormatting.pegjs';
  7. const operatorTokens = new Set([
  8. TokenType.PLUS,
  9. TokenType.MINUS,
  10. TokenType.MULTIPLY,
  11. TokenType.DIVIDE,
  12. ]);
  13. interface EquationFormatterProps {
  14. equation: string;
  15. errors?: {
  16. message: string;
  17. start: number;
  18. end?: number;
  19. }[];
  20. }
  21. export function EquationFormatter({equation: formula, errors}: EquationFormatterProps) {
  22. const theme = useTheme();
  23. const tokens = useMemo(() => {
  24. try {
  25. return grammar.parse(formula);
  26. } catch (err) {
  27. return undefined;
  28. }
  29. }, [formula]);
  30. if (!tokens) {
  31. // If the formula cannot be parsed, we simply return it without any highlighting
  32. return <Fragment>{formula}</Fragment>;
  33. }
  34. const findMatchingError = (charCount: number) => {
  35. if (!errors || errors.length === 0) {
  36. return null;
  37. }
  38. return errors.find(
  39. error => error.start <= charCount && (!error.end || error.end >= charCount)
  40. );
  41. };
  42. let charCount = 0;
  43. let hasActiveTooltip = false;
  44. const renderedTokens = (
  45. <Fragment>
  46. {tokens.map((token, index) => {
  47. const error = findMatchingError(charCount);
  48. charCount += token.content.length;
  49. if (error) {
  50. const content = (
  51. <Token key={index} style={{color: theme.errorText}}>
  52. {token.content}
  53. </Token>
  54. );
  55. // Only show one tooltip at a time
  56. const showTooltip = !hasActiveTooltip;
  57. hasActiveTooltip = true;
  58. return showTooltip ? (
  59. <Tooltip title={error.message} key={index} forceVisible>
  60. {content}
  61. </Tooltip>
  62. ) : (
  63. content
  64. );
  65. }
  66. if (token.type === TokenType.VARIABLE) {
  67. return (
  68. <Token key={index} style={{color: theme.yellow400, fontWeight: 'bold'}}>
  69. {token.content}
  70. </Token>
  71. );
  72. }
  73. if (operatorTokens.has(token.type)) {
  74. return (
  75. <Token key={index} style={{color: theme.blue300}}>
  76. {token.content}
  77. </Token>
  78. );
  79. }
  80. return (
  81. <Token key={index} style={{color: theme.gray500}}>
  82. {token.content}
  83. </Token>
  84. );
  85. })}
  86. </Fragment>
  87. );
  88. // Unexpected EOL might not match a token
  89. const remainingError = !hasActiveTooltip && findMatchingError(charCount);
  90. return (
  91. <Fragment>
  92. {renderedTokens}
  93. {remainingError && (
  94. <Tooltip title={findMatchingError(charCount)?.message} forceVisible>
  95. &nbsp;
  96. </Tooltip>
  97. )}
  98. </Fragment>
  99. );
  100. }
  101. const Token = styled('span')`
  102. white-space: pre;
  103. `;