From a5086f56622ad3c8c9b54e756d0db6f6ad813118 Mon Sep 17 00:00:00 2001 From: Alessandro Martini <18130319+martini97@users.noreply.github.com> Date: Thu, 29 Jun 2023 15:34:38 -0300 Subject: [PATCH] refactor(sexp): use loop instead of try catch We can know how deep we are in a list with the syntax-ppss function, with this we can use a simple loop instead of going up until something explodes --- testrun-sexp.el | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/testrun-sexp.el b/testrun-sexp.el index f9399da..7662aa8 100644 --- a/testrun-sexp.el +++ b/testrun-sexp.el @@ -31,6 +31,7 @@ ;;; Code: (require 'seq) +(require 'syntax) (require 'thingatpt) (defun testrun-sexp--beginning-of-thing-at-point-p (thing) @@ -49,6 +50,10 @@ (thing-at-point 'list)) (thing-at-point 'list)))) +(defun testrun-sexp--current-depth () + "Get current point depth in parentheses, counting from 0." + (car (syntax-ppss))) + (defun testrun-sexp--parents () "Return a list with all the parent sexps of the current point. @@ -57,11 +62,9 @@ If point is at the beginning of a list then it will also be included." (list (testrun-sexp--read-list-at-point)) '()))) (save-excursion - (condition-case _err - (while t - (backward-up-list) - (push (testrun-sexp--read-list-at-point) parents)) - (scan-error nil))) + (cl-loop for i from 0 below (testrun-sexp--current-depth) do + (backward-up-list) + (push (testrun-sexp--read-list-at-point) parents))) parents)) (defun testrun-sexp--filter-car-memq (lists wanted)