CodeMirror 代码完成分析光标位置
我想将代码完成功能集成到基于 CodeMirror 的 xml 编辑器中。它基本上解析 xml 文件的模式,并根据模式及其结构提供代码完成。但有件事我无法做到。例如,当光标移动到 xml 标记内部的位置时,代码完成必须做出相应的行为。它应该知道光标位于标签内等。我该怎么做?
我认为代码镜像的 XML 解析器可以通过一些改变提供有关光标位置的语义位置的线索。这可能吗?
或者是否有任何通用方法来分析光标位置并相应地表现?
提前致谢。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您使用 CodeMirror 2,则可以使用 getTokenAt 方法来分析给定位置的解析器状态。您可以在 http://codemirror.net/2/demo 中看到它用于自动完成本地 JavaScript 变量/complete.html 。对于 XML 解析器,您可以检查状态的 context 属性,它是一个对象的链接列表,每个对象都包含一个 tagName 属性和一个链接到其上方上下文的 prev 属性。
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.