用Vim爲C#代碼生成 #region

將光標移動到Func上, 執行 MakeRegionPair() 腳本便可生成. shell

#region 生成在當前行上面的第一個空行處.code

#endregion 生成在與開始的 '{' 匹配的 '}' 處, 注意 '{' 必須是單獨一行.orm

 

 

生成結果:get

代碼:io

function! GetHeadSpace()
    normal ^
    let colPos = col(".")
    if ( colPos > 1 )
        normal hv0
        normal "ay
        return @a
    endif

    return ""
endfunction


function! MakeRegionPair()
    let empty_pattern = "^\\s*$"
    let brace_pattern = "^\\s*{\\s*$"

    normal viwy
    let text = getreg("@*")
    let headSpace = GetHeadSpace()

    let regiontext = "\n" . headSpace . "#region " . text
    let endregiontext = headSpace . "#endregion //" . text . "\n"
    normal mm

    let curr_line = line(".")
    let line_b = FindLine( curr_line, 0, empty_pattern )
    execute "normal " . line_b . "G"
    call setreg('c', regiontext)
    normal "cP

    normal 'm
    
    let line_e = FindLine(curr_line, 1, brace_pattern )
    
    execute "normal " . line_e . "G"

    normal %
    call setreg('c', endregiontext)
    normal "cp

endfunction


" 
" dir 0 - up, 1 - down 
"
function! FindLine( baseLine, dir, pattern )
    let maxLine = line("$")

    if (a:dir == 0 )
        let step = -1
        let result = 1
    else
        let step = 1
        let result = maxLine
    endif


    let i = a:baseLine 
    let i = i + step
    while 1
        let theLine = getline(i)
        let mr = match( theLine, a:pattern)
        
        if ( mr != -1 )
            let result = i
            break
        else
            let i = i + step
        endif

        if i<= 0 || i > maxLine
            break
        endif
    endwhile
    return result
endfunction
相關文章
相關標籤/搜索