Skip to content

Commit bc75bf0

Browse files
authored
Merge pull request #286 from vasily-kirichenko/fix-if-dicective-on-first-line
Fix compiler directive on first line & compound directives
2 parents b5b7801 + ad7715e commit bc75bf0

File tree

3 files changed

+44
-5
lines changed

3 files changed

+44
-5
lines changed

src/Fantomas.Tests/CompilerDirectivesTests.fs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,3 +259,29 @@ let x = 1
259259
let x = 1
260260
#endif
261261
"""
262+
263+
[<Test>]
264+
let ``should handle #if on the first line``() =
265+
formatSourceString false """
266+
#if INTERACTIVE
267+
let x = 1
268+
#endif
269+
""" config
270+
|> should equal """
271+
#if INTERACTIVE
272+
let x = 1
273+
#endif
274+
"""
275+
276+
[<Test>]
277+
let ``should handle combined #if``() =
278+
formatSourceString false """
279+
#if INTERACTIVE || (FOO && BAR) || BUZZ
280+
let x = 1
281+
#endif
282+
""" config
283+
|> should equal """
284+
#if INTERACTIVE || (FOO && BAR) || BUZZ
285+
let x = 1
286+
#endif
287+
"""

src/Fantomas.Tests/FormattingPropertyTests.fs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -450,7 +450,9 @@ let rec shrinkSynExpr = function
450450
| SynExpr.MatchLambda(_, _, clauses, _, _) ->
451451
seq { yield! Seq.collect collectSynMatchClause clauses }
452452
| SynExpr.TryWith(expr, _, clauses, _, _, _, _)
453-
| SynExpr.Match(_, expr, clauses, _, _) ->
453+
| SynExpr.Match(_, expr, clauses, _, _) ->
454+
// TODO: Add include support for MatchBang, add unit test first! See https://github.com/fsprojects/fantomas/issues/262
455+
//| SynExpr.MatchBang(_, expr, clauses, _, _) ->
454456
seq { yield! collectSynExpr expr; yield! Seq.collect collectSynMatchClause clauses }
455457
| SynExpr.LetOrUse(_, _, bindings, expr, _) ->
456458
seq { yield! Seq.collect collectSynBinding bindings; yield! collectSynExpr expr }

src/Fantomas/TokenMatcher.fs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,17 @@ let (|Ident|_|) = function
345345
| Wrapped(RawIdent tokText) -> Some tokText
346346
| _ -> None
347347

348-
let (|PreprocessorDirectiveChunk|_|) = function
348+
let (|PreprocessorDirectiveChunk|_|) tokens =
349+
match tokens with
350+
/// #if FOO || BAR && BUZZ
351+
| PreprocessorKeywordToken "#if" t1 ::
352+
SpaceToken t2 ::
353+
Ident t3 ::
354+
Wrapped (Space t4) ::
355+
moreOrigTokens ->
356+
Some ([t1; t2; t3 + t4], moreOrigTokens)
357+
358+
// #if FOO
349359
| PreprocessorKeywordToken "#if" t1 ::
350360
SpaceToken t2 ::
351361
Ident t3 ::
@@ -431,17 +441,17 @@ let (|OpenChunk|_|) = function
431441

432442
/// Assume that originalText and newText are derived from the same AST.
433443
/// Pick all comments and directives from originalText to insert into newText
434-
let integrateComments isPreserveEOL CompilationDefines (originalText : string) (newText : string) =
444+
let integrateComments isPreserveEOL compilationDefines (originalText : string) (newText : string) =
435445
let trim (txt : string) =
436446
if not isPreserveEOL then txt
437447
else Regex.Replace(String.normalizeNewLine txt, @"[ \t]+$", "", RegexOptions.Multiline)
438448

439449
let trimOrig = trim originalText
440450
let trimNew = trim newText
441451

442-
let origTokens = tokenize CompilationDefines trimOrig |> markStickiness |> Seq.toList
452+
let origTokens = tokenize compilationDefines trimOrig |> markStickiness |> Seq.toList
443453
//Seq.iter (fun (Marked(_, s, t)) -> Console.WriteLine("sticky information: {0} -- {1}", s, t)) origTokens
444-
let newTokens = tokenize CompilationDefines trimNew |> Seq.toList
454+
let newTokens = tokenize compilationDefines trimNew |> Seq.toList
445455

446456
let buffer = System.Text.StringBuilder()
447457
let column = ref 0
@@ -604,6 +614,7 @@ let integrateComments isPreserveEOL CompilationDefines (originalText : string) (
604614
newTokens
605615
| [] -> []
606616
else newTokens
617+
607618
loop moreOrigTokens moreNewTokens
608619

609620
// Inject inactive code

0 commit comments

Comments
 (0)