Skip to content

Commit

Permalink
Skip querying for any line with >= 512 characters
Browse files Browse the repository at this point in the history
until consistencies in range values are fixed.

Fix #83. Fix #85.
  • Loading branch information
dungpa committed Jan 5, 2014
1 parent de5be4a commit 1094078
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 26 deletions.
22 changes: 21 additions & 1 deletion src/Fantomas.Tests/TypeProviderTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,24 @@ type IntegerRegex = FSharpx.Regex< @"(?<value>\d+)" >
let ``should throw FormatException on unparsed input``() =
formatSourceString false """
type GeoResults = JsonProvider<Sample= "A" + "GitHub.json" >""" config
|> ignore
|> ignore

[<Test>]
let ``should handle lines with more than 512 characters``() =
formatSourceString false """
(new CsvFile<string * decimal * decimal>(new Func<obj, string[], string * decimal * decimal>(fun (parent : obj) (row : string[]) -> CommonRuntime.GetNonOptionalValue("Name", CommonRuntime.ConvertString(TextConversions.AsOption(row.[0])), TextConversions.AsOption(row.[0])), CommonRuntime.GetNonOptionalValue("Distance", CommonRuntime.ConvertDecimal("", TextConversions.AsOption(row.[1])), TextConversions.AsOption(row.[1])), CommonRuntime.GetNonOptionalValue("Time", CommonRuntime.ConvertDecimal("", TextConversions.AsOption(row.[2])), TextConversions.AsOption(row.[2]))), new Func<string * decimal * decimal, string[]>(fun (row : string * decimal * decimal) -> [| CommonRuntime.ConvertStringBack(CommonRuntime.GetOptionalValue((let x, _, _ = row in x))); CommonRuntime.ConvertDecimalBack("", CommonRuntime.GetOptionalValue((let _, x, _ = row in x))); CommonRuntime.ConvertDecimalBack("", CommonRuntime.GetOptionalValue((let _, _, x = row in x))) |]), (ProviderFileSystem.readTextAtRunTimeWithDesignTimeOptions @"C:\Dev\FSharp.Data-master\src\..\tests\FSharp.Data.Tests\Data" "" "SmallTest.csv"), "", '"', true, false)).Cache()
""" config
|> prepend newline
|> should equal """
(new CsvFile<string * decimal * decimal>(new Func<obj, string [], string * decimal * decimal>(fun (parent : obj) (row : string []) -> CommonRuntime.GetNonOptionalValue("Name", CommonRuntime.ConvertString(TextConversions.AsOption(row.[0])), TextConversions.AsOption(row.[0])), CommonRuntime.GetNonOptionalValue("Distance", CommonRuntime.ConvertDecimal("", TextConversions.AsOption(row.[1])), TextConversions.AsOption(row.[1])), CommonRuntime.GetNonOptionalValue("Time", CommonRuntime.ConvertDecimal("", TextConversions.AsOption(row.[2])), TextConversions.AsOption(row.[2]))),
new Func<string * decimal * decimal, string []>(fun (row : string * decimal * decimal) ->
[| CommonRuntime.ConvertStringBack(CommonRuntime.GetOptionalValue((let x, _, _ = row
x)))
CommonRuntime.ConvertDecimalBack("",
CommonRuntime.GetOptionalValue((let _, x, _ = row
x)))
CommonRuntime.ConvertDecimalBack("",
CommonRuntime.GetOptionalValue((let _, _, x = row
x))) |]), (ProviderFileSystem.readTextAtRunTimeWithDesignTimeOptions @"C:\Dev\FSharp.Data-master\src\..\tests\FSharp.Data.Tests\Data" "" "SmallTest.csv"), "", '"', true, false))
.Cache()
"""
15 changes: 4 additions & 11 deletions src/Fantomas/CodeFormatter.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ let test (s : string) =
fsi.AddPrinter (fun (p : Microsoft.FSharp.Compiler.Range.pos) -> p.ToString())
fsi.AddPrinter (fun (r : Microsoft.FSharp.Compiler.Range.range) -> r.ToString())

test """
(new CsvFile<string * decimal * decimal>(new Func<obj, string[], string * decimal * decimal>(fun (parent : obj) (row : string[]) -> CommonRuntime.GetNonOptionalValue("Name", CommonRuntime.ConvertString(TextConversions.AsOption(row.[0])), TextConversions.AsOption(row.[0])), CommonRuntime.GetNonOptionalValue("Distance", CommonRuntime.ConvertDecimal("", TextConversions.AsOption(row.[1])), TextConversions.AsOption(row.[1])), CommonRuntime.GetNonOptionalValue("Time", CommonRuntime.ConvertDecimal("", TextConversions.AsOption(row.[2])), TextConversions.AsOption(row.[2]))), new Func<string * decimal * decimal, string[]>(fun (row : string * decimal * decimal) -> [| CommonRuntime.ConvertStringBack(CommonRuntime.GetOptionalValue((let x, _, _ = row in x))); CommonRuntime.ConvertDecimalBack("", CommonRuntime.GetOptionalValue((let _, x, _ = row in x))); CommonRuntime.ConvertDecimalBack("", CommonRuntime.GetOptionalValue((let _, _, x = row in x))) |]), (ProviderFileSystem.readTextAtRunTimeWithDesignTimeOptions @"C:\Dev\FSharp.Data-master\src\..\tests\FSharp.Data.Tests\Data" "" "SmallTest.csv"), "", '"', true, false)).Cache()
""";;

test "
[<EntryPoint>]
let main argv =
Expand All @@ -35,17 +39,6 @@ let main argv =
0
";;

test "
type GetList() =
let switchvox_users_voicemail_getList_response = \"\"\"
</response>\"\"\"
let switchvox_users_voicemail_getList = \"\"\"
</request>\"\"\"
member self.X = switchvox_users_voicemail_getList_response
";;

test """
(new CsvFile<_>(new Func<_, _, _>(fun (parent : obj) (row : string[]) -> CommonRuntime.GetNonOptionalValue("Name", CommonRuntime.ConvertString(TextConversions.AsOption(row.[0])), TextConversions.AsOption(row.[0])), CommonRuntime.GetNonOptionalValue("Distance", CommonRuntime.ConvertDecimal("", TextConversions.AsOption(row.[1])), TextConversions.AsOption(row.[1])), CommonRuntime.GetNonOptionalValue("Time", CommonRuntime.ConvertDecimal("", TextConversions.AsOption(row.[2])), TextConversions.AsOption(row.[2]))), new Func<_, _>(fun (row : _ * _ * _) -> [| CommonRuntime.ConvertStringBack(CommonRuntime.GetOptionalValue((let x, _, _ = row in x))); CommonRuntime.ConvertDecimalBack("", CommonRuntime.GetOptionalValue((let _, x, _ = row in x))); CommonRuntime.ConvertDecimalBack("", CommonRuntime.GetOptionalValue((let _, _, x = row in x))) |]), (ProviderFileSystem.readTextAtRunTimeWithDesignTimeOptions @"C:\Dev\FSharp.Data-master\src\..\tests\FSharp.Data.Tests\Data" "" "SmallTest.csv"), "", '"', true, false)).Cache()
"""
Expand Down
32 changes: 21 additions & 11 deletions src/Fantomas/SourceParser.fs
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,32 @@ type Composite<'a, 'b> =
type Debug = Console
#endif

[<Literal>]
let maxLength = 512

/// Get source string content based on range value
let lookup (r : range) (c : Context) =
if r.EndLine <= c.Positions.Length then
if r.EndLine < c.Positions.Length then
let start = c.Positions.[r.StartLine-1] + r.StartColumn
let startLength = c.Positions.[r.StartLine] - c.Positions.[r.StartLine-1]
let finish = c.Positions.[r.EndLine-1] + r.EndColumn - 1
let finishLength = c.Positions.[r.EndLine] - c.Positions.[r.EndLine-1]
let content = c.Content
let s = content.[start..finish]
Debug.WriteLine("Content: {0} at start = {1}, finish = {2}", s, start, finish)
if s.Contains("\\\n") then
// Terrible hack to compensate the offset made by F# compiler
let last = content.[c.Positions.[r.EndLine-1]..finish]
let offset = min (last.Length - last.TrimStart(' ').Length) (content.Length - finish - 1)
Debug.WriteLine("Content after patch: {0} with offset = {1}", s, offset)
content.[start..finish + offset]
else s
else ""
// Any line with more than 512 characters isn't reliable for querying
if start > finish || startLength >= maxLength || finishLength >= maxLength then
Debug.WriteLine("Can't lookup between start = {0} and finish = {1}", start, finish)
None
else
let s = content.[start..finish]
Debug.WriteLine("Content: {0} at start = {1}, finish = {2}", s, start, finish)
if s.Contains("\\\n") then
// Terrible hack to compensate the offset made by F# compiler
let last = content.[c.Positions.[r.EndLine-1]..finish]
let offset = min (last.Length - last.TrimStart(' ').Length) (content.Length - finish - 1)
Debug.WriteLine("Content after patch: {0} with offset = {1}", s, offset)
Some content.[start..finish + offset]
else Some s
else None

let (|Ident|) (s : Ident) =
let ident = s.idText
Expand Down
6 changes: 3 additions & 3 deletions src/Fantomas/SourceTransformer.fs
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,12 @@ let genConst (Unresolved(c, r, s)) =
if ctx.Config.StrictMode then
str s ctx
else
let s' = lookup r' ctx
let s' = defaultArg (lookup r' ctx) s
str s' ctx

/// Check whether a range starting with a specified token
let startWith s (r : range) ctx =
(lookup r ctx).StartsWith(s)
let startWith prefix (r : range) ctx =
lookup r ctx |> Option.exists (fun s -> s.StartsWith(prefix))

// A few active patterns for printing purpose

Expand Down

0 comments on commit 1094078

Please sign in to comment.