From cb7c349f0774f0352d1765ad4818ed4013b889d3 Mon Sep 17 00:00:00 2001 From: Ody Mbegbu Date: Sun, 3 Dec 2017 06:12:40 +0100 Subject: [PATCH] Fixes Formatting Bug --- .../vs/ServiceFormatting/TokenMatcher.fs | 10 +++++++++ .../ServiceFormatting/CommentTests.fs | 22 +++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/src/fsharp/vs/ServiceFormatting/TokenMatcher.fs b/src/fsharp/vs/ServiceFormatting/TokenMatcher.fs index 0d0af9f2d4f..abd6643fd76 100644 --- a/src/fsharp/vs/ServiceFormatting/TokenMatcher.fs +++ b/src/fsharp/vs/ServiceFormatting/TokenMatcher.fs @@ -76,6 +76,11 @@ let (|RawDelimiter|_|) = function Some origTokText | _ -> None +let (|RawComment|_|) = function + | (Token origTok, origTokText) when origTok.CharClass = FSharpTokenCharKind.Comment -> + Some origTokText + | _ -> None + let (|RawAttribute|_|) = function | RawDelimiter "[<" :: moreOrigTokens -> let rec loop ts acc = @@ -572,6 +577,11 @@ let integrateComments (originalText : string) (newText : string) = maintainIndent (fun () -> for x in commentTokensText do addText x) loop moreOrigTokens moreNewTokens + + | (_ :: moreOrigTokens), (RawComment newTokText :: moreNewTokens) -> + addText newTokText + loop moreOrigTokens moreNewTokens + // Emit end-of-line from new tokens | _, (NewLine newTokText :: moreNewTokens) -> diff --git a/vsintegration/tests/unittests/ServiceFormatting/CommentTests.fs b/vsintegration/tests/unittests/ServiceFormatting/CommentTests.fs index 26db7c827cc..b176a4005d1 100644 --- a/vsintegration/tests/unittests/ServiceFormatting/CommentTests.fs +++ b/vsintegration/tests/unittests/ServiceFormatting/CommentTests.fs @@ -363,3 +363,25 @@ let hello() = "hello world" let hello() = "hello world" (* This is a comment. *) """ + +[] +let ``should keep comments inside unit``() = + formatSourceString false """ +let x = + ((*comment*)) + printf "a" + // another comment 1 + printf "b" + // another comment 2 + printf "c" +""" config + |> prepend newline + |> should equal """ +let x = + ((*comment*)) + printf "a" + // another comment 1 + printf "b" + // another comment 2 + printf "c" +""" \ No newline at end of file