Skip to content

Commit

Permalink
Merge pull request #465 from brandon1024/renderer-support-textprops
Browse files Browse the repository at this point in the history
feat: add support for rendering text with props
  • Loading branch information
lambdalisue authored Mar 10, 2023
2 parents 1856f03 + 21c0472 commit dae5eb2
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 4 deletions.
1 change: 1 addition & 0 deletions autoload/fern.vim
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ call s:Config.config(expand('<sfile>:p'), {
\ 'default_exclude': '',
\ 'renderer': 'default',
\ 'renderers': {},
\ 'enable_textprop_support': 0,
\ 'comparator': 'default',
\ 'comparators': {},
\ 'drawer_width': 30,
Expand Down
30 changes: 28 additions & 2 deletions autoload/fern/internal/buffer.vim
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,40 @@ function! fern#internal#buffer#replace(bufnr, content) abort
let modifiable_saved = getbufvar(a:bufnr, '&modifiable')
try
call setbufvar(a:bufnr, '&modifiable', 1)
call setbufline(a:bufnr, 1, a:content)
call deletebufline(a:bufnr, len(a:content) + 1, '$')

if g:fern#enable_textprop_support
call s:replace_buffer_content(a:bufnr, a:content)
else
call setbufline(a:bufnr, 1, a:content)
call deletebufline(a:bufnr, len(a:content) + 1, '$')
endif
finally
call setbufvar(a:bufnr, '&modifiable', modifiable_saved)
call setbufvar(a:bufnr, '&modified', modified_saved)
endtry
endfunction

" Replace buffer content with lines of text with (optional) text properties.
function! s:replace_buffer_content(bufnr, content) abort
for lnum in range(len(a:content))
let line = a:content[lnum]
let [text, props] = type(line) is# v:t_dict
\ ? [line.text, get(line, 'props', [])]
\ : [line, []]

call setbufline(a:bufnr, lnum + 1, text)

if exists('*prop_add')
for prop in props
let prop.bufnr = a:bufnr
call prop_add(lnum + 1, prop.col, prop)
endfor
endif
endfor

call deletebufline(a:bufnr, len(a:content) + 1, '$')
endfunction

function! fern#internal#buffer#open(bufname, ...) abort
let options = extend({
\ 'opener': 'edit',
Expand Down
2 changes: 1 addition & 1 deletion autoload/fern/internal/drawer/hover_popup.vim
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ function! s:show() abort
call setbufline('%', 1, line)
call helper.fern.renderer.syntax()
call helper.fern.renderer.highlight()
syntax clear FernRoot
syntax clear FernRootSymbol
syntax clear FernRootText

setlocal nowrap cursorline noswapfile nobuflisted buftype=nofile bufhidden=hide
Expand Down
10 changes: 9 additions & 1 deletion doc/fern-develop.txt
Original file line number Diff line number Diff line change
Expand Up @@ -558,7 +558,15 @@ nodes as a tree.

*fern-develop-renderer.render()*
.render({nodes})
Return a promise which is resolved to a list of |String|.
Return a promise which is resolved to:

a list of |String|, or
if |g:fern#enable_textprop_support| is 1, a list of |Dictionary|
with the following entries:
text |String| with the text to display.
props A list of text properties (|Dictionary|). Optional. Not supported
for Neovim. Each entry is a dictionary, like the third argument
of |prop_add()|, but specifying a column with a "col" entry.

Change (v1.6.0):~
Second argument ({marks}) has removed.
Expand Down
5 changes: 5 additions & 0 deletions doc/fern.txt
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,11 @@ VARIABLE *fern-variable*
is a key of |g:fern#renderers|.
Default: "default"

*g:fern#enable_textprop_support*
If set to 1, renderers may return lines of text with text properties.
May incur slight performance penalty. See |fern-develop-renderer|.
Default: 0

*g:fern#comparator*
A |String| name of comparator used to sort tree items. Allowed value
is a key of |g:fern#comparators|.
Expand Down
49 changes: 49 additions & 0 deletions test/fern/internal/buffer.vimspec
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,16 @@ Describe fern#internal#buffer

Before
%bwipeout!
if !has('nvim')
call prop_type_add('test_prop', { 'highlight': 'Question' })
endif
End

After
let g:fern#enable_textprop_support = 0
if !has('nvim')
call prop_type_delete('test_prop')
endif
End

Describe #replace()
Expand Down Expand Up @@ -115,6 +125,45 @@ Describe fern#internal#buffer
\])
Assert Equals(getbufvar(bufnr, '&modified'), 1)
End

It should replace buffer content with text and properties
if has('nvim')
Assert Skip('text properties are only supported in vim')
endif

let g:fern#enable_textprop_support = 1

let bufnr = bufnr('%')
call setline(1, [
\ "Hello",
\ "Darkness",
\ "My",
\ "Old",
\ "Friend",
\])
new
call fern#internal#buffer#replace(bufnr, [
\ { 'text': 'Empty Props', 'props': [] },
\ { 'text': 'Undefined Props' },
\ { 'text': 'With Properties!', 'props':
\ [{ 'col': 1, 'length': 4, 'type': 'test_prop' }]
\ },
\])
Assert Equals(getbufline(bufnr, 1, '$'), [
\ "Empty Props",
\ "Undefined Props",
\ "With Properties!",
\])

Assert True(empty(prop_list(1, { 'bufnr': bufnr })))
Assert True(empty(prop_list(2, { 'bufnr': bufnr })))

let result_props = prop_list(3, { 'bufnr': bufnr })
Assert Equal(len(result_props), 1)
Assert Equal(result_props[0].col, 1)
Assert Equal(result_props[0].length, 4)
Assert Equal(result_props[0].type, 'test_prop')
End
End

Describe #open()
Expand Down

0 comments on commit dae5eb2

Please sign in to comment.