The regular expression matching __.net of vim custom functions

Source: Internet
Author: User
Tags add days


The original intention is to implement a Vim plug-in, be able to automate the implementation of the method declared in H-header files in C + + into the CPP implementation file (the C.vim I know is not implemented as I meant, implemented well in the source menu in Eclipse), using the line that needs to parse the current cursor, such as:



Need to be



void SayHello (int msg,string buffer);






Converted to



void Hello::sayhello (int msg,string msg2) {






}






Must parse out the return value function name, the parameter and so on, uses the VIM built-in function to be possible to solve, because the regular expression and the general wording is slightly different, has made the memo, for instance






() need to add days add \ \ (\)



[] No



{} needs to be written as \{\}



? need to write \?






Matchlist (' Hello World (int msg,string msg2) ', ' (. *\) \s\+\ (. *\) \s* (\ (. *\))






This function returns a list






[' Hello World (int msg,string msg2); ', ' Hello ', ' world ', ' int msg,string msg2 ', ', ', ', ', ', ', ', ', ', ', ', '






You can resolve the return list to continue.






The complete plugin is as follows (need to install switch header file and implement file plugin A.vim)








if exists("loaded_generate_impl_corey")
        finish  
endif
let loaded_generate_impl_corey=1
 
function <SID>GetClassName(lineNum)
        let tmplinenum=a:lineNum
        while tmplinenum>0
                let tmplinenum-=1
                let tmpline=getline(tmplinenum)
                let clslist=matchlist(tmpline,'\s*class\s*\(\S*\)\s*.*')
                if stridx(tmpline,'class ')>=0
                        return get(clslist,1)
                endif   
        endwhile
        return ""
endfunction


function <SID>ToImpl()
        let s:curline=getline(line("."))
        let s:flist= matchlist(s:curline, '\s*\(.*\)\s\+\(.*\)\s*(\(.*\));')
        if strlen(get(s:flist,0))== 0 || strlen(get(s:flist,2))== 0
                echo "this is not a function"
                return  
        endif   
        let s:rtn=get(s:flist,1)
        if s:rtn=='virtual'
                let s:rtn=""
        endif   
        let s:method=get(s:flist,2)
        let s:args=get(s:flist,3)
        let s:classname=<SID>GetClassName(line("."))
        let s:implclause="\n\t".s:rtn . (strlen(s:rtn)==0?"":" ") .s:classname."::". s:method . "(". s:args . ")" . "{" . "\n" ."\n"."\t"."}"
        call setreg('',s:implclause)
        execute ":A" 
        execute "normal G"
        execute "normal p"
        call cursor(line(".")+2,0)
endfunction     

nnoremap zz :call <SID>ToImpl()<CR>





Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.