Show function name in status line

后端 未结 7 596
感情败类
感情败类 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: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.

提交回复
热议问题