Skip to content

Commit 01240c7

Browse files
committed
CSharpLanguageServer.Handlers.DocumentFormatting: rewrite in a more functional approach
1 parent 8f829fd commit 01240c7

File tree

3 files changed

+31
-17
lines changed

3 files changed

+31
-17
lines changed

src/CSharpLanguageServer/Handlers/DocumentFormatting.fs

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,29 @@ open Ionide.LanguageServerProtocol.Types
55
open Ionide.LanguageServerProtocol.JsonRpc
66

77
open CSharpLanguageServer.State
8+
open CSharpLanguageServer.Util
89
open CSharpLanguageServer.Roslyn.Document
910

1011
[<RequireQualifiedAccess>]
11-
module DocumentFormatting =
12-
let provider (clientCapabilities: ClientCapabilities) : U2<bool, DocumentFormattingOptions> option =
13-
Some(U2.C1 true)
1412

15-
let handle (context: ServerRequestContext) (p: DocumentFormattingParams) : AsyncLspResult<TextEdit[] option> = async {
16-
let lspFormattingOptions =
17-
if context.State.Settings.ApplyFormattingOptions then
18-
Some p.Options
19-
else
20-
None
13+
module DocumentFormatting =
14+
let provider (_cc: ClientCapabilities) : U2<bool, DocumentFormattingOptions> option = Some(U2.C1 true)
2115

22-
match context.GetUserDocument p.TextDocument.Uri with
23-
| None -> return None |> LspResult.success
24-
| Some doc ->
25-
let! ct = Async.CancellationToken
26-
let! options = getDocumentFormattingOptionSet doc lspFormattingOptions
27-
let! newDoc = Formatter.FormatAsync(doc, options, cancellationToken = ct) |> Async.AwaitTask
28-
let! textEdits = getDocumentDiffAsLspTextEdits newDoc doc
29-
return textEdits |> Some |> LspResult.success
16+
let formatDocument lspFormattingOptions doc : Async<TextEdit array option> = async {
17+
let! ct = Async.CancellationToken
18+
let! options = getDocumentFormattingOptionSet doc lspFormattingOptions
19+
let! newDoc = Formatter.FormatAsync(doc, options, cancellationToken = ct) |> Async.AwaitTask
20+
let! textEdits = getDocumentDiffAsLspTextEdits newDoc doc
21+
return textEdits |> Some
3022
}
23+
24+
let handle (context: ServerRequestContext) (p: DocumentFormattingParams) : AsyncLspResult<TextEdit[] option> =
25+
let formatDocument =
26+
p.Options
27+
|> context.State.Settings.GetEffectiveFormattingOptions
28+
|> formatDocument
29+
30+
context.GetUserDocument p.TextDocument.Uri
31+
|> async.Return
32+
|> Async.bindOption formatDocument
33+
|> Async.map LspResult.success

src/CSharpLanguageServer/Types.fs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ type ServerSettings =
1111
ApplyFormattingOptions: bool
1212
DebugMode: bool }
1313

14+
member this.GetEffectiveFormattingOptions options =
15+
if this.ApplyFormattingOptions then
16+
Some options
17+
else
18+
None
19+
1420
static member Default: ServerSettings =
1521
{ SolutionPath = None
1622
LogLevel = LogLevel.Information

src/CSharpLanguageServer/Util.fs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,11 @@ module Async =
103103
let map f computation =
104104
async.Bind(computation, f >> async.Return)
105105

106+
let bindOption f computation =
107+
async.Bind(computation, fun v -> match v with
108+
| Some v -> f v
109+
| None -> async.Return None)
110+
106111
module Map =
107112
let union map1 map2 =
108113
Map.fold (fun acc key value -> Map.add key value acc) map1 map2

0 commit comments

Comments
 (0)