Function: combobulate-proffer-indentation

combobulate-proffer-indentation is an interactive and natively compiled function defined in combobulate-python.el.

Signature

(combobulate-proffer-indentation NODE)

Documentation

Intelligently indent the region or NODE at point.

Key Bindings

This command is not in any keymaps.

Source Code

;; Defined in /nix/store/b5fvrwzi3zvkabyzx0in6gw1cj963z46-emacs-packages-deps/share/emacs/site-lisp/combobulate-python.el
(defun combobulate-proffer-indentation (node)
  "Intelligently indent the region or NODE at point."
  (interactive)
  (let* ((indentation (save-excursion
                        (combobulate--goto-node node)
                        (python-indent-calculate-levels)))
         (indent-nodes (mapcar
                        (lambda (level)
                          (save-excursion
                            (let ((proxy-node
                                   ;; A region may, potentially, signify a valid
                                   ;; node range, but it is unlikely. When a user
                                   ;; wants to indent a region they want to --
                                   ;; presumably -- indent code that may not be
                                   ;; syntactically sound. For that we'll create a
                                   ;; special proxy node that will be used to
                                   ;; indent the region.
                                   (if (use-region-p)
                                       (combobulate-proxy-node-make-from-range (region-beginning) (region-end))
                                     ;; if we're not dealing with a region, we
                                     ;; make a proxy node for the closest node.
                                     (combobulate-proxy-node-make-from-nodes node))))
                              (setf (combobulate-proxy-node-extra proxy-node)
                                    (- (current-indentation) level))
                              proxy-node)))
                        (python-indent-calculate-levels)))
         (current-position (1+ (or (seq-position indentation (current-indentation)) 0)))
         (number-of-levels (length (python-indent-calculate-levels)))
         (at-last-level (= number-of-levels current-position)))
    (when-let (selected-node (combobulate-proffer-choices
                              (if at-last-level (reverse indent-nodes) indent-nodes)
                              (lambda-slots (refactor-id current-node)
                                (combobulate-refactor (:id refactor-id)
                                  (mark-node-highlighted current-node)
                                  (combobulate-proffer-indentation-1 current-node)))
                              ;; Try to pick a sensible starting index
                              ;; based on whether we're at the end,
                              ;; taking into account of the fact that
                              ;; there might only be one node to
                              ;; choose from.
                              :start-index (if at-last-level
                                               (min (1- (length indent-nodes)) 1)
                                             (mod current-position number-of-levels))
                              :flash-node t
                              ;; do not filter unique nodes. all our nodes are conceptually
                              ;; identical except for the `extra' field.
                              :allow-numeric-selection t
                              :unique-only nil))
      (combobulate-proffer-indentation-1 selected-node))))