CodeMirror code completion analyzing cursor location

3.4k Views Asked by At

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 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.

1

There are 1 best solutions below

0
On

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.