1. Audrius Kažukauskas
  2. dotvim

Commits

Audrius Kažukauskas  committed 602caba

Tweak Python folding to work better in a few edge cases.

- Ignore code examples inside docstrings.
- Continue fold even if unindented multiline string (e.g. SQL query) or comment
at the beginning of line is encountered.

  • Participants
  • Parent commits 20080e9
  • Branches default

Comments (0)

Files changed (1)

File ftplugin/python/python_fold.vim

View file
 let w:nestinglevel = 0
 
 function! GetPythonFold(lnum)   " {{{
+  " Skip lines that begin with strings to avoid folding code examples inside
+  " docstrings.
+  if synIDattr(synID(a:lnum, 1, 0), 'name') =~? 'string' &&
+        \ synIDattr(synID(a:lnum + 1, 1, 0), 'name') =~? 'string'
+    return "="
+  endif
+
+  let nnum = nextnonblank(a:lnum + 1)
+  let nind = indent(nnum)
+  let ind = indent(a:lnum)
+
   " Classes and functions get their own folds.
-  if getline(a:lnum) =~ '^\s*\(class\|def\)\s'
+  if getline(a:lnum) =~# '^\s*\(class\|def\)\s'
     " Verify if the next line is a class or function definition as well.
-    let nnum = nextnonblank(a:lnum + 1)
-    let nind = indent(nnum)
-    let ind = indent(a:lnum)
     if ind >= nind
       let nline = getline(nnum)
       let w:nestinglevel = nind
       return "<" . ((w:nestinglevel + &sw) / &sw)
     endif
-    let w:nestinglevel = indent(a:lnum)
+    let w:nestinglevel = ind
     return ">" . ((w:nestinglevel + &sw) / &sw)
   endif
 
-  " If next line has less or equal indentation than the first one, we end a
-  " fold.
-  let nind = indent(nextnonblank(a:lnum + 1))
-  if nind <= w:nestinglevel
-    let w:nestinglevel = nind
-    return "<" . ((w:nestinglevel + &sw) / &sw)
-  else
-    let ind = indent(a:lnum)
-    if ind == w:nestinglevel + &sw && nind < ind
+  " Skip lines that begin with strings (unindented multiline strings, e.g. SQL
+  " queries) or comments (may be used for code markers).
+  if synIDattr(synID(nnum, 1, 0), 'name') !~? 'string\|comment'
+    " If next line has less or equal indentation than the first one, we end a
+    " fold.
+    if nind <= w:nestinglevel || (ind == w:nestinglevel + &sw && nind < ind)
       let w:nestinglevel = nind
       return "<" . ((w:nestinglevel + &sw) / &sw)
     endif