Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Folding #25

Open
reactormonk opened this issue Dec 12, 2014 · 13 comments
Open

Folding #25

reactormonk opened this issue Dec 12, 2014 · 13 comments

Comments

@reactormonk
Copy link

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

@ghost
Copy link

ghost commented Dec 23, 2015

+1

@wasamasa
Copy link
Collaborator

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
Copy link

vibrog commented Sep 29, 2016

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
Copy link

rschwarz commented Dec 4, 2018

@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.

rschwarz added a commit to rschwarz/etc that referenced this issue Dec 4, 2018
@seblemaguer
Copy link

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
Copy link

@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
Copy link

leoc commented Mar 8, 2021

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
Copy link

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

vedang added a commit to vedang/emacs-up that referenced this issue Mar 23, 2021
- selective-display: for folding everything at a particular level
  + from: https://gist.github.com/antonj/874106 @antonj
- outline-minor-mode: for folding at various levels, when you only
  want to concentrate on specific parts of the file.
  + from: yoshiki/yaml-mode#25 @leoc

Despite this hackery, the folding experience remains sub-par and
broken (but it's better than having nothing)
@phye
Copy link

phye commented Mar 17, 2022

@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
Copy link

leoc commented Mar 17, 2022

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

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

@phye
Copy link

phye commented Mar 18, 2022

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

@phye
Copy link

phye commented Mar 18, 2022

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
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants