Skip to content

Commit 0edbdbe

Browse files
committed
Folding of /**/ displays the content of the first non-empty line
1 parent abf28a0 commit 0edbdbe

File tree

1 file changed

+52
-63
lines changed

1 file changed

+52
-63
lines changed

.vimrc

+52-63
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
22
" Marciovmf (N)VIM config
33
" https://github.com/marciovmf/vimstuff
4-
" v2.0
4+
" @version: 2.01
5+
" @changelog:
6+
" - fold metod for /**/ displays the content of the first non-empty line
57
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
68
" -NEOVIDE---------------------------------------------------------------------
79

@@ -23,8 +25,6 @@
2325
let $SWAPDIR = $VIMHOME."/swap//"
2426
augroup vimrc
2527
autocmd!
26-
autocmd BufEnter */.vimrc set foldmethod=indent
27-
autocmd BufLeave */.vimrc set foldmethod=syntax
2828
autocmd! BufWritePost $MYVIMRC source $MYVIMRC "Automatically source .vimrc when saving it
2929
autocmd! BufWritePost "~/.vimrc" source "~/.vimrc" "Automatically source .vimrc when saving it
3030

@@ -204,49 +204,13 @@
204204
call UpdateTitleBar()
205205
endfunction
206206

207-
" -Folding v1------------------------------------------------------------------
208-
" This function defines a custom display format for folded lines in Vim.
209-
" It enhances readability by providing context about the content of each fold,
210-
" including the starting and ending lines within the fold and the number of lines
211-
" in the folded section.
212-
"
213-
" Function Behavior:
214-
" - If the fold starts with `/*` (typically indicating a comment block),
215-
" the function finds the first non-empty line within the fold and uses it
216-
" as the display text for the fold.
217-
" - Otherwise, it uses the first line of the fold as the starting text.
218-
" - Displays the number of lines folded at the end in parentheses.
219-
" - Handles line numbers, relative numbers, and signs by adjusting padding as needed
220-
" to ensure the fold text aligns well in the editor window.
221-
"
222-
" Process:
223-
" 1. Calculates padding (`lpadding`) to adjust for line numbers, relative numbers,
224-
" and signs (like error markers) in the fold column.
225-
" 2. Retrieves the first line of the fold (`l:start`) and replaces tabs with spaces
226-
" for a cleaner display.
227-
" 3. Checks if the fold starts with `/*`. If so, it searches through the fold for the
228-
" first non-empty line and uses this line as the starting display text.
229-
" 4. Retrieves the last line of the fold (`l:end`) and removes leading whitespace.
230-
" 5. Calculates the available display width (`l:width`) by subtracting padding and
231-
" the length of the fold information (number of lines).
232-
" 6. Truncates `l:start` if necessary to fit within the available width and combines
233-
" it with `l:end` using a separator (`…`) to indicate omitted content.
234-
" 7. Returns the final fold text, including padding and line count, ensuring the
235-
" display fits within the editor's window width.
236-
"
237-
" Example Output:
238-
" - A fold might display as:
239-
" "This is the first line of the fold … last line of the fold (5)"
240-
" This includes the first and last lines within the fold, separated by ellipses,
241-
" with the total number of lines in the fold shown at the end.
242-
"
243-
" Note:
244-
" - Ensure that 'set foldtext=FoldText()' is in your Vim configuration to enable
245-
" this custom fold display.
246-
" - This function is designed for use with syntax-based folding ('set foldmethod=syntax').
247-
function! FoldText()
207+
" Based on https://coderwall.com/p/usd_cw/a-pretty-vim-foldtext-function
208+
set foldtext=FoldText()
248209

210+
function! FoldText()
211+
" Calculate padding for the fold text
249212
let l:lpadding = &fdc
213+
let l:extra_padding = 0
250214
redir => l:signs
251215
execute 'silent sign place buffer='.bufnr('%')
252216
redir End
@@ -264,37 +228,62 @@ function! FoldText()
264228
endif
265229
endif
266230

