]> git.ipfire.org Git - thirdparty/vim.git/commitdiff
runtime(typst): add folding to typst ftplugin
authorLuca Saccarola <github.e41mv@aleeas.com>
Mon, 21 Oct 2024 20:01:10 +0000 (22:01 +0200)
committerChristian Brabandt <cb@256bit.org>
Mon, 21 Oct 2024 20:01:10 +0000 (22:01 +0200)
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>
runtime/autoload/typst.vim
runtime/doc/filetype.txt
runtime/doc/tags
runtime/ftplugin/typst.vim

index 55edd23928c2646f6de412f5bb3846aceeae94ba..baf765a30bdf44179a6fbcc9cbb5f2e6a1dcaa8f 100644 (file)
@@ -1,6 +1,6 @@
 " Language:    Typst
 " Maintainer:  Gregory Anders
-" Last Change: 2024-07-14
+" Last Change: 2024 Oct 21
 " Based on:    https://github.com/kaarmu/typst.vim
 
 function! typst#indentexpr() abort
@@ -31,6 +31,31 @@ function! typst#indentexpr() abort
     return l:ind
 endfunction
 
+function typst#foldexpr()
+    let line = getline(v:lnum)
+
+    " Whenever the user wants to fold nested headers under the parent
+    let nested = get(g:, "typst_foldnested", 1)
+
+    " Regular headers
+    let depth = match(line, '\(^=\+\)\@<=\( .*$\)\@=')
+
+    " Do not fold nested regular headers
+    if depth > 1 && !nested
+        let depth = 1
+    endif
+
+    if depth > 0
+        " check syntax, it should be typstMarkupHeading
+        let syncode = synstack(v:lnum, 1)
+        if len(syncode) > 0 && synIDattr(syncode[0], 'name') ==# 'typstMarkupHeading'
+            return ">" . depth
+        endif
+    endif
+
+    return "="
+endfunction
+
 " Gets the previous non-blank line that is not a comment.
 function! s:get_prev_nonblank(lnum) abort
     let l:lnum = prevnonblank(a:lnum)
index 252551c35988ee71c8fab74bd2c28e0d50155289..be3ba021d0382964c1d26501310cfdcac35bfb6a 100644 (file)
@@ -1,4 +1,4 @@
-*filetype.txt* For Vim version 9.1.  Last change: 2024 Oct 05
+*filetype.txt* For Vim version 9.1.  Last change: 2024 Oct 21
 
 
                  VIM REFERENCE MANUAL    by Bram Moolenaar
@@ -939,6 +939,19 @@ TYPST                                                      *ft-typst-plugin*
                                                        *g:typst_conceal*
 When |TRUE| the Typst filetype plugin will set the 'conceallevel' option to 2.
 
+                                                       *g:typst_folding*
+When |TRUE| the Typst filetype plugin will fold headings. (default: |FALSE|)
+
+To enable: >
+       let g:typst_folding = 1
+<
+                                                       *g:typst_foldnested*
+When |TRUE| the Typst filetype plugin will fold nested heading under their parents
+(default: |TRUE|)
+
+To disable: >
+       let g:typst_foldnested = 0
+<
 VIM                                                    *ft-vim-plugin*
 
 The Vim filetype plugin defines mappings to move to the start and end of
index 06e15d4b64720c96540c66b2dab003f5bc376080..f8359ac115fb91380149188999278f081f9db9f0 100644 (file)
@@ -7747,6 +7747,8 @@ g:typescript_host_keyword syntax.txt      /*g:typescript_host_keyword*
 g:typst_cmd    quickfix.txt    /*g:typst_cmd*
 g:typst_conceal        filetype.txt    /*g:typst_conceal*
 g:typst_embedded_languages     syntax.txt      /*g:typst_embedded_languages*
+g:typst_folding        filetype.txt    /*g:typst_folding*
+g:typst_foldnested     filetype.txt    /*g:typst_foldnested*
 g:var  eval.txt        /*g:var*
 g:vim_indent   indent.txt      /*g:vim_indent*
 g:vim_indent_cont      indent.txt      /*g:vim_indent_cont*
index 895fc688d9c937f88790792f6543632238df1fa2..3841e427f3039c12a4cbed869f87ab5718e0ed0a 100644 (file)
@@ -1,7 +1,7 @@
 " Vim filetype plugin file
 " Language:    Typst
 " Maintainer:  Gregory Anders
-" Last Change: 2024 Oct 04
+" Last Change: 2024 Oct 21
 " Based on:    https://github.com/kaarmu/typst.vim
 
 if exists('b:did_ftplugin')
@@ -21,6 +21,12 @@ if get(g:, 'typst_conceal', 0)
   let b:undo_ftplugin .= ' cole<'
 endif
 
+if has("folding") && get(g:, 'typst_folding', 0)
+    setlocal foldexpr=typst#foldexpr()
+    setlocal foldmethod=expr
+    let b:undo_ftplugin .= "|setl foldexpr< foldmethod<"
+endif
+
 if !exists('current_compiler')
   compiler typst
   let b:undo_ftplugin ..= "| compiler make"