Do Any Editors Support Customisable Code collapsing?

172 views
Skip to first unread message

kez.o...@gmail.com

unread,
Jul 17, 2019, 8:37:48 PM7/17/19
to golang-nuts
Context:
1. Golang can be very verbose, for example checking if err != nil after a function call takes 3 lines:
if err != nil {
   
return nil, fmt.Errorf("some context: %v", err)
}


2. Editors like VSCode can already collapse blocks of statements, where the above would be displayed as:
if err != nil {...


My question:
Are there any editors that support some kind of customisable collapsing behaviour? Where the above code could be collapsed to something like:
...check...fmt.Errorf("some context: %v", err)...

Has anyone tried to implement this kind of behaviour? Is it difficult to achieve?



Michael Jones

unread,
Jul 17, 2019, 11:37:53 PM7/17/19
to kez.o...@gmail.com, golang-nuts
There is a special “collapse if err !=  nil blocks plugin for VS code. 

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/e15556dd-b957-4d79-9ca4-8b6ed8eb6e6c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
Michael T. Jones
michae...@gmail.com

Jim Robinson

unread,
Jul 18, 2019, 1:32:35 PM7/18/19
to golang-nuts
Is it named try? :P


On Wednesday, July 17, 2019 at 8:37:53 PM UTC-7, Michael Jones wrote:
There is a special “collapse if err !=  nil blocks plugin for VS code. 
On Wed, Jul 17, 2019 at 5:37 PM <kez....@gmail.com> wrote:
Context:
1. Golang can be very verbose, for example checking if err != nil after a function call takes 3 lines:
if err != nil {
   
return nil, fmt.Errorf("some context: %v", err)
}


2. Editors like VSCode can already collapse blocks of statements, where the above would be displayed as:
if err != nil {...


My question:
Are there any editors that support some kind of customisable collapsing behaviour? Where the above code could be collapsed to something like:
...check...fmt.Errorf("some context: %v", err)...

Has anyone tried to implement this kind of behaviour? Is it difficult to achieve?



--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golan...@googlegroups.com.

Michael Jones

unread,
Jul 18, 2019, 3:45:00 PM7/18/19
to Jim Robinson, golang-nuts
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/920a48e6-431d-40b2-8081-d0b207cb1840%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

kez.o...@gmail.com

unread,
Jul 18, 2019, 7:32:16 PM7/18/19
to golang-nuts
That's cool, having quick actions to do the folding is part way there.

Hehe, I am genuinely looking for a 'try' alternative! I think that an editor-supported solution would keep everybody happy.
It would let the code be read as if it were written with some syntactic sugar macro, but the code underneath is just plain golang code.
The Golang language/standard library doesn't have to support any macros, which keeps the language team happy,
and since the code is unchanged, it lets each person decide which collapsings they want to use (if any at all).

It looks like VSCode has a feature suggestion ticket in their backlog here: https://github.com/microsoft/vscode/issues/70794
Which would at least make it possible for an extension to implement the code folding I'm after.
I wonder if we can encourage VSCode to start working on it, or whether we can get a group of Golang devs to work on it for them (it
is open source after all)... 


On Friday, 19 July 2019 05:45:00 UTC+10, Michael Jones wrote:

Mike Schinkel

unread,
Jul 18, 2019, 8:18:52 PM7/18/19
to golang-nuts
Are there any editors that support some kind of customisable collapsing behaviour? Where the above code could be collapsed to something like:

Might be worth reading, commenting on and/or possibly upvoting this feature proposal for GoLand?

https://youtrack.jetbrains.com/issue/GO-7747

kez.o...@gmail.com

unread,
Jul 18, 2019, 10:58:19 PM7/18/19
to golang-nuts
Thanks! I haven't heard of GoLand before.

Looks like the One Line Returns feature is like an 80% solution, I would toggle that on all the time.

I'm not sold on completely hiding the error line like you want, but I'll definitely start a thread with that GoLand dev about how hard it would be to make the folding slightly more generic.

Florin Pățan

unread,
Jul 19, 2019, 12:48:24 AM7/19/19
to golang-nuts
I don't know how this works for vscode, but for GoLand you can write your own syntax-aware custom folding plugin, see this plugin as an example of how to do it: https://plugins.jetbrains.com/plugin/9320-advanced-java-folding

This doesn't mean we are not open to new ideas on how to improve the error handling display to all users by default and we are happy to listen on your opinions/needs.

kez.o...@gmail.com

unread,
Jul 21, 2019, 7:50:52 PM7/21/19
to golang-nuts
Writing my own syntax-aware custom folding plugin looks like a bit more work than I have time for right now, but I'm glad the feature exists.

I think I'm working out what would need to be supported in Go, and what would need to be supported in editors to do what I'm imagining.

For instance, some kind of 'reverse macro' / 'unmacro' / 'folding macro' definition syntax would need to be a Golang thing:
```
checkFoldUnmacro(rets, wrappedErr)
matches:
if err != nil {
  return rets, wrappedErr
}
foldsTo:
check wrappedErr
```

But then that golang 'folding macro' feature would hook into really simple/generic custom folding APIs for different editors.
Reply all
Reply to author
Forward
0 new messages