267-
" Determine fold display text based on whether fold starts with "/*"
231+
" Calculate the info text (fold size) and its length
232+
let l:info = ' (' . (v:foldend - v:foldstart + 1) . ')'
233+
let l:infolen = strlen(l:info)
234+
235+
" Initialize variables for start and end lines of the fold
268236
let l:start = substitute(getline(v:foldstart), '\t', repeat(' ', &tabstop), 'g')
237+
let l:end = substitute(getline(v:foldend), '\t', repeat(' ', &tabstop), 'g')
238+
let l:custom_fold_display = 0
239+
240+
" Check if the fold starts with "/*" to apply specific formatting
269241
if l:start =~ '^\s*/\*'
270-
" Loop to find the first non-empty line within the fold
271-
let l:first_non_empty = ''
272-
for l:i in range(v:foldstart + 1, v:foldend)
273-
let l:line = substitute(getline(l:i), '\t', repeat(' ', &tabstop), 'g')
274-
if l:line !~ '^\s*$' " Check if the line is not empty
275-
let l:first_non_empty = l:line
242+
" Find the first non-empty line within the fold after the start line
243+
let l:found = ''
244+
for l:line in range(v:foldstart + 1, v:foldend)
245+
let l:current_line = substitute(getline(l:line), '\t', repeat(' ', &tabstop), 'g')
246+
if l:current_line !~ '^\s*$' " Check for a non-empty line
247+
let l:found = l:current_line
276248
break
277249
endif
278250
endfor
279-
if l:first_non_empty != ''
280-
let l:start = l:first_non_empty
251+
252+
" If a non-empty line was found, format it for display
253+
if l:found != ''
254+
" Remove leading "*" if present, along with any leading whitespace
255+
let l:found = substitute(l:found, '^\s*\*\s*', '', '')
256+
257+
" Format the start line to show "/* <first non-empty line> */"
258+
let l:start = '/* ' . l:found . ' */'
259+
let l:end = ''
260+
let l:extra_padding = 2
261+
let l:custom_fold_display = 1
281262
endif
282263
endif
283264

284-
" Process the end line and other settings as before
285-
let l:end = substitute(substitute(getline(v:foldend), '\t', repeat(' ', &tabstop), 'g'), '^\s*', '', 'g')
286-
let l:info = ' (' . (v:foldend - v:foldstart) . ')'
287-
let l:infolen = strlen(substitute(l:info, '.', 'x', 'g'))
288-
let l:width = winwidth(0) - l:lpadding - l:infolen
265+
" Calculate the maximum width available for fold text, including padding for `(nn)`
266+
let l:width = winwidth(0) - l:lpadding - l:extra_padding
289267

290-
let l:separator = ''
291-
let l:separatorlen = strlen(substitute(l:separator, '.', 'x', 'g'))
292-
let l:start = strpart(l:start , 0, l:width - strlen(substitute(l:end, '.', 'x', 'g')) - l:separatorlen)
293-
let l:text = l:start . '' . l:end
268+
" Handle fold text formatting separately for custom `/* ... */` folds
269+
if l:custom_fold_display
270+
" Truncate `l:start` to fit within the width, leaving exact space for `(nn)`
271+
let l:width_adjusted = l:width - l:infolen - 1
272+
let l:start = strpart(l:start, 0, l:width_adjusted)
273+
let l:text = l:start
274+
else
275+
" For non-`/* ... */` folds, use the standard "start … end" format
276+
let l:separator = ''
277+
let l:separatorlen = strlen(l:separator)
278+
let l:start = strpart(l:start, 0, l:width - l:infolen - l:separatorlen - strlen(l:end) - 1)
279+
let l:text = l:start . l:separator . l:end
280+
endif
294281

295-
return l:text . repeat(' ', l:width - strlen(substitute(l:text, ".", "x", "g"))) . l:info
282+
" Return the formatted fold text with line count right-aligned
283+
return l:text . repeat(' ', l:width - strlen(l:text) - l:infolen) . l:info
296284
endfunction
297285

286+
298287
" -Building--------------------------------------------------------------------
299288
compiler msvc
300289

0 commit comments

Comments
 (0)