fantomas-for-vs icon indicating copy to clipboard operation
fantomas-for-vs copied to clipboard

Reformating on selection messes text following selection

Open lkluc opened this issue 3 years ago • 5 comments

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 !

lkluc avatar Jun 10 '22 17:06 lkluc

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.

deviousasti avatar Jun 12 '22 09:06 deviousasti

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. :)

lkluc avatar Jun 12 '22 14:06 lkluc

Hello, format selection is being reworked in https://github.com/fsprojects/fantomas/pull/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.

nojaf avatar Jun 13 '22 07:06 nojaf

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!

lkluc avatar Jun 13 '22 12:06 lkluc

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.

nojaf avatar Jul 12 '22 08:07 nojaf