Closed josephrocca closed 2 days ago
Attached patch changes the way StreamLanguage
registers its indentation function to also work in a nested parser context. However, stream languages are not defined in a way that allows them to be aware of the parent indentation context around it, so though this is an improvement, it'll still sometimes produce an unexpected indentation, especially at the top level.
Thank you, Marijn! 🙏
For others using the JavaScript legacy StreamLanguage
, a hacky fix the top-level indent issue mentioned by Marijn is to change this line from this:
else return lexical.indented + (closing ? 0 : cx.unit);
to this:
else return lexical.column === 0 ? null : Math.max(0, lexical.indented) + (closing ? 0 : cx.unit);
I haven't extensively tested this, and it was kind of a guess-and-check solution, but it seems to solve indenting in the common top-level cases, at least. I'll update this comment if I find issues with it as I continue to test with more real-world code.
Describe the issue
Following advice from this issue:
I'm using the legacy JavaScript parser in order to get local/global variables highlighted differently. I've integrated it with
@codemirror/lang-html
like you can see in the code below. This works great except it doesn't use theindent
function of@codemirror/legacy-modes/mode/javascript
, so the indentation is incorrect when pressing enter to create a new line.As you can see, the indenting is incorrect in the above example. If I put a
console.log
within theindent
function here in the legacy JavaScript parser, then I can see that there aren't any logs, so it's not being used/called.You can compare the above example to the case where we just use the
@codemirror/lang-html
extension without any nested language overrides:In this case indenting is correct, of course.
I'm wondering if it is a bug or expected behavior that indentation does not follow the nested language parser's
indent
function?Browser and platform
Chrome v129, Ubuntu 22.04
Reproduction link
https://codemirror.net/try/?c=aW1wb3J0IHtiYXNpY1NldHVwLCBFZGl0b3JWaWV3fSBmcm9tICJjb2RlbWlycm9yIgppbXBvcnQge1N0cmVhbUxhbmd1YWdlfSBmcm9tICJAY29kZW1pcnJvci9sYW5ndWFnZSI7CmltcG9ydCB7amF2YXNjcmlwdH0gZnJvbSAiQGNvZGVtaXJyb3IvbGVnYWN5LW1vZGVzL21vZGUvamF2YXNjcmlwdCI7CmltcG9ydCB7aHRtbH0gZnJvbSAiQGNvZGVtaXJyb3IvbGFuZy1odG1sIjsKCmxldCBqYXZhc2NyaXB0U3RyZWFtTGFuZ3VhZ2UgPSBTdHJlYW1MYW5ndWFnZS5kZWZ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