Accessing language state

Hey! I’m trying to hook into the CodeMirrors language state to track existing variables in the code. My task is to detect when a new variable has been introduced or removed and do something with it.

I found that LanguageState is inside state.values, However, the values field isn’t accessible via TS types, so I assume there’s a better way to get to it safely than using state.values.find(...) as LanguageState.

Thank you so much for the amazing library and for helping the community to answer the questions. I appreciate it a lot!

LanguageState isn’t public, but syntaxTree is, which may be what you’re looking for.

1 Like

I’m looking for the tree, but I still can’t find a way to access it. I explored both state and update and still can’t find it. I’m probably missing something obvious. Sorry about that.

You use the function I linked in my message. import {syntaxTree} from "@codemirror/language", and then syntaxTree(state).

Thank you for your help! :bowing_man: