Repository navigation
add raw-string literal support - #9956
Conversation
i vote 👍 because, if you want to have single and double quotes in strings, you are currently forced to escape some of them right now 😮 |
|
@amtoine Good point. This works too. ❯ echo r'"""``""dafssa""``"""'
"""``""dafssa""``"""
❯ echo r"'''''''```one\ntwo''' (blah)``` ($var)'"
'''''''```one\ntwo''' (blah)``` ($var)'Right now the requirement is that the raw string start and end with the same single or double quote mark. Backtick quotes aren't part of it. |
|
Feels like we get a lot of what you're looking for with: |
@jntrnr agreed. the same with single quotes. it's just more verbose, plus using backticks always makes me think of bash as "evaluate this". backticks should just be banned altogether, imo. like I said above, I'm not sure if it's helpful or not. I do like the coloring of the string though. I think that part is helpful. e.g. light purple means you don't evaluate, green means it evaluates. That could be a good fix if we could do it somehow. for escaping-strings, interpolated strings, non-escaping strings, and paths. |
|
After talking about this, we've decided to close this PR. The best thing about this PR (IMO), that we're going to continue working on, is adding the ability to highlight different types of strings in separate colors. We think this would be a nice QOL repl improvement. |
|
@jntrnr echo a raw string works, however assignment does not work:
|
|
Apparently we never did anything with "the best thing about the PR" nor did we answer hustcer's point. I'd still like to see this added as a QOL improvement. |
|
Agreed, we need this feature. |
|
@hustcer I just learned that this works now > let raw = echo `one\ntwo (blah) ($var)`
> $raw
one\ntwo (blah) ($var) |
Thanks, still not perfect enough, as: # This one works
let raw = echo `one\ntwo (blah) ($var)`
# However, this one doesn't
let raw = `one\ntwo (blah) ($var)`
Error: nu::shell::external_command
× External command failed
╭─[entry #2:1:1]
1 │ let raw = `one\ntwo (blah) ($var)`
· ────────────┬───────────
· ╰── executable was not found
╰────
help: 系统找不到指定的路径。 (os error 3) |
|
Raw strings are much needed, would love to see this in the language. Except, can we go all the way and add Rust's |
I'd initially be against |
|
What about the essence of Or, if the user writing the script doesn't care about a custom marker, it can be omitted: This also intuitively resonates with Bash people that use arbitrary heredoc markers like where |
|
I tried to update this old PR to the latest main. May have missed some things. One thing that doesn't work is: let a = r"one\ntwo\n (blah) ($var)"I'm not sure This doesn't work either echo r"Look, I can use "double quotes"!" |
+1 to this and it'd be great if it can support interpolation in it as well (and be able to disable it too) |
That's a murky area, as it kind of defeats the purpose of it being a raw string. I know what you mean, and I've been at the receiving end of this: it's both a blessing and annoying at the same time that in Bash, you can use variables in heredoc: And this What if raw strings were pure raw with no embellishments whatsoever? You could still insert computed values by concatenating raw strings using |
|
Ugh, this is going to take some surgery. parse_value is not passing the right span to parse_raw_string. If anyone wants to pick this up and take it over, please volunteer. I doubt I'll work on it. |
The primary reason I'd be interested in having interpolation in heredocs is exactly what I'm looking for! 😅 Generally for the nu language most of the time I think about them in the sense of modern programming/scripting languages and not syntax or following how things work in things like bash. In programs I've written, heredocs, both with optional delimiter for multiline strings and optionally disable/enable interpolation can be really useful and code easy to read. So it would be a really welcomed addition IMO. Big fan of Ruby's heredocs and I use them a lot and is helpful for other folks reading my code. |
|
By the way, in Bash you have control over interpolation, where will expand will not, due to single quotes around |
|
It should close: #5091 |
Fix raw string with let like: let x = r@'abc'@
|
A small concern I have with the
|
|
We can always pick another symbol like echo r!'one\ntwo (blah) ($var)'! |
|
The only potential issue I see is if we allow def cmd [arg: r@'options'] {}If we remove Then again, it could still look confusing/inconsistent: def cmd [arg: number@'options'] {} # almost looks like `r@''@` or `@''@`So, I would also be fine with changing the symbol/syntax. |
|
As we should be parsimonous with the available special sauce characters, and have something as identifiable as possible for the users, among the options I prefer the Rust-like raw strings
|
|
To be honest, I don't think it's a big issue, because we only use raw string outside of function signature, and custom completer only exists inside function signature. They don't have overlap inside the same space. C# using @ in various places: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/tokens/verbatim |
|
My personal favorite is still prefix: I think the point about double quotes vs single quotes is probably a good one, so I'm good with single quotes. I spent 20+ years writing C# but I still like |
Yeah, sounds good. Just to reiterate my point, I sympathize with the fact that let a = dir #"this is a comment"
let b = r#"this is a raw string"#Had I not known that comments required a preceding space (I just learned that now lol), then I would have assumed that The So now I'm leaning towards |
|
@WindSoilder can we change this one more time to |
|
I noticed that even though we've added Here's the output of |
# Description As discovered by @yizhepku in a [comment](#9956 (comment)) in #9956, raw strings are not parsed properly when they are used as an argument to an external command. This PR fixes that. # Tests + Formatting Added a test.
# Description As discovered by @yizhepku in a [comment](nushell#9956 (comment)) in nushell#9956, raw strings are not parsed properly when they are used as an argument to an external command. This PR fixes that. # Tests + Formatting Added a test.
|
This is a great feature. Thanks all! |
|
It was definitely an uphill battle and took almost a year but I'm glad we got it landed thanks mostly to @WindSoilder. |
Description
This PR adds raw string support by using
r#at the beginning of single quoted strings and#at the end.Notice that escapes do not process, even within single quotes, parentheses don't mean anything, $variables don't mean anything. It's just a string.
Notice how they work without
echoorprintand how they work without carriage returns.They also have a special configurable color in the repl. (use single quotes though)

They should work like rust raw literals and allow
r##,r###,r####, etc, to help with having one or many#'s in the middle of your raw-string.They should work with
letas well.closes #5091
User-Facing Changes
Tests + Formatting
After Submitting