Skip to content

Commit

Permalink
[Treelib] Add on-expand and on-collapse arguments.
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander-Miller committed Sep 27, 2023
1 parent 835e761 commit d94bbda
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 11 deletions.
1 change: 1 addition & 0 deletions Changelog.org
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
- Added ~treemacs-copy-absolute-path-at-point~
- Made it possible to disbale workspace with a ~COMMENT~ directive
- Added option to sort alphabetic-numerically (as with ~string-version-lessp~)
- Added ~:on-expand~ and ~:on-collapse~ options to treelib nodes
** v3.1
- Added ~treemacs-create-workspace-from-project~ command
- Added ~treemacs-project-follow-into-home~ option
Expand Down
7 changes: 6 additions & 1 deletion Extensions.org
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@ Finally ~:children~ is special in that it has access to 2 parameters:
- The ~btn~ for the node at point, as returned by ~treemacs-current-button~
(the value is a text-properties button as it would be created by the builtin button.el library, hence the name)

~:on-expand~ and ~on-collapse~ are optional callbacks that are called at the end of the expand/collapse cycle. They too are
called with ~btn~ as their parameter.

#+BEGIN_SRC emacs-lisp
(treemacs-define-expandable-node-type showcase-buffer-group
:closed-icon "+ "
Expand All @@ -120,7 +123,9 @@ Finally ~:children~ is special in that it has access to 2 parameters:
:key item
:children (treemacs-showcase--buffers-by-mode (treemacs-button-get btn :major-mode))
:child-type 'showcase-buffer-leaf
:more-properties `(:major-mode ,item))
:more-properties `(:major-mode ,item)
:on-expand (message "Expanding node with key %s" (treemacs-button-get btn :key))
:on-collapse (message "Collapsing node with key %s" (treemacs-button-get btn :key)))
#+END_SRC

Finally all that's left is to define the leaves of our tree - the nodes for the individual buffers.
Expand Down
44 changes: 34 additions & 10 deletions src/elisp/treemacs-treelib.el
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,13 @@ Set by `treemacs--async-update-part-complete'.")
async?

;; used as a check when the extension is enabled
entry-point?)
entry-point?

;; callback to run when a node is expanded
on-expand

;; callback to run when a node is collapsed
on-collapse)

(define-inline treemacs--ext-symbol-to-instance (symbol)
"Derive an extension instance from the given SYMBOL."
Expand Down Expand Up @@ -251,7 +257,9 @@ Also pass additional DATA to predicate function.")
no-tab?
variadic?
async?
entry-point?)
entry-point?
on-expand
on-collapse)

"Base building block for extension node setup.
Not meant for direct use. Instead one of the following macros should be
Expand All @@ -270,7 +278,7 @@ CHILDREN is a form to query a list of items to be rendered as children when a
node is expanded. The node being expanded is available as a variable under the
name `btn'. It is a `button' in the sense of the built-in button.el library
\(really just a marker to a buffer position), so its text-properties can be
extracted via `(treemacs-button-get node :property)' (see also MORE-PROPERTIES).
extracted via `(treemacs-button-get btn :property)' (see also MORE-PROPERTIES).
In addition the item (as produced by the form passed here) that was used to
create the node will also be available under the name `item'.
Expand Down Expand Up @@ -321,7 +329,11 @@ cleanup and logging the error.
ENTRY-POINT indicates that the node type defined here is an entry-point for an
extension, it will be used as a type-check when enabling an extension with e.g.
`treemacs-enable-top-level-extension'."
`treemacs-enable-top-level-extension'.
ON-EXPAND and ON-COLLAPSE are forms to be invoked at the very end of the
expand/collapse process. They are invoked with the current `btn' as their sole
argument."

(declare (indent 1))

Expand All @@ -348,7 +360,9 @@ extension, it will be used as a type-check when enabling an extension with e.g.
:more-properties (lambda (&optional btn item) "" (ignore item) (ignore btn) ,more-properties)
:child-type (lambda () "" (symbol-value ',child-name))
:open-state (lambda () "" ',open-state)
:closed-state (lambda () "" ',closed-state)))
:closed-state (lambda () "" ',closed-state)
:on-expand (lambda (&optional btn ) "" (ignore btn) ,on-expand)
:on-collapse (lambda (&optional btn ) "" (ignore btn) ,on-collapse)))

(treemacs-define-TAB-action
',closed-state
Expand Down Expand Up @@ -412,13 +426,15 @@ For a detailed description of all arguments see
child-type
more-properties
ret-action
on-expand
on-collapse
async?)

"Define a general-purpose expandable node-type.
The NAME, CLOSED-ICON, OPEN-ICON LABEL, KEY, CHILDREN and CHILD-TYPE arguments
are mandatory.
MORE-PROPERTIES, RET-ACTION and ASYNC are optional.
MORE-PROPERTIES, RET-ACTION, ON-EXPAND, ON-COLLAPSE and ASYNC are optional.
For a detailed description of all arguments see
`treemacs-do-define-extension-type'."
Expand All @@ -441,7 +457,9 @@ For a detailed description of all arguments see
:child-type ,child-type
:more-properties ,more-properties
:ret-action ,ret-action
:async? ,async?))
:async? ,async?
:on-expand ,on-expand
:on-collapse ,on-collapse))

(cl-defmacro treemacs-define-entry-node-type
(name &key
Expand All @@ -453,14 +471,16 @@ For a detailed description of all arguments see
child-type
more-properties
ret-action
on-expand
on-collapse
async?)

"Define a node type with NAME that serves as an entry-point for an extension.
The KEY, LABEL, OPEN-ICON CLOSED-ICON, CHILDREN and CHILD-TYPE arguments are
mandatory.
MORE-PROPERTIES, RET-ACTION and ASYNC are optional.
MORE-PROPERTIES, RET-ACTION, ON-EXPAND, ON-COLLAPSE and ASYNC are optional.
For a detailed description of all arguments see
`treemacs-do-define-extension-type'."
Expand All @@ -484,6 +504,8 @@ For a detailed description of all arguments see
:more-properties ,more-properties
:async? ,async?
:ret-action ,ret-action
:on-expand ,on-expand
:on-collapse ,on-collapse
:entry-point? t))

(cl-defmacro treemacs-define-variadic-entry-node-type
Expand Down Expand Up @@ -746,7 +768,8 @@ If a prefix ARG is provided expand recursively."
((treemacs-extension->async? ext)
(treemacs--do-expand-async-extension-node btn ext arg))
(t
(treemacs--do-expand-extension-node btn ext nil arg))))))))
(treemacs--do-expand-extension-node btn ext nil arg)))
(treemacs-extension->get ext :on-expand btn))))))

(defun treemacs-collapse-extension-node (&optional arg)
"Collapse a node created with the extension api.
Expand All @@ -757,7 +780,8 @@ If a prefix ARG is provided expand recursively."
(ext (alist-get state treemacs--extension-registry)))
(when (null ext)
(error "No extension is registered for state '%s'" state))
(treemacs--do-collapse-extension-node btn ext arg)))
(treemacs--do-collapse-extension-node btn ext arg)
(treemacs-extension->get ext :on-collapse btn)))

(defun treemacs--do-expand-async-extension-node (btn ext &optional arg)
"Expand an async extension node BTN for the given extension EXT.
Expand Down

0 comments on commit d94bbda

Please sign in to comment.