lang-python highlight function definition

In the python mode for codemirror 5, function definitions are highlighted with the class “cm-def”. For example

def function(x, y):
  return x**2 + y**2

would yield for the first line

<span class="cm-keyword">def</span> <span class="cm-def">function</span>(<span class="cm-variable">x</span>, <span class="cm-variable">y</span>):

I am trying to do the same with codemirror 6. Reading the documentation, it seems like this line from lang-python is the one that applies it:

"CallExpression/VariableName": t.function(t.variableName)

And then as a simple highlightstyle, I’m using

export const highlightStyle = HighlightStyle.define(
  {tag: t.function(t.variableName),
   color: "#6f42c1"},
)

The issue is that the function token isn’t highlighted with t.function(t.variableName); it only seems to get a style class if I use t.variableName only (but that also highlights the function parameters which I want to avoid).

What’s the correct way to target the function token in the code snippet?

A function definition isn’t a CallExpression node—it’s a FunctionDefinition. See these tests. Does this patch help?

1 Like

Ah that makes sense. I thought the t.function was sufficient since the description said “called or defined as a function”; didn’t know both t.function and t.definition were required.

Yeah the patch is perfect:

python

Thanks for the fast response! :slight_smile: