-
Notifications
You must be signed in to change notification settings - Fork 3.6k
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
(Swift) Support raw strings #2820
Conversation
@@ -67,6 +67,9 @@ export default function(hljs) { | |||
variants: [ | |||
{begin: /"""/, end: /"""/}, | |||
{begin: /"/, end: /"/}, | |||
{begin: /#"/, end: /"#/}, | |||
{begin: /##"/, end: /"##/}, | |||
{begin: /###"/, end: /"###/}, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can such strings contain unescaped newlines?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same subst and backlash escape rules apply to raw strings as regular strings or is that different?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, but you can have raw multiline strings (wrapped in #"""
and """#
). I also tried those and they highlight correct for me, with the rules above. Same goes for strings with interpolation or escaped characters.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm confused what makes them "raw"... if this has no bearing on the escaping, etc?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FYI, here are all the code examples in my document that use raw strings:
print(#"Use \t to insert a tab."#)
print(#"""
Use \t to insert a tab. The following example:
print("\tHello")
prints:
Hello
"""#)
print(##"Use #" and "# to delimit a raw string."##)
print(#"""
Use \t to insert a tab. The following example:
print("\tHello")
prints:
\#tHello
"""#)
print(#"Hi \#(player), your score is: \#(score)."#)
These all highlight correctly with the new rules.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What makes them raw is that they interpret "special characters" (such as \t
) as normal character sequences (backslash and t) instead of their special meaning (tab). However, you can still use escape sequences and string interpolation by adding the same number of hashes after the backslash.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we need more markup tests... sounds like there is more to be done here... at the very least we don't match normal escapes (as with every other string)... so we should have a negative test case for that... (showing that it workes properly, NOT highlighting what would be an escape)
And we should have some test cases of this new variant of escaping (and then decide whether to handle it or not)... if not then a comment in the test would explain, etc.
https://www.hackingwithswift.com/articles/162/how-to-use-raw-strings-in-swift
I wonder if we should highlight such things? |
So it sounds like the |
var a = not actually code | ||
""" | ||
|
||
print(##"Use #" and "# to delimit a raw string."##) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we handle three variants (#, ##, ###) in the language then we'd optimally want all 3 listed here in markup tests also.
I'm afraid this is about as much as I can contribute in the short term, so I'm hoping you can take that up. I even had to install NPM specifically for this, which should tell you how familiar I am with JS codebases 😅 |
But even if you don't, I'm hoping this small fix can at least make it in. At the moment, raw strings are highlighted wrong. This would at least highlight them as a string, even if it's not perfect. I also don't see any difference with normal Swift strings. They also highlight as one big hljs-string, even when they contain escapes or interpolated values. For example: "1.\tACE\t976\n2.\tBOB\t952\n3.\tCU2\t897" becomes: <span class="hljs-string">"1.\tACE\t976\n2.\tBOB\t952\n3.\tCU2\t897"</span> and "Hi \(player), your score is: \(score)." becomes: <span class="hljs-string">"Hi \(player), your score is: \(score)."</span> |
It's not visual (though it well could be in the future - we highlight these in some languages). Right now an extra escape would throw everything off:
This sting never ends because |
If not I, someone else. Thanks for the effort! :) |
Thanks for having Swift support in the first place 😅 |
Closing this for now until someone has time to circle back to it and complete it fully. The issue remains open of course (with link to this PR). |
Resolves #2819
Changes
Add support for raw strings with up to three leading/trailing hashes. In theory, raw strings can have any number of hashes, as long as they’re balanced, but more than one is rarely used.
Checklist
CHANGES.md
AUTHORS.txt
, under Contributors