Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
23 changes: 17 additions & 6 deletions src/Fantomas.Core/CodeFormatter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,31 @@ type CodeFormatter =
static member ParseAsync(isSignature, source) : Async<(ParsedInput * string list) array> =
CodeFormatterImpl.getSourceText source |> CodeFormatterImpl.parse isSignature

static member FormatASTAsync(ast: ParsedInput, ?source, ?config) : Async<FormatResult> =
let sourceText = Option.map CodeFormatterImpl.getSourceText source
let config = Option.defaultValue FormatConfig.Default config
static member FormatASTAsync(ast: ParsedInput) : Async<FormatResult> =
CodeFormatterImpl.formatAST ast None FormatConfig.Default None |> async.Return

static member FormatASTAsync(ast: ParsedInput, config) : Async<FormatResult> =
CodeFormatterImpl.formatAST ast None config None |> async.Return

static member FormatASTAsync(ast: ParsedInput, source) : Async<FormatResult> =
let sourceText = Some(CodeFormatterImpl.getSourceText source)

CodeFormatterImpl.formatAST ast sourceText FormatConfig.Default None
|> async.Return

static member FormatASTAsync(ast: ParsedInput, source, config) : Async<FormatResult> =
let sourceText = Some(CodeFormatterImpl.getSourceText source)
CodeFormatterImpl.formatAST ast sourceText config None |> async.Return

static member FormatDocumentAsync(isSignature, source, ?config, ?cursor: Position) =
let config = Option.defaultValue FormatConfig.Default config
CodeFormatterImpl.formatDocument config isSignature (CodeFormatterImpl.getSourceText source) cursor

static member FormatSelectionAsync(isSignature, source, selection, config) =
let config = Option.defaultValue FormatConfig.Default config
static member FormatSelectionAsync(isSignature, source, selection) =
CodeFormatterImpl.getSourceText source
|> Selection.formatSelection FormatConfig.Default isSignature selection

static member FormatSelectionAsync(isSignature, source, selection, config) =
CodeFormatterImpl.getSourceText source
|> Selection.formatSelection config isSignature selection

Expand All @@ -35,7 +47,6 @@ type CodeFormatter =

static member MakePosition(line, column) = Position.mkPos line column

[<Experimental "Only for local development">]
static member ParseOakAsync(isSignature: bool, source: string) : Async<(Oak * string list) array> =
async {
let sourceText = CodeFormatterImpl.getSourceText source
Expand Down
19 changes: 16 additions & 3 deletions src/Fantomas.Core/CodeFormatter.fsi
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,17 @@ type CodeFormatter =
/// Parse a source string using given config
static member ParseAsync: isSignature: bool * source: string -> Async<(ParsedInput * string list) array>

/// Format an abstract syntax tree using an optional source for trivia processing
static member FormatASTAsync: ast: ParsedInput * ?source: string * ?config: FormatConfig -> Async<FormatResult>
/// Format an abstract syntax tree
static member FormatASTAsync: ast: ParsedInput -> Async<FormatResult>

/// Format an abstract syntax tree using a given config
static member FormatASTAsync: ast: ParsedInput * config: FormatConfig -> Async<FormatResult>

/// Format an abstract syntax tree with the original source for trivia processing
static member FormatASTAsync: ast: ParsedInput * source: string -> Async<FormatResult>

/// Format an abstract syntax tree with the original source for trivia processing using a given config
static member FormatASTAsync: ast: ParsedInput * source: string * config: FormatConfig -> Async<FormatResult>

/// <summary>
/// Format a source string using an optional config.
Expand All @@ -22,10 +31,14 @@ type CodeFormatter =
static member FormatDocumentAsync:
isSignature: bool * source: string * ?config: FormatConfig * ?cursor: pos -> Async<FormatResult>

/// Format a part of a source string and return the (formatted) selected part only.
/// Beware that the range argument is inclusive. The closest expression inside the selection will be formatted if possible.
static member FormatSelectionAsync: isSignature: bool * source: string * selection: range -> Async<string * range>

/// Format a part of source string using given config, and return the (formatted) selected part only.
/// Beware that the range argument is inclusive. The closest expression inside the selection will be formatted if possible.
static member FormatSelectionAsync:
isSignature: bool * source: string * selection: range * ?config: FormatConfig -> Async<string * range>
isSignature: bool * source: string * selection: range * config: FormatConfig -> Async<string * range>

/// Check whether an input string is invalid in F# by attempting to parse the code.
static member IsValidFSharpCodeAsync: isSignature: bool * source: string -> Async<bool>
Expand Down