All files / src/excellent token-styles.ts

100% Statements 145/145
83.33% Branches 5/6
100% Functions 3/3
100% Lines 145/145

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 443x 443x 443x 443x 443x 443x 443x 443x 443x 443x 443x 443x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x 108x  
import { css, unsafeCSS } from 'lit';
import { TokenType, Token } from './tokenizer';
 
/** Default colors for expression token syntax highlighting. */
export const TOKEN_COLORS = {
  expression: '#0086e0',
  fn: '#0086e0',
  string: '#06a810',
  number: '#c25ceb',
  keyword: '#1750eb',
  operator: '#666',
  paren: '#999',
  parenUnmatched: '#ff0011'
};
 
export const TOKEN_CLASS_MAP: Record<string, string> = {
  [TokenType.Text]: 'tok-text',
  [TokenType.ExpressionPrefix]: 'tok-prefix',
  [TokenType.Identifier]: 'tok-id',
  [TokenType.FunctionName]: 'tok-fn',
  [TokenType.StringLiteral]: 'tok-str',
  [TokenType.NumberLiteral]: 'tok-num',
  [TokenType.Keyword]: 'tok-kw',
  [TokenType.Operator]: 'tok-op',
  [TokenType.ContextRef]: 'tok-ctx',
  [TokenType.Separator]: 'tok-sep',
  [TokenType.Whitespace]: 'tok-ws',
  [TokenType.Arrow]: 'tok-arrow',
  [TokenType.Bracket]: 'tok-bracket',
  [TokenType.EscapedAt]: 'tok-text',
  [TokenType.Paren]: 'tok-paren'
};
 
/** Expression token types get monospace font. */
export const EXPRESSION_TOKENS = new Set([
  TokenType.ExpressionPrefix,
  TokenType.Identifier,
  TokenType.FunctionName,
  TokenType.StringLiteral,
  TokenType.NumberLiteral,
  TokenType.Keyword,
  TokenType.Operator,
  TokenType.ContextRef,
  TokenType.Separator,
  TokenType.Whitespace,
  TokenType.Arrow,
  TokenType.Bracket,
  TokenType.Paren
]);
 
export function getTokenClass(token: Token): string {
  if (token.type === TokenType.Paren && token.balanced === false) {
    return 'tok-paren-unmatched';
  }
  return TOKEN_CLASS_MAP[token.type] || 'tok-text';
}
 
const c = (key: keyof typeof TOKEN_COLORS) => unsafeCSS(TOKEN_COLORS[key]);
 
/** Shared CSS for token syntax highlighting. */
export const tokenCss = css`
  .tok-text {
    color: inherit;
  }
 
  .tok-prefix {
    color: var(--expression-color, ${c('expression')});
    font-weight: 600;
  }
 
  .tok-id {
    color: var(--expression-color, ${c('expression')});
  }
 
  .tok-fn {
    color: var(--expression-fn-color, ${c('fn')});
    font-weight: 900;
  }
 
  .tok-str {
    color: var(--expression-string-color, ${c('string')});
  }
 
  .tok-num {
    color: var(--expression-number-color, ${c('number')});
  }
 
  .tok-kw {
    color: var(--expression-keyword-color, ${c('keyword')});
  }
 
  .tok-op {
    color: var(--expression-operator-color, ${c('operator')});
  }
 
  .tok-ctx {
    color: var(--expression-color, ${c('expression')});
  }
 
  .tok-sep {
    color: var(--expression-operator-color, ${c('operator')});
  }
 
  .tok-arrow {
    color: var(--expression-operator-color, ${c('operator')});
  }
 
  .tok-bracket {
    color: var(--expression-operator-color, ${c('operator')});
  }
 
  .tok-ws {
    /* whitespace tokens — no special color */
  }
 
  .tok-newline {
    /* Newline chars rendered via white-space: pre-wrap on parent */
  }
 
  .tok-paren {
    color: ${c('paren')};
  }
 
  .tok-paren-unmatched {
    color: var(--expression-paren-unmatched-color, ${c('parenUnmatched')});
    font-weight: 900;
  }
 
  .tok-fn-invalid {
    text-decoration: wavy underline ${c('parenUnmatched')};
    text-underline-offset: 3px;
  }
 
  .tok-mono {
    font-family: var(
      --expression-font-family,
      'SFMono-Regular',
      'Consolas',
      'Liberation Mono',
      'Menlo',
      monospace
    );
    font-size: 0.95em;
  }
`;