L
L
lgick2014-03-18 09:55:27
vim
lgick, 2014-03-18 09:55:27

vim script. How to write a function that collapses all functions in a document?

Help me write a function for vim that would collapse all the functions in a document.
The functions in the document look like this:

function () {
  // тут тело функции
}

As I thought:
you need to bypass all the lines in a loop, checking them first for the word 'function', and then for '{'
If found, collapse the block with the zf% command . You
can bypass all the lines like this:
function! CloseFunctions()

  let i = 0
  let lenline = line('$')

  while i <= lenline
      let str = getline(i)
      echo str
      let i += 1
  endwhile

endfunction

But how to do a search in the current line?
vim has a search() method that searches the entire document. Is there a method that searches in a string?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
L
lgick, 2014-03-18
@lgick

Wrote:

function! CloseFunctions()

  execute 'normal zE'

  let i = 0
  let lenline = line('$')

  call inputsave()
  let space = input('how many space (dafault: 2)? ')
  call inputrestore()

  if !strlen(space)
    let space = 2
  endif

  while i <= lenline
    let str = getline(i)
    if match(str, '\S') == space
      if match(str, 'function') > 0
        if match(str, '{') > 0
          execute i + 'G'
          execute 'normal $zf%'
        endif
      endif
    endif
    let i += 1
  endwhile

endfunction

You need to bind to a key.
When called, it asks how many indents the function blocks contain (by default, 2 indents).
After that, it searches for all lines with the specified indentation, then searches for the word 'function' and the bracket '{' in the found ones
and closes the function blocks

V
v_prom, 2014-03-18
@v_prom

I understand that you need folding, if so, then read this: proft.me/2008/11/17/svorachivanie-folding-v-vim

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question