Skip to content
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

Add js2r-string-to-single-quote and js2r-string-to-double-quote #118

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

Zirak
Copy link

@Zirak Zirak commented Dec 14, 2018

Allows converting a string to single-quote and double-quote delimiters, not
just template strings.

Allows converting a string to single-quote and double-quote delimiters, not
just tempate strings.
@Zirak
Copy link
Author

Zirak commented Dec 14, 2018

As an added bonus, this does something cute (I hope?) of unescaping the original delimiter. e.g.

var foo = "I will not buy this tobacconist's, it is scratched.";
// js2r-string-to-single-quote
var foo = 'I will not buy this tobacconist\'s, it is scratched.';
// notice the added escape, as js2r previously did with `, but now!
// js2r-string-to-double-quote
var foo = "I will not buy this tobacconist's, it is scratched.";
// we got rid of the escape, howdy ho!

@NicolasPetton
Copy link
Collaborator

Thanks a lot for the PR!

I like what it does, but it would be even nicer IMO if it was js2r-string-toggle-single-quote. js2r could detect if the string at point has single or double quotes and do the right thing. What do you think?

@ArneBab
Copy link

ArneBab commented Feb 10, 2021

I think from the keymap it makes sense to keep this as it is: C RET st for template, C RET s' for single-quote, and C RET s" for double-quote.

@sandinmyjoints
Copy link
Contributor

I've seen similar sorts of cases handled by cycle functions, something like js2r-cycle-string-literal-type that would go from single -> double -> template -> single etc. when invoked. Seems like a nice way to handle it to me.

@sandinmyjoints
Copy link
Contributor

sandinmyjoints commented Sep 19, 2022

Here's my quick stab at it, making use of this PR's js2r--convert-string-delimiter:

(defun js2r-cycle-string-literal-type ()
  "Cycle: single -> double -> template -> single, etc."
  (interactive)
  (let ((node (js2-node-at-point)))
    (when (js2-string-node-p node)
      (let* ((start (js2-node-abs-pos node))
             (prev-delim (char-after start)))
        (pcase prev-delim
          (?' (js2r--convert-string-delimiter "\""))
          (?\" (js2r--convert-string-delimiter "`"))
          (?` (js2r--convert-string-delimiter "'")))))))

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.

4 participants