Skip to content
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

Format raw string literals indentation #975

Closed
jods4 opened this issue Oct 20, 2023 · 4 comments · Fixed by #977
Closed

Format raw string literals indentation #975

jods4 opened this issue Oct 20, 2023 · 4 comments · Fixed by #977
Milestone

Comments

@jods4
Copy link

jods4 commented Oct 20, 2023

CSharpier (as of 0.25.0) does not modify indentation of string literals.
The following code is left as-is:

public class ClassName 
{
    public string M()
    {
        return """
                            This is a string that 
                            spans multiple lines
                            thanks to the new raw string feature.
                            """;
    }
}

I think it would be desirable to standardize the indentation of raw string literals to 1 indent more than the previous line.
I would expect the following result, which represents exactly the same string as before:

public class ClassName 
{
    public string M()
    {
        return """
            This is a string that 
            spans multiple lines
            thanks to the new raw string feature.
            """;
    }
}
@belav belav added this to the 0.26.0 milestone Oct 20, 2023
@belav
Copy link
Owner

belav commented Oct 20, 2023

This won't be super straightforward like I was hoping, but there is code somewhere else that does something similar if I can track it down.

I'm thinking that if someone already has the raw string literal dedented completely, that it should stay where it is. Something like this

public class ClassName 
{
    public string M()
    {
        return """
This is a string that 
spans multiple lines
and is really wide so someone dedented it completely
""";

        return """
            Also keep in mind
                not to dedent this line
                    and this as well
            so really just trimming the same amount off each line, which maybe is in multiline comments
            """;
    }
}

@belav
Copy link
Owner

belav commented Oct 20, 2023

Scanning through all the string literals in https://github.com/belav/csharpier-repos/pull/91/files there is a mix of "dedent completely" and "indent about one more than the previous line". Possibly more of the later.

@jods4
Copy link
Author

jods4 commented Oct 20, 2023

One objective argument FOR reformatting is that CSharpier might change indentation of surrounding lines.

I personally would make no exception as the indented string just looks better and some editors don't handle code folding properly with those dedented strings.

But if you want to make an exception when the raw string indentation is exactly zero, I guess that's reasonable too.

(This is a slightly tricky feature indeed, as the spec. of raw strings is not trivial. You have to ignore empty lines, keep extra indentation on further lines and consider some literals might be invalid: less indented later lines, text on first or last line...)

@belav
Copy link
Owner

belav commented Oct 26, 2023

After seeing this case in the spec, I think we definitely want to have an exception for the end delimiter having zero indentation. If someone wants the resulting string to have a specific indentation, they can easily do it like this. Then they can copy/paste the raw string literal and it has the exact indentation they expect.

var xml = """
          <element attr="content">
            <body>
            </body>
          </element>
""";

If that was reformatted and indented like this, they would lose that ability.

var xml = """
              <element attr="content">
                <body>
                </body>
              </element>
    """;

I did get it close to working in the linked PR, just have to deal with tabs and an edge case or two.

@belav belav modified the milestones: 0.26.0, 0.27.0 Nov 6, 2023
shocklateboy92 added a commit that referenced this issue Dec 25, 2023
* This isn't correct, but I wanna see what 3rd party code does

closes #975

* Getting a basic version of this working

* Handling everything except for tabs

* format files

* Fixing issues with tabs + passing options around

* self code review

* more tweaks

* formatting files

---------

Co-authored-by: Lasath Fernando <devel@lasath.org>
belav added a commit that referenced this issue Jan 15, 2024
… (#1106)

* fixing some issues with the recent raw string literal changes

* Getting this mostly figured out

* Fixing issues with interpolated raw strings

* Fixing issues with interpolated raw strings

* Update InterpolatedStringExpressions.test

* remove using
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants