Skip to content

Commit

Permalink
Allow any whitespace to precede a comment, not only space.
Browse files Browse the repository at this point in the history
  • Loading branch information
fsoikin committed Jul 2, 2017
1 parent 5dc3772 commit 59f8c00
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
16 changes: 9 additions & 7 deletions src/Paket.Core/Common/Utils.fs
Original file line number Diff line number Diff line change
Expand Up @@ -901,13 +901,15 @@ let normalizeLineEndings (text : string) =
text.Replace("\r\n", "\n").Replace("\r", "\n").Replace("\n", Environment.NewLine)

let removeComment (text:string) =
let stripComment pos =
if pos = 0 || text.[pos-1] = ' ' then text.Substring(0,pos).Trim()
else text
match text.IndexOf "//", text.IndexOf "#" with
| -1 , -1 -> text
| -1, p | p , -1 -> stripComment p
| p1, p2 -> stripComment (min p1 p2)
let rec stripComment pos =
if pos = 0 || (Char.IsWhiteSpace text.[pos-1]) then text.Substring(0,pos).Trim()
else remove (pos+1)
and remove (startAt: int) =
match text.IndexOf( "//", startAt ), text.IndexOf( "#", startAt ) with
| -1 , -1 -> text
| -1, p | p , -1 -> stripComment p
| p1, p2 -> stripComment (min p1 p2)
remove 0

// adapted from MiniRx
// http://minirx.codeplex.com/
Expand Down
7 changes: 4 additions & 3 deletions tests/Paket.Tests/ReferencesFile/ReferencesFileSpecs.fs
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,7 @@ let refFileWithComments = """
# separate-line comment with hash
Castle.Windsor # same-line comment with hash
// separate-line comment with slashes
Newtonsoft.Json // same-line comment with slashes
Newtonsoft.Json\t// same-line comment with slashes
// multiline
// comment
Expand All @@ -474,7 +474,7 @@ Newtonsoft.Json // same-line comment with slashes
# and a hash
FSharp.Core //
File: Some//File#With#Hashes.dot #and a comment after
File: Some//File#With#Hashes.dot\t#and a comment after
File: AnotherFile.txt //pluscomment
// Some empty comments:
Expand All @@ -485,7 +485,8 @@ File: AnotherFile.txt //pluscomment

[<Test>]
let ``should parse and serialize reffiles with comments``() =
let refFile = ReferencesFile.FromLines(toLines refFileWithComments).Groups.[Constants.MainDependencyGroup]
let file = refFileWithComments.Replace( "\\t", "\t" )
let refFile = ReferencesFile.FromLines(toLines file).Groups.[Constants.MainDependencyGroup]

[for p in refFile.NugetPackages -> p.Name.Name]
|> shouldEqual ["Castle.Windsor"; "Newtonsoft.Json"; "FSharp.Core"]
Expand Down

0 comments on commit 59f8c00

Please sign in to comment.