<commit_before>" Copyright 2011 The Go Authors. All rights reserved.
" Use of this source code is governed by a BSD-style
" license that can be found in the LICENSE file.
"
" fmt.vim: Vim command to format Go files with gofmt.
"
" This filetype plugin add a new commands for go buffers:
"
"   :Fmt
"
"       Filter the current Go buffer through gofmt.
"       It tries to preserve cursor position and avoids
"       replacing the buffer with stderr output.
"
" Options:
"
"   g:go_fmt_command [default="gofmt"]
"
"       Flag naming the gofmt executable to use.
"
"   g:go_fmt_autosave [default=1]
"
"       Flag to auto call :Fmt when saved file
"

if !exists("g:go_fmt_command")
    let g:go_fmt_command = "gofmt"
endif

if !exists("g:go_goimports_bin")
    let g:go_goimports_bin = "goimports"
endif

if !exists('g:go_fmt_fail_silently')
    let g:go_fmt_fail_silently = 0
endif

if !exists('g:go_fmt_options')
    let g:go_fmt_options = ''
endif

if !exists("g:go_fmt_experimental")
    let g:go_fmt_experimental = 0
endif

"  we have those problems : 
"  http://stackoverflow.com/questions/12741977/prevent-vim-from-updating-its-undo-tree
"  http://stackoverflow.com/questions/18532692/golang-formatter-and-vim-how-to-destroy-history-record?rq=1
"
"  The below function is an improved version that aims to fix all problems.
"  it doesn't undo changes and break undo history.  If you are here reading
"  this and have VimL experience, please look at the function for
"  improvements, patches are welcome :)
function! go#fmt#Format(withGoimport)
    if g:go_fmt_experimental == 1
        " Using winsaveview to save/restore cursor state has the problem of
        " closing folds on save:
        "   https://github.com/fatih/vim-go/issues/502
        " One fix is to use mkview instead. Unfortunately, this sometimes causes
        " other bad side effects:
        "   https://github.com/fatih/vim-go/issues/728
        " and still closes all folds if foldlevel>0:
        "   https://github.com/fatih/vim-go/issues/732
        let l:curw = {}
        try
            mkview!
        catch
            let l:curw = winsaveview()
        endtry
    else
        " Save cursor position and many other things.
        let l:curw = winsaveview()
    endif

    " Write current unsaved buffer to a temp file
    let l:tmpname = tempname()
    call writefile(getline(1, '$'), l:tmpname)

    if g:go_fmt_experimental == 1
        " save our undo file to be restored after we are done. This is needed to
        " prevent an additional undo jump due to BufWritePre auto command and also
        " restore 'redo' history because it's getting being destroyed every
        " BufWritePre
        let tmpundofile = tempname()
        exe 'wundo! ' . tmpundofile
    endif

    " get the command first so we can test it
    let fmt_command = g:go_fmt_command
    if a:withGoimport  == 1
        let fmt_command  = g:go_goimports_bin
    endif

    " check if the user has installed command binary.
    " For example if it's goimports, let us check if it's installed,
    " if not the user get's a warning via go#path#CheckBinPath()
    let bin_path = go#path#CheckBinPath(fmt_command)
    if empty(bin_path)
        return
    endif

    if fmt_command != "gofmt"
        " change GOPATH too, so goimports can pick up the correct library
        let old_gopath = $GOPATH
        let $GOPATH = go#path#Detect()

        let fmt_command = bin_path
    endif

    " populate the final command with user based fmt options
    let command = fmt_command . ' -w '
    if a:withGoimport  != 1
        let command  = command . g:go_fmt_options
    endif

    if fmt_command == "goimports"
        if !exists('b:goimports_vendor_compatible')
            let out = go#util#System(bin_path . " --help")
            if out !~ "-srcdir"
                call go#util#EchoWarning("vim-go: goimports does not support srcdir. update with: :GoUpdateBinaries")
            else
               let b:goimports_vendor_compatible = 1
            endif
        endif

        if exists('b:goimports_vendor_compatible') && b:goimports_vendor_compatible
            let ssl_save = &shellslash
            set noshellslash
            let command  = command . '-srcdir ' . shellescape(expand("%:p:h"))
            let &shellslash = ssl_save
        endif
    endif

    " execute our command...
    if go#util#IsWin()
        let l:tmpname = tr(l:tmpname, '\', '/')
    endif
    let out = go#util#System(command . " " . l:tmpname)

    if fmt_command != "gofmt"
        let $GOPATH = old_gopath
    endif

    let l:listtype = "locationlist"
    "if there is no error on the temp file replace the output with the current
    "file (if this fails, we can always check the outputs first line with:<commit_msg>fmt: fix goimports picking up -srcdir<commit_after>" Copyright 2011 The Go Authors. All rights reserved.
" Use of this source code is governed by a BSD-style
" license that can be found in the LICENSE file.
"
" fmt.vim: Vim command to format Go files with gofmt.
"
" This filetype plugin add a new commands for go buffers:
"
"   :Fmt
"
"       Filter the current Go buffer through gofmt.
"       It tries to preserve cursor position and avoids
"       replacing the buffer with stderr output.
"
" Options:
"
"   g:go_fmt_command [default="gofmt"]
"
"       Flag naming the gofmt executable to use.
"
"   g:go_fmt_autosave [default=1]
"
"       Flag to auto call :Fmt when saved file
"

if !exists("g:go_fmt_command")
    let g:go_fmt_command = "gofmt"
endif

if !exists("g:go_goimports_bin")
    let g:go_goimports_bin = "goimports"
endif

if !exists('g:go_fmt_fail_silently')
    let g:go_fmt_fail_silently = 0
endif

if !exists('g:go_fmt_options')
    let g:go_fmt_options = ''
endif

if !exists("g:go_fmt_experimental")
    let g:go_fmt_experimental = 0
endif

"  we have those problems : 
"  http://stackoverflow.com/questions/12741977/prevent-vim-from-updating-its-undo-tree
"  http://stackoverflow.com/questions/18532692/golang-formatter-and-vim-how-to-destroy-history-record?rq=1
"
"  The below function is an improved version that aims to fix all problems.
"  it doesn't undo changes and break undo history.  If you are here reading
"  this and have VimL experience, please look at the function for
"  improvements, patches are welcome :)
function! go#fmt#Format(withGoimport)
    if g:go_fmt_experimental == 1
        " Using winsaveview to save/restore cursor state has the problem of
        " closing folds on save:
        "   https://github.com/fatih/vim-go/issues/502
        " One fix is to use mkview instead. Unfortunately, this sometimes causes
        " other bad side effects:
        "   https://github.com/fatih/vim-go/issues/728
        " and still closes all folds if foldlevel>0:
        "   https://github.com/fatih/vim-go/issues/732
        let l:curw = {}
        try
            mkview!
        catch
            let l:curw = winsaveview()
        endtry
    else
        " Save cursor position and many other things.
        let l:curw = winsaveview()
    endif

    " Write current unsaved buffer to a temp file
    let l:tmpname = tempname()
    call writefile(getline(1, '$'), l:tmpname)

    if g:go_fmt_experimental == 1
        " save our undo file to be restored after we are done. This is needed to
        " prevent an additional undo jump due to BufWritePre auto command and also
        " restore 'redo' history because it's getting being destroyed every
        " BufWritePre
        let tmpundofile = tempname()
        exe 'wundo! ' . tmpundofile
    endif

    " get the command first so we can test it
    let bin_name = g:go_fmt_command
    if a:withGoimport  == 1
        let bin_name  = g:go_goimports_bin
    endif

    " check if the user has installed command binary.
    " For example if it's goimports, let us check if it's installed,
    " if not the user get's a warning via go#path#CheckBinPath()
    let bin_path = go#path#CheckBinPath(bin_name)
    if empty(bin_path)
        return
    endif

    if bin_name != "gofmt"
        " change GOPATH too, so goimports can pick up the correct library
        let old_gopath = $GOPATH
        let $GOPATH = go#path#Detect()
    endif

    " populate the final command with user based fmt options
    let command = bin_path . ' -w '
    if a:withGoimport  != 1
        let command  = command . g:go_fmt_options
    endif

    if bin_name == "goimports"
        if !exists('b:goimports_vendor_compatible')
            let out = go#util#System(bin_path . " --help")
            if out !~ "-srcdir"
                call go#util#EchoWarning("vim-go: goimports does not support srcdir. update with: :GoUpdateBinaries")
            else
               let b:goimports_vendor_compatible = 1
            endif
        endif

        if exists('b:goimports_vendor_compatible') && b:goimports_vendor_compatible
            let ssl_save = &shellslash
            set noshellslash
            let command  = command . '-srcdir ' . shellescape(expand("%:p:h"))
            let &shellslash = ssl_save
        endif
    endif

    " execute our command...
    if go#util#IsWin()
        let l:tmpname = tr(l:tmpname, '\', '/')
    endif
    let out = go#util#System(command . " " . l:tmpname)

    if bin_name != "gofmt"
        let $GOPATH = old_gopath
    endif

    let l:listtype = "locationlist"
    "if there is no error on the temp file replace the output with the current
    "file (if this fails, we can always check the outputs first line with:
{'fraction_non_alphanumeric': 0.08565423016290068, 'fraction_numerical': 0.008407777193904361, 'mean_word_length': 2.5828313253012047, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 6, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5857784', 'n_tokens_mistral': 3076, 'n_tokens_neox': 2903, 'n_words': 975}