-
Notifications
You must be signed in to change notification settings - Fork 56
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Porperly indented folds #92
Comments
Not quite sure what you're trying to accomplish. This is how we do it: https://github.com/tmhedberg/SimpylFold/blob/master/autoload/SimpylFold.vim#L364-L389 |
@DerWeh : I used your fold function but I'd have to say that I like SimpyIFold more. Maybe because of colorscheme or something else, have to look on the right to get number of lines cause distracted. |
@tuyenpm9 I agree that it is a matter of preference (and my fold-text surely isn't optimal, neither is my colorscheme). I myself am hardly ever interested in the number of folded lines. Either I am interested in the folded code and look into it, or a fold it away to get a better overview. If I look to the class I think the option to choose a fold text putting the numbers to the right and thus keeping the indent should be given. Python is indent-based and that should be respected. |
There a probably some issues with my function. It was mostly try and error until I found the result to be acceptable. @tuyenpm9 I am not exactly sure what you mean with incorrect, it is what I intended. The number of let l:foldLevelStr = repeat('+--', v:foldlevel) But I realized the code doesn't respect line numbers and the sign column, here issues might arise. Likely there are more readable possibilities to do it, I simply didn't care. The issue with let l:markexpr = escape(substitute(&foldmarker, ',', '|', 'g'),'{')
" TODO: check comment sign for filetype, add first line if comment
let l:whitespace = '(\w\s*)?["|#]\s*|\s*$'
let l:strip_line = substitute(l:line, '\v'.l:markexpr.'|'.l:whitespace, '', 'g')
let l:strip_line = substitute(l:strip_line, '\v'.l:whitespace, '', 'g')
let l:strip_line = substitute(l:strip_line, '\v^(\s*)', '\1<', '').'>' Does the stripping.
|
I'm considering use your fold method but without |
I worked a bit on the fold text and came up with something rather satisfying: scriptencoding utf-8
let s:docstring_re = '^\s*[bBfFrRuU]\{0,2}\\\@<!\(''''''\|"""\|[''"]\)'
let s:docstring_idedentifier_re = '[bBfFrRuU]\{0,2}\\\@<!\(''''''\|"""\|[''"]\)'
function! foldtext#python() abort
let l:docstring = SimpylFold#FoldText()
let l:lnum = v:foldstart
" get first non-blank line
while getline(l:lnum) =~# '^\s*$' | let l:lnum = nextnonblank(l:lnum + 1)
endwhile
if l:lnum > v:foldend
let l:line = getline(v:foldstart)
else
let l:line = substitute(getline(l:lnum), '\t', repeat(' ', &tabstop), 'g')
endif
" if line is docstring than strip, is clear from that folding that it is docstring
let l:is_docstring = !empty(matchlist(l:line, s:docstring_re))
if l:is_docstring
let l:line = substitute(l:line, s:docstring_idedentifier_re, '⟨', '')
" let l:docstring = substitute(l:docstring, '^\s', '', '')
let l:docstring = l:docstring[1:] " split leading whitespace
endif
let l:width = winwidth(0) - &foldcolumn - (&number ? 8 : 0) " available space
let l:foldSize = 1 + v:foldend - v:foldstart
let l:foldSizeStr = ' ' . l:foldSize . ' '
let l:foldLevelStr = repeat('+--', v:foldlevel)
let l:lineCount = line('$')
let l:foldPercentage = printf('[%.1f', (l:foldSize*1.0)/l:lineCount*100) . '%] '
" space remaining space to be filled
let l:space = l:width - strwidth(l:foldSizeStr.l:line.l:foldLevelStr.l:foldPercentage.l:docstring)
if l:space < 0
" trim part of docstring that doesn't fit
return l:line . l:docstring[:l:space-1] . '⟩' . l:foldSizeStr . l:foldPercentage . l:foldLevelStr
else
" fill empty space
let l:expansionString = repeat('•', l:space)
return l:line . l:docstring . l:expansionString . l:foldSizeStr . l:foldPercentage . l:foldLevelStr
endif
endfunction Again you can strip the parts you don't like, especially the Unicode chars. They are purely cosmetic. The indent is preserved, to long doc-strings will be cut to fit, and doc-string only lines start with a |
@DerWeh : Look perfect to me :), will try. EDIT: just tried, thanks a million :), extractly what I need for years. happy new year 2019 early. 💃 |
if we set SimpylFold options ( I'm looking for only fold python docstring by default when open a python file. |
Is there any way to only fold python docstring on python when open? but using this customfold. EDIT: can achieve this by using modeline with or may be a mapping will be better solution. |
I'm also curious about the ability of setting |
I can't find |
@tuyenpm9 The I use I agree, setting the colors would increase readability. However, I think you have to modify how your colorsheme highlights folds to achieve this. I have no idea how to do this, but I am interested if you find a solution. |
Python focuses on indents for readability, thus I think the fold-text should respect the indent for readability. The indent based on the fold-level as we get here is in my opinion intrusive.
I use a custom fold text putting the fold information at the end of the line and indenting the line as it would be without a fold. I do it with the following fold text
Sadly I have no idea how to get the same thing with your awesome docstring preview. (I am still amazed that I somehow manage to get the proper folds, but I have no idea what I have done).
The text was updated successfully, but these errors were encountered: