Skip to content

Don't pair escaped parens in swift-mode #1226

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

samglt
Copy link

@samglt samglt commented Feb 6, 2025

Related to #1225

This disables pairing escaped parens together, but still allows a "(" to be paired and become "()" which is commonly used for string interpolation.

@woolsweater
Copy link
Contributor

Nice, thanks for fixing this; I've been meaning to get around to it!

I would suggest a bit fuller of a definition:

(sp-local-pair "\\(" ")" :actions '(insert wrap) :when '(sp-in-string-p))

Including the correct closing delimiter, rather than nil, lets other Smartparens facilities like sp-unwrap-sexp work correctly. The action list allows wrapping with a region. I.e., while it's a bit of an odd edit, if the region is active and denoted by the | characters:

let s = "foo |bar|"

then typing a backslash followed by an open paren correctly wraps "bar" in an interpolation.

Finally, while you're right that there's no case currently in Swift for escaped parens outside of a string, I don't think it hurts to include the :when predicate.

@samglt
Copy link
Author

samglt commented Feb 8, 2025

Good points. Trying that out though, unwrapping only removes the parens and not the initial backslash, so "\(|foo)" becomes "\|foo". The normal "(" ")" pairing has to take precedence I think

@woolsweater
Copy link
Contributor

Hmm, interesting 🤔 Are you using https://github.com/swift-emacs/swift-mode? I forgot actually that that mode does not propertize the interpolation delimiters such that smartparens can work with them (for example sp-mark-sexp, sp-forward-sexp etc. do not work). I just get an error trying to unwrap when using that mode.

swift-mode

Using https://codeberg.org/woolsweater/swift-ts-mode (authored by me :) does unwrap correctly:

swift-ts-mode

I have not, however, tried the other swift-ts-mode.

@samglt
Copy link
Author

samglt commented Feb 9, 2025

That must be it. I'm using the the other one. I'll try yours out

@woolsweater
Copy link
Contributor

This will be a bit of a tangle for smartparens. 🙁

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants