【问题标题】:File listing in custom autocomplete for VimVim 的自定义自动完成中的文件列表
【发布时间】:2011-11-05 03:12:29
【问题描述】:

我正在使用 Vim 的 Utl 插件,我正在寻找一种方法来创建自定义自动完成功能以生成指向文件中 id 标记的链接。我要使用的格式是:

:CommandName <file> <id tag in file>

我希望函数的作用类似于第一个参数的标准目录完成。对于第二个参数,我希望它在第一个参数中指定的文件中搜索所有以“id=”开头的字符串并返回值。

我已经从主 Utl 包中复制了一个类似的功能,但我还没有接近让它工作,它目前看起来像这样:

fu! CompleteArgs(dummy_argLead, cmdLine, dummy_cursorPos)

" Split cmdLine to figure out how many arguments have been provided by user
" so far. If Using argument keepempty=1 for split will provide empty last
" arg in case of a new arg is about to begin or an non empty last argument
" in case of an incomplete last argument. So can just remove the last arg.
exe "echo \"cmdline:\" \"".a:cmdLine."\""
let utlArgs=split(a:cmdLine, '\s\+', 1)
execute "echo" string(utlArgs)
echo "echo" "test complete"
"remove the function name
call remove(utlArgs, -1)

" 1st arg to complete
if len(utlArgs)==1
return string(glob("*"))
endi

" 2nd arg to complete
if len(utlArgs)==2
    "insert code here
endif
endfun

有人有什么想法吗?

【问题讨论】:

    标签: function vim autocomplete arguments


    【解决方案1】:

    虽然answering 非常similar question,但我写了一个 确定命令参数数量的完整函数 完全的。以下是该功能的一个版本,适用于您的情况。

    command! -nargs=* -complete=custom,FooComplete Foo echo [<f-args>]
    
    function! FooComplete(arg, line, pos)
        let l = split(a:line[:a:pos-1], '\%(\%(\%(^\|[^\\]\)\\\)\@<!\s\)\+', 1)
        let n = len(l) - index(l, 'Foo') - 1
        if n == 1
            return string(glob('*'))
        endif
        return "1\n2\n3"  " Replace this with appropriate id-completion logic.
    endfunction
    

    该函数正确处理转义的空格(作为 参数,而不是分隔符)和命令名称前的空格。 请注意,建议候选中的空白字符应该是 转义,否则 Vim 会将单个参数视为两个 论据。

    【讨论】:

      【解决方案2】:

      你可以试试frawor。如果你安装它,代码如下:

      execute frawor#Setup('0.0', {'@/fwc': '0.2',
                  \           '@/commands': '0.0',})
      " No need to write bang here: above command will forbid script to be sourced 
      " twice, see :h frawor#Reload for how it can be updated.
      function s:F.cmdfunc(file, tag)
          " It will be called when the command launches. Alternatively you can replace 
          " `s:F.cmdfunc' in the below command.add call with a string you already had 
          " before. Note that you will have to replace s: in function names with <SID> 
          " and s:* variables will be just unaccessible.
      endfunction
      function s:F.gettags(file)
          " This assumes that format is the same as used in ~/.vim/doc/tags. Note that 
          " if there may be any spaces, then you must escape them.
          return map(readfile(a:file), 'matchstr(v:val, "\\v^.{-}\\t")[:-2]')
      endfunction
      " This replaces direct :command call, see :h frawor-f-command.add
      call s:_f.command.add('CommandName', s:F.cmdfunc,
                  \{  'nargs': '+',
                  \'complete': ['path in*F.gettags(@<)']})
      

      【讨论】:

      • 谢谢,这几乎是完美的。
      猜你喜欢
      • 1970-01-01
      • 2013-10-15
      • 1970-01-01
      • 2016-07-24
      • 1970-01-01
      • 2023-03-30
      • 1970-01-01
      • 2011-12-06
      • 1970-01-01
      相关资源
      最近更新 更多