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

Reformating on selection messes text following selection #33

Open
lkluc opened this issue Jun 10, 2022 · 5 comments
Open

Reformating on selection messes text following selection #33

lkluc opened this issue Jun 10, 2022 · 5 comments

Comments

@lkluc
Copy link

lkluc commented Jun 10, 2022

Hello,

I encountered the following issue when using CTRL-K, CTRL-F (reformat selection). The text immediately following the selection gets duplicated :

example before :

unnamed

after :

unnamed

The code :

module Test

/// Fantomas-for-vs does works well with CTRL-K CTRL-F

open System.Text.RegularExpressions

// Select this active function below, then hit CTRL-K CTRL-F
let (|Regex|_|) pattern input =
    let m = Regex.Match(input, pattern)

    if m.Success then
        Some(List.tail [ for g in m.Groups -> g.Value ])
    else
        None

// Then this comment should be messed-up !

File encoding is UTF-8 with BOM

Visual Studio Extention version used is 1.1.0.

Thanks !

@deviousasti
Copy link
Member

Formatting a selection is a bit of a known issue for Fantomas.
This is the output I get back from Fantomas LSP:

image

I think @nojaf might already be aware of this.

Aside:

/// Fantomas-for-vs does works well with CTRL-K CTRL-F

is an invalid comment (because of 3 slashes) in that position, because it's interpreted as an xmldoc. This might make it invisible to Fantomas in the first place.

@lkluc
Copy link
Author

lkluc commented Jun 12, 2022

Hello deviousasti,

First, thanks for the fast reply ! :)

And I'm glad you can reproduce my problem, it's usually the first step to a fix ;)

But I humbly beg to differ on the "invalid" part of the comment, on the MS website it seems clearly legit and interpreted as if I had enclosed it with summary tags, MS even seems to encourage it for quick XML summaries :

https://docs.microsoft.com/en-us/dotnet/fsharp/language-reference/xml-documentation#comments-without-xml-tags

Although I don't know for the other part where invalid XML comments are invisible to Fantomas.

Just to mention, I had the same thought at first (that the XML doc style comment might be the reason for the bug to occur) but think I tried with a regular comment and had the same badly formatted result. I'll give it a try just to be sure.

But thank you for looking into this problem, its appreciated. :)

@nojaf
Copy link
Contributor

nojaf commented Jun 13, 2022

Hello, format selection is being reworked in fsprojects/fantomas#2272.
This might solve your problem, it might not.
For now, Fantomas works best on the entire file.

But I humbly beg to differ on the "invalid" part of the comment,

The F# compiler stored /// comments in certain locations.
For example, when placed above a function, it will be part of the AST node of said function.

When stored anywhere else, you can already see a warning sign:
image
It is subtle but there are dots at the start here. In future versions of the compiler, you will receive warnings when you use triple-slash where you shouldn't.

Fantomas does restore invalid triple-slash comments, but I highly recommend that you only use them where you should.

@lkluc
Copy link
Author

lkluc commented Jun 13, 2022

Hello nojaf,

Thanks for the follow-up.

Glad to hear you're working on it, it's a very essential tool for me!

As for the comment, I see, my bad, I did not understood deviousasti's comment : it is on the placement that makes it illegal, not on the implicit XML tags, got it !

Thanks!

@nojaf
Copy link
Contributor

nojaf commented Jul 12, 2022

Leaving this as an up-for-grabs todo item:
https://www.nuget.org/packages/Fantomas.Client/0.6.0 was released. This contains a change in the selection API.

The FantomasResponse now exposes the range of the selection that was formatted.

https://github.com/fsprojects/fantomas/blob/98a7449918c2167c0478a151ec89bbde826a74c0/src/Fantomas.Client/Contracts.fs#L61-L70

So, if the user selected any additional whitespace, the response will contain the range that should be replaced with the format selection result.

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

No branches or pull requests

3 participants