Vim 中的 CamelCase 扩展类似于 Intellij Idea?

发布于 2024-11-17 20:21:24 字数 189 浏览 4 评论 0原文

在 Intellij Idea 中,有一个功能。假设我在代码中的某处使用了变量 myCamelCase 。然后,如果我输入 mCC 并按 Ctrl-Enter 或某些此类组合键,它会扩展为 myCamelCase。 Vim 里有类似的东西吗?

In Intellij Idea, there's a feature. Let's say I have used a variable myCamelCase somewhere in my code. Then if I type mCC and press Ctrl-Enter or some such key combination, it expands to myCamelCase. Is there something similar in Vim?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

江湖彼岸 2024-11-24 20:21:24

好吧,请原谅我回答两次,但由于我的第一次尝试没有抓住要点,所以我会再试一次。这比我想象的要复杂,但可能不像我做的那么复杂(!)。

现已修改为建议所有匹配的变量名称。

首先,这是一个从“myCamelCase”字符串生成“mCC”缩写的函数:

function! Camel_Initials(camel)
    let first_char = matchstr(a:camel,"^.")
    let other_char = substitute(a:camel,"\\U","","g")
    return first_char . other_char
endfunction

现在,这是一个采用缩写 (' mCC')并扫描当前缓冲区(从当前行向后)以查找具有此缩写的“单词”。 返回所有匹配项的列表:

function! Expand_Camel_Initials(abbrev)
    let winview=winsaveview()
    let candidate=a:abbrev
    let matches=[]
    try
        let resline = line(".")
        while resline >= 1
            let sstr = '\<' . matchstr(a:abbrev,"^.") . '[a-zA-Z]*\>'
            keepjumps let resline=search(sstr,"bW")
            let candidate=expand("<cword>")
            if candidate != a:abbrev && Camel_Initials(candidate) == a:abbrev
                call add( matches, candidate )
            endif
        endwhile
    finally
        call winrestview(winview)
        if len(matches) == 0
            echo "No expansion found"
        endif
        return sort(candidate)
    endtry
endfunction

接下来,这是一个自定义完成函数,它读取光标下的单词并建议上述函数返回的匹配项:

function! Camel_Complete( findstart, base )
    if a:findstart
        let line = getline('.')
        let start = col('.') - 1
        while start > 0 && line[start - 1] =~ '[A-Za-z_]'
            let start -= 1
        endwhile
        return start
    else
        return Expand_Camel_Initials( a:base )
    endif
endfunction

要使用它,您必须定义“completefunc”:

setlocal completefunc=Camel_Complete

要使用插入模式完成,请键入 CTRL-X CTRL-U,但我通常将其映射到CTRL-L

inoremap <c-l> <c-x><c-u>

使用 vimrc 中的此代码,您应该会发现键入 mCC 后跟 CTRL-L 将进行预期的替换。如果没有找到匹配的扩展,则缩写保持不变。

该代码并非无懈可击,但它适用于我测试的所有简单情况。希望有帮助。如果有什么需要澄清的请告诉我。

Okay, forgive me for answering twice, but since my first attempt missed the point, I'll have another go. This is more complicated than I thought, but possibly not as complicated as I have made it (!).

This is now modified to suggest all matching variable names.

First of all, here's a function to generate the 'mCC' abbreviation from the 'myCamelCase' string:

function! Camel_Initials(camel)
    let first_char = matchstr(a:camel,"^.")
    let other_char = substitute(a:camel,"\\U","","g")
    return first_char . other_char
endfunction

Now, here's a function that takes an abbreviation ('mCC') and scans the current buffer (backwards from the current line) for "words" that have this abbreviation. A list of all matches is returned:

function! Expand_Camel_Initials(abbrev)
    let winview=winsaveview()
    let candidate=a:abbrev
    let matches=[]
    try
        let resline = line(".")
        while resline >= 1
            let sstr = '\<' . matchstr(a:abbrev,"^.") . '[a-zA-Z]*\>'
            keepjumps let resline=search(sstr,"bW")
            let candidate=expand("<cword>")
            if candidate != a:abbrev && Camel_Initials(candidate) == a:abbrev
                call add( matches, candidate )
            endif
        endwhile
    finally
        call winrestview(winview)
        if len(matches) == 0
            echo "No expansion found"
        endif
        return sort(candidate)
    endtry
endfunction

Next, here's a custom-completion function that reads the word under the cursor and suggests the matches returned by the above functions:

function! Camel_Complete( findstart, base )
    if a:findstart
        let line = getline('.')
        let start = col('.') - 1
        while start > 0 && line[start - 1] =~ '[A-Za-z_]'
            let start -= 1
        endwhile
        return start
    else
        return Expand_Camel_Initials( a:base )
    endif
endfunction

To make use of this, you must define the "completefunc":

setlocal completefunc=Camel_Complete

To use insert-mode completion, type CTRL-X CTRL-U, but I usually map this to CTRL-L:

inoremap <c-l> <c-x><c-u>

With this code in your vimrc you should find that typing mCC followed by CTRL-L will make the expected replacement. If no matching expansion is found, the abbreviation is unchanged.

The code isn't water-tight, but it works in all the simple cases I tested. Hope it helps. Let me know if anything needs elucidating.

十六岁半 2024-11-24 20:21:24

Vim 中有一个名为 vim-abolish 的插件。使用映射 crc 扩展为驼峰式大小写。

There is a plugin for this in Vim called vim-abolish. Use the map crc to expand to camel case.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文