-
-
Notifications
You must be signed in to change notification settings - Fork 245
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
Swap Quotes fails (or works inconsistently) for embedded/interpolated Ruby strings #578
Comments
Can you give me context? Language this is tried in (if an external package is used for the language, please specify what package). Code that you are trying it on (a minimal reproducible example). @gir-bot add S: more-info-needed |
Sure! I will also update my original description with this info:
attempting to swap the quotes of Curiously, attempting to swap the outer double-quotes of the entire string for single-quotes does work on the first try (resulting in |
Two other things I've also noticed during my testing today:
|
I'm not sure what the errors are, I'd have to see them here to debug such errors, but I can tell what part of the issue is, and I'm not sure I can easily offer a fix. Maybe, but let me explain. You'll notice that the quotes around the cursor are not matching, instead the round brackets are: This is because we are doing sub-bracket matching. There are certain brackets that are hard to match, and those are ones where the start are the same as the end. Why is this hard? Because BH matches in a sliding window, so when there isn't a clear start syntax, and a bracket slides into that window, we aren't sure if it is a start or an end bracket. So, how do we match them then? Well, we first look at scope. In this case, we look at the string scope. We see the cursor is on a string, so we capture the full extent of the scope (the text within that scope) and check the ends and see if they are quotes. Now strings don't normally have the same context as the rest of the code as they can be literally anything, and scope no longer comes into play as a way we can filter things out, so we then do a limited sub-bracket match inside strings, usually just round, square, and braces. You can imagine that it can be difficult to match We don't have any logic to do scope bracket matches inside scope bracket matches, and I'm not sure how far the rabbit hole can go with interpolation strings. |
I'll at least see if I can figure out why you are getting inconsistent results, but I'm not sure I have an easy way to fix nested scope brackets. I can maybe take a look and see if there is something clever that can be done, but BH does have some limits. |
I see a couple of issues here:
We can add logic to avoid the above case because what is happening below is wrong. We swapped the outer quotes from double to single, and then single quotes inside were escaped, but '#{ENV.fetch(\'HOSTNAME\', "name-of-app")}-#{ENV.fetch(\'ENVIRONMENT\', Rails.env)}' Another issue, as I stated above, we only match a limited subset of brackets within strings, strings are not matched inside other strings. I'm not sure yet how, or if, I would address this. One answer could be that we don't match strings inside of strings. I'll need time to think about this. Maybe there is some other approach we can take. |
I know very little about Sublime Text scopes and other internals, and I'm not very fluent in python, so I'm just kind of spitballing here -- feel free to tell me that any portion of this is impossible! When I look at the scope of one of the nested strings, it shows up to me as a In fact, for multiply-nested strings (which I tried mostly as an experiment -- nobody should do this in the wild), a new In your earlier comment you said:
Would it be possible to do a "full" (non-limited) match inside the innermost Am I off in left field somewhere, or is this plausible? Thanks! |
Thinking more about the embedded scopes -- at the very least, perhaps whatever code BH uses to determine "am I inside a string?" could know about the existence of the embedded scopes. E.G. "If I'm in a string, but I'm also in an embedded scope inside that string, then I'm not in a string (and I can use regular matching here)." Again, just spitballing, not sure if this is a viable option. |
@rusterholz, it is definitely something to explore. The nesting stuff can get tricky though as you have to probably specify each allowed nesting configuration. Anyways, it is no small issue though, and will require a lot of thought when I have time to devote to it. |
Thanks for your consideration on this so far! I'll keep an eye here for updates. 👍 |
Description
The "Swap Quotes" function, intended to switch a double-quoted string to a single-quoted string or vice versa, sometimes does nothing, but results in an error dumped to the console. (This does not always occur with every attempt to swap quotes -- sometimes it works. It is inconsistent.)
This is with Sublime Text v3.2.2, build 3211 (which states "no updates available") and BracketHighlighter v2.29.0 (believed to be the most recent version because Package Control says no updates are available).
This occurs in Ruby files (.rb suffix). I am just using the built-in Ruby support in Sublime Text 3, no additional plugins or packages. It seems to occur most-often when I attempt to swap the quotes of a string that is inside another string's interpolation. For example, in the following statement:
config.x.error_source = "#{ENV.fetch('HOSTNAME', "name-of-app")}-#{ENV.fetch('ENVIRONMENT', Rails.env)}"
attempting to swap the quotes of
'HOSTNAME'
,"name-of-app"
, or'ENVIRONMENT'
will all fail. This is true for both the single-quoted strings like'HOSTNAME'
as well as the double-quoted string"name-of-app"
.Swap Quotes also sometimes fails on strings that are not part of another string's interpolation. For example, attempting to swap the outer double-quotes of the example string above for single-quotes does work on the first try (resulting in
'#{ENV.fetch(\'HOSTNAME\', "name-of-app")}-#{ENV.fetch(\'ENVIRONMENT\', Rails.env)}'
, but attempting to then re-swap those single-quotes back to double-quotes also fails.Support Info
Steps to Reproduce Issue
Expected Behavior
The single-quotes around the string should be replaced with double-quotes.
Actual Behavior
No visible change is made in the open file, but the following error sometimes appears on the Sublime Text console:
The text was updated successfully, but these errors were encountered: