Show function name in status line

后端 未结 7 547
感情败类
感情败类 2020-12-15 03:10

I edit a large C, C++, or Java file, say, about 15000 lines, with pretty long function definitions, say, about 400 lines. When the cursor is in middle of a function definiti

相关标签:
7条回答
  • 2020-12-15 03:27

    There are several plugins for status line or on-demand with a mapping, e.g.:

    • http://www.vim.org/scripts/script.php?script_id=1094
    • http://www.vim.org/scripts/script.php?script_id=2805
    • http://www.vim.org/scripts/script.php?script_id=1553
    0 讨论(0)
  • 2020-12-15 03:28

    You can use ctags.vim for this, it will show the current function name in the title or status bar.

    SOURCE: https://superuser.com/questions/279651/how-can-i-make-vim-show-the-current-class-and-method-im-editing

    0 讨论(0)
  • 2020-12-15 03:29

    My solution is as follows:

    set stl=%f%h%m%r\ %{Options()}%=%l,%c-%v\ %{line('$')}
    fu! PlusOpt(opt)
      let option = a:opt
      if option
        return "+"
      else
        return "-"
      endif
    endf
    
    fu! Options()
      let opt="ic".PlusOpt(&ic)
      let opt=opt." ".&ff
      let opt=opt." ".&ft
      if &ft==?"cpp" || &ft==?"perl"
        let text = " {" . FindCurrentFunction() . "}"
        let opt= opt.text
      endif
      return opt
    
    fu! FindCurrentFunction()
      let text =''
    
      let save_cursor = getpos(".")
    
      let opening_brace = searchpair('{','','}','bWr', '', '', 100)
      if opening_brace > 0
        let oldmagic = &magic
        let &magic = 1
    
        let operators='operator\s*\%((\s*)\|\[]\|[+*/%^&|~!=<>-]=\?\|[<>&|+-]\{2}\|>>=\|<<=\|->\*\|,\|->\|(\s*)\)\s*'
        let class_func_string = '\(\([[:alpha:]_]\w*\)\s*::\s*\)*\s*\%(\~\2\|'.operators
        let class_func_string = class_func_string . '\|[[:alpha:]_]\w*\)\ze\s*('
    
        let searchstring = '\_^\S.\{-}\%('.operators
        let searchstring = searchstring.'\|[[:alpha:]_]\w*\)\s*(.*\n\%(\_^\s.*\n\)*\_^{'
    
        let l = search(searchstring, 'bW', line(".")-20 )
    
        if l != 0
          let line_text = getline(l)
          let matched_text = matchstr(line_text, class_func_string)
          let matched_text = substitute(matched_text, '\s', '', 'g')
          let text = matched_text
        endif
    
        call setpos('.', save_cursor)
    
        let &magic = oldmagic
      endif
    
      return text
    endfunction
    

    I'm actually attempting to match the C/C++/Java allowed names for functions. This generally works for me (including for overloaded operators) but assumes that the opening { is at column 0 on a line by itself.

    I just noticed today that it fails if included in a namespace {}, even if otherwise formatted as expected.

    0 讨论(0)
  • 2020-12-15 03:32

    Based on @manav m-n's answer

    The 'n' flag in search() won't move the cursor, so a shorter version of this with the same functionality would be:

    fun! ShowFuncName()
      echohl ModeMsg
      echo getline(search("^[^ \t#/]\\{2}.*[^:]\s*$", 'bWn'))
      echohl None
    endfun
    map f :call ShowFuncName() <CR>
    

    Reference: run :help search()

    0 讨论(0)
  • 2020-12-15 03:36

    I use https://github.com/mgedmin/chelper.vim for this. It doesn't needs a tags file, instead it parses the source code on the fly.

    0 讨论(0)
  • 2020-12-15 03:40

    To show current function name in C programs add following in your vimrc:

    fun! ShowFuncName()
      let lnum = line(".")
      let col = col(".")
      echohl ModeMsg
      echo getline(search("^[^ \t#/]\\{2}.*[^:]\s*$", 'bW'))
      echohl None
      call search("\\%" . lnum . "l" . "\\%" . col . "c")
    endfun
    map f :call ShowFuncName() <CR>
    

    Or if you need the "f" key, just map the function to whatever you like.

    0 讨论(0)
提交回复
热议问题