Skip to content

Commit 421ed14

Browse files
saccarosiumchrisbra
authored andcommitted
runtime(typst): add folding to typst ftplugin
closes: #15897 Signed-off-by: Gregory Anders <greg@gpanders.com> Signed-off-by: Luca Saccarola <github.e41mv@aleeas.com> Signed-off-by: Christian Brabandt <cb@256bit.org>
1 parent a7d4561 commit 421ed14

4 files changed

Lines changed: 49 additions & 3 deletions

File tree

runtime/autoload/typst.vim

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
" Language: Typst
22
" Maintainer: Gregory Anders
3-
" Last Change: 2024-07-14
3+
" Last Change: 2024 Oct 21
44
" Based on: https://github.com/kaarmu/typst.vim
55

66
function! typst#indentexpr() abort
@@ -31,6 +31,31 @@ function! typst#indentexpr() abort
3131
return l:ind
3232
endfunction
3333

34+
function typst#foldexpr()
35+
let line = getline(v:lnum)
36+
37+
" Whenever the user wants to fold nested headers under the parent
38+
let nested = get(g:, "typst_foldnested", 1)
39+
40+
" Regular headers
41+
let depth = match(line, '\(^=\+\)\@<=\( .*$\)\@=')
42+
43+
" Do not fold nested regular headers
44+
if depth > 1 && !nested
45+
let depth = 1
46+
endif
47+
48+
if depth > 0
49+
" check syntax, it should be typstMarkupHeading
50+
let syncode = synstack(v:lnum, 1)
51+
if len(syncode) > 0 && synIDattr(syncode[0], 'name') ==# 'typstMarkupHeading'
52+
return ">" . depth
53+
endif
54+
endif
55+
56+
return "="
57+
endfunction
58+
3459
" Gets the previous non-blank line that is not a comment.
3560
function! s:get_prev_nonblank(lnum) abort
3661
let l:lnum = prevnonblank(a:lnum)

runtime/doc/filetype.txt

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
*filetype.txt* For Vim version 9.1. Last change: 2024 Oct 05
1+
*filetype.txt* For Vim version 9.1. Last change: 2024 Oct 21
22

33

44
VIM REFERENCE MANUAL by Bram Moolenaar
@@ -939,6 +939,19 @@ TYPST *ft-typst-plugin*
939939
*g:typst_conceal*
940940
When |TRUE| the Typst filetype plugin will set the 'conceallevel' option to 2.
941941

942+
*g:typst_folding*
943+
When |TRUE| the Typst filetype plugin will fold headings. (default: |FALSE|)
944+
945+
To enable: >
946+
let g:typst_folding = 1
947+
<
948+
*g:typst_foldnested*
949+
When |TRUE| the Typst filetype plugin will fold nested heading under their parents
950+
(default: |TRUE|)
951+
952+
To disable: >
953+
let g:typst_foldnested = 0
954+
<
942955
VIM *ft-vim-plugin*
943956

944957
The Vim filetype plugin defines mappings to move to the start and end of

runtime/doc/tags

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7747,6 +7747,8 @@ g:typescript_host_keyword syntax.txt /*g:typescript_host_keyword*
77477747
g:typst_cmd quickfix.txt /*g:typst_cmd*
77487748
g:typst_conceal filetype.txt /*g:typst_conceal*
77497749
g:typst_embedded_languages syntax.txt /*g:typst_embedded_languages*
7750+
g:typst_folding filetype.txt /*g:typst_folding*
7751+
g:typst_foldnested filetype.txt /*g:typst_foldnested*
77507752
g:var eval.txt /*g:var*
77517753
g:vim_indent indent.txt /*g:vim_indent*
77527754
g:vim_indent_cont indent.txt /*g:vim_indent_cont*

runtime/ftplugin/typst.vim

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
" Vim filetype plugin file
22
" Language: Typst
33
" Maintainer: Gregory Anders
4-
" Last Change: 2024 Oct 04
4+
" Last Change: 2024 Oct 21
55
" Based on: https://github.com/kaarmu/typst.vim
66

77
if exists('b:did_ftplugin')
@@ -21,6 +21,12 @@ if get(g:, 'typst_conceal', 0)
2121
let b:undo_ftplugin .= ' cole<'
2222
endif
2323

24+
if has("folding") && get(g:, 'typst_folding', 0)
25+
setlocal foldexpr=typst#foldexpr()
26+
setlocal foldmethod=expr
27+
let b:undo_ftplugin .= "|setl foldexpr< foldmethod<"
28+
endif
29+
2430
if !exists('current_compiler')
2531
compiler typst
2632
let b:undo_ftplugin ..= "| compiler make"

0 commit comments

Comments
 (0)