Skip to content

Commit 1094078

Browse files
committed
Skip querying for any line with >= 512 characters
until consistencies in range values are fixed. Fix #83. Fix #85.
1 parent de5be4a commit 1094078

File tree

4 files changed

+49
-26
lines changed

4 files changed

+49
-26
lines changed

src/Fantomas.Tests/TypeProviderTests.fs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,24 @@ type IntegerRegex = FSharpx.Regex< @"(?<value>\d+)" >
2828
let ``should throw FormatException on unparsed input``() =
2929
formatSourceString false """
3030
type GeoResults = JsonProvider<Sample= "A" + "GitHub.json" >""" config
31-
|> ignore
31+
|> ignore
32+
33+
[<Test>]
34+
let ``should handle lines with more than 512 characters``() =
35+
formatSourceString false """
36+
(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()
37+
""" config
38+
|> prepend newline
39+
|> should equal """
40+
(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]))),
41+
new Func<string * decimal * decimal, string []>(fun (row : string * decimal * decimal) ->
42+
[| CommonRuntime.ConvertStringBack(CommonRuntime.GetOptionalValue((let x, _, _ = row
43+
x)))
44+
CommonRuntime.ConvertDecimalBack("",
45+
CommonRuntime.GetOptionalValue((let _, x, _ = row
46+
x)))
47+
CommonRuntime.ConvertDecimalBack("",
48+
CommonRuntime.GetOptionalValue((let _, _, x = row
49+
x))) |]), (ProviderFileSystem.readTextAtRunTimeWithDesignTimeOptions @"C:\Dev\FSharp.Data-master\src\..\tests\FSharp.Data.Tests\Data" "" "SmallTest.csv"), "", '"', true, false))
50+
.Cache()
51+
"""

src/Fantomas/CodeFormatter.fsx

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ let test (s : string) =
2121
fsi.AddPrinter (fun (p : Microsoft.FSharp.Compiler.Range.pos) -> p.ToString())
2222
fsi.AddPrinter (fun (r : Microsoft.FSharp.Compiler.Range.range) -> r.ToString())
2323

24+
test """
25+
(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()
26+
""";;
27+
2428
test "
2529
[<EntryPoint>]
2630
let main argv =
@@ -35,17 +39,6 @@ let main argv =
3539
0
3640
";;
3741

38-
test "
39-
type GetList() =
40-
let switchvox_users_voicemail_getList_response = \"\"\"
41-
</response>\"\"\"
42-
43-
let switchvox_users_voicemail_getList = \"\"\"
44-
</request>\"\"\"
45-
46-
member self.X = switchvox_users_voicemail_getList_response
47-
";;
48-
4942
test """
5043
(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()
5144
"""

src/Fantomas/SourceParser.fs

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,32 @@ type Composite<'a, 'b> =
1717
type Debug = Console
1818
#endif
1919

20+
[<Literal>]
21+
let maxLength = 512
22+
2023
/// Get source string content based on range value
2124
let lookup (r : range) (c : Context) =
22-
if r.EndLine <= c.Positions.Length then
25+
if r.EndLine < c.Positions.Length then
2326
let start = c.Positions.[r.StartLine-1] + r.StartColumn
27+
let startLength = c.Positions.[r.StartLine] - c.Positions.[r.StartLine-1]
2428
let finish = c.Positions.[r.EndLine-1] + r.EndColumn - 1
29+
let finishLength = c.Positions.[r.EndLine] - c.Positions.[r.EndLine-1]
2530
let content = c.Content
26-
let s = content.[start..finish]
27-
Debug.WriteLine("Content: {0} at start = {1}, finish = {2}", s, start, finish)
28-
if s.Contains("\\\n") then
29-
// Terrible hack to compensate the offset made by F# compiler
30-
let last = content.[c.Positions.[r.EndLine-1]..finish]
31-
let offset = min (last.Length - last.TrimStart(' ').Length) (content.Length - finish - 1)
32-
Debug.WriteLine("Content after patch: {0} with offset = {1}", s, offset)
33-
content.[start..finish + offset]
34-
else s
35-
else ""
31+
// Any line with more than 512 characters isn't reliable for querying
32+
if start > finish || startLength >= maxLength || finishLength >= maxLength then
33+
Debug.WriteLine("Can't lookup between start = {0} and finish = {1}", start, finish)
34+
None
35+
else
36+
let s = content.[start..finish]
37+
Debug.WriteLine("Content: {0} at start = {1}, finish = {2}", s, start, finish)
38+
if s.Contains("\\\n") then
39+
// Terrible hack to compensate the offset made by F# compiler
40+
let last = content.[c.Positions.[r.EndLine-1]..finish]
41+
let offset = min (last.Length - last.TrimStart(' ').Length) (content.Length - finish - 1)
42+
Debug.WriteLine("Content after patch: {0} with offset = {1}", s, offset)
43+
Some content.[start..finish + offset]
44+
else Some s
45+
else None
3646

3747
let (|Ident|) (s : Ident) =
3848
let ident = s.idText

src/Fantomas/SourceTransformer.fs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,12 +103,12 @@ let genConst (Unresolved(c, r, s)) =
103103
if ctx.Config.StrictMode then
104104
str s ctx
105105
else
106-
let s' = lookup r' ctx
106+
let s' = defaultArg (lookup r' ctx) s
107107
str s' ctx
108108

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

113113
// A few active patterns for printing purpose
114114

0 commit comments

Comments
 (0)