From 6e0d6c3638ddb7ef620675fa07432f94c96ab676 Mon Sep 17 00:00:00 2001 From: Fatih Arslan Date: Fri, 29 Jul 2016 14:39:09 -0400 Subject: [PATCH] commands: add :GoAddTags This adds a new feature called `:GoAddTags` that add field tags for a given struct. Checkout the doc for more info. --- autoload/go/util.vim | 44 ++++++++++++++++++++++++++++++++++++++++ doc/vim-go.txt | 14 +++++++++++++ ftplugin/go/commands.vim | 2 ++ 3 files changed, 60 insertions(+) diff --git a/autoload/go/util.vim b/autoload/go/util.vim index 8e573f3135..077549cb6a 100644 --- a/autoload/go/util.vim +++ b/autoload/go/util.vim @@ -231,6 +231,50 @@ function! go#util#camelcase(word) endif endfunction +function! go#util#AddTags(line1, line2, ...) + " default is json + let l:keys = ["json"] + if a:0 + let l:keys = a:000 + endif + + let l:line1 = a:line1 + let l:line2 = a:line2 + + " If we're inside a struct and just call this function let us add the tags + " to all fields + " TODO(arslan): I don't like using patterns. Check if we can move it to + " `motion` and do it via AST based position + let ln1 = searchpair('struct {', '', '}', 'bcnW') + if ln1 == 0 + echon "vim-go: " | echohl ErrorMsg | echon "cursor is outside the struct" | echohl None + return + endif + + " searchpair only returns a single position + let ln2 = search('}', "cnW") + + " if no range is given we apply for the whole struct + if l:line1 == l:line2 + let l:line1 = ln1 + 1 + let l:line2 = ln2 - 1 + endif + + for line in range(l:line1, l:line2) + " get the field name (word) that are not part of a commented line + let l:matched = matchstr(getline(line), '\(\/\/.*\)\@ + :GoAddTags xml db +< *:GoAutoTypeInfoToggle* :GoAutoTypeInfoToggle diff --git a/ftplugin/go/commands.vim b/ftplugin/go/commands.vim index 6f972db170..89c1a984de 100644 --- a/ftplugin/go/commands.vim +++ b/ftplugin/go/commands.vim @@ -15,6 +15,8 @@ command! -range=% GoReferrers call go#guru#Referrers() command! -nargs=? GoGuruTags call go#guru#Tags() +command! -nargs=* -range GoAddTags call go#util#AddTags(, , ) + command! -range=% GoSameIds call go#guru#SameIds() command! -range=0 GoSameIdsClear call go#guru#ClearSameIds() command! -range=% GoSameIdsToggle call go#guru#ToggleSameIds()