Skip to content

Raw string assists #1730

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

Closed
matklad opened this issue Aug 23, 2019 · 14 comments · Fixed by #1850
Closed

Raw string assists #1730

matklad opened this issue Aug 23, 2019 · 14 comments · Fixed by #1850
Labels
E-easy E-has-instructions Issue has some instructions and pointers to code to get started good first issue

Comments

@matklad
Copy link
Member

matklad commented Aug 23, 2019

Add the following assists:

  • make raw string: "hello" -> r#"hello"#
  • add hash: r#"hello"# -> r##"hello"##
  • remove hash: r#"hello"# -> r"hello"
  • make usual string: r"hello" -> "hello"

Note that, ideally, we should apply escape/unescape for the first and last assist, but it's ok to not do it for the first pass.

Example assits are here: https://github.com/rust-analyzer/rust-analyzer/blob/e055cfacdfe3b3451484dae5d6ed08aefba133ca/crates/ra_assists/src/lib.rs

Each familiy lives in a separate mode, which contains the tests as well. See #1281 for an example of PR which adds an assist.

@matklad matklad added good first issue E-easy E-has-instructions Issue has some instructions and pointers to code to get started labels Aug 23, 2019
@bjorn3
Copy link
Member

bjorn3 commented Aug 23, 2019

-make usual string: r"hello" -> r"hello"
+make usual string: r"hello" -> "hello"

@bjorn3
Copy link
Member

bjorn3 commented Aug 23, 2019

Note that, ideally, we should apply escape/unescape for the first and last assist, but it's ok to not do it for the first pass.

Maybe add two variants of the first assist. One which doesn't unescape and one which does. You may have the invalid "C:\Users" (\U is not a valid escape) and want to turn it into r"C:\Users", or you may want to make a valid normal string into a raw string to prevent having to escape stuff.

@matklad
Copy link
Member Author

matklad commented Aug 23, 2019

The unescaping one is better as a quick fix for lexer error, and not as a separe assits, but yeah, this sounds right (but out of scope for this particular issue).

@Geobert
Copy link
Contributor

Geobert commented Sep 12, 2019

I'm going to try this one :)

@Geobert
Copy link
Contributor

Geobert commented Sep 15, 2019

What should be the result of applying make_raw_string to "aaa\nbbb"?
Same question with make_usual_string and different cases.

@matklad
Copy link
Member Author

matklad commented Sep 16, 2019

What should be the result of applying make_raw_string to "aaa\nbbb"?

Just r#"aaa\nbbb"# for the first pass. Ideally, it shoould be

r#"aaa
bbb"#

You can use rustc_lexer::unescape module to deal with escape sequqneces properly.

@Geobert
Copy link
Contributor

Geobert commented Sep 19, 2019

Quick notice: I'm still on this, holidays are gone so less time, but I'll complete that PR :)

bors bot added a commit that referenced this issue Sep 20, 2019
1850: feat(assists): raw string <-> usual string manipulation r=matklad a=Geobert

Fixes #1730

Co-authored-by: Geobert Quach <[email protected]>
@bors bors bot closed this as completed in b6d5529 Sep 20, 2019
@Geobert
Copy link
Contributor

Geobert commented Sep 20, 2019

Maybe not closing yet, I still need to do the unescape version of make_raw_string

@matklad matklad reopened this Sep 20, 2019
@Geobert
Copy link
Contributor

Geobert commented Sep 22, 2019

You can use rustc_lexer::unescape module to deal with escape sequqneces properly.

Is there example of the usage of unescape_str not in an error context? (as in ra_syntax validation.rs)

@Geobert
Copy link
Contributor

Geobert commented Sep 22, 2019

Thanks! You mean it's unescape_raw_str that I need?

@matklad
Copy link
Member Author

matklad commented Sep 23, 2019

I am actually not sure I you realllu need thouse functions, or if escape default would be enough. But thouse functions definitely can tell you anything about the semantics of the literals

@Geobert
Copy link
Contributor

Geobert commented Sep 23, 2019

I think I need them, escape_default is doing \n -> \\n and we want \n -> <LF>

My question was about choosing between unescape_raw_str and unescape_str

bors bot added a commit that referenced this issue Oct 8, 2019
1922: feat(assists): Make raw string unescaped r=matklad a=Geobert

Last piece of #1730

Co-authored-by: Geobert Quach <[email protected]>
@Geobert
Copy link
Contributor

Geobert commented Oct 9, 2019

So it's done now?

@matklad matklad closed this as completed Oct 9, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
E-easy E-has-instructions Issue has some instructions and pointers to code to get started good first issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants