Skip to content

Commit

Permalink
Merge pull request #539 from Wilfred/ignore_trailing_colons
Browse files Browse the repository at this point in the history
When slurping forward, don't include punctuation at end of line.
  • Loading branch information
Fuco1 committed Nov 27, 2015
2 parents 4725bbb + 960667d commit 8fe439d
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 42 deletions.
93 changes: 51 additions & 42 deletions smartparens.el
Expand Up @@ -5921,49 +5921,58 @@ Because the structure is much looser in these languages, this
command currently does not support all the prefix argument
triggers that `sp-forward-slurp-sexp' does."
(interactive)
(-if-let* ((enc (sp-get-enclosing-sexp))
(bsexp (save-excursion
(sp-get enc (goto-char :end))
(when (sp-compare-sexps (sp-forward-sexp) enc >)
(sp-get-hybrid-sexp)))))
(save-excursion
(sp-get enc
(goto-char :end-suf)
(delete-char (- (+ :cl-l :suffix-l)))
(let ((start-line (line-number-at-pos))
slurped-within-line)
(-if-let* ((enc (sp-get-enclosing-sexp))
(bsexp (save-excursion
(sp-get enc (goto-char :end))
(when (sp-compare-sexps (sp-forward-sexp) enc >)
(sp-get-hybrid-sexp)))))
(save-excursion
(sp-get enc
(goto-char :end-suf)
(delete-char (- (+ :cl-l :suffix-l)))
;; TODO: move to hook
(when (sp-point-in-blank-line)
(delete-region (line-beginning-position) (1+ (line-end-position))))
(sp-forward-sexp)

(when (eq (line-number-at-pos) start-line)
(setq slurped-within-line t))
;; If we're slurping over multiple lines, include the suffix on the next line.
;; I.e. while () {|} -> while () {\n foo(); \n}
(unless slurped-within-line
(sp-get (sp-get-hybrid-sexp) (goto-char :end-suf)))
(insert :cl :suffix))
;; TODO: move to hook
(when (sp-point-in-blank-line)
(delete-region (line-beginning-position) (1+ (line-end-position))))
(sp-forward-sexp)
(sp-get (sp-get-hybrid-sexp) (goto-char :end-suf))
(insert :cl :suffix))
;; TODO: move to hook
(sp-get (sp--next-thing-selection -1)
(save-excursion
(if (save-excursion
(goto-char :beg-in)
(looking-at "[ \t]*$"))
(progn
(goto-char :end-in)
(newline))
;; copy the whitespace after opening delim and put it in
;; front of the closing. This will ensure pretty { foo }
;; or {foo}
(goto-char :end-in)
(insert (buffer-substring-no-properties
:beg-in
(+ :beg-in (save-excursion
(goto-char :beg-in)
(skip-syntax-forward " ")))))))
(unless (or (looking-at "[ \t]*$")
(looking-at (sp--get-stringlike-regexp))
(looking-at (sp--get-closing-regexp)))
(newline)))
(sp-get (sp--next-thing-selection -1) (sp--indent-region :beg :end))
;; we need to call this again to get the new structure after
;; indent.
(sp--next-thing-selection -1))
(sp-message :invalid-structure)
nil))
(sp-get (sp--next-thing-selection -1)
(save-excursion
(if (save-excursion
(goto-char :beg-in)
(looking-at "[ \t]*$"))
(progn
(goto-char :end-in)
(newline))
;; copy the whitespace after opening delim and put it in
;; front of the closing. This will ensure pretty { foo }
;; or {foo}
(goto-char :end-in)
(insert (buffer-substring-no-properties
:beg-in
(+ :beg-in (save-excursion
(goto-char :beg-in)
(skip-syntax-forward " ")))))))
(unless (or (looking-at "[ \t]*$")
(looking-at (sp--get-stringlike-regexp))
(looking-at (sp--get-closing-regexp))
slurped-within-line)
(newline)))
(sp-get (sp--next-thing-selection -1) (sp--indent-region :beg :end))
;; we need to call this again to get the new structure after
;; indent.
(sp--next-thing-selection -1))
(sp-message :invalid-structure)
nil)))


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Expand Down
2 changes: 2 additions & 0 deletions test/smartparens-python-test.el
@@ -1,3 +1,5 @@
(require 'smartparens)

(ert-deftest sp-test-sp-autoescape-string-quote-if-empty ()
(sp-test-with-temp-buffer "def foo():\n |"
(python-mode)
Expand Down
42 changes: 42 additions & 0 deletions test/smartparens-slurp-test.el
@@ -0,0 +1,42 @@
(require 'smartparens)

(ert-deftest sp-test-js-slurp-excludes-semicolon ()
(sp-test-with-temp-buffer "var foo = bar(|)baz;"
(js-mode)
(sp-slurp-hybrid-sexp)
(should (equal (buffer-string) "var foo = bar(baz);"))))

(ert-deftest sp-test-js-slurp-curly-paren ()
(sp-test-with-temp-buffer "while (true) {|}
bar;"
(js-mode)
(sp-slurp-hybrid-sexp)
(should (equal (buffer-string) "while (true) {
bar;
}"))))

(ert-deftest sp-test-python-slurp-exclude-colon ()
(sp-test-with-temp-buffer "if bar(|)foo:"
(python-mode)
(sp-slurp-hybrid-sexp)
(should (equal (buffer-string) "if bar(foo):"))))

(ert-deftest sp-test-python-slurp-include-dot ()
(sp-test-with-temp-buffer "(|foo).bar"
(python-mode)
(sp-slurp-hybrid-sexp)
(should (equal (buffer-string) "(foo.bar)"))))

(ert-deftest sp-test-c++-slurp-include-semicolon ()
(sp-test-with-temp-buffer "class foo {|
public:
int a;
};
int b = 7;"
(c++-mode)
(sp-slurp-hybrid-sexp)
(should (equal (buffer-string) "class foo {
public:
int a;
int b = 7;
};"))))

0 comments on commit 8fe439d

Please sign in to comment.