Skip to content

Commit

Permalink
[Fix #365] Check string fence syntax on backward search
Browse files Browse the repository at this point in the history
  • Loading branch information
Fuco1 committed Jun 14, 2016
1 parent 1321757 commit 59adcfd
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
24 changes: 19 additions & 5 deletions smartparens.el
Original file line number Diff line number Diff line change
Expand Up @@ -3804,6 +3804,16 @@ The expressions considered are those delimited by pairs on
;; in C-like language. In this case, we want to report the
;; context as comment.

;; In some languages, special paren syntax with a prefix
;; serves to mark strings. This means that regular
;; delimiters, like () are used to delimit strings. For
;; example, in ruby the sequence %w(...) signifies a
;; string. If the point is after such a sequence and we
;; are searching back, we must use the string context,
;; because the paren is now a string delimiter. This is
;; usually implemented with "string fence" syntax, so we
;; will simply check for that.

;; Thanks for being consistent at handling syntax bounds Emacs!
(in-string-or-comment (if back
(let ((in-comment (sp-point-in-comment))
Expand All @@ -3812,11 +3822,15 @@ The expressions considered are those delimited by pairs on
(unless (= (point) (point-min))
(backward-char)
(cond
(in-comment (and in-comment (sp-point-in-comment)))
((and (not in-comment) (sp-point-in-comment)) t)
((or in-comment in-string))))))
(sp-point-in-string-or-comment)))
(string-bounds (and in-string-or-comment (sp--get-string-or-comment-bounds)))
((eq (car (syntax-after (point))) 15) (point))
(in-comment (when (sp-point-in-comment) (1+ (point))))
((and (not in-comment) (sp-point-in-comment)) (1+ (point)))
((or in-comment in-string) (1+ (point)))))))
(when (sp-point-in-string-or-comment) (point))))
(string-bounds (and in-string-or-comment
(progn
(goto-char in-string-or-comment)
(sp--get-string-or-comment-bounds))))
(fw-bound (if in-string-or-comment (cdr string-bounds) (point-max)))
(bw-bound (if in-string-or-comment (car string-bounds) (point-min)))
s e active-pair forward mb me ms r done
Expand Down
5 changes: 4 additions & 1 deletion test/smartparens-get-paired-expression-ruby-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
(let ((sp-pairs '((t . ((:open "def" :close "end" :actions (insert wrap autoskip navigate))
(:open "if" :close "end" :actions (insert wrap autoskip navigate))
(:open "do" :close "end" :actions (insert wrap autoskip navigate))
(:open "begin" :close "end" :actions (insert wrap autoskip navigate)))))))
(:open "begin" :close "end" :actions (insert wrap autoskip navigate))
(:open "(" :close ")" :actions (insert wrap autoskip navigate)))))))
(sp-test-with-temp-buffer initial
(ruby-mode)
(should (equal (sp-get-paired-expression back) result)))))
Expand All @@ -19,6 +20,8 @@
(sp-test--paired-expression-parse-in-ruby "begin| end" '(:beg 1 :end 10 :op "begin" :cl "end" :prefix "" :suffix "") t)
(sp-test--paired-expression-parse-in-ruby "def foo bar if blaz end end|" '(:beg 1 :end 28 :op "def" :cl "end" :prefix "" :suffix "") t)
(sp-test--paired-expression-parse-in-ruby "def foo end;|" '(:beg 1 :end 12 :op "def" :cl "end" :prefix "" :suffix "") t)
(sp-test--paired-expression-parse-in-ruby "asd (asd)|#asdas" '(:beg 5 :end 10 :op "(" :cl ")" :prefix "" :suffix "") t)
(sp-test--paired-expression-parse-in-ruby "C = %w(asd)|#asdas" '(:beg 7 :end 12 :op "(" :cl ")" :prefix "" :suffix "") t)
)

(ert-deftest sp-test-get-paired-expression-ruby-fail ()
Expand Down

0 comments on commit 59adcfd

Please sign in to comment.