emacs-elpa-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[nongnu] elpa/lua-mode b170500 416/468: Add backport of string-trim not


From: Philip Kaludercic
Subject: [nongnu] elpa/lua-mode b170500 416/468: Add backport of string-trim not available in Emacs 24
Date: Thu, 5 Aug 2021 04:59:20 -0400 (EDT)

branch: elpa/lua-mode
commit b17050051164f9ff79451f2e744bc7891206e83d
Author: immerrr <immerrr+lua@gmail.com>
Commit: immerrr <immerrr+lua@gmail.com>

    Add backport of string-trim not available in Emacs 24
---
 test/test-indentation.el |  7 ++-----
 test/utils.el            | 31 +++++++++++++++----------------
 2 files changed, 17 insertions(+), 21 deletions(-)

diff --git a/test/test-indentation.el b/test/test-indentation.el
index 8cc4356..7cc6d12 100644
--- a/test/test-indentation.el
+++ b/test/test-indentation.el
@@ -7,9 +7,6 @@
 (require 'buttercup)
 (require 'cl-lib)
 
-(defun lua--string-trim-safe (str)
-  (save-match-data (string-trim str)))
-
 
 (defun lua--get-indentation-test-sections (file-path)
   (with-temp-buffer
@@ -26,10 +23,10 @@
         ;; Scan towards the next comment or end of file, save the comment as
         ;; the name for the section that comes AFTER the current one.
         (setq next-section-name
-              (when (re-search-forward "^--\\(.*\\)" nil 'noerror) 
(lua--string-trim-safe (match-string-no-properties 1))))
+              (when (re-search-forward "^--\\(.*\\)" nil 'noerror) 
(lua--string-trim (match-string-no-properties 1))))
         ;; Record current section bounds and contents
         (setq end (if next-section-name (match-beginning 0) (point-max)))
-        (setq cur-str (lua--string-trim-safe (buffer-substring-no-properties 
begin end)))
+        (setq cur-str (lua--string-trim (buffer-substring-no-properties begin 
end)))
         ;; Save current section to be returned
         (if (> (length cur-str) 0)
             (push (list (or section-name (format "section %d" (1+ (length 
results))))
diff --git a/test/utils.el b/test/utils.el
index 8636108..4192db0 100644
--- a/test/utils.el
+++ b/test/utils.el
@@ -230,20 +230,19 @@ This is a mere typing/reading aid for lua-mode's 
font-lock tests."
   (apply #'with-point-at-matcher `(:lua-code ,(car args) :with-point-at ,@(cdr 
args))))
 
 
-(require 'subr-x)
-
-;; (describe "foo"
-;;   (it "runs hello"
-;;     (expect "function foo()\nreturn 123\nend" 
:to-be-reindented-the-same-way)))
-
-;; (defun lua--explain-indentation-mismatch (strs indented-strs)
-;;   (cl-loop for i in (number-sequence 1 (length strs))
-;;            for s1 in strs
-;;            for s2 in indented-strs
-;;            if (not (string-equal s1 s2))
-;;            collect (format "Mismatch on line %s:\nExpected: %S\nActual  : 
%S" i s1 s2)))
-
-
+(defun lua--string-trim (string &optional trim-left trim-right)
+  ;; Backport of string-trim for Emacs 24 that doesn't have subr-x lib.
+  (let ((sub-start 0) sub-end)
+    (or trim-left (setq trim-left "[ \t\n\r]+"))
+    (or trim-right (setq trim-right "[ \t\n\r]+"))
+    (save-match-data
+      (when (string-match (concat "\\`" trim-left) string)
+        (setq sub-start (match-end 0)))
+      (when (string-match (concat trim-right "\\'") string sub-start)
+        (setq sub-end (match-beginning 0))))
+    (if (or sub-start sub-end)
+        (substring string sub-start sub-end)
+      string)))
 
 
 (buttercup-define-matcher :to-be-reindented-the-same-way (str)
@@ -251,7 +250,7 @@ This is a mere typing/reading aid for lua-mode's font-lock 
tests."
          (indented-lines (lua-get-indented-strs lines)))
     (buttercup--test-expectation (equal lines indented-lines)
       :expect-match-phrase (format "Indentation check 
failed:\n=========\nExpected:\n---------\n%s\n---------\nActual:\n---------\n%s\n========="
-                                   (string-trim (mapconcat 'identity lines 
"\n"))
-                                   (string-trim (mapconcat 'identity 
indented-lines "\n")))
+                                   (lua--string-trim (mapconcat 'identity 
lines "\n"))
+                                   (lua--string-trim (mapconcat 'identity 
indented-lines "\n")))
       :expect-mismatch-phrase (format "Expected `%S' to not be reindented like 
that"
                                       lines))))



reply via email to

[Prev in Thread] Current Thread [Next in Thread]