Skip to content

Commit

Permalink
Merge pull request #1 from Krzysztof-Cieslak/fix
Browse files Browse the repository at this point in the history
Enable DocumentFormattingProvider
  • Loading branch information
nojaf authored Sep 18, 2019
2 parents ba36914 + 674e1d0 commit 5984c71
Showing 1 changed file with 21 additions and 20 deletions.
41 changes: 21 additions & 20 deletions src/FsAutoComplete/FsAutoComplete.Lsp.fs
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,7 @@ type FsharpLspServer(commands: Commands, lspClient: FSharpLspClient) =
DocumentHighlightProvider = Some true
DocumentSymbolProvider = Some true
WorkspaceSymbolProvider = Some true
DocumentFormattingProvider = Some true
SignatureHelpProvider = Some {
SignatureHelpOptions.TriggerCharacters = Some [| "("; ","|]
}
Expand Down Expand Up @@ -854,30 +855,30 @@ type FsharpLspServer(commands: Commands, lspClient: FSharpLspClient) =
override __.TextDocumentFormatting(p) = async {
let doc = p.TextDocument
let fileName = doc.GetFilePath()
let source, range =
let zero = { Line = 0; Character = 0 }
commands.Files.TryFind(p.TextDocument.Uri)
|> Option.map (fun { Lines = lines } ->
match commands.TryGetFileCheckerOptionsWithLines fileName with
| Result.Ok (opts, lines) ->
let range =
let zero = { Line = 0; Character = 0 }
let endLine = Array.length lines - 1
let endCharacter =
Array.tryLast lines
|> Option.map (fun line -> line.Length - 1)
|> Option.map (fun line -> if line.Length = 0 then 0 else line.Length - 1)
|> Option.defaultValue 0
String.concat "\n" lines, { Start = zero; End = { Line = endLine; Character = endCharacter } }
)
|> Option.defaultValue (System.String.Empty, { Start = zero; End = zero })
let files = Array.singleton fileName

let parsingOptions = { FSharpParsingOptions.Default with SourceFiles = files }
let checker : FSharpChecker = commands.GetChecker()
let! formatted =
Fantomas.CodeFormatter.FormatDocumentAsync(fileName,
Fantomas.SourceOrigin.SourceString source,
Fantomas.FormatConfig.FormatConfig.Default,
parsingOptions,
checker)

return LspResult.success(Some([| { Range = range; NewText = formatted } |]))
{ Start = zero; End = { Line = endLine; Character = endCharacter } }

let source = String.concat "\n" lines
let parsingOptions = Utils.projectOptionsToParseOptions opts
let checker : FSharpChecker = commands.GetChecker()
let! formatted =
Fantomas.CodeFormatter.FormatDocumentAsync(fileName,
Fantomas.SourceOrigin.SourceString source,
Fantomas.FormatConfig.FormatConfig.Default,
parsingOptions,
checker)

return LspResult.success(Some([| { Range = range; NewText = formatted } |]))
| Result.Error er ->
return LspResult.notImplemented
}

override __.TextDocumentRangeFormatting(documentRangeFormattingParams) = async {
Expand Down

0 comments on commit 5984c71

Please sign in to comment.