开发者

CodeMirror code completion analyzing cursor location

开发者 https://www.devze.com 2023-02-12 18:25 出处:网络
I want to integrate code completion feature to CodeMirror based xml editor. It basically parses the schema of the xml file and provide code completion according to schema and its structure. But there

I want to integrate code completion feature to CodeMirror based xml editor. It basically parses the schema of the xml file and provide code completion according to schema and its structure. But there is something i could not manage to do. For example, when the cursor开发者_JAVA技巧 is moved to a location which is inside and xml tag, code completion must behave accordingly. It should aware of that the cursor is inside the tag, etc. How can i do that?

I think XML parser of the code mirror may give a clue about semantic position of the cursor location with some alteration. Is that possible?

Or is there any generic way to analyze cursor location and behave accordingly?

Thanks in advance.


If you use CodeMirror 2, there's a getTokenAt method that allows you to analyse the parser state at a given position. You can see this being used to autocomplete local JavaScript variables in http://codemirror.net/2/demo/complete.html . For the XML parser, you can inspect the context property of the state, which is a linked list of objects, each containing a tagName property and a prev property linking to the context above it.

0

精彩评论

暂无评论...
验证码 换一张
取 消