Skip to content

Commit

Permalink
Allow custom filter functions for jsonian-find (#56)
Browse files Browse the repository at this point in the history
Allow custom filter functions
  • Loading branch information
iwahbe authored Dec 29, 2023
1 parent 22bd5e2 commit f200035
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
16 changes: 14 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ function `jsonian-enable-flycheck` which adds `jsonian-mode` to all checkers tha

### Functions

#### jsonian-path (&optional POS BUFFER)
#### jsonian-path

Return the JSON path (as a list) of POINT in BUFFER.
It is assumed that BUFFER is entirely JSON and that the json is
Expand Down Expand Up @@ -134,6 +134,18 @@ b. leveraging C code whenever possible.

By default, this command is bound to `C-c C-p`.

##### Customization

`jsonian-find` needs to filter it's results. By default, it filters by prefix. You can
customize the prefix function by setting
`jsonian-find-filter-fn`. [Orderless](https://github.com/oantolin/orderless) provides an
excellent fuzzy search implementation, which you can use via:

``` emacs-lisp
(with-eval-after-load 'orderless
(setq jsonian-find-filter-fn #'orderless-filter))
```

#### jsonian-edit-string

Edit the string at point in another buffer. The string is expanded when being edited and
Expand All @@ -158,7 +170,7 @@ buffer. When the element is selected, jump to that point in the buffer.

By default, this command is bound to `C-c C-f`.

### jsonian-format-region
#### jsonian-format-region

Maximize the JSON contents of the region. This is equivalent to the built-in function
`json-pretty-print`, but much faster (see "\#\# Benchmarks"). For example:
Expand Down
11 changes: 10 additions & 1 deletion jsonian.el
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ nil means that `jsonian-mode' will infer the correct indentation."
:type 'integer
:group 'jsonian)

(defcustom jsonian-find-filter-fn #'jsonian--filter-prefix
"The function used to filter `jsonian-find' results."
:type 'func
:group 'jsonian)

(defgroup jsonian-c nil
"A major mode for editing JSON with comments."
:prefix "jsonian-c-" :group 'jsonian)
Expand Down Expand Up @@ -1372,14 +1377,18 @@ CACHE is the value of `jsonian--cache' for the buffer being completed against."
(or (and node (jsonian--cached-node-preview node)) ""))))
paths)))

(defun jsonian--filter-prefix (prefix paths)
"Filter out entries in PATHS that do not start with PREFIX."
(seq-filter (apply-partially #'string-prefix-p prefix) paths))

(defun jsonian--completing-sort (prefix paths)
"The completing sort function for `jsonian--find-completion'.
PREFIX is the string to compare against.
PATHS is the list of returned paths."
(if-let* ((segment (car-safe (last (jsonian--parse-path prefix))))
(prefix (jsonian--display-segment-end segment)))
(sort
(seq-filter (apply-partially #'string-prefix-p prefix) paths)
(funcall jsonian-find-filter-fn prefix paths)
(if (seq-every-p (apply-partially #'string-match-p "^[0-9]+\]$") paths)
;; We are in an array, and indexes are numbers like "42]". We should sort them low to high.
(lambda (x y) (< (string-to-number x) (string-to-number y)))
Expand Down

0 comments on commit f200035

Please sign in to comment.