• Stars
    star
    111
  • Rank 303,971 (Top 7 %)
  • Language
    Emacs Lisp
  • License
    GNU General Publi...
  • Created almost 2 years ago
  • Updated about 2 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Edit YAML in Emacs like a pro

yaml-pro: tools for editing YAML leveraging tree-sitter/parser

MELPA

yaml-pro is a package that provides conveniences for editing yaml.

This package has been written to leverage tree-sitter parsing facilities, allowing all of these actions to be performed fast and accurate, even in the absence of parsing errors. The tree-sitter version is orders of magnitudes faster and I highly recommend its usage if your Emacs version permits.

Tree-sitter support

The latest version of yaml-pro contains tree-sitter support (activated via yaml-pro-ts-mode). In order to use this you will need to have a version of Emacs installed that supports tree-sitter, as well as the yaml tree-sitter parser from here: https://github.com/ikatyang/tree-sitter-yaml . The tree-sitter re-implementation supports everything below, as well as the following:

  • yaml-pro-ts-meta-return (M-return): Add a new list item after the current item, similar to org-meta-return.
  • yaml-pro-ts-convolute-tree (M-?): Swap the key of the current item's parent with that items parent.
  • yaml-pro-ts-mark-subtree (C-c @): Mark the current YAML item. If given a prefix N, mark the Nth parent above. If called repeatedly, mark subsequent items.
  • yaml-pro-ts-paste-subtree (C-c C-x C-y): Paste a YAML subtree, fixing indentation to be correct for different levels. When yaml-pro-ts-yank-subtrees is non-nil, yank will automatically call yaml-pro-ts-paste-subtree for subtrees.
  • imenu: an index of all keys and their path are build for imenu
  • eldoc: current path is shown as eldoc documentation. (enable with eldoc-mode)

Note that folding is not implemented. I assume some tree-sitter folding library will come along which will unify this feature.

Note that all the functions are infixed with "-ts-" to distinguish the tree-sitter and non-tree-sitter variants.

Demo

Formatter

formatter

New in yaml-pro, a pretty formatter in Emacs Lisp powered by treesitter.

Eldoc (tree-sitter)

eldoc

Imenu

imenu

Subtree pasting (tree-sitter)

screenshot

Editing text in detached buffer

screenshot

Never have to consult https://yaml-multiline.info/ again! With yaml-pro-edit-scalar, you can edit a scalar value in a detached buffer and convert between the various styles with ease.

Jump to heading (legacy parser)

jumping feature

You can use imenu as well.

Moving subtrees up and down

moving subtrees feature

Folding subtrees (legacy parser)

folding feature

This is not available for tree-sitter variant. Presumably some tree-sitter folding package will exist in the future.

Killing subtrees

killing feature

Indenting subtrees

indenting feature

Tree-sitter version

To activate the mode, run M-x yaml-pro-ts-mode. You can have this activated automatically by adding this to your configuration: (add-hook 'yaml-mode-hook 'yaml-pro-ts-mode 100) (or 'yaml-ts-mode-hook).

In order to run the tree-sitter version of this program, you should have a version of Emacs that supports tree-sitter, as well as the yaml tree-sitter library installed (https://github.com/ikatyang/tree-sitter-yaml). Ex: having the file libyaml.dylib in /usr/local/lib. With these in place, (treesit-ready-p 'yaml) should return t.

Usage

  • yaml-pro-format
  • yaml-pro-ts-kill-subtree (C-c C-x C-w)
  • yaml-pro-ts-up-level (C-c C-u)
  • yaml-pro-ts-down-level (C-c C-d)
  • yaml-pro-ts-next-subtree (C-c C-n)
  • yaml-pro-ts-prev-subtree (C-c C-p)
  • yaml-pro-ts-move-subtree-up (s-up)
  • yaml-pro-ts-move-subtree-down (s-down)
  • yaml-pro-ts-meta-return (M-)
  • yaml-pro-ts-convolute-tree (M-?)
  • yaml-pro-ts-indent-subtree (C-c >)
  • yaml-pro-ts-unindent-subtree (C-c <)
  • yaml-pro-ts-mark-subtree (C-c @)
  • yaml-pro-ts-paste-subtree (C-c C-x C-y)
  • yaml-pro-edit-ts-scalar (C-c ')
    • (use prefix argument C-u to supply an initialization command to set major mode)

Pretty formatter

yaml-pro now comes with the addition of a new formatting command yaml-pro-format. This requires tree-sitter to work. There are different settings you can toggle on the formatter. For ease of customization I recommend usind M-x customize-variable. The following is the customization options.

  • indent When present, indent each line by `yaml-pro-indent'.

  • reduce-newlines When present, remove adjacent newlines so at most one one remains. New lines in strings won't be removed.

  • oneline-flow When present, reduce flow mappings to one line.

  • block-formatting When present, format the spacing around block components' colons and dashes.

  • reduce-spaces When present, attempt to reduce multiple adjacent space characters down to one.

  • bm-fn-next-line When present, move the value of a key-value pair to the next line, indented, if it's width is longer than yaml-pro-format-print-width.

  • expand-log-flow When present, flatten flow elements that pass column yaml-pro-format-print-width.

  • single-to-double When present, convert single quoted strings to double quoted, when it wouldn't change its meaning.

  • remove-spaces-before-comments When present, remove spaces before a comment until it is one space after the preceeding content (e.g. "a: b # space before here").

  • clean-doc-end When present, remove unnecessary document-end indicators ("...").

  • document-separator-own-line When present, makes sure the document separator "---" isn't on a line with another element.

Easy movement with repeat map

With the following configuration and repeat-mode enabled, you can easily move around the YAML tree after executing one of the movement commands.

;; the original bindings will work as well, these are shorter if you prefer them.
(keymap-set yaml-pro-ts-mode-map "C-M-n" #'yaml-pro-ts-next-subtree)
(keymap-set yaml-pro-ts-mode-map "C-M-p" #'yaml-pro-ts-prev-subtree)
(keymap-set yaml-pro-ts-mode-map "C-M-u" #'yaml-pro-ts-up-level)
(keymap-set yaml-pro-ts-mode-map "C-M-d" #'yaml-pro-ts-down-level)
(keymap-set yaml-pro-ts-mode-map "C-M-k" #'yaml-pro-ts-kill-subtree)
(keymap-set yaml-pro-ts-mode-map "C-M-<backspace>" #'yaml-pro-ts-kill-subtree)
(keymap-set yaml-pro-ts-mode-map "C-M-a" #'yaml-pro-ts-first-sibling)
(keymap-set yaml-pro-ts-mode-map "C-M-e" #'yaml-pro-ts-last-sibling)

(defvar-keymap my/yaml-pro/tree-repeat-map
  :repeat t
  "n" #'yaml-pro-ts-next-subtree
  "p" #'yaml-pro-ts-prev-subtree
  "u" #'yaml-pro-ts-up-level
  "d" #'yaml-pro-ts-down-level
  "m" #'yaml-pro-ts-mark-subtree
  "k" #'yaml-pro-ts-kill-subtree
  "a" #'yaml-pro-ts-first-sibling
  "e" #'yaml-pro-ts-last-sibling
  "SPC" #'my/yaml-pro/set-mark)

(defun my/yaml-pro/set-mark ()
  (interactive)
  (my/region/set-mark 'my/yaml-pro/set-mark))

(defun my/region/set-mark (command-name)
  (if (eq last-command command-name)
      (if (region-active-p)
          (progn
            (deactivate-mark)
            (message "Mark deactivated"))
        (activate-mark)
        (message "Mark activated"))
    (set-mark-command nil)))

Special thanks to @uqix for sharing this configuration.

Legacy Parser Version

The following documentation is not applicable if you are using yaml-pro-ts-mode as your entry point. It's not recommented however due to its slow parsing speed.

Installation

You can install this package with MELPA under the id yaml-pro. IMPORTANT: You have to have the latest version of yaml.el installed or else this package won't work properly. If your noticing any errors try making sure that you have the correct version of yaml.el installed (https://melpa.org/#/yaml). You can see the parser version with the variable yaml-parser-version and the required version with the variable yaml-pro-required-yaml-parser-version.

You can have yaml-pro-mode setup on yaml-mode loading with the configuration: (add-hook 'yaml-mode-hook #'yaml-pro-mode 100)

Usage

Run the command yaml-pro-mode to initialize the mode. From there you have the following commands available (with default keybindings).

  • yaml-pro-kill-subtree (C-c C-x C-w)
  • yaml-pro-up-level (C-c C-u)
  • yaml-pro-next-subtree (C-c C-n)
  • yaml-pro-prev-subtree (C-c C-p)
  • yaml-pro-fold-at-point (C-c C-f)
  • yaml-pro-unfold-at-point (C-c C-o)
  • yaml-pro-indent-subtree (C-c >)
  • yaml-pro-unindent-subtree (C-c <)
  • yaml-pro-move-subtree-up (s-up)
  • yaml-pro-move-subtree-down (s-down)
  • yaml-pro-edit-scalar (C-c ')
    • (use prefix argument C-u to supply an initialization command to set major mode)
  • If your buffer is in yaml-pro-mode, imenu should index the entire buffer's paths.

The default bindings are subject to change as this package is in beta

Configuration

Yaml.el, being an Emacs lisp parser, struggles with very large files. You can configure the parser to parse a smaller section of the buffer via a heuristic (probably error prone). Set the custom variable yaml-pro-max-parse-size to be the size of the buffer after which such a heuristic is used.

Recommendations

  • Yaml-pro's features compliments LSP and will enhance your YAML editing capabilities even further.

Roadmap

  • Edit yaml values in separate buffer (like org-edit-special)
    • block options for how to store the string.
    • save default init command on a path basis
  • Easy navigation (yaml-pro-jump)
  • Partial tree-parsing for large files
  • Implement internally path-at-point.
  • Move functionality to tree-sitter (for better error handling), perhaps when tree-sitter in Emacs reaches some critical mass.
  • Common YAML mistakes linter
  • Tools to work with various template modes. Go-templated YAML is very common but greatly hinders the effectiveness of tools like LSP. Is there something that could be done (even if it's kind of hacky) to alleviate this?

Contributing

Have a suggestion for this package? Feel free to create an issue. I'd love to hear others pain-points when editing YAML.

More Repositories

1

asm-blox

Programming game in Emacs involving blocks of WAT.
Emacs Lisp
52
star
2

golang-tetris

A Tetris game written in Go using the faiface/pixel 2D game engine.
Go
34
star
3

yaml.el

YAML parser in Elisp
Emacs Lisp
32
star
4

tessellation

a library to assist in the design of geometric designs
Racket
25
star
5

awqat

Ψ£ΩˆΩ‚Ψ§Ψͺ Ψ§Ω„Ψ΅Ω„Ψ§Ψ©
Emacs Lisp
13
star
6

go-sed

A sed implementation in Go.
Go
10
star
7

solar-viz.el

Various visualizations of day and night
Emacs Lisp
9
star
8

intentional.el

Org-mode/Emacs powered website blocking for focusing
Emacs Lisp
7
star
9

tiktoken.el

tiktoken.el is an Emacs Lisp port of OpenAI's tiktoken library for BPE tokenization
Emacs Lisp
5
star
10

neogen.el

Annotation generator using tree-sitter
Emacs Lisp
4
star
11

opus-packet-decoder

A simple tool to decode opus packets and get their raw data (pcm)
Go
4
star
12

go-ttest.el

Helpers for editing table tests in Go
Emacs Lisp
3
star
13

paragraph-chain-indent

Format short paragraphs separated by spaces to be indented instead
Emacs Lisp
3
star
14

go-mod-mode

Makes working with go.mod files in Emacs a bit easier.
Emacs Lisp
2
star
15

libra

A utility to analize code content of directory
Go
2
star
16

youtube.el

simple interface for youtube-dl+YouTube Data API so you don't have to use youtube.com (and hopefully avoid a lot of distraction that way)
Emacs Lisp
1
star
17

rabbit-hole.el

micro task-manager for layering contexts
Emacs Lisp
1
star
18

glo-gen

Go code generator using Clojure data literals
Clojure
1
star
19

stalemoves

Train yourself to move around in Vim better by punishing repeated movements
Vim Script
1
star
20

experimentfile

Declarative language for defining test configurations their traffic distributions.
Racket
1
star
21

racket-test-server

Let's test the performance of Racket web-server: Given a list of URLs, perform a GET on all of them, aggregate the results, and return the result... all under 500ms.
Racket
1
star
22

p-search

Probabilistic seach tool composer
Emacs Lisp
1
star
23

.emacs.d-v2

My personal emacs configuration
Emacs Lisp
1
star
24

advent-of-code

solutions to puzzles
Emacs Lisp
1
star
25

go-contribute

A simple webapp that scans GitHub repositories, looking for ones that are looking for contributions.
Go
1
star