Skip to content

Commit 1c4b2e0

Browse files
authored
Merge pull request #906 from Thorium/code-cleanup
Minor code clean-ups and performance improvements
2 parents b50b70e + c4f4e0d commit 1c4b2e0

File tree

19 files changed

+84
-69
lines changed

19 files changed

+84
-69
lines changed

src/Common/Collections.fs

Lines changed: 2 additions & 2 deletions
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

src/Common/StringParsing.fs

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,12 @@ 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
21+
ValueSome()
22+
else
23+
ValueNone
2024

2125
/// Returns a string trimmed from both start and end
2226
let (|TrimBoth|) (text: string) = text.Trim()
@@ -117,15 +121,20 @@ module String =
117121

118122
module StringPosition =
119123
/// Matches when a string is a whitespace or null
124+
[<return: Struct>]
120125
let (|WhiteSpace|_|) (s, _n: MarkdownRange) =
121-
if String.IsNullOrWhiteSpace(s) then Some() else None
126+
if String.IsNullOrWhiteSpace(s) then
127+
ValueSome()
128+
else
129+
ValueNone
122130

123131
/// Matches when a string does starts with non-whitespace
132+
[<return: Struct>]
124133
let (|Unindented|_|) (s: string, _n: MarkdownRange) =
125134
if not (String.IsNullOrWhiteSpace(s)) && s.TrimStart() = s then
126-
Some()
135+
ValueSome()
127136
else
128-
None
137+
ValueNone
129138

130139
/// Returns a string trimmed from both start and end
131140
let (|TrimBoth|) (text: string, n: MarkdownRange) =
@@ -174,11 +183,12 @@ module StringPosition =
174183
StartColumn = n.StartColumn + text.Length - trimmed.Length })
175184

176185
/// Matches when a string starts with any of the specified sub-strings
186+
[<return: Struct>]
177187
let (|StartsWithAny|_|) (starts: string seq) (text: string, _n: MarkdownRange) =
178188
if starts |> Seq.exists (fun s -> text.StartsWith(s, StringComparison.Ordinal)) then
179-
Some()
189+
ValueSome()
180190
else
181-
None
191+
ValueNone
182192

183193
/// Matches when a string starts with the specified sub-string
184194
let (|StartsWith|_|) (start: string) (text: string, n: MarkdownRange) =
@@ -297,15 +307,16 @@ module StringPosition =
297307

298308
/// Matches when a string consists of some number of
299309
/// complete repetitions of a specified sub-string.
310+
[<return: Struct>]
300311
let (|EqualsRepeated|_|) (repeated, _n: MarkdownRange) =
301312
function
302-
| StartsWithRepeated repeated (_n, (v, _)) when (String.IsNullOrWhiteSpace v) -> Some()
303-
| _ -> None
313+
| StartsWithRepeated repeated (_n, (v, _)) when (String.IsNullOrWhiteSpace v) -> ValueSome()
314+
| _ -> ValueNone
304315

305316
module List =
306317
/// Matches a list if it starts with a sub-list that is delimited
307318
/// using the specified delimiters. Returns a wrapped list and the rest.
308-
let inline (|DelimitedWith|_|) startl endl input =
319+
let inline internal (|DelimitedWith|_|) startl endl input =
309320
if List.startsWith startl input then
310321
match List.partitionUntilEquals endl (List.skip startl.Length input) with
311322
| Some(pre, post) -> Some(pre, List.skip endl.Length post, startl.Length, endl.Length)
@@ -314,14 +325,14 @@ module List =
314325
None
315326

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

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

324-
let inline (|DelimitedNTimes|_|) str input =
335+
let inline internal (|DelimitedNTimes|_|) str input =
325336
let strs, _items = List.partitionWhile (fun i -> i = str) input
326337

327338
match strs with
@@ -403,9 +414,8 @@ module Lines =
403414
let (|TrimParagraphLines|) lines =
404415
lines
405416
// 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) ->
417+
// then remove all additional spaces at the end, but keep two spaces if existent
418+
|> List.map (fun (StringPosition.TrimStart(s, n)) ->
409419
let endsWithTwoSpaces = s.EndsWith(" ", StringComparison.Ordinal)
410420

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

src/FSharp.Formatting.ApiDocs/Categorise.fs

Lines changed: 2 additions & 2 deletions
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 (

src/FSharp.Formatting.ApiDocs/GenerateModel.fs

Lines changed: 11 additions & 10 deletions
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

src/FSharp.Formatting.CodeFormat/CodeFormatAgent.fs

Lines changed: 1 addition & 1 deletion
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)

src/FSharp.Formatting.CodeFormat/SourceCode.fs

Lines changed: 2 additions & 2 deletions
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

src/FSharp.Formatting.Common/PynbModel.fs

Lines changed: 1 addition & 2 deletions
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 =

src/FSharp.Formatting.Common/Templating.fs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -280,14 +280,12 @@ 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 -> "\n\n" + lookupTips
288+
| false, _ -> "")
291289
| Some templateText -> ApplySubstitutionsInText substitutions templateText
292290

293291
let UseFileAsSimpleTemplate (substitutions, templateOpt, outputFile) =

src/FSharp.Formatting.Common/YaafFSharpScripting.fs

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

254253
let getProjectReferencesSimple frameworkVersion (dllFiles: string list) =
255254
getProjectReferences frameworkVersion None None dllFiles |> resolve dllFiles
@@ -464,26 +463,29 @@ module internal ArgParser =
464463
else
465464
None
466465

466+
[<return: Struct>]
467467
let (|FsiBoolArg|_|) argName s =
468468
match s with
469469
| StartsWith argName rest ->
470470
if String.IsNullOrWhiteSpace rest then
471-
Some true
471+
ValueSome true
472472
else
473473
match rest with
474-
| "+" -> Some true
475-
| "-" -> Some false
476-
| _ -> None
477-
| _ -> None
474+
| "+" -> ValueSome true
475+
| "-" -> ValueSome false
476+
| _ -> ValueNone
477+
| _ -> ValueNone
478478

479479
open ArgParser
480480

481+
[<Struct>]
481482
type internal DebugMode =
482483
| Full
483484
| PdbOnly
484485
| Portable
485486
| NoDebug
486487

488+
[<Struct>]
487489
type internal OptimizationType =
488490
| NoJitOptimize
489491
| NoJitTracking
@@ -761,7 +763,7 @@ type internal FsiOptions =
761763
| [] -> Seq.empty
762764
| opts ->
763765
opts
764-
|> Seq.map (fun (enable, types) ->
766+
|> Seq.collect (fun (enable, types) ->
765767
seq {
766768
yield sprintf "--optimize%s" (getMinusPlus enable)
767769

@@ -778,7 +780,6 @@ type internal FsiOptions =
778780
| NoTailCalls -> "notailcalls")
779781
|> String.concat ","
780782
})
781-
|> Seq.concat
782783

783784
yield! getSimpleBoolArg "--quiet" x.Quiet
784785
yield! getSimpleBoolArg "--quotations-debug" x.QuotationsDebug

src/FSharp.Formatting.Literate/Contexts.fs

Lines changed: 1 addition & 1 deletion
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

0 commit comments

Comments
 (0)