Skip to content

Commit 482a8df

Browse files
authored
Minor performance improvements / code cleanup (#1424)
* Minor performance improvements / code cleanup * Fantomas commit
1 parent 1e94ea0 commit 482a8df

File tree

11 files changed

+46
-29
lines changed

11 files changed

+46
-29
lines changed

src/FsAutoComplete.Core/CodeGeneration.fs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -543,7 +543,7 @@ module CodeGenerationUtils =
543543
m.IsDispatchSlot
544544
// this member doesn't implement anything
545545
&& (try
546-
m.ImplementedAbstractSignatures <> null
546+
(not (isNull m.ImplementedAbstractSignatures))
547547
&& m.ImplementedAbstractSignatures.Count = 0
548548
with _ ->
549549
true) // exceptions here trying to access the member means we're safe
@@ -672,7 +672,7 @@ module CodeGenerationUtils =
672672
// This rule match when we are at the end of the namespace and there is no more tokens
673673
// interface System.Collections.ICollection
674674
// ^
675-
| potentialDot :: validIdentifier :: [] when
675+
| potentialDot :: [ validIdentifier ] when
676676
potentialDot.CharClass = FSharpTokenCharKind.Delimiter
677677
&& potentialDot.TokenName = "DOT"
678678
&& validIdentifier.CharClass = FSharpTokenCharKind.Identifier

src/FsAutoComplete.Core/Commands.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -728,7 +728,7 @@ module Commands =
728728
Position.mkPos l indentation
729729
else
730730
pos
731-
| Pos(_, _) -> pos
731+
| Pos _ -> pos
732732

733733
{ Namespace = n
734734
Position = pos

src/FsAutoComplete.Core/CompilerServiceInterface.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ type FSharpCompilerServiceChecker
267267
|> Seq.distinctBy (fun o -> o.ProjectFileName)
268268
|> Seq.filter (fun o ->
269269
o.ReferencedProjectsPath
270-
|> List.map (fun p -> Path.GetFullPath p)
270+
|> List.map Path.GetFullPath
271271
|> List.contains option.ProjectFileName) ])
272272

273273
member private __.GetNetFxScriptSnapshot(file: string<LocalPath>, source) =

src/FsAutoComplete.Core/DocumentationFormatter.fs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ module DocumentationFormatter =
5050

5151
$"<a href='command:fsharp.showDocumentation?%s{content}'>%s{name}</a>", name.Length
5252

53+
let tag = Regex """<.*>"""
54+
5355
let rec formatType (displayContext: FSharpDisplayContext) (typ: FSharpType) : string * int =
5456
let combineParts (parts: (string * int) seq) : string * int =
5557
// make a single type name out of all of the tuple parts, since each part is correct by construction
@@ -101,7 +103,7 @@ module DocumentationFormatter =
101103
// we set this context specifically because we want to enforce prefix-generic form on tooltip displays
102104
let newContext = displayContext.WithPrefixGenericParameters()
103105
let org = typ.Format newContext
104-
let t = Regex.Replace(org, """<.*>""", "<")
106+
let t = tag.Replace(org, "<")
105107

106108
[ yield formatShowDocumentationLink t xmlDocSig assemblyName
107109
if t.EndsWith("<", StringComparison.Ordinal) then

src/FsAutoComplete.Core/FCSPatches.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ module internal SynExprAppLocationsImpl =
2020
let res = loop exprs ranges
2121
Some(res), None
2222

23-
| SynExpr.Paren(SynExpr.Paren(_, _, _, _) as synExpr, _, _, _parenRange) ->
23+
| SynExpr.Paren(SynExpr.Paren _ as synExpr, _, _, _parenRange) ->
2424
let r, _cacheOpt = searchSynArgExpr traverseSynExpr synExpr ranges
2525
r, None
2626

src/FsAutoComplete.Core/Fsdn.fs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,7 @@ let query (queryStr: string) =
5050
"language", "fsharp"
5151
"single_letter_as_variable", "enabled"
5252
"limit", "50" ]
53-
|> List.map (fun (k, v) -> k, System.Uri.EscapeDataString(v))
54-
|> List.map (fun (k, v) -> sprintf "%s=%s" k v)
53+
|> List.map (fun (k, v) -> sprintf "%s=%s" k (System.Uri.EscapeDataString v))
5554
|> String.concat "&"
5655

5756
let! req =

src/FsAutoComplete.Core/TipFormatter.fs

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ module private Section =
3333
content
3434
|> Seq.map (fun kv ->
3535
let text =
36-
if kv.Value.Contains("\n") then
36+
if kv.Value.Contains '\n' then
3737
kv.Value.Split('\n')
3838
|> Seq.map (fun line -> "> " + line.TrimStart())
3939
|> String.concat Environment.NewLine
@@ -453,6 +453,8 @@ module private Format =
453453
text
454454

455455

456+
let private thsPattern = Regex "<th\s?>"
457+
456458
let private convertTable =
457459
{ TagName = "table"
458460
Formatter =
@@ -461,7 +463,7 @@ module private Format =
461463

462464
| NonVoidElement(innerText, _) ->
463465

464-
let rowCount = Regex.Matches(innerText, "<th\s?>").Count
466+
let rowCount = thsPattern.Matches(innerText).Count
465467

466468
let convertedTable =
467469
innerText
@@ -488,6 +490,7 @@ module private Format =
488490
type private Term = string
489491
type private Definition = string
490492

493+
[<Struct>]
491494
type private ListStyle =
492495
| Bulleted
493496
| Numbered
@@ -535,8 +538,10 @@ module private Format =
535538

536539
let tryGetTerm (text: string) = tryGetInnerTextOnNonVoidElement text "term"
537540

541+
let itmPattern = Regex(tagPattern "item", RegexOptions.IgnoreCase)
542+
538543
let rec extractItemList (res: ItemList list) (text: string) =
539-
match Regex.Match(text, tagPattern "item", RegexOptions.IgnoreCase) with
544+
match itmPattern.Match text with
540545
| m when m.Success ->
541546
let newText = text.Substring(m.Value.Length)
542547

@@ -559,8 +564,10 @@ module private Format =
559564
extractItemList res newText
560565
| _ -> res
561566

567+
let listHeader = Regex(tagPattern "listheader", RegexOptions.IgnoreCase)
568+
562569
let rec extractColumnHeader (res: string list) (text: string) =
563-
match Regex.Match(text, tagPattern "listheader", RegexOptions.IgnoreCase) with
570+
match listHeader.Match text with
564571
| m when m.Success ->
565572
let newText = text.Substring(m.Value.Length)
566573

@@ -580,9 +587,10 @@ module private Format =
580587
extractColumnHeader res newText
581588
| _ -> res
582589

590+
let itemPattern = Regex(tagPattern "item", RegexOptions.IgnoreCase)
583591

584592
let rec extractRowsForTable (res: (string list) list) (text: string) =
585-
match Regex.Match(text, tagPattern "item", RegexOptions.IgnoreCase) with
593+
match itemPattern.Match text with
586594
| m when m.Success ->
587595
let newText = text.Substring(m.Value.Length)
588596

@@ -707,7 +715,7 @@ module private Format =
707715
|> handleMicrosoftOrList
708716
|> unescapeSpecialCharacters
709717

710-
[<RequireQualifiedAccess>]
718+
[<RequireQualifiedAccess; Struct>]
711719
type FormatCommentStyle =
712720
| Legacy
713721
| FullEnhanced
@@ -907,9 +915,11 @@ let private findLocalizedXmlFile (xmlFile: string) =
907915

908916
findCultures System.Globalization.CultureInfo.CurrentUICulture
909917
|> List.map (fun culture -> Path.Combine(path, culture, xmlName))
910-
|> List.tryFind (fun i -> File.Exists i)
918+
|> List.tryFind File.Exists
911919
|> Option.defaultValue xmlFile
912920

921+
let pPattern = Regex """(<p .*?>)+(.*)(<\/?p>)*"""
922+
913923
let private getXmlDoc dllFile =
914924
let xmlFile = Path.ChangeExtension(dllFile, ".xml")
915925
//Workaround for netstandard.dll
@@ -925,9 +935,9 @@ let private getXmlDoc dllFile =
925935

926936
let xmlFile = findLocalizedXmlFile xmlFile
927937

928-
if xmlDocCache.ContainsKey xmlFile then
929-
Some xmlDocCache.[xmlFile]
930-
else
938+
match xmlDocCache.TryGetValue xmlFile with
939+
| true, cachedXmlFile -> Some cachedXmlFile
940+
| false, _ ->
931941
let rec exists filePath tryAgain =
932942
match File.Exists filePath, tryAgain with
933943
| true, _ -> Some filePath
@@ -948,7 +958,7 @@ let private getXmlDoc dllFile =
948958
//Workaround for netstandard xmlDoc
949959
let cnt =
950960
if actualXmlFile.Contains "netstandard.xml" then
951-
let cnt = Regex.Replace(cnt, """(<p .*?>)+(.*)(<\/?p>)*""", "$2")
961+
let cnt = pPattern.Replace(cnt, "$2")
952962

953963
cnt.Replace("<p>", "").Replace("</p>", "").Replace("<br>", "")
954964
else
@@ -995,7 +1005,7 @@ let private tryGetXmlDocMember (xmlDoc: FSharpXmlDoc) =
9951005
let rec findIndentationSize (lines: string list) =
9961006
match lines with
9971007
| head :: tail ->
998-
let lesserThanIndex = head.IndexOf("<", StringComparison.Ordinal)
1008+
let lesserThanIndex = head.IndexOf('<', StringComparison.Ordinal)
9991009

10001010
if lesserThanIndex <> -1 then
10011011
lesserThanIndex
@@ -1012,8 +1022,10 @@ let private tryGetXmlDocMember (xmlDoc: FSharpXmlDoc) =
10121022

10131023
| FSharpXmlDoc.FromXmlFile(dllFile, memberName) ->
10141024
match getXmlDoc dllFile with
1015-
| Some doc when doc.ContainsKey memberName -> TryGetXmlDocMemberResult.Some doc.[memberName]
1016-
1025+
| Some doc ->
1026+
match doc.TryGetValue memberName with
1027+
| true, docmember -> TryGetXmlDocMemberResult.Some docmember
1028+
| false, _ -> TryGetXmlDocMemberResult.None
10171029
| _ -> TryGetXmlDocMemberResult.None
10181030

10191031
| FSharpXmlDoc.None -> TryGetXmlDocMemberResult.None

src/FsAutoComplete.Core/TipFormatter.fsi

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ module private Format =
3737
type private Term = string
3838
type private Definition = string
3939

40+
[<Struct>]
4041
type private ListStyle =
4142
| Bulleted
4243
| Numbered
@@ -55,7 +56,7 @@ module private Format =
5556

5657
val applyAll: text: string -> string
5758

58-
[<RequireQualifiedAccess>]
59+
[<RequireQualifiedAccess; Struct>]
5960
type FormatCommentStyle =
6061
| Legacy
6162
| FullEnhanced

src/FsAutoComplete.Core/TypedAstUtils.fs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ module TypedAstExtensionHelpers =
8282
match fullName with
8383
| Some fullName ->
8484
match Option.attempt (fun _ -> x.DisplayName) with
85-
| Some shortDisplayName when not (shortDisplayName.Contains ".") ->
85+
| Some shortDisplayName when not (shortDisplayName.Contains '.') ->
8686
Some(fullName |> Array.replace (fullName.Length - 1) shortDisplayName)
8787
| _ -> Some fullName
8888
| None -> None
@@ -171,7 +171,7 @@ module TypedAstExtensionHelpers =
171171
match fullName with
172172
| Some fullName ->
173173
match Option.attempt (fun _ -> x.DisplayName) with
174-
| Some shortDisplayName when not (shortDisplayName.Contains ".") ->
174+
| Some shortDisplayName when not (shortDisplayName.Contains '.') ->
175175
Some(fullName |> Array.replace (fullName.Length - 1) shortDisplayName)
176176
| _ -> Some fullName
177177
| None -> None

src/FsAutoComplete.Core/Utils.fs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -886,16 +886,19 @@ module Tracing =
886886
let activity =
887887
activitySource.StartActivity(ActivityKind.Server, name = request.Method, tags = tags)
888888

889-
if activity <> null then
889+
if not (isNull activity) then
890890
activity.TraceStateString <- request.TraceState
891891

892-
if request.TraceParent <> null then
892+
if not (isNull request.TraceParent) then
893893
activity.SetParentId(request.TraceParent) |> ignore
894894

895895
activity
896896

897897
member this.ApplyOutboundActivity(request: Protocol.JsonRpcRequest) : unit =
898-
if Activity.Current <> null && Activity.Current.IdFormat = ActivityIdFormat.W3C then
898+
if
899+
not (isNull Activity.Current)
900+
&& Activity.Current.IdFormat = ActivityIdFormat.W3C
901+
then
899902
request.TraceParent <- Activity.Current.Id
900903
request.TraceState <- Activity.Current.TraceStateString
901904

0 commit comments

Comments
 (0)