Skip to content

Commit 495bc37

Browse files
committed
Minor code clean-ups and performance improvements
1 parent 0a310f2 commit 495bc37

File tree

19 files changed

+79
-67
lines changed

19 files changed

+79
-67
lines changed

Diff for: src/Common/Collections.fs

+2-2
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ module internal List =
4949
f x
5050
g ()
5151
iterInterleaved f g (y :: tl)
52-
| x :: [] -> f x
52+
| [x] -> f x
5353
| [] -> ()
5454

5555
/// Tests whether a list starts with the elements of another
@@ -90,7 +90,7 @@ module internal List =
9090
let other, rest = partitionUntil (f >> not) other
9191
yield last, other
9292
yield! loop rest
93-
| [] when other = [] -> ()
93+
| [] when List.isEmpty other -> ()
9494
| _ -> invalidArg "" "Should start with true"
9595
}
9696

Diff for: src/Common/StringParsing.fs

+19-15
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@ open FSharp.Formatting.Markdown
1515

1616
module String =
1717
/// Matches when a string is a whitespace or null
18+
[<return: Struct>]
1819
let (|WhiteSpace|_|) (s) =
19-
if String.IsNullOrWhiteSpace(s) then Some() else None
20+
if String.IsNullOrWhiteSpace(s) then ValueSome() else ValueNone
2021

2122
/// Returns a string trimmed from both start and end
2223
let (|TrimBoth|) (text: string) = text.Trim()
@@ -117,15 +118,17 @@ module String =
117118

118119
module StringPosition =
119120
/// Matches when a string is a whitespace or null
121+
[<return:Struct>]
120122
let (|WhiteSpace|_|) (s, _n: MarkdownRange) =
121-
if String.IsNullOrWhiteSpace(s) then Some() else None
123+
if String.IsNullOrWhiteSpace(s) then ValueSome() else ValueNone
122124

123125
/// Matches when a string does starts with non-whitespace
126+
[<return:Struct>]
124127
let (|Unindented|_|) (s: string, _n: MarkdownRange) =
125128
if not (String.IsNullOrWhiteSpace(s)) && s.TrimStart() = s then
126-
Some()
129+
ValueSome()
127130
else
128-
None
131+
ValueNone
129132

130133
/// Returns a string trimmed from both start and end
131134
let (|TrimBoth|) (text: string, n: MarkdownRange) =
@@ -174,11 +177,12 @@ module StringPosition =
174177
StartColumn = n.StartColumn + text.Length - trimmed.Length })
175178

176179
/// Matches when a string starts with any of the specified sub-strings
180+
[<return:Struct>]
177181
let (|StartsWithAny|_|) (starts: string seq) (text: string, _n: MarkdownRange) =
178182
if starts |> Seq.exists (fun s -> text.StartsWith(s, StringComparison.Ordinal)) then
179-
Some()
183+
ValueSome()
180184
else
181-
None
185+
ValueNone
182186

183187
/// Matches when a string starts with the specified sub-string
184188
let (|StartsWith|_|) (start: string) (text: string, n: MarkdownRange) =
@@ -297,15 +301,16 @@ module StringPosition =
297301

298302
/// Matches when a string consists of some number of
299303
/// complete repetitions of a specified sub-string.
304+
[<return:Struct>]
300305
let (|EqualsRepeated|_|) (repeated, _n: MarkdownRange) =
301306
function
302-
| StartsWithRepeated repeated (_n, (v, _)) when (String.IsNullOrWhiteSpace v) -> Some()
303-
| _ -> None
307+
| StartsWithRepeated repeated (_n, (v, _)) when (String.IsNullOrWhiteSpace v) -> ValueSome()
308+
| _ -> ValueNone
304309

305310
module List =
306311
/// Matches a list if it starts with a sub-list that is delimited
307312
/// using the specified delimiters. Returns a wrapped list and the rest.
308-
let inline (|DelimitedWith|_|) startl endl input =
313+
let inline internal (|DelimitedWith|_|) startl endl input =
309314
if List.startsWith startl input then
310315
match List.partitionUntilEquals endl (List.skip startl.Length input) with
311316
| Some(pre, post) -> Some(pre, List.skip endl.Length post, startl.Length, endl.Length)
@@ -314,14 +319,14 @@ module List =
314319
None
315320

316321
/// Matches a list if it starts with a sub-list. Returns the list.
317-
let inline (|StartsWith|_|) startl input =
322+
let inline internal (|StartsWith|_|) startl input =
318323
if List.startsWith startl input then Some input else None
319324

320325
/// Matches a list if it starts with a sub-list that is delimited
321326
/// using the specified delimiter. Returns a wrapped list and the rest.
322-
let inline (|Delimited|_|) str = (|DelimitedWith|_|) str str
327+
let inline internal (|Delimited|_|) str = (|DelimitedWith|_|) str str
323328

324-
let inline (|DelimitedNTimes|_|) str input =
329+
let inline internal (|DelimitedNTimes|_|) str input =
325330
let strs, _items = List.partitionWhile (fun i -> i = str) input
326331

327332
match strs with
@@ -403,9 +408,8 @@ module Lines =
403408
let (|TrimParagraphLines|) lines =
404409
lines
405410
// first remove all whitespace on the beginning of the line
406-
|> List.map (fun (StringPosition.TrimStart s) -> s)
407-
// Now remove all additional spaces at the end, but keep two spaces if existent
408-
|> List.map (fun (s, n) ->
411+
// then remove all additional spaces at the end, but keep two spaces if existent
412+
|> List.map (fun (StringPosition.TrimStart (s, n)) ->
409413
let endsWithTwoSpaces = s.EndsWith(" ", StringComparison.Ordinal)
410414

411415
let trimmed = s.TrimEnd([| ' ' |]) + if endsWithTwoSpaces then " " else ""

Diff for: src/FSharp.Formatting.ApiDocs/Categorise.fs

+2-2
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,9 @@ let entities (nsIndex: int, ns: ApiDocNamespace, suppress) =
5959
//
6060
// See https://github.com/fsharp/fsharp-core-docs/issues/57, we may rethink this
6161
|> List.filter (fun e ->
62-
not (e.Symbol.Namespace = Some "Microsoft.FSharp.Data.UnitSystems.SI.UnitSymbols"))
62+
(e.Symbol.Namespace <> Some "Microsoft.FSharp.Data.UnitSystems.SI.UnitSymbols"))
6363
|> List.filter (fun e ->
64-
not (e.Symbol.Namespace = Some "Microsoft.FSharp.Data.UnitSystems.SI.UnitNames"))
64+
(e.Symbol.Namespace <> Some "Microsoft.FSharp.Data.UnitSystems.SI.UnitNames"))
6565
// Don't show 'AnonymousObject' in list-of-namespaces navigation
6666
|> List.filter (fun e ->
6767
not (

Diff for: src/FSharp.Formatting.ApiDocs/GenerateModel.fs

+11-10
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,16 @@ module internal Utils =
6262
else
6363
None
6464

65+
[<return:Struct>]
6566
let (|MeasureOne|_|) (typ: FSharpType) =
6667
if
6768
typ.HasTypeDefinition
6869
&& typ.TypeDefinition.LogicalName = "1"
6970
&& typ.GenericArguments.Count = 0
7071
then
71-
Some()
72+
ValueSome()
7273
else
73-
None
74+
ValueNone
7475

7576
let tryGetLocation (symbol: FSharpSymbol) =
7677
match symbol.ImplementationLocation with
@@ -105,7 +106,7 @@ module internal Utils =
105106
member x.TryAttr(attr: string) =
106107
let a = x.Attribute(XName.Get attr)
107108

108-
if a = null then None
109+
if isNull a then None
109110
else if String.IsNullOrEmpty a.Value then None
110111
else Some a.Value
111112

@@ -1757,7 +1758,7 @@ module internal SymbolReader =
17571758
[ let mutable line = ""
17581759

17591760
while (line <- reader.ReadLine()
1760-
line <> null) do
1761+
not(isNull line)) do
17611762
yield line ]
17621763

17631764
String.removeSpaces lines
@@ -1905,14 +1906,14 @@ module internal SymbolReader =
19051906
let name = elem.Attribute(XName.Get "name")
19061907
let nameAsHtml = HttpUtility.HtmlEncode name.Value
19071908

1908-
if name <> null then
1909+
if not(isNull name) then
19091910
html.AppendFormat("<span class=\"fsdocs-param-name\">{0}</span>", nameAsHtml)
19101911
|> ignore
19111912
| "see"
19121913
| "seealso" ->
19131914
let cref = elem.Attribute(XName.Get "cref")
19141915

1915-
if cref <> null then
1916+
if not(isNull cref) then
19161917
if System.String.IsNullOrEmpty(cref.Value) || cref.Value.Length < 3 then
19171918
printfn "ignoring invalid cref specified in: %A" e
19181919

@@ -2018,7 +2019,7 @@ module internal SymbolReader =
20182019
let remarks =
20192020
let remarkNodes = doc.Elements(XName.Get "remarks") |> Seq.toList
20202021

2021-
if List.length remarkNodes > 0 then
2022+
if not(List.isEmpty remarkNodes) then
20222023
let html = new StringBuilder()
20232024

20242025
for (id, e) in List.indexed remarkNodes do
@@ -2053,7 +2054,7 @@ module internal SymbolReader =
20532054
[ for e in exceptionNodes do
20542055
let cref = e.Attribute(XName.Get "cref")
20552056

2056-
if cref <> null then
2057+
if not(isNull cref) then
20572058
if String.IsNullOrEmpty(cref.Value) || cref.Value.Length < 3 then
20582059
printfn "Warning: Invalid cref specified in: %A" doc
20592060

@@ -2581,7 +2582,7 @@ module internal SymbolReader =
25812582
|> Seq.choose (fun p ->
25822583
let nameAttr = p.Attribute(XName.Get "name")
25832584

2584-
if nameAttr = null then
2585+
if isNull nameAttr then
25852586
None
25862587
else
25872588
Some(nameAttr.Value, p.Value))
@@ -2821,7 +2822,7 @@ module internal SymbolReader =
28212822
[ for e in doc.Descendants(XName.Get "member") do
28222823
let attr = e.Attribute(XName.Get "name")
28232824

2824-
if attr <> null && not (String.IsNullOrEmpty(attr.Value)) then
2825+
if (not(isNull attr)) && not (String.IsNullOrEmpty(attr.Value)) then
28252826
yield attr.Value, e ] do
28262827
// NOTE: We completely ignore duplicate keys and I don't see
28272828
// an easy way to detect where "value" is coming from, because the entries

Diff for: src/FSharp.Formatting.CodeFormat/CodeFormatAgent.fs

+1-1
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ module CodeFormatter =
352352
[| let line = ref ""
353353

354354
while (line := reader.ReadLine()
355-
line.Value <> null) do
355+
not(isNull line.Value)) do
356356
yield line.Value |]
357357
// Get options for a standalone script file (this adds some
358358
// default references and doesn't require full project information)

Diff for: src/FSharp.Formatting.CodeFormat/SourceCode.fs

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ type ToolTipSpan =
2020
| HardLineBreak
2121

2222
/// Classifies tokens reported by the FCS
23-
[<RequireQualifiedAccess>]
23+
[<RequireQualifiedAccess; Struct>]
2424
type TokenKind =
2525
| Keyword
2626
| String
@@ -49,7 +49,7 @@ type TokenKind =
4949

5050

5151
/// Represents a kind of error reported from the F# compiler (warning or error)
52-
[<RequireQualifiedAccess>]
52+
[<RequireQualifiedAccess; Struct>]
5353
type ErrorKind =
5454
| Error
5555
| Warning

Diff for: src/FSharp.Formatting.Common/PynbModel.fs

+1-2
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,7 @@ type Cell =
8888
| Some(x) -> string<int> x)
8989
(this.outputs |> Array.map string<Output> |> String.concat ",\n")))
9090
(this.source
91-
|> Array.map addLineEnd
92-
|> Array.map escapeAndQuote
91+
|> Array.map (addLineEnd >> escapeAndQuote)
9392
|> String.concat ",\n ")
9493

9594
type Kernelspec =

Diff for: src/FSharp.Formatting.Common/Templating.fs

+7-8
Original file line numberDiff line numberDiff line change
@@ -280,14 +280,13 @@ module internal SimpleTemplating =
280280
// If there is no template or the template is an empty file, return just document + tooltips (tooltips empty if not HTML)
281281
let lookup = readOnlyDict substitutions
282282

283-
(if lookup.ContainsKey ParamKeys.``fsdocs-content`` then
284-
lookup.[ParamKeys.``fsdocs-content``]
285-
else
286-
"")
287-
+ (if lookup.ContainsKey ParamKeys.``fsdocs-tooltips`` then
288-
"\n\n" + lookup.[ParamKeys.``fsdocs-tooltips``]
289-
else
290-
"")
283+
(match lookup.TryGetValue ParamKeys.``fsdocs-content`` with
284+
| true, lookupContent -> lookupContent
285+
| false, _ -> "")
286+
+ (match lookup.TryGetValue ParamKeys.``fsdocs-tooltips`` with
287+
| true, lookupTips ->
288+
"\n\n" + lookupTips
289+
| false, _ -> "")
291290
| Some templateText -> ApplySubstitutionsInText substitutions templateText
292291

293292
let UseFileAsSimpleTemplate (substitutions, templateOpt, outputFile) =

Diff for: src/FSharp.Formatting.Common/YaafFSharpScripting.fs

+13-10
Original file line numberDiff line numberDiff line change
@@ -246,9 +246,10 @@ module internal CompilerServiceExtensions =
246246
dllFiles
247247
|> List.map (fun file ->
248248
file,
249-
(if referenceDict.ContainsKey file then
250-
Some referenceDict.[file]
251-
else
249+
(match referenceDict.TryGetValue file with
250+
| true, refFile ->
251+
Some refFile
252+
| false, _ ->
252253
None))
253254

254255
let getProjectReferencesSimple frameworkVersion (dllFiles: string list) =
@@ -464,26 +465,29 @@ module internal ArgParser =
464465
else
465466
None
466467

468+
[<return:Struct>]
467469
let (|FsiBoolArg|_|) argName s =
468470
match s with
469471
| StartsWith argName rest ->
470472
if String.IsNullOrWhiteSpace rest then
471-
Some true
473+
ValueSome true
472474
else
473475
match rest with
474-
| "+" -> Some true
475-
| "-" -> Some false
476-
| _ -> None
477-
| _ -> None
476+
| "+" -> ValueSome true
477+
| "-" -> ValueSome false
478+
| _ -> ValueNone
479+
| _ -> ValueNone
478480

479481
open ArgParser
480482

483+
[<Struct>]
481484
type internal DebugMode =
482485
| Full
483486
| PdbOnly
484487
| Portable
485488
| NoDebug
486489

490+
[<Struct>]
487491
type internal OptimizationType =
488492
| NoJitOptimize
489493
| NoJitTracking
@@ -761,7 +765,7 @@ type internal FsiOptions =
761765
| [] -> Seq.empty
762766
| opts ->
763767
opts
764-
|> Seq.map (fun (enable, types) ->
768+
|> Seq.collect (fun (enable, types) ->
765769
seq {
766770
yield sprintf "--optimize%s" (getMinusPlus enable)
767771

@@ -778,7 +782,6 @@ type internal FsiOptions =
778782
| NoTailCalls -> "notailcalls")
779783
|> String.concat ","
780784
})
781-
|> Seq.concat
782785

783786
yield! getSimpleBoolArg "--quiet" x.Quiet
784787
yield! getSimpleBoolArg "--quotations-debug" x.QuotationsDebug

Diff for: src/FSharp.Formatting.Literate/Contexts.fs

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ type internal CompilerContext =
2323
}
2424

2525
/// Defines the possible output types from literate script (HTML, Latex, Pynb)
26-
[<RequireQualifiedAccess>]
26+
[<RequireQualifiedAccess; Struct>]
2727
type OutputKind =
2828
/// Requests HTML output
2929
| Html

Diff for: src/FSharp.Formatting.Literate/Evaluator.fs

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ open FSharp.Formatting.Internal
1515
/// <namespacedoc>
1616
/// <summary>Functionality to support literate evaluation for F# scripts</summary>
1717
/// </namespacedoc>
18-
[<RequireQualifiedAccessAttribute>]
18+
[<RequireQualifiedAccessAttribute; Struct>]
1919
type FsiEmbedKind =
2020
/// The FSI output
2121
| FsiOutput

Diff for: src/FSharp.Formatting.Literate/ParseScript.fs

+3-2
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,8 @@ type internal ParseScript(parseOptions, ctx: CompilerContext) =
163163
| Command "condition" name when not (String.IsNullOrWhiteSpace name) -> { Condition = Some name }
164164
| _ -> { Condition = None }
165165

166-
let (|EmptyString|_|) (v: string) = if v.Length = 0 then Some() else None
166+
[<return:Struct>]
167+
let (|EmptyString|_|) (v: string) = if v.Length = 0 then ValueSome() else ValueNone
167168

168169
/// Transform list of code blocks (snippet/comment/command)
169170
/// into a formatted Markdown document, with link definitions
@@ -386,7 +387,7 @@ type internal ParseScript(parseOptions, ctx: CompilerContext) =
386387

387388
let parsedBlocks =
388389
[ for Snippet(name, lines) in sourceSnippets do
389-
if name <> null then
390+
if not(isNull name) then
390391
yield BlockComment("## " + name)
391392

392393
yield! parseScriptFile (lines) ]

0 commit comments

Comments
 (0)