Vim:获取光标下语法元素的内容

发布于 2024-11-03 14:13:50 字数 349 浏览 0 评论 0原文

我正在处理突出显示的复杂语法元素,并希望获取其内容。你能想出什么方法来做到这一点吗?

也许有某种方法可以搜索正则表达式以使其包含光标

编辑

好的,举个例子。光标在一个字符串内,我想获取文本,即该语法元素的内容。考虑以下行:

String myString = "Foobar, [CURSOR]cool \"string\""; // And some other "rubbish"

我想编写一个返回的函数

"Foobar, cool \"string\""

I'm on a highlighted complex syntax element and would like to get it's content. Can you think of any way to do this?

Maybe there's some way to search for a regular expression so that it contains the cursor?

EDIT

Okay, example. The cursor is inside a string, and I want to get the text, the content of this syntactic element. Consider the following line:

String myString = "Foobar, [CURSOR]cool \"string\""; // And some other "rubbish"

I want to write a function that returns

"Foobar, cool \"string\""

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

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

发布评论

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

评论(4

ぽ尐不点ル 2024-11-10 14:13:50

如果我理解这个问题。我不久前发现了这个宝石,但不记得在哪里,但我曾经了解语法高亮在 vim 中的工作原理:

" Show syntax highlighting groups for word under cursor
nmap <leader>z :call <SID>SynStack()<CR>
function! <SID>SynStack()
  if !exists("*synstack")
    return
  endif
  echo map(synstack(line('.'), col('.')), 'synIDattr(v:val, "name")')
endfunc

if I understood the question. I found this gem some time ago and don't remember where but i used to understand how syntax hilighting works in vim:

" Show syntax highlighting groups for word under cursor
nmap <leader>z :call <SID>SynStack()<CR>
function! <SID>SynStack()
  if !exists("*synstack")
    return
  endif
  echo map(synstack(line('.'), col('.')), 'synIDattr(v:val, "name")')
endfunc
新人笑 2024-11-10 14:13:50

textobj-syntax 插件可能会有所帮助。它创建一个自定义文本对象,以便您可以运行 viy 来直观地选择当前语法突出显示的元素。该插件依赖于 textobj-user 插件,这是一个框架创建自定义文本对象。

The textobj-syntax plugin might help. It creates a custom text object, so that you can run viy to visually select the current syntax highlighted element. The plugin depends on the textobj-user plugin, which is a framework for creating custom text objects.

一抹微笑 2024-11-10 14:13:50

这对于文本对象(:help text-objects)来说是一个很好的用途。要获取您要查找的内容(Foobar,cool \"string\"),您只需执行以下操作:

yi"

y = yank
i" = the text object "inner quoted string"

默认情况下,yank 命令使用未命名寄存器("",请参阅 :help registers),因此您可以使用 getreg() 函数或简写 @{register-name} 以编程方式访问提取的内容code>:

:echo 'String last yanked was:' getreg('"')
:echo 'String last yanked was:' @"

或者您可以将内容拉入不同的寄存器:

"qyi"

将内部带引号的字符串拉入 "q 寄存器,因此它不会与标准寄存器用法冲突(并且可以作为 <代码>@q变量)。

This is a good use for text objects (:help text-objects). To get the content you're looking for (Foobar, cool \"string\"), you can just do:

yi"

y = yank
i" = the text object "inner quoted string"

The yank command by default uses the unnamed register ("", see :help registers), so you can access the yanked contents programmatically using the getreg() function or the shorthand @{register-name}:

:echo 'String last yanked was:' getreg('"')
:echo 'String last yanked was:' @"

Or you can yank the contents into a different register:

"qyi"

yanks the inner quoted string into the "q register, so it doesn't conflict with standard register usage (and can be accessed as the @q variable).

病女 2024-11-10 14:13:50

编辑:看到nelstrom提到的插件与我原来的方法类似,我选择了这个稍微更优雅的解决方案:

fu s:OnLink()
    let stack = synstack(line("."), col("."))
    return !empty(stack)
endf

normal mc 

normal $
let lineLength = col(".")
normal `c

while col(".") > 1
    normal h
    if !s:OnLink()
        normal l
        break
    endif
endwhile
normal ma`c

while col(".") < lineLength
    normal l
    if !s:OnLink()
        normal h
        break
    endif
endwhile
normal mb`av`by

EDIT: Seeing that the plugin mentioned by nelstrom works similar to my original approach, I settled on this slightly more elegant solution:

fu s:OnLink()
    let stack = synstack(line("."), col("."))
    return !empty(stack)
endf

normal mc 

normal $
let lineLength = col(".")
normal `c

while col(".") > 1
    normal h
    if !s:OnLink()
        normal l
        break
    endif
endwhile
normal ma`c

while col(".") < lineLength
    normal l
    if !s:OnLink()
        normal h
        break
    endif
endwhile
normal mb`av`by
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文