第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Golang Formatter 和 Vim - 如何銷毀歷史記錄?

Golang Formatter 和 Vim - 如何銷毀歷史記錄?

Go
白板的微信 2021-06-24 18:03:14
Go(Golang)編程語言附帶了一個名為go fmt. 它是一個代碼格式化程序,可以自動格式化您的代碼(對齊、字母排序、制表符、間距、習(xí)語......)。它真的很棒。所以我發(fā)現(xiàn)了這個在 Vim 中使用它的小自動命令,每次將緩沖區(qū)保存到文件時。 au FileType go au BufWritePre <buffer> Fmt fmt 是 Go vim 插件自帶的功能。這真的很棒,但它有 1 個問題。每次格式化程序?qū)懭刖彌_區(qū)時,它都會在撤消/重做歷史記錄中創(chuàng)建一個跳轉(zhuǎn)。嘗試撤消/重做更改時會變得非常痛苦,因為每第二次更改都是格式化程序(使光標(biāo)跳轉(zhuǎn)到第 1 行)。所以我想知道,有沒有辦法在觸發(fā)后丟棄撤消/重做歷史記錄中的最新更改Fmt?編輯: 好的,到目前為止我有: au FileType go au BufWritePre <buffer> undojoin | Fmt 但它還不是很好。根據(jù):h undojoin,撤消后不允許撤消加入。果然,當(dāng)我嘗試:w撤消后,它會觸發(fā)錯誤。那么我如何實現(xiàn)這樣的偽代碼:if lastAction != undo then    au FileType go au BufWritePre <buffer> undojoin | Fmtend如果我弄明白了最后一點,我想我有一個解決方案。
查看完整描述

3 回答

?
叮當(dāng)貓咪

TA貢獻(xiàn)1776條經(jīng)驗 獲得超12個贊

我認(rèn)為這幾乎就在那里,完成了你的要求,但我看到它正在刪除一個撤消點(我認(rèn)為這是預(yù)期的undojoin):


function! GoFmt()

    try                

        exe "undojoin"

        exe "Fmt"

    catch              

    endtry

endfunction

au FileType go au BufWritePre <buffer> call GoFmt()

編輯

根據(jù) MattyW 的回答,我想起了另一個選擇:


au FileType go au BufWritePre <buffer> %!gofmt

:%!<some command>在緩沖區(qū)上執(zhí)行一個 shell 命令,所以我在將它寫入文件之前執(zhí)行它。而且,它會將光標(biāo)放在文件頂部......


查看完整回答
反對 回復(fù) 2021-06-28
?
藍(lán)山帝景

TA貢獻(xiàn)1843條經(jīng)驗 獲得超7個贊

這是我的做法。它似乎在讀/寫 autocmds 和綁定到一個鍵上都運行良好。它將光標(biāo)放回并且不包括撤消中的文件頂部事件。


function! GoFormatBuffer()

    if &modifiable == 1

        let l:curw=winsaveview()

        let l:tmpname=tempname()

        call writefile(getline(1,'$'), l:tmpname)

        call system("gofmt " . l:tmpname ." > /dev/null 2>&1")

        if v:shell_error == 0

            try | silent undojoin | catch | endtry

            silent %!gofmt -tabwidth=4

        endif

        call delete(l:tmpname)

        call winrestview(l:curw)

    endif

endfunction

我檢查可修改,因為我使用 vim 作為我的尋呼機(jī)。


查看完整回答
反對 回復(fù) 2021-06-28
?
楊__羊羊

TA貢獻(xiàn)1943條經(jīng)驗 獲得超7個贊

我試圖使用@pepper_chino 的答案,但遇到了一些問題,如果 fmt 錯誤,那么 vim 會在運行之前撤消最后一次更改GoFmt。我以一種漫長而稍微復(fù)雜的方式解決了這個問題:


" Fmt calls 'go fmt' to convert the file to go's format standards. This being

" run often makes the undo buffer long and difficult to use. This function

" wraps the Fmt function causing it to join the format with the last action.

" This has to have a try/catch since you can't undojoin if the previous

" command was itself an undo.

function! GoFmt()

  " Save cursor/view info.

  let view = winsaveview()


  " Check if Fmt will succeed or not. If it will fail run again to populate location window. If it succeeds then we call it with an undojoin.


  " Copy the file to a temp file and attempt to run gofmt on it

  let TempFile = tempname()

  let SaveModified = &modified

  exe 'w ' . TempFile

  let &modified = SaveModified

  silent exe '! ' . g:gofmt_command . ' ' . TempFile

  call delete(TempFile)


  if v:shell_error

    " Execute Fmt to populate the location window

    silent Fmt

  else

    " Now that we know Fmt will succeed we can now run Fmt with its undo

    " joined to the previous edit in the current buffer

    try                

      silent undojoin | silent Fmt

    catch              

    endtry

  endif

  " Restore the saved cursor/view info.

  call winrestview(view)

endfunction

command! GoFmt call GoFmt()


查看完整回答
反對 回復(fù) 2021-06-28
  • 3 回答
  • 0 關(guān)注
  • 277 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號