lezer-parser / highlight

Syntax highlighting from Lezer trees
MIT License
31 stars 9 forks source link

Tags may be null when using legacy StreamParser. #5

Closed yfwz100 closed 1 year ago

yfwz100 commented 1 year ago

Tags may be null/undefined when using legacy StreamParser provided in @codemirror/language.

It will break the highlight code in the following position:

https://github.com/lezer-parser/highlight/blame/5b04ee057d7b0b3d54e8814f31a638c2df707fbe/src/highlight.ts#LL244C10-L244C10

For reference in @codemirror/language:

https://github.com/codemirror/language/blob/76955d9a33b9ebe5b3118a1b4a33fa958c13e507/src/stream-parser.ts#L96

The root node does not have tags defined.

marijnh commented 1 year ago

If you mean the tags argument, that is defined to not be null.

yfwz100 commented 1 year ago

So... should stream parser be modified to have tags built in?

yfwz100 commented 1 year ago

Or I have to wrap the classHighlighter with the following code:

syntaxHighlighting({
  style(tags) {
    if (!tags) {
      return null;
    }
    return classHighlighter.style(tags);
  },
  scope(node) {
    return classHighlighter.scope?.(node) ?? false;
  }
}),
marijnh commented 1 year ago

So... should stream parser be modified to have tags built in?

I don't see why that is necessary. Highlighters are called with an array, not null. I don't see any place where that is not happening.

yfwz100 commented 1 year ago

I created a minimal editor with the above setup.

It's very strange that the facet in the local code is different from the online editor.

The local code in getStyleTags() reports the root node as: image

However, the online one reports: image

The props are different.

I don't know what it's happening...

yfwz100 commented 1 year ago

I finally find the root cause of the above error.

It's because @lezer/common is being downloaded multiple times in node_modules because of the mismatched versions. The NodeProp's ID is determined by a module-specific global counter. If modules are not shared, the ID is not increased. So the ID is messy.