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

fix unicode characters in F# compiler diagnostic messages #1265

Merged
merged 5 commits into from
Apr 16, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
32 changes: 32 additions & 0 deletions test/FsAutoComplete.Tests.Lsp/CoreTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -467,3 +467,35 @@ 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 {
printfn "starting..."
MrLuje marked this conversation as resolved.
Show resolved Hide resolved
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

ftestList
"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

printfn "waiting for compiler..."
MrLuje marked this conversation as resolved.
Show resolved Hide resolved
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😬 damn how long did I have this here


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
Loading