yoshiki / yaml-mode

The emacs major mode for editing files in the YAML data serialization format.
GNU General Public License v3.0
481 stars 132 forks source link

Folding #25

Open reactormonk opened 9 years ago

reactormonk commented 9 years ago

It would be nice to have an org-mode style folding for nested keys.

ghost commented 8 years ago

+1

wasamasa commented 8 years ago

It looks as if markdown-mode provides this feature, though it has an easier time with providing it because it just needs to look at headers.

vibrog commented 7 years ago

Something like this in .emacs

(add-hook 'yaml-mode-hook
  (lambda ()
    (outline-minor-mode)
    (define-key yaml-mode-map (kbd "TAB") 'outline-toggle-children)
    (setq outline-regexp "^ *\\([A-Za-z0-9_-]*: *[>|]?$\\|-\\b\\)")))

Or individual YAML files

Local Variables:
mode: outline-minor
outline-regexp: "^ *\\([A-Za-z0-9_-]*: *[>|]?$\\|-\\b\\)"
End:
rschwarz commented 5 years ago

@vibrog Thanks for the solution, it almost works for me, but the regexp you provide does not accept some of the keys that I would like to use as outlines.

I'm editing a YAML file containing an OpenAPI 3 spec, and I have some paths like this:

paths:
    /jobs:
        foo: 'bar'
    '/jobs/{id}':
        foo: 'baz'

I tried editing the regex, so that the first [] group also contains '/{}, but without success.

seblemaguer commented 5 years ago

Hello,

in case someone is interested, on my side I have this for now,:

(use-package yaml-mode
  :ensure t
  :mode (".yaml$")
  :hook
  (yaml-mode . yaml-mode-outline-hook)

  :init
  (defun yaml-outline-level ()
    "Return the outline level based on the indentation, hardcoded at 2 spaces."
    (s-count-matches "[ ]\\{2\\}" (match-string 0)))

  (defun yaml-mode-outline-hook ()
    (outline-minor-mode)
    (setq outline-regexp "^\\([ ]\\{2\\}\\)*\\([-] \\)?\\([\"][^\"]*[\"]\\|[a-zA-Z0-9_-]*\\): *\\([>|]\\|&[a-zA-Z0-9_-]*\\)?$")
    (setq outline-level 'yaml-outline-level))
  )
yurikhan commented 4 years ago

@seblemaguer This is not entirely correct. For a counterexample:

foo:
  bar:
    baz: 42
  quux: 3.14
plugh:
  xyzzy: false

Putting point on bar: and invoking (outline-hide-subtree) hides baz and quux, because neither is considered to be a heading and thus both are subordinate to bar.

I cropped the regexp after : and it seems to work for me.

leoc commented 3 years ago

This is what works pretty well for me:

(setq outline-regexp
      (rx
       (seq
    bol
    (group (zero-or-more "  ")
           (or (group
            (seq (or (seq "\"" (*? (not (in "\"" "\n"))) "\"")
                 (seq "'" (*? (not (in "'" "\n"))) "'")
                 (*? (not (in ":" "\n"))))
             ":"
             (?? (seq
                  (*? " ")
                  (or (seq "&" (one-or-more nonl))
                  (seq ">-")
                  (seq "|"))
                  eol))))
           (group (seq
               "- "
               (+ (not (in ":" "\n")))
               ":"
               (+ nonl)
               eol)))))))

With this YAML file for testing:

# file test
test:
  some: 'key'
  yea: 'hi'
  sub:
    subkey: is nice
    # some abc
    test: >-
      some text and such
      I can put things here, without care <3
    other: |
      asdf
  and: hahaha
other:
  yes:
    - 'string'
    - 'string'
  ups:
  - geht
  - trotzdem
  other_ups:
    - "hey": abc
      sup: false
array_of_hashes: &name
  - hello: my dear
    this: works
    "nice thing": yaya
    subobjects:
      ear: worm
  - other: my friend
seblemaguer commented 3 years ago

@leoc thanks, yours is definitely better than mine :D

phye commented 2 years ago

@leoc Is your regex working properly for the ups in your emacs? When I tried to hide elements beneeth ups, nothing got folded. I suspect it's because that - geht is getting the same indent level with ups.

Everything (except the case mentioned above) works perfectly. Thanks for this helpful regex!

ups:
- geht
- trotzdem
other_ups:
  - "hey": abc
    sup: false
leoc commented 2 years ago

Hi @phye, yes, it works for me. This is my full yaml-mode config:

https://gist.github.com/leoc/f8c0868051003c4ea6eff638bc614575

phye commented 2 years ago

Thansk for your confirmation. I'll check further with your config :)

phye commented 2 years ago

Just checked further, if I changed your test case a little bit(add value to geht and trotzdem. Folding at ups won't work.

ups
- geht: world
- trotzdem: new

Not sure if you can reproduce it :)

jakob1379 commented 9 months ago

It would be amazing if the the spacing conformed to the conventional block/indentation such that hs and other default frameworks could know what to fold.