Skip to content

Commit

Permalink
chore: fix highlighting of backtick identifiers
Browse files Browse the repository at this point in the history
  • Loading branch information
kswenson committed Oct 2, 2024
1 parent dcb2c80 commit 8f63155
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
5 changes: 4 additions & 1 deletion v3/src/components/common/formula-editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,10 @@ function addCodapHighlightingClasses(view: EditorView) {
// Traverse the syntax tree
tree.iterate({
enter(node) {
const nodeText = view.state.doc.sliceString(node.from, node.to)
let nodeText = view.state.doc.sliceString(node.from, node.to)
// highlight attribute names in backticks
const execResult = /^`(.+)`$/.exec(nodeText)
if (execResult?.[1]) nodeText = execResult[1]
const highlightClass = highlightClasses[node.type.name]?.(nodeText, dataSet, options ?? kAllOptions)
if (highlightClass) {
builder.add(node.from, node.to, Decoration.mark({ class: highlightClass }))
Expand Down
12 changes: 8 additions & 4 deletions v3/src/models/formula/lezer/formula-highlight.scss
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
color: #888
}

// colors match v2 colors

.cm-boolean, .cm-number {
color: #164;
}
Expand All @@ -25,19 +27,21 @@
color: blue;
}

.codap-attribute .cm-variable {
// codap-class can occur with or without cm-variable

.codap-attribute, .codap-attribute .cm-variable {
color: magenta;
}

.codap-constant .cm-variable {
.codap-constant, .codap-constant .cm-variable {
color: teal;
}

.codap-global .cm-variable {
.codap-global, .codap-global .cm-variable {
color: maroon;
}

.codap-special .cm-variable {
.codap-special, .codap-special .cm-variable {
color: red;
}
}
Expand Down
1 change: 1 addition & 0 deletions v3/src/models/formula/lezer/formula.grammar
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ boolean { @specialize[@name=BooleanLiteral]<identifier, "true" | "false"> }
FunctionName { identifier }

// identifiers can be specified with backtick strings
// TODO: separate backticks from variable name so variable name can be highlighted without the backticks
VariableName { identifier | BackTickString }

questionOp[@name=LogicOp] { "?" }
Expand Down

0 comments on commit 8f63155

Please sign in to comment.