Skip to content

Commit

Permalink
fix unicode characters in F# compiler diagnostic messages (#1265)
Browse files Browse the repository at this point in the history
* fix unicode chars in F# compiler diagnostic messages

* fix typo in ShadowedTimeouts focused tests

* fixup! fix unicode chars in F# compiler diagnostic messages

* remove focused tests...

* remove debug prints

Co-authored-by: Jimmy Byrd <[email protected]>

---------

Co-authored-by: Jimmy Byrd <[email protected]>
  • Loading branch information
MrLuje and TheAngryByrd authored Apr 16, 2024
1 parent bc827fb commit 5b9bc1d
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 3 deletions.
7 changes: 6 additions & 1 deletion src/FsAutoComplete/LspHelpers.fs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,11 @@ module Conversions =
let urlForCompilerCode (number: int) =
$"https://docs.microsoft.com/en-us/dotnet/fsharp/language-reference/compiler-messages/fs%04d{number}"

[<Literal>]
let unicodeParagraphCharacter: string = "\u001d"

let private handleUnicodeParagraph (message: string) = message.Replace(unicodeParagraphCharacter, Environment.NewLine)

let fcsErrorToDiagnostic (error: FSharpDiagnostic) =
{ Range =
{ Start =
Expand All @@ -108,7 +113,7 @@ module Conversions =
Character = error.EndColumn } }
Severity = fcsSeverityToDiagnostic error.Severity
Source = Some "F# Compiler"
Message = error.Message
Message = handleUnicodeParagraph error.Message
Code = Some(string error.ErrorNumber)
RelatedInformation = Some [||]
Tags = None
Expand Down
3 changes: 3 additions & 0 deletions src/FsAutoComplete/LspHelpers.fsi
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ module Conversions =
val fcsRangeToLspLocation: range: FcsRange -> Location
val findDeclToLspLocation: decl: FsAutoComplete.FindDeclarationResult -> Location

[<Literal>]
val unicodeParagraphCharacter: string = "\u001d"

type TextDocumentIdentifier with

member GetFilePath: unit -> string
Expand Down
37 changes: 37 additions & 0 deletions test/FsAutoComplete.Tests.Lsp/CoreTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -467,3 +467,40 @@ let closeTests state =
let! diags = doc |> Document.waitForLatestDiagnostics (TimeSpan.FromSeconds 5.0)
Expect.isNonEmpty diags "There should be no publishDiagnostics without any diags after close"
}) ])

let diagnosticsTest state =
let server =
async {
let path = Path.Combine(__SOURCE_DIRECTORY__, "TestCases", "DiagnosticFormatting")
let! (server, events) = serverInitialize path defaultConfigDto state
let path = Path.Combine(path, "Program.fs")
do! waitForWorkspaceFinishedParsing events
return (server, events, path)
}
|> Async.Cache

testList
"Diagnostics formatting Tests"
[ testCaseAsync
"replacing unicode paragraph by newline"
(async {
let! (server, events, path) = server

let tdop: DidOpenTextDocumentParams = { TextDocument = loadDocument path }
do! server.TextDocumentDidOpen tdop

let! compilerResults = waitForCompilerDiagnosticsForFile "Program.fs" events |> Async.StartChild

match! compilerResults with
| Ok() -> failtest "should get an F# compiler checking error"
| Core.Result.Error errors ->
Expect.exists
errors
(fun error -> error.Code = Some "39" || error.Code = Some "41")
"should have an error FS0039(identifier not defined) or FS0041(a unique overload for method 'TryParse' could not be determined based on type information prior to this program point)"

Expect.all
errors
(fun error -> not <| error.Message.Contains(unicodeParagraphCharacter))
"message should not contains unicode paragraph characters"
}) ]
4 changes: 2 additions & 2 deletions test/FsAutoComplete.Tests.Lsp/Helpers.fs
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ module Expecto =

let testCase = testCaseWithTimeout DEFAULT_TIMEOUT
let ptestCase = ptestCaseWithTimeout DEFAULT_TIMEOUT
let ftestCase = ptestCaseWithTimeout DEFAULT_TIMEOUT
let ftestCase = ftestCaseWithTimeout DEFAULT_TIMEOUT

let testCaseAsync = testCaseAsyncWithTimeout DEFAULT_TIMEOUT
let ptestCaseAsync = ptestCaseAsyncWithTimeout DEFAULT_TIMEOUT
let ftestCaseAsync = ptestCaseAsyncWithTimeout DEFAULT_TIMEOUT
let ftestCaseAsync = ftestCaseAsyncWithTimeout DEFAULT_TIMEOUT

let rec private copyDirectory sourceDir destDir =
// Get the subdirectories for the specified directory.
Expand Down
1 change: 1 addition & 0 deletions test/FsAutoComplete.Tests.Lsp/Program.fs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ let lspTests =
UnusedDeclarationsTests.tests createServer
EmptyFileTests.tests createServer
CallHierarchy.tests createServer
diagnosticsTest createServer
] ]

/// Tests that do not require a LSP server
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<Compile Include="Test.fs" />
<Compile Include="Program.fs" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Learn more about F# at http://fsharp.org

open System

[<EntryPoint>]
let main argv =
let isOk, integer = System.Int32.TryParse nope
printfn "Hello World from F#!"
0 // return an integer exit code

0 comments on commit 5b9bc1d

Please sign in to comment.