我为sublme text 3
. 我需要最接近光标字的代码。
例如,如果我在图像上获得光标...
...这个插件应该返回单词run
....或至少与行10
中字符位置相对应的数字:u
4
然后我将能够run
从 line 的数字10
和文本中获取单词4
。
但现在我只能linetext
得到光标所在的位置:
import sublime, sublime_plugin
class line_nearest_to_cursor_wordCommand(sublime_plugin.WindowCommand):
def run(self):
print('line_nearest_to_cursor_word called')
view = self.window.active_view()
region1 = view.sel()[0]
line = view.line(region1)
linetext = view.substr(line)
print(linetext)
当我打印region1
时,它给出了两个数字,但它们很奇怪:
print (region1) # prints (113, 113), but 113 is not position of char "u" in line " def run(self):"
所以问题归结为写作方法getXcoordPositionOnLine
......
xCoord = getXcoordPositionOnLine(region1)
print xCoord
...这将给我光标在线的 x 位置。
或者也许有更好的解决方案。