I have a proposed patch for the sh.vim syntax file. I emailed it to the
maintainer a little over two weeks ago but have not heard back, so thought I'd
reach out here.
I recently discovered this sort of construct in shell-scripting:
{ # Prevent execution if this script was only partially downloaded
foo() {
...
}
...
}
I've also found that in addition to providing I/O redirection en-masse, an {
expression-list; } can be a convenient way of organizing code.
Given a snippet of code like the example above, and with ":set
foldmethod=syntax", I wanted to be able to fold like so,
{ # <- fold here
foo() { # <- and here
...
}
}
I've attached two patch-files that implement this. The first allows a shExpr to
be folded, and the second allows a function nested within a shExpr to be
recognized as such, by adding the function groups to the groups shExpr can
contain.
I did my best to remain consistent with the framework the maintainer laid in
place. Specifically, the conditional that decides whether to make shExpr
regions foldable relies on the same sort of bitwise AND logic used for
functions, heredocs, and ifdofor.
Please let me know how I should proceed.
--
--
You received this message from the "vim_dev" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php
---
You received this message because you are subscribed to the Google Groups
"vim_dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.
>From fb65475d2449838fc3c84dc7c80512794bc99e71 Mon Sep 17 00:00:00 2001
From: ivanbrennan <[email protected]>
Date: Mon, 3 Jul 2017 13:21:17 -0400
Subject: [PATCH 1/2] runtime sh syntax: { expression-list; } folding
Add support for folding compound expressions, for example:
{ # <- fold here
echo 'Inside a compound group'
echo 'doing more stuff...'
}
---
runtime/doc/syntax.txt | 1 +
runtime/syntax/sh.vim | 11 ++++++++++-
2 files changed, 11 insertions(+), 1 deletion(-)
diff --git a/runtime/doc/syntax.txt b/runtime/doc/syntax.txt
index 6606524ab..022c3d117 100644
--- a/runtime/doc/syntax.txt
+++ b/runtime/doc/syntax.txt
@@ -2908,6 +2908,7 @@ The syntax/sh.vim file provides several levels of syntax-based folding: >
let g:sh_fold_enabled= 1 (enable function folding)
let g:sh_fold_enabled= 2 (enable heredoc folding)
let g:sh_fold_enabled= 4 (enable if/do/for folding)
+ let g:sh_fold_enabled= 8 (enable { expression-list; } folding)
>
then various syntax items (ie. HereDocuments and function bodies) become
syntax-foldable (see |:syn-fold|). You also may add these together
diff --git a/runtime/syntax/sh.vim b/runtime/syntax/sh.vim
index f97299cde..7c6e12f5c 100644
--- a/runtime/syntax/sh.vim
+++ b/runtime/syntax/sh.vim
@@ -81,6 +81,9 @@ endif
if !exists("s:sh_fold_ifdofor")
let s:sh_fold_ifdofor = and(g:sh_fold_enabled,4)
endif
+if !exists("s:sh_fold_expressions")
+ let s:sh_fold_expressions = and(g:sh_fold_enabled,8)
+endif
if g:sh_fold_enabled && &fdm == "manual"
" Given that the user provided g:sh_fold_enabled
" AND g:sh_fold_enabled is manual (usual default)
@@ -114,6 +117,11 @@ if s:sh_fold_ifdofor
else
com! -nargs=* ShFoldIfDoFor <args>
endif
+if s:sh_fold_expressions
+ com! -nargs=* ShFoldExpr <args> fold
+else
+ com! -nargs=* ShFoldExpr <args>
+endif
" sh syntax is case sensitive {{{1
syn case match
@@ -213,7 +221,7 @@ syn match shPattern "\<\S\+\())\)\@=" contained contains=shExSingleQuote,shSin
" Subshells: {{{1
" ==========
-syn region shExpr transparent matchgroup=shExprRegion start="{" end="}" contains=@shExprList2 nextgroup=shSpecialNxt
+ShFoldExpr syn region shExpr transparent matchgroup=shExprRegion start="{" end="}" contains=@shExprList2 nextgroup=shSpecialNxt
syn region shSubSh transparent matchgroup=shSubShRegion start="[^(]\zs(" end=")" contains=@shSubShList nextgroup=shSpecialNxt
" Tests: {{{1
@@ -711,6 +719,7 @@ endif
delc ShFoldFunctions
delc ShFoldHereDoc
delc ShFoldIfDoFor
+delc ShFoldExpr
" Set Current Syntax: {{{1
" ===================
--
2.11.1
>From 1631f02d2dd84c3cf337e4d6f9a743710b315547 Mon Sep 17 00:00:00 2001
From: ivanbrennan <[email protected]>
Date: Mon, 3 Jul 2017 13:30:39 -0400
Subject: [PATCH 2/2] sh.vim syntax: let shExpr contain shFunction
Allow functions nested within a { expression-list; } to be recognized as
such. For example,
{ # Prevent execution if this script was only partially downloaded
foo() {
...
}
bar() {
...
}
}
---
runtime/syntax/sh.vim | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/runtime/syntax/sh.vim b/runtime/syntax/sh.vim
index 7c6e12f5c..ab1dc2df5 100644
--- a/runtime/syntax/sh.vim
+++ b/runtime/syntax/sh.vim
@@ -143,7 +143,7 @@ syn cluster shDerefList contains=shDeref,shDerefSimple,shDerefVar,shDerefSpecial
syn cluster shDerefVarList contains=shDerefOff,shDerefOp,shDerefVarArray,shDerefOpError
syn cluster shEchoList contains=shArithmetic,shCommandSub,shDeref,shDerefSimple,shEscape,shExpr,shExSingleQuote,shExDoubleQuote,shSingleQuote,shDoubleQuote,shCtrlSeq,shEchoQuote
syn cluster shExprList1 contains=shCharClass,shNumber,shOperator,shExSingleQuote,shExDoubleQuote,shSingleQuote,shDoubleQuote,shExpr,shDblBrace,shDeref,shDerefSimple,shCtrlSeq
-syn cluster shExprList2 contains=@shExprList1,@shCaseList,shTest
+syn cluster shExprList2 contains=@shExprList1,@shCaseList,shTest,shFunctionOne,shFunctionTwo,shFunctionThree,shFunctionFour
syn cluster shFunctionList contains=@shCommandSubList,shCaseEsac,shColon,shCommandSub,shComment,shDo,shEcho,shExpr,shFor,shHereDoc,shIf,shOption,shHereString,shRedir,shSetList,shSource,shStatement,shVariable,shOperator,shCtrlSeq
if exists("b:is_kornshell") || exists("b:is_bash")
syn cluster shFunctionList add=shRepeat
--
2.11.1