Sublime Forum

Code folding (yet again...)

#1

Hi there.

I have used Sublime Text in different times, but I always find myself going back to another editor because of the (mainly lack of) code folding system. I really want to give it another opportunity, for in may other regards it is great.

So, do you know any good alternative (package) for fixing Sublime´s native behaviour? Specifically, how to:

  1. Completely deactivate the default code folding based on indentation, so inconvenient.

  2. Set a specific syntax code for folding/unfolding. This is, establish personalized start and end markers for the folding. In my case, I use the editor for Python, and I would like to use “#+” and “#-

  3. Include the last line (the one with the “#-” marker) in the folding, as is the case in any editor that gives you a “+” when collapsed and a “-” when uncollapsed. I mean, that the endmark line should also disappear when code is folded.

  4. On that topic, have those symbols ("+" and “-”) on the left, much more intuitive than the triangles now used by Sublime.

With the package “SyntaxFold” I have managed to do number 2, but not the rest.
Maybe it is possible tweaking some parameters of its code somewhere?

In short, I am really surprised about the (lack of) proper code folding options in Sublime.
I really think it is kind of a basic feature in a professional text editor…

Many thanks for your help and all the best.

0 Likes

#2

Completely deactivate the default code folding based on indentation, so inconvenient.

See the "fold_style" setting.

Set a specific syntax code for folding/unfolding. This is, establish personalized start and end markers for the folding. In my case, I use the editor for Python, and I would like to use “#+” and “#-

Code folding is syntax-specific. You can make folding use markers like that, but it requires altering the syntax definition.

0 Likes

#3

FWIW, I don’t think you’ll get happy with disabling indentation based folding in python as syntax can’t provide required tokens to use syntax based folding under all circumstances.

0 Likes

#4

Many thanks for your answer.

The "fold-style" setting says:

// The style of code folding to use
// - "auto": Use whatever the default is for the syntax in use
// - "force_indentation": Always use indentation-based code folding
// - "scope_only": Only allow syntax-based code folding

So, it seems that I need to use "scope_only".
My doubt is, how can I set the syntax definition?
Couldn´t find anything in the settings file.

0 Likes

#5

I also find it very unintuitive to have “Code folding” under the “Edit” menu.
I always go instinctively to “View”. I think makes much more sense…
I mean, code folding affects how you view the context, it doesn´t alter (edit) anything…

1 Like

#6

Syntax definitions assign scopes to tokens, which then can be used to specify fold regions.

Fold regions are defined in tmPreference files like https://github.com/sublimehq/Packages/blob/master/JavaScript/Fold.tmPreferences.

0 Likes

#7

I’m looking for a way to completely deactivate that and instead use custom markers for folding, specifically “#+” for the start and “#-” for the end in my Python code. Ideally, I’d like the last line with the “#-” marker to also disappear when folded, similar to other editors that show a “+” when collapsed and a “-” when expanded. I tried using the SyntaxFold package, which helped with the custom markers, but I still can’t seem to achieve the other desired functionalities.

0 Likes

#8

To achieve that,

  1. create a Packages/User/Python (with Fold Markers).sublime-syntax extending default Python.
    %YAML 1.2
    ---
    name: Python (Fold Markers)
    scope: source.python.with-fold-markers
    version: 2
    
    extends: Packages/Python/Python.sublime-syntax
    
    file_extensions:
      - py
      - py3
      - pyw
      - pyi
      - pyx
      - pyx.in
      - pxd
      - pxd.in
      - pxi
      - pxi.in
      - rpy
      - cpy
      - gyp
      - gypi
      - vpy
      - smk
      - wscript
      - bazel
      - bzl
    
    hidden_file_extensions:
      - SConstruct
      - SConscript
      - Snakefile
    
    first_line_match: |-
      (?xi:
        ^ \#! .* \bpython(?:\d(?:\.\d+)?)?\b                        # shebang
      | ^ \s* \# .*? -\*- .*? \bpython(?:\d(?:\.\d+)?)?\b .*? -\*-  # editorconfig
      )
    
    contexts:
      comments:
        - meta_prepend: true
        - match: (#\+)[^\n]*(\n)
          scope: comment.line.python
          captures:
            1: punctuation.definition.comment.python
            2: fold.begin.python
        - match: (#\-)[^\n]*(\n)
          scope: comment.line.python
          captures:
            1: punctuation.definition.comment.python
            2: fold.end.python
    
  2. create a corresponding Packages/User/Fold Python.tmPreferences to disable indentation based folding and add declare fold tokens of fold.[begin|end] scope.
    <?xml version="1.0" encoding="UTF-8"?>
    <plist version="1.0">
    <dict>
        <key>scope</key>
        <string>source.python.with-fold-markers</string>
        <key>settings</key>
        <dict>
            <key>indentationFoldingEnabled</key>
            <false/>
            <key>foldScopes</key>
            <array>
                <dict>
                    <key>begin</key>
                    <string>fold.begin</string>
                    <key>end</key>
                    <string>fold.end</string>
                    <key>excludeTrailingNewlines</key>
                    <false/>
                </dict>
            </array>
        </dict>
    </dict>
    </plist>
    
1 Like