branch: externals/indent-bars commit ceb2eb00e94d11883c12f5ff596e5d77ad15193e Author: JD Smith <93749+jdtsm...@users.noreply.github.com> Commit: JD Smith <93749+jdtsm...@users.noreply.github.com>
ppss-based no-descend-string/list --- indent-bars-ts.el | 19 +++++-------------- indent-bars.el | 50 +++++++++++++++++++++++++++++++++++++++++++------- 2 files changed, 48 insertions(+), 21 deletions(-) diff --git a/indent-bars-ts.el b/indent-bars-ts.el index 38b170c81e..f2e713bb86 100644 --- a/indent-bars-ts.el +++ b/indent-bars-ts.el @@ -96,16 +96,6 @@ is set." (repeat :tag "Node types" string)) :group 'indent-bars-ts) -(defcustom indent-bars-no-descend-string 'string - "Configure bar behavior inside treesitter-matched strings. -If non-nil, set to a symbol naming a tree-sitter string node type -into which bars will go no deeper than their starting line. If -this node type is invalid, a message is printed and the feature -is disabled." - :local t - :type '(choice (const :tag "Disable" nil) (symbol :tag "Node Type")) - :group 'indent-bars-ts) - (defcustom indent-bars-ts-update-delay 0.125 "Minimum delay time in seconds between treesitter scope updates. Has effect only if `indent-bars-treesit-scope' is non-nil." @@ -187,13 +177,14 @@ arguments)." (if-let (((not (bobp))) (node (treesit-node-on (1- (point)) (point) indent-bars-ts--parser)) (dnew - (if (and indent-bars-no-descend-string + (if (and indent-bars-ts--string-query (indent-bars-ts--node-query node indent-bars-ts--string-query t)) ;; A string: do not descend (1+ (indent-bars--depth (indent-bars--indent-at-node node))) ;; Check wrap context - (when-let ((ctx (indent-bars-ts--node-query + (when-let (( indent-bars-ts--wrap-query) + (ctx (indent-bars-ts--node-query node indent-bars-ts--wrap-query nil t))) (1+ (indent-bars--depth (indent-bars--indent-at-node ctx))))))) @@ -363,8 +354,8 @@ performed." indent-bars--update-depth-function #'indent-bars-ts--update-indentation-depth)) - ;; Strings (avoid descending deeper inside strings) - (when indent-bars-no-descend-string + ;; Strings (avoid descending deeper inside strings using TS) + (when (stringp indent-bars-no-descend-string) (let ((query `([(,indent-bars-no-descend-string)] @s)) (pm (point-min))) (setq indent-bars-ts--string-query (treesit-query-compile lang query)) diff --git a/indent-bars.el b/indent-bars.el index 0201e9b902..7829d30581 100644 --- a/indent-bars.el +++ b/indent-bars.el @@ -365,6 +365,22 @@ non-nil. Set to 0 for instant depth updates." :type 'boolean :group 'indent-bars) +(defcustom indent-bars-no-descend-string t + "Configure bar behavior inside strings. +If non-nil, bars will go no deeper than their starting line of a +string." + :local t + :type 'boolean + :group 'indent-bars) + +(defcustom indent-bars-no-descend-lists t + "Configure bar behavior inside lists. +If non-nil, bars will go no deeper than the starting line of a +list." + :local t + :type 'boolean + :group 'indent-bars) + (defcustom indent-bars-prefer-character nil "Use characters instead of stipple to draw bars. Normally characters are used on terminal only. A non-nil value @@ -815,15 +831,35 @@ Note that the first bar is expected at `indent-bars-starting-column'." 0)) (defvar indent-bars--update-depth-function nil) -(defun indent-bars--current-indentation-depth (&optional on-bar) +(defun indent-bars--current-indentation-depth (&optional on-bar pos) "Calculate current indentation depth. -If ON-BAR is non-nil, report a line with content beginning on a -bar position at that position. If -`indent-bars--update-depth-function' is non-nil, it will be -called with the indentation depth, and can return an updated -depth." +If ON-BAR is non-nil and content begins at a column where a bar +would otherwise have fallen, report the depth of that (undrawn) +bar. Otherwise, the depth of the last possible visible bar is +returned. + +If `indent-bars-no-descend-string' is non-nil and point at line +beginning is inside a string, do not add bars deeper than one +more than the string's start. If `indent-bars-no-descend-lists' +is non-nil, perform the same check for lists. If POS is passed, +it is used as the position to check with `syntax-ppss'. + +If `indent-bars--update-depth-function' is non-nil, it will be +called with the indentation depth (prior to the ON-BAR check), +and can return an updated depth." (let* ((c (current-indentation)) (d (indent-bars--depth c))) + (when (or indent-bars-no-descend-string indent-bars-no-descend-lists) + (let* ((p (point)) + (ppss (syntax-ppss pos)) ; moves point + (ss (and indent-bars-no-descend-string (nth 8 ppss))) + (sl (and indent-bars-no-descend-lists (nth 1 ppss))) + (s (if (and ss sl) (max ss sl) (or ss sl)))) + (when s + (goto-char s) + (setq c (current-indentation) + d (min d (1+ (indent-bars--depth c))))) + (goto-char p))) (if indent-bars--update-depth-function (setq d (funcall indent-bars--update-depth-function d))) (if (and on-bar (= c (+ indent-bars--offset (* d indent-bars-spacing)))) @@ -961,7 +997,7 @@ font-lock properties." BEG and END should be on the same line. STYLE, SWITCH-AFTER and STYLE2 are as in `indent-bars--draw-line'. If STYLE is not passed, uses `indent-bars-style' for drawing." - (let* ((n (indent-bars--current-indentation-depth))) + (let ((n (indent-bars--current-indentation-depth nil beg))) (when (> n 0) (indent-bars--draw-line style n beg end nil switch-after style2))))