获取 Atom 中的光标位置

Get cursor position in Atom

在查看我正在编写的包的 Atom API 时,它说您使用游标 class 来访问游标信息。我试图获取光标缓冲区位置,所以我尝试使用 getBufferPosition()。整行代码是:

cursorRow = atom.workspace.getActiveTextEditor().Cursor.getBufferPosition()[0]

只获取该行。但是,它不断抛出此错误:

Uncaught TypeError: Cannot read property 'getBufferPosition' of undefined

顺便说一下,这一切都在 CoffeeScript 中。我不知道我做错了什么,因为 API 说 getBufferPosition 是 Cursor class 的一种方法,所以它应该可以工作。我究竟做错了什么?或者有更好的方法来解决这个问题吗?

提前致谢!

您可以使用 getCursorBufferPosition of TextEditor,即

cursorPosition = atom.workspace.getActiveTextEditor().getCursorBufferPosition()

您看到的错误告诉您文本编辑器上没有名为 Cursor 的 属性。 Cursor class 上可能定义了一个 getBufferPosition,但您不是在 Cursor 的实例上调用它,而是在 undefined 上调用它。