为vim插件ultisnips定义上下文

2024-09-27 00:23:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我想使用vim命令定义一个python函数,这样当光标位于两个math字之间时,如下所示:

math A = [cursor]B+C math

python函数给出True,否则为false

此函数允许定义Ultisnips上下文,这是vim中代码片段的插件

我从hisrepo复制了这样一个函数的(著名)示例,但我不太理解它。 如果有帮助,我在这里加上:

  global !p                                                                                                                                                  
  texmathzones = ['texmathzone'+x for x in ['a', 'as', 'b', 'bs', 'c', 'cs', 'd', 'ds', 'e', 'es', 'f', 'fs', 'g', 'gs', 'h', 'hs', 'i', 'is', 'j', 'js',        'k', 'ks', 'l', 'ls', 'ds', 'v', 'w', 'x', 'y', 'z']]                                                                                                      
                                                                                                                                                             
  texignoremathzones = ['texmathtext']                                                                                                                       
                                                                                                                                                             
  texmathzoneids = vim.eval('map('+str(texmathzones)+", 'hlid(v:val)')")                                                                                     
  texignoremathzoneids = vim.eval('map('+str(texignoremathzones)+", 'hlid(v:val)')")                                                                         
                                                                                                                                                             
  ignore = texignoremathzoneids[0]                                                                                                                           
                                                                                                                                                             
  def math():                                                                                                                                                
      synstackids = vim.eval("synstack(line('.'), col('.') - (col('.')>=2 ? 1 : 0))")                                                                        
      try:                                                                                                                                                   
          first = next(i for i in reversed(synstackids) if i in texignoremathzoneids or i in texmathzoneids)                                                 
          return first != ignore                                                                                                                             
      except stopiteration:                                                                                                                                  
          return false                                                                                                                                       
  endglobal                                                                                                                                                  

此函数在与Ultisnips插件关联的.snippets文件中定义,如果我的光标位于$或类似的LaTeX数学结构之间,则为1

我想也许修改这个函数会有帮助?或者可以创建一个自定义文本对象来实现这一点

我知道python,但不知道vim脚本


Tags: 函数in插件falsefor定义evalds

热门问题