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

Fantomas integration #454

Merged
merged 14 commits into from
Oct 2, 2019
3 changes: 2 additions & 1 deletion paket.dependencies
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ source https://api.nuget.org/v3/index.json
storage: none

nuget Argu ~> 5.2.0
nuget FSharp.Compiler.Service ~> 31.0
nuget Fantomas 3.0.0-beta-006
nuget FSharp.Compiler.Service ~> 32.0
nuget Dotnet.ProjInfo ~> 0.36.0
nuget Dotnet.ProjInfo.Workspace.FCS ~> 0.36.0
nuget FSharp.Data 3.0.1
Expand Down
671 changes: 239 additions & 432 deletions paket.lock

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions src/FsAutoComplete.Core/Commands.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1114,3 +1114,5 @@ type Commands (serialize : Serializer, backgroundServiceEnabled) =
let! runtimePath = FakeSupport.getFakeRuntime ()
return [CoreResponse.FakeRuntime runtimePath]
}

member x.GetChecker () = checker.GetFSharpChecker()
2 changes: 2 additions & 0 deletions src/FsAutoComplete.Core/CompilerServiceInterface.fs
Original file line number Diff line number Diff line change
Expand Up @@ -537,3 +537,5 @@ type FSharpCompilerServiceChecker(backgroundServiceEnabled) =
}

member __.Compile = checker.Compile

member internal x.GetFSharpChecker() = checker
7 changes: 6 additions & 1 deletion src/FsAutoComplete.Core/UntypedAstUtils.fs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ let (|ConstructorPats|) = function
| SynConstructorArgs.Pats ps -> ps
| SynConstructorArgs.NamePatPairs(xs, _) -> List.map snd xs

/// A pattern that collects all attributes from a `SynAttributes` into a single flat list
/// A pattern that collects all attributes from a `SynAttributes` into a single flat list
let (|AllAttrs|) (attrs: SynAttributes) =
attrs |> List.collect (fun attrList -> attrList.Attributes)

Expand Down Expand Up @@ -870,6 +870,11 @@ let internal getRangesAtPosition (input: ParsedInput option) (r: pos) : range li
| SynExpr.YieldOrReturnFrom (_, e, r) ->
addIfInside r
walkExpr e
| SynExpr.SequentialOrImplicitYield(_, e1, e2, ifNotE, r) ->
addIfInside r
walkExpr e1
walkExpr e2
walkExpr ifNotE
| SynExpr.Lambda (_, _, pats, e, r) ->
addIfInside r
walkSimplePats pats
Expand Down
54 changes: 54 additions & 0 deletions src/FsAutoComplete/FsAutoComplete.Lsp.fs
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,8 @@ type FsharpLspServer(commands: Commands, lspClient: FSharpLspClient) =
DocumentHighlightProvider = Some true
DocumentSymbolProvider = Some true
WorkspaceSymbolProvider = Some true
DocumentFormattingProvider = Some true
DocumentRangeFormattingProvider = Some true
SignatureHelpProvider = Some {
SignatureHelpOptions.TriggerCharacters = Some [| "("; ","|]
}
Expand Down Expand Up @@ -851,6 +853,58 @@ type FsharpLspServer(commands: Commands, lspClient: FSharpLspClient) =
return res
}

override __.TextDocumentFormatting(p) = async {
let doc = p.TextDocument
let fileName = doc.GetFilePath()
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 -> if line.Length = 0 then 0 else line.Length - 1)
|> Option.defaultValue 0
{ 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(p) = async {
let doc = p.TextDocument
let fileName = doc.GetFilePath()
match commands.TryGetFileCheckerOptionsWithLines fileName with
| Result.Ok (opts, lines) ->
let range = Fantomas.CodeFormatter.MakeRange(fileName, (p.Range.Start.Line + 1), (p.Range.Start.Character + 1), (p.Range.End.Line + 1), (p.Range.End.Character + 1))

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

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

member private x.HandleTypeCheckCodeAction file pos f =
async {
return!
Expand Down
3 changes: 2 additions & 1 deletion src/FsAutoComplete/paket.references
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ Dapper
FSharp.Data
Microsoft.Data.Sqlite
OptimizedPriorityQueue
ICSharpCode.Decompiler
ICSharpCode.Decompiler
Fantomas