Can someone recommend a plugin to fold python long dict spanning over many lines?

86 views
Skip to first unread message

Bao Niu

unread,
Feb 9, 2015, 11:37:33 PM2/9/15
to vim...@googlegroups.com
I'm looking for a plugin in that can fold something like this:

config = {
'/': {
'tools.db.on': True
},
"/UI_JS": {
"tools.staticdir.on": True,
"tools.staticdir.root": os.path.abspath(os.getcwd()),
"tools.staticdir.dir": "./UI_JS"
},
"/UI_CSS": {
"tools.staticdir.on": True,
"tools.staticdir.root": os.path.abspath(os.getcwd()),
"tools.staticdir.dir": "./UI_CSS"
},
"/UI_images": {
"tools.staticdir.on": True,
"tools.staticdir.root": os.path.abspath(os.getcwd()),
"tools.staticdir.dir": "./UI_images"
},
"/outsource_plugins": {
"tools.staticdir.on": True,
"tools.staticdir.root": os.path.abspath(os.getcwd()),
"tools.staticdir.dir": "./outsource_plugins"
},
"/favicon.ico": {
"tools.staticfile.on": True,
"tools.staticfile.filename": os.path.join(os.getcwd(), "UI_images/favicon.ico"),
"tools.staticfile.content_types": {"ico": "image/x-icon"}
}
}




I am currently using SimpylFold, which is fatanstic except doesn't fold long dict like this. And google it doesn't seem to yield anything helpful.
Thanks.

Ben Fritz

unread,
Feb 10, 2015, 12:57:23 PM2/10/15
to vim...@googlegroups.com
Many languages have built-in syntax-based folding if you set the foldmethod to "syntax". What language is this?

There is also marker-based folding for this sort of thing if the syntax doesn't support it.

Tim Chase

unread,
Feb 10, 2015, 1:15:03 PM2/10/15
to vim...@googlegroups.com
On 2015-02-10 09:57, Ben Fritz wrote:
> On Monday, February 9, 2015 at 10:37:33 PM UTC-6, Bao Niu wrote:
> > I'm looking for a plugin in that can fold something like this:
> >
> > config = {
> > '/': {
> > 'tools.db.on': True
> > },
> > "/UI_JS": {
> > "tools.staticdir.on": True,
> > "tools.staticdir.root": os.path.abspath(os.getcwd()),
> > "tools.staticdir.dir": "./UI_JS"
> > },
[snip]
> > }
> >
>
> Many languages have built-in syntax-based folding if you set the
> foldmethod to "syntax". What language is this?

Based on the subject line, I'd say it's Python ;-)

However, they're literals (dict-literals in this case as the
curly-brackets indicate, but list-literals use square brackets, and
tuple-literals use parens).

So it sounds like the OP wants a blend of Python's indentation-based
folding as well as matched-bracket/brace/paren folding.

That said, given the above file, indentation-based folding doesn't do
a bad job of this.

-tim



Bao Niu

unread,
Feb 11, 2015, 1:52:12 AM2/11/15
to vim...@googlegroups.com

No, sorry for this abrupt assertion but indentation is no good in this case. That's why there are many plugins based on expr. Folding is simply too rigid for this job.

I just want to write a wrapper function to fold those long ducts. Any thoughts?

--
--
You received this message from the "vim_use" 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 a topic in the Google Groups "vim_use" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/vim_use/2OdPTKIAOrQ/unsubscribe.
To unsubscribe from this group and all its topics, send an email to vim_use+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Ben Fritz

unread,
Feb 11, 2015, 11:02:10 AM2/11/15
to vim...@googlegroups.com
On Wednesday, February 11, 2015 at 12:52:12 AM UTC-6, Bao Niu wrote:
> No, sorry for this abrupt assertion but indentation is no good in this case. That's why there are many plugins based on expr. Folding is simply too rigid for this job.
>
> I just want to write a wrapper function to fold those long ducts. Any thoughts?
>

Sorry for my blunder earlier, I'll try to make it up.

It looks like Python does not provide syntax folding. As Tim suggests, "indent" folding is normally recommended since Python indentation is semantic.

But others have tried making plugins for better folding, I don't know how good they are (and there may be more, these are from http://vim.wikia.com/wiki/Syntax_folding_of_Python_files#Plugins):

http://www.vim.org/scripts/script.php?script_id=2527
http://www.vim.org/scripts/script.php?script_id=515

If you write your own, you probably can use the indent() function to use the indentation of a line, except for in certain circumstances which you define. Take a look at :help fold-expr for how to write a function that defines the folding, then do a :setl foldmethod=expr foldexpr=MyPythonFoldFunction() to use it.

Nikolay Pavlov

unread,
Feb 11, 2015, 3:39:57 PM2/11/15
to vim...@googlegroups.com
2015-02-11 19:02 GMT+03:00 Ben Fritz <fritzo...@gmail.com>:
On Wednesday, February 11, 2015 at 12:52:12 AM UTC-6, Bao Niu wrote:
> No, sorry for this abrupt assertion but indentation is no good in this case. That's why there are many plugins based on expr. Folding is simply too rigid for this job.
>
> I just want to write a wrapper function to fold those long ducts. Any thoughts?
>

Sorry for my blunder earlier, I'll try to make it up.

It looks like Python does not provide syntax folding. As Tim suggests, "indent" folding is normally recommended since Python indentation is semantic.

I have no idea who may ever want indentation folding. Usually in any case indentation matters indented code looks like

    start of block
        indented block
    [end of block]

and you want `start of block` be folded with indented block and displayed in a fold text because it is usually a block header. *Not* just fold out the indented block and display rather meaningless first line of indented block. And also include end of block as well if it is there.

Things become more complicated if `start of block` spans on multiple lines, but simple indentation folding is not convenient here as well because it can partly fold `start of block`.
 

But others have tried making plugins for better folding, I don't know how good they are (and there may be more, these are from http://vim.wikia.com/wiki/Syntax_folding_of_Python_files#Plugins):

http://www.vim.org/scripts/script.php?script_id=2527
http://www.vim.org/scripts/script.php?script_id=515

If you write your own, you probably can use the indent() function to use the indentation of a line, except for in certain circumstances which you define. Take a look at :help fold-expr for how to write a function that defines the folding, then do a :setl foldmethod=expr foldexpr=MyPythonFoldFunction() to use it.
--
--
You received this message from the "vim_use" 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_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+u...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages