Skip to content

Commit 773f930

Browse files
authored
Merge pull request #1229 from thatismatt/master
fix: ignore prefix syntax not in prefix position
2 parents b0d935c + 4756255 commit 773f930

2 files changed

Lines changed: 61 additions & 15 deletions

File tree

smartparens.el

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1944,29 +1944,33 @@ See `sp-get-buffer-char-syntax'."
19441944
(setq p (or p (point)))
19451945
(sp-get-buffer-char-syntax (1- p)))
19461946

1947-
(defun sp-syntax-after-is-prefix (&optional p)
1947+
(defun sp-syntax-after-is-prefix (check-prefix-flag &optional p)
19481948
"Check that the character after P has prefix syntax.
19491949

19501950
Prefix syntax can either come from the global major-mode syntax
19511951
table, from the text property syntax-table or from the syntax
1952-
flag (20)."
1952+
flag (20). The flag must be ignored once inside a symbol, this
1953+
is done by passing CHECK-PREFIX-FLAG as nil."
19531954
(setq p (or p (point)))
19541955
(let ((parse-sexp-lookup-properties t))
19551956
(when-let ((syntax (syntax-after p)))
19561957
(or (= (syntax-class syntax) 6)
1957-
(/= 0 (logand (lsh 1 20) (car syntax)))))))
1958+
(and check-prefix-flag
1959+
(/= 0 (logand (lsh 1 20) (car syntax))))))))
19581960

1959-
(defun sp-syntax-before-is-prefix (&optional p)
1961+
(defun sp-syntax-before-is-prefix (check-prefix-flag &optional p)
19601962
"Check that the character before P has prefix syntax.
19611963

19621964
Prefix syntax can either come from the global major-mode syntax
19631965
table, from the text property syntax-table or from the syntax
1964-
flag (20)."
1966+
flag (20). The flag must be ignored once inside a symbol, this
1967+
is done by passing CHECK-PREFIX-FLAG as nil."
19651968
(setq p (or p (point)))
19661969
(let ((parse-sexp-lookup-properties t))
19671970
(when-let ((syntax (syntax-after (1- p))))
19681971
(or (= (syntax-class syntax) 6)
1969-
(/= 0 (logand (lsh 1 20) (car syntax)))))))
1972+
(and check-prefix-flag
1973+
(/= 0 (logand (lsh 1 20) (car syntax))))))))
19701974

19711975
(defun sp-syntax-after-is-word-or-symbol (&optional p)
19721976
"Check that the character after P has word or symbol syntax.
@@ -1977,9 +1981,9 @@ regularly the character would be (for example according to the
19771981
syntax table)."
19781982
(setq p (or p (point)))
19791983
(and (memq (sp-syntax-after p) '(?w ?_))
1980-
(not (sp-syntax-after-is-prefix p))))
1984+
(not (sp-syntax-after-is-prefix (not (sp-syntax-before-is-word-or-symbol t)) p))))
19811985

1982-
(defun sp-syntax-before-is-word-or-symbol (&optional p)
1986+
(defun sp-syntax-before-is-word-or-symbol (check-prefix-flag &optional p)
19831987
"Check that the character before P has word or symbol syntax.
19841988

19851989
In case the character has a special syntax flag 'p', meaning a
@@ -1988,7 +1992,7 @@ regularly the character would be (for example according to the
19881992
syntax table)."
19891993
(setq p (or p (point)))
19901994
(and (memq (sp-syntax-before p) '(?w ?_))
1991-
(not (sp-syntax-before-is-prefix p))))
1995+
(not (sp-syntax-before-is-prefix check-prefix-flag p))))
19921996

19931997
(defun sp-point-in-string (&optional p)
19941998
"Return non-nil if point is inside string or documentation string.
@@ -7812,8 +7816,10 @@ Examples:
78127816
,(if forward '(sp-syntax-after) '(sp-syntax-before))
78137817
'(?< ?> ?! ?| ?\ ?\\ ?\" ?' ?.))
78147818
,(if forward
7815-
'(sp-syntax-after-is-prefix (point))
7816-
'(sp-syntax-before-is-prefix (point)))
7819+
;; ignore prefix syntax not in a prefix position
7820+
'(and (not (sp-syntax-before-is-word-or-symbol t))
7821+
(sp-syntax-after-is-prefix t))
7822+
'(sp-syntax-before-is-prefix t))
78177823
(unless in-comment (sp-point-in-comment))
78187824
;; This is the case where we are starting at
78197825
;; pair (looking at it) and there is some
@@ -7985,7 +7991,7 @@ Examples:
79857991
(while (> n 0)
79867992
(while (cond
79877993
((bobp) nil)
7988-
((not (sp-syntax-before-is-word-or-symbol))
7994+
((not (sp-syntax-before-is-word-or-symbol t))
79897995
(backward-char)
79907996
t)
79917997
((sp--valid-initial-delimiter-p (sp--looking-back open))
@@ -7995,7 +8001,7 @@ Examples:
79958001
(while (and (not (bobp))
79968002
(not (or (sp--valid-initial-delimiter-p (sp--looking-back open))
79978003
(sp--valid-initial-delimiter-p (sp--looking-back close))))
7998-
(or (sp-syntax-before-is-word-or-symbol)
8004+
(or (sp-syntax-before-is-word-or-symbol nil) ;; now inside symbol so ignore prefix flag
79998005
;; Specifically for lisp, we consider
80008006
;; sequences of ?\<ANYTHING> a symbol
80018007
;; sequence

test/smartparens-commands-test.el

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,13 +200,53 @@ be."
200200

201201
(sp-test-command sp-forward-symbol
202202
((nil
203-
("|foo bar" "foo| bar" "foo bar|"))
203+
("|foo bar" "foo| bar" "foo bar|")
204+
("|,foo ,bar" ",foo| ,bar" ",foo ,bar|")
205+
("|'foo 'bar" "'foo| 'bar" "'foo 'bar|"))
204206
(((mode 'clojure))
205207
("|(map #(identity) {:a 1})"
206208
"(map| #(identity) {:a 1})"
207209
"(map #(identity|) {:a 1})"
208210
"(map #(identity) {:a| 1})"
209-
"(map #(identity) {:a 1|})"))))
211+
"(map #(identity) {:a 1|})")
212+
("|foo' 'bar qu'ux" "foo'| 'bar qu'ux" "foo' 'bar| qu'ux" "foo' 'bar qu'ux|")
213+
("|and# ~x ~@next" "and#| ~x ~@next" "and# ~x| ~@next" "and# ~x ~@next|"))))
214+
215+
(sp-test-command sp-backward-symbol
216+
((nil
217+
("foo bar|" "foo |bar" "|foo bar")
218+
(",foo ,bar|" ",foo ,|bar" ",|foo ,bar" "|,foo ,bar")
219+
("'foo 'bar|" "'foo '|bar" "'|foo 'bar" "|'foo 'bar"))
220+
(((mode 'clojure))
221+
("(map #(identity) {:a 1})|"
222+
"(map #(identity) {:a |1})"
223+
"(map #(identity) {|:a 1})"
224+
"(map #(|identity) {:a 1})"
225+
"(|map #(identity) {:a 1})")
226+
("foo' 'bar qu'ux|" "foo' 'bar |qu'ux" "foo' '|bar qu'ux" "|foo' 'bar qu'ux")
227+
("and# ~x ~@next|" "and# ~x ~@|next" "and# ~|x ~@next" "|and# ~x ~@next"))))
228+
229+
(sp-test-command sp-skip-forward-to-symbol
230+
((nil
231+
("foo| bar" "foo |bar")
232+
("foo| 'bar" "foo '|bar")
233+
("foo| [bar baz]" "foo |[bar baz]"))))
234+
235+
(sp-test-command sp-wrap-round
236+
((nil
237+
("|foo bar" "(|foo) bar")
238+
("fo|o bar" "(|foo) bar")
239+
("foo| bar" "foo (|bar)")
240+
("'|foo" "'(|foo)")
241+
("'f|oo" "(|'foo)")
242+
("|'foo" "(|'foo)"))
243+
(((mode 'clojure))
244+
("f|n?" "(|fn?)")
245+
("fn|?" "(|fn?)")
246+
("|#(foo)" "(|#(foo))")
247+
("#|(foo)" "#(|(foo))")
248+
("?|#(:clj x)" "(|?#(:clj x))")
249+
("?#|(:clj x)" "?#(|(:clj x))"))))
210250

211251
(sp-test-command sp-forward-parallel-sexp
212252
((nil

0 commit comments

Comments
 (0)