Skip to content

Commit

Permalink
WIP, fixes fsprojects#362
Browse files Browse the repository at this point in the history
  • Loading branch information
nojaf committed Nov 25, 2018
1 parent 75e7609 commit 738f77f
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 8 deletions.
27 changes: 27 additions & 0 deletions src/Fantomas.Tests/PreserveEOLTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -489,4 +489,31 @@ type QueryOption =
type MessageTypeQueryMeta =
{ Options : QueryOption list }
"""

[<Test>]
let ``indentation should be preserved when delimiter is false`` () =
let config: Fantomas.FormatConfig.FormatConfig =
{ PreserveEndOfLine = true
SpaceAfterComma = true
IndentSpaceNum = 4
PageWidth = 80
SpaceBeforeArgument = false
IndentOnTryWith = false
SpaceAroundDelimiter = false
SemicolonAtEndOfLine = false
SpaceBeforeColon = false
SpaceAfterSemicolon = false
ReorderOpenDeclaration = false
StrictMode = false }

formatSourceString false """
let config =
[ ("n", "1")
("d", "2") ]
""" config
|> should equal """
let config =
[("n", "1")
("d", "2")]
"""
2 changes: 1 addition & 1 deletion src/Fantomas/CodeFormatterImpl.fs
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ let formatWith ast formatContext config =
|> genParsedInput { ASTContext.Default with TopLevelModuleName = moduleName } ast
|> dump
|> if config.StrictMode then id
else integrateComments config.PreserveEndOfLine formatContext.ProjectOptions.ConditionalCompilationDefines normalizedSourceCode
else integrateComments config.PreserveEndOfLine config.SpaceAroundDelimiter formatContext.ProjectOptions.ConditionalCompilationDefines normalizedSourceCode

// Sometimes F# parser gives a partial AST for incorrect input
if input.IsSome && String.IsNullOrWhiteSpace normalizedSourceCode <> String.IsNullOrWhiteSpace formattedSourceCode then
Expand Down
4 changes: 2 additions & 2 deletions src/Fantomas/FormatConfig.fs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ type FormatConfig =
StrictMode = strictMode }

/// Wrapping IndentedTextWriter with current column position
type internal ColumnIndentedTextWriter(tw : TextWriter) =
type ColumnIndentedTextWriter(tw : TextWriter) =
let indentWriter = new IndentedTextWriter(tw, " ")
let mutable col = indentWriter.Indent

Expand Down Expand Up @@ -116,7 +116,7 @@ type internal ColumnIndentedTextWriter(tw : TextWriter) =
member __.Dispose() =
indentWriter.Dispose()

type internal Context =
type Context =
{ Config : FormatConfig;
Writer : ColumnIndentedTextWriter;
mutable BreakLines : bool;
Expand Down
19 changes: 14 additions & 5 deletions src/Fantomas/TokenMatcher.fs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ let (|WhiteSpaces|_|) = function
Some (loop moreOrigTokens [t1])
| _ -> None

let (|LParen|_|) = function
| (Token origTok, origTokText) when origTok.TokenName = "LPAREN" -> Some origTokText
| _ -> None

let (|RawDelimiter|_|) = function
| (Token origTok, origTokText) when origTok.CharClass = FSharpTokenCharKind.Delimiter ->
Some origTokText
Expand Down Expand Up @@ -441,7 +445,7 @@ let (|OpenChunk|_|) = function

/// Assume that originalText and newText are derived from the same AST.
/// Pick all comments and directives from originalText to insert into newText
let integrateComments isPreserveEOL compilationDefines (originalText : string) (newText : string) =
let integrateComments isPreserveEOL isSpaceAroundDelimiter compilationDefines (originalText : string) (newText : string) =
let trim (txt : string) =
if not isPreserveEOL then txt
else Regex.Replace(String.normalizeNewLine txt, @"[ \t]+$", "", RegexOptions.Multiline)
Expand All @@ -456,6 +460,8 @@ let integrateComments isPreserveEOL compilationDefines (originalText : string) (
let buffer = System.Text.StringBuilder()
let column = ref 0
let indent = ref 0

let printBuffer() = Debug.WriteLine(buffer.ToString())

let addText (text : string) =
//Debug.WriteLine("ADDING '{0}'", text)
Expand Down Expand Up @@ -561,7 +567,8 @@ let integrateComments isPreserveEOL compilationDefines (originalText : string) (
| (Marked(Token origTok, _, _) :: moreOrigTokens), _
when origTok.CharClass = FSharpTokenCharKind.WhiteSpace && origTok.ColorClass <> FSharpTokenColorKind.InactiveCode
&& origTok.ColorClass <> FSharpTokenColorKind.PreprocessorKeyword ->
Debug.WriteLine "dropping whitespace from orig tokens"
printBuffer()
Debug.WriteLine "dropping whitespace from orig tokens"
loop moreOrigTokens newTokens

| (NewLineToken _ :: moreOrigTokens), _ ->
Expand Down Expand Up @@ -682,7 +689,7 @@ let integrateComments isPreserveEOL compilationDefines (originalText : string) (

// Emit end-of-line from new tokens
| (Marked(inToken,_,_)::oldTokens), (NewLine newTokText :: moreNewTokens) ->
Debug.WriteLine("emitting newline in new tokens '{0}'", newTokText)
Debug.WriteLine(sprintf "emitting newline in new tokens '%s'" newTokText)
let nextOldTokens =
match (inToken) with
| Tok(fsInToken,_) when (fsInToken.TokenName = "IN") ->
Expand Down Expand Up @@ -712,7 +719,9 @@ let integrateComments isPreserveEOL compilationDefines (originalText : string) (
moreNewTokens
else
match moreNewTokens with
| Space t::rs ->
| Space _::LParen _::rs when (not isSpaceAroundDelimiter) ->
List.skip 1 moreNewTokens
| Space t::rs ->
addText " "
rs
| _ -> moreNewTokens
Expand All @@ -727,7 +736,7 @@ let integrateComments isPreserveEOL compilationDefines (originalText : string) (

| (Delimiter tokText :: newTokens), (RawDelimiter newTokText :: moreNewTokens)
when tokText = newTokText && newTokText <> "[<" && newTokText <> ">]" && newTokText <> "|" ->
Debug.WriteLine("emitting matching delimiter '{0}' in new tokens", newTokText |> box)
Debug.WriteLine(sprintf "emitting matching delimiter '%s' in new tokens" newTokText)
addText newTokText
loop newTokens moreNewTokens

Expand Down

0 comments on commit 738f77f

Please sign in to comment.