atom / toggle-quotes

An Atom package to toggle between single and double quotes
MIT License
77 stars 36 forks source link

Add support for javascript / es6 ` characters #35

Closed ProLoser closed 6 years ago

ProLoser commented 9 years ago

In javascript there's a new ` character that can be used to encapsulate template strings. Quite often I will do things like convert 'bob and ' + name into `bob and ${name}` and wish I could use your plugin to switch to this quote variation.

Now I'm not expecting you to convert the string concatenation into string templates, but it would be nice if there was a setting I could enable that add a third quote character variation (possibly scoped only to javascript syntaxes) so it would cycle from ' to " to `

KnutHelland commented 9 years ago

It is configurable already. In my config.cson I have:

"*":
  "toggle-quotes":
    quoteCharacters: "\"'`"

You can probably scope that config per language as well if you want.

But yes, it would be really nice if it could change from concatenation to interpolation. I wonder if there will be many edge cases to such behaviour, or if it will be straight forward...

PhiLhoSoft commented 8 years ago

See #14 The "concatenation to interpolation" part is interesting, though, although might be hard to do correctly.

aqzhyi commented 8 years ago

:+1:

acontreras89 commented 8 years ago

:-1: to converting from concatenation to interpolation (or vice-versa.) As pointed by @KnutHelland, the ``` character can be added to the rotation and I can't see why this package should be responsible for doing anything beyond that.

aqzhyi commented 8 years ago

Hi, @KnutHelland @ProLoser

I am using Atom 1.5.4 and toggle-quotes 1.0.0

I use setup:

"*":
  "toggle-quotes":
    quoteCharacters: "\"'`"

but once I got switched to `, it will broken. I can't switch anymore.

but if I add some line in plugin toggle-quotes.js:

  if (range == null) {
    // Attempt to match the current invalid region if it is wrapped in quotes
    // This is useful for languages where changing the quotes makes the range
    // invalid and so toggling again should properly restore the valid quotes

    range = editor.displayBuffer.bufferRangeForScopeAtPosition('.invalid.illegal', position)

    /*ADD*/ if (range == null) {
    /*ADD*/   range = editor.displayBuffer.bufferRangeForScopeAtPosition('.string.quasi', position)
    /*ADD*/ }

    if (range) {
      let inner = quoteChars.split('').map(character => `${character}.*${character}`).join('|')

      if (!RegExp(`^(${inner})$`, 'g').test(editor.getTextInBufferRange(range))) {
        return
      }
    }
  }

It works.

jkeen commented 7 years ago

This is still a problem, and the above fix doesn't work in all cases. In a ruby file for example, .string.quasi isn't the current scope but rather .string.interpolated.ruby.

Seems like if we the .invalid.illegal range search returns nothing we just need to look at the current scope? Something like this:

if (range == null) {
   // try current scope
   let cursor = editor.getLastCursor();
   let scopes = cursor.getScopeDescriptor().getScopesArray();
   let currentScope = scopes[scopes.length - 1];
   range = editor.displayBuffer.bufferRangeForScopeAtPosition(currentScope, position);
}
wottpal commented 7 years ago

I've already added the ` quote-character to my settings but it would be great if toggle-quotes keeps this scoped to .js-files. Because for example in .php-files I can't cycle between all my options. (It stucks always at `) :(

jinglesthula commented 6 years ago

Nested template literals would be tricky, for sure, and especially since they can contain js expressions. I think the only safe way is to just switch the actual enclosing characters between quotes and backticks and let the coder work out the rest. Or refuse to switch if there's any interpolation.

ProLoser commented 6 years ago

I'm happy with the configuration solution

wottpal commented 6 years ago

But the plugin is basically not usable with (modern) JS files and PHP files together :(

ProLoser commented 6 years ago

@wottpal create a new issue for scoped settings or a default setting and scoped overrides.

softwarecreations commented 4 years ago

@hilezi your fix does not work in JSX files when you want to change JSX element properties eg: It will get stuck on backticks around hello.

aqzhyi commented 4 years ago

@hilezi your fix does not work in JSX files when you want to change JSX element properties eg: It will get stuck on backticks around hello.

Hi @softwarecreations, this post is 4 years ago. I currently didn't use atom now.