Sublime Forum

Self defined text fold and unfold

#1

I want to define my text fold and unfold rules in the following figure. However, I can not figure it out. I am not familiar with sublime-syntax, please give me some advices, thanks.

The related link:

Code folding (yet again…)

The text used in figure:

anything
anything

"""
anything
anything
+++ fold.begin
def anything():
    pass
def anything():
    pass
--- fold.end
"""

anything
anything

The sublime-syntax:

  push_pop_triplequote:
    - meta_scope: geryPushPop

    - match: '\"\"\"$'
      scope: geryColorGray
      pop: true

    - match: (\+\+\+)
      captures:
        1: fold.begin

    - match: (---)
      captures:
        1: fold.end

The tmPreferences:

<?xml version="1.0" encoding="UTF-8"?>
<plist version="1.0">
<dict>
    <key>scope</key>
    <string>text.txtgg</string>
    <key>settings</key>
    <dict>
        <key>scopeFoldingEnabled</key>
        <true/>
        <key>foldScopes</key>
        <array>
            <dict>
                <key>begin</key>
                <string>fold.begin</string>
                <key>end</key>
                <string>fold.end</string>
                <key>excludeTrailingNewlines</key>
                <true/>
            </dict>
        </array>
    </dict>
</dict>
</plist>

The pywith example:

#+ fold.begin
def anything():
    pass

#- fold.end

# --------------------------------

#+ fold.begin
def anything():
    pass

#- fold.end

# --------------------------------
0 Likes

#2

Syntax based folding in general requires two things:

  1. a syntax definition scoping tokens (single or small sequence of characters) to be used as fold region begin and end markers
  2. a meta data file with scope selectors to declare those scoped tokens as fold markers.

To enable folding via #+ and #+ comments an extended Python syntax with proper rules to scope those comments is requied.

Syntax Definition: Packages/User/Python (with Custom Folding).sublime-syntax

%YAML 1.2
---
name: Python (with Custom Folding)
scope: source.python.with-custom-folding
version: 2

extends: Packages/Python/Python.sublime-syntax

contexts:
  comments:
    # add folding comments to python line comments
    - meta_prepend: true
    - include: comment-folds

  comment-folds:
    # #+ [fold.begin]
    - match: \#\+
      scope: punctuation.definition.comment.python
      push: comment-fold-begin-body
    # #+ [fold.end]
    - match: \#\-
      scope: punctuation.definition.comment.python
      push: comment-fold-end-body

  comment-fold-begin-body:
    - meta_scope: comment.line.number-sign.python
    - match: \n
      scope: fold.begin.python
      pop: 1

  comment-fold-end-body:
    - meta_scope: comment.line.number-sign.python
    - match: \n
      scope: fold.end.python
      pop: 1

Meta Data: Packages/User/Python Custom Fold.tmPreferences

<?xml version="1.0" encoding="UTF-8"?>
<plist version="1.0">
<dict>
    <key>scope</key>
    <string>source.python.with-custom-folding</string>
    <key>settings</key>
    <dict>
        <key>scopeFoldingEnabled</key>
        <true/>
        <key>foldScopes</key>
        <array>
            <dict>
                <key>begin</key>
                <string>fold.begin</string>
                <key>end</key>
                <string>fold.end</string>
                <key>excludeTrailingNewlines</key>
                <true/>
            </dict>
        </array>
    </dict>
</dict>
</plist>
1 Like

#3

Thank you~

Although I do not understand all the details, but I will try to figure out.

These two examples (the link and this one) you give are really valuable.

0 Likes