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

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

[nongnu] elpa/sass-mode fb636bb 24/31: sass-output-region removing trail


From: ELPA Syncer
Subject: [nongnu] elpa/sass-mode fb636bb 24/31: sass-output-region removing trailing indentation.
Date: Sun, 29 Aug 2021 11:29:41 -0400 (EDT)

branch: elpa/sass-mode
commit fb636bbef97e5194afa6b90d1f21480f7405ca84
Author: Daniel Luna <dancluna@gmail.com>
Commit: Daniel Luna <dancluna@gmail.com>

    sass-output-region removing trailing indentation.
    
    The previous version of this function did not work for this (valid) SASS
    code:
    
    ```sass
    @for $i from 1 through 13
      $pixels_offset: ($i)px
      .element:nth-child(#{$i * 7 + 1})
        margin-left: ($pixels_offset + 3px)
    ```
    
    This commit fixes that by introducing the sass-remove-trailing-indent
    function, that removes trailing indent in a buffer containing the
    region's SASS code.
---
 sass-mode.el | 29 ++++++++++++++++++++++++-----
 1 file changed, 24 insertions(+), 5 deletions(-)

diff --git a/sass-mode.el b/sass-mode.el
index 157d309..d0038ed 100644
--- a/sass-mode.el
+++ b/sass-mode.el
@@ -213,6 +213,20 @@ LIMIT is the limit of the search."
            if (looking-at opener) return nil
            finally return t))
 
+;; Utility
+
+(defun sass-remove-trailing-indent ()
+  "Removes the first n empty characters in every line, with n being the 
trailing whitespace in the first line."
+  (interactive)
+  (let ((min-indent nil))
+    (goto-char (point-min))
+    (back-to-indentation)
+    (setq min-indent (1- (point)))
+    (while (not (equal (line-end-position) (point-max)))
+      (beginning-of-line)
+      (delete-forward-char min-indent)
+      (forward-line))))
+
 ;; Command
 
 (defun sass-output-region (start end)
@@ -220,11 +234,16 @@ LIMIT is the limit of the search."
 Called from a program, START and END specify the region to indent."
   (interactive "r")
   (let ((output-buffer "*sass-output*")
-        (errors-buffer "*sass-errors*"))
-    (shell-command-on-region start end "sass --stdin"
-                             output-buffer
-                             nil
-                             errors-buffer)
+        (errors-buffer "*sass-errors*")
+        (region-contents (buffer-substring start end)))
+    (with-temp-buffer
+      (insert region-contents)
+      (newline-and-indent)
+      (sass-remove-trailing-indent)
+      (shell-command-on-region (point-min) (point-max) "sass --stdin"
+                               output-buffer
+                               nil
+                               errors-buffer))
     (when (fboundp 'css-mode)
       (with-current-buffer output-buffer
         (css-mode)))



reply via email to

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