From 2205fb3c7294027cf3db0e6c1eb2ea75ef25e6b2 Mon Sep 17 00:00:00 2001 From: Bronson Bata Date: Tue, 25 Jun 2024 08:36:54 -0500 Subject: [PATCH] Minor Seq Optimizations (#1308) * Seq optimizations * Added variable for code readability --- src/FsAutoComplete.Core/CodeGeneration.fs | 3 +-- .../DocumentationFormatter.fs | 12 ++++++----- src/FsAutoComplete.Core/InlayHints.fs | 6 ++---- src/FsAutoComplete.Core/SignatureFormatter.fs | 12 ++++++----- .../CodeFixes/AddMissingXmlDocumentation.fs | 21 ++++++++++--------- .../CodeFixes/AdjustConstant.fs | 7 +++++-- .../LspServers/AdaptiveServerState.fs | 7 +++++-- .../LspServers/ProjectWorkspace.fs | 19 ++++++++++------- 8 files changed, 50 insertions(+), 37 deletions(-) diff --git a/src/FsAutoComplete.Core/CodeGeneration.fs b/src/FsAutoComplete.Core/CodeGeneration.fs index 49a3706d2..9f5144f21 100644 --- a/src/FsAutoComplete.Core/CodeGeneration.fs +++ b/src/FsAutoComplete.Core/CodeGeneration.fs @@ -759,8 +759,7 @@ module CodeGenerationUtils = let duplicatedMembers = missingMembers |> Seq.countBy (fun (m, insts) -> m.DisplayName, insts |> Seq.length) - |> Seq.filter (snd >> (<) 1) - |> Seq.map (fst >> fst) + |> Seq.choose (fun (m, insts) -> if insts > 1 then fst m |> Some else None) |> Set.ofSeq let getReturnType v = snd (getArgTypes ctx v) diff --git a/src/FsAutoComplete.Core/DocumentationFormatter.fs b/src/FsAutoComplete.Core/DocumentationFormatter.fs index 8423b0347..c497c855d 100644 --- a/src/FsAutoComplete.Core/DocumentationFormatter.fs +++ b/src/FsAutoComplete.Core/DocumentationFormatter.fs @@ -721,16 +721,18 @@ module DocumentationFormatter = let types = fse.NestedEntities - |> Seq.filter (fun ne -> + |> Seq.choose (fun ne -> let isCompilerGenerated = ne.Attributes |> Seq.tryFind (fun attribute -> attribute.AttributeType.CompiledName = "CompilerGeneratedAttribute") |> Option.isSome - not ne.IsNamespace && not isCompilerGenerated) - |> Seq.map (fun ne -> - (typeName ne) - ++ fst (formatShowDocumentationLink ne.DisplayName ne.XmlDocSig ne.Assembly.SimpleName)) + if not ne.IsNamespace && not isCompilerGenerated then + (typeName ne) + ++ fst (formatShowDocumentationLink ne.DisplayName ne.XmlDocSig ne.Assembly.SimpleName) + |> Some + else + None) |> Seq.toArray { Constructors = constructors diff --git a/src/FsAutoComplete.Core/InlayHints.fs b/src/FsAutoComplete.Core/InlayHints.fs index f70beb042..86ae87912 100644 --- a/src/FsAutoComplete.Core/InlayHints.fs +++ b/src/FsAutoComplete.Core/InlayHints.fs @@ -954,14 +954,12 @@ let provideHints let parms = appliedArgRanges - |> Array.ofList - |> Array.mapi (fun i v -> + |> List.indexed + |> List.choose (fun (i, v) -> if i < definitionArgs.Length then Some(definitionArgs.[i], v) else None) - |> Array.filter Option.isSome - |> Array.map Option.get for (definitionArg, appliedArgRange) in parms do let! appliedArgText = text[appliedArgRange] diff --git a/src/FsAutoComplete.Core/SignatureFormatter.fs b/src/FsAutoComplete.Core/SignatureFormatter.fs index d78fb002b..4609388aa 100644 --- a/src/FsAutoComplete.Core/SignatureFormatter.fs +++ b/src/FsAutoComplete.Core/SignatureFormatter.fs @@ -593,11 +593,13 @@ module SignatureFormatter = let enumTip () = $" ={nl} |" ++ (fse.FSharpFields - |> Seq.filter (fun f -> not f.IsCompilerGenerated) - |> Seq.map (fun field -> - match field.LiteralValue with - | Some lv -> field.Name + " = " + (string lv) - | None -> field.Name) + |> Seq.choose (fun field -> + if field.IsCompilerGenerated then + None + else + match field.LiteralValue with + | Some lv -> field.Name + " = " + (string lv) |> Some + | None -> Some field.Name) |> String.concat $"{nl} | ") let unionTip () = diff --git a/src/FsAutoComplete/CodeFixes/AddMissingXmlDocumentation.fs b/src/FsAutoComplete/CodeFixes/AddMissingXmlDocumentation.fs index 6a6b75bf6..1f09b8887 100644 --- a/src/FsAutoComplete/CodeFixes/AddMissingXmlDocumentation.fs +++ b/src/FsAutoComplete/CodeFixes/AddMissingXmlDocumentation.fs @@ -96,11 +96,11 @@ let fix (getParseResultsForFile: GetParseResultsForFile) : CodeFix = | parameters -> parameters |> List.concat - |> List.filter (fun (parameter, _) -> - docLines - |> List.exists (fun c -> c.Contains($"")) - |> not) - |> List.mapi (fun _index parameter -> parameterSection parameter) + |> List.choose (fun (p, o) -> + let hasParam = + docLines |> List.exists (fun c -> c.Contains($"")) + + if hasParam then None else parameterSection (p, o) |> Some) match indexForParams with | None -> List.append docLines missingParams @@ -117,11 +117,12 @@ let fix (getParseResultsForFile: GetParseResultsForFile) : CodeFix = | [] -> [] | generics -> generics - |> List.filter (fun generic -> - docLines - |> List.exists (fun c -> c.Contains($"")) - |> not) - |> List.mapi (fun _index generic -> genericArg generic) + |> List.choose (fun generic -> + let hasTypeParams = + docLines + |> List.exists (fun c -> c.Contains($"")) + + if hasTypeParams then None else genericArg generic |> Some) match indexForTypeParams with | None -> List.append withAdded missingTypeParams diff --git a/src/FsAutoComplete/CodeFixes/AdjustConstant.fs b/src/FsAutoComplete/CodeFixes/AdjustConstant.fs index dd628a799..a50970cf1 100644 --- a/src/FsAutoComplete/CodeFixes/AdjustConstant.fs +++ b/src/FsAutoComplete/CodeFixes/AdjustConstant.fs @@ -756,8 +756,11 @@ module CommonFixes = let ty = assemblies - |> Seq.filter (isSystemAssembly) - |> Seq.tryPick (fun system -> system.Contents.FindEntityByPath [ "System"; name ]) + |> Seq.tryPick (fun system -> + if isSystemAssembly system then + system.Contents.FindEntityByPath [ "System"; name ] + else + None) |> Option.map (fun ent -> ent.AsType()) // Note: `ty` should never be `None`: we're only looking up standard dotnet types -- which should always be available. diff --git a/src/FsAutoComplete/LspServers/AdaptiveServerState.fs b/src/FsAutoComplete/LspServers/AdaptiveServerState.fs index 0d581c916..85e664921 100644 --- a/src/FsAutoComplete/LspServers/AdaptiveServerState.fs +++ b/src/FsAutoComplete/LspServers/AdaptiveServerState.fs @@ -970,8 +970,11 @@ type AdaptiveState | MSBuildAllProjects v -> yield! v - |> Array.filter (fun x -> x.EndsWith(".props", StringComparison.Ordinal) && isWithinObjFolder x) - |> Array.map (Utils.normalizePath >> projectFileChanges) + |> Array.choose (fun x -> + if x.EndsWith(".props", StringComparison.Ordinal) && isWithinObjFolder x then + Utils.normalizePath x |> projectFileChanges |> Some + else + None) | _ -> () ] HashMap.ofList diff --git a/src/FsAutoComplete/LspServers/ProjectWorkspace.fs b/src/FsAutoComplete/LspServers/ProjectWorkspace.fs index ce83352dc..a8e942e7b 100644 --- a/src/FsAutoComplete/LspServers/ProjectWorkspace.fs +++ b/src/FsAutoComplete/LspServers/ProjectWorkspace.fs @@ -185,11 +185,15 @@ module Snapshots = ) loadedProjectsA - |> AMap.filter (fun k _ -> - project.ReferencedProjects - |> List.exists (fun x -> normalizePath x.ProjectFileName = k)) - |> AMap.map (fun _ proj -> - if proj.ProjectFileName.EndsWith ".fsproj" then + |> AMap.choose (fun localPath proj -> + let loadedProjectNotReferenced = + project.ReferencedProjects + |> List.exists (fun x -> normalizePath x.ProjectFileName = localPath) + |> not + + if loadedProjectNotReferenced then + None + else if proj.ProjectFileName.EndsWith ".fsproj" then let resolvedTargetPath = aval { @@ -205,10 +209,11 @@ module Snapshots = sourceTextFactory (createReferences cachedSnapshots inMemorySourceFiles sourceTextFactory loadedProjectsA) |> createReferencedProjectsFSharpReference resolvedTargetPath + |> Some else // TODO: Find if this needs to be adaptive or if `getStamp` in a PEReference will be enough to break thru the caching in FCS - loadFromDotnetDll proj |> AVal.constant) + loadFromDotnetDll proj |> AVal.constant |> Some) |> AMap.toASetValues /// Creates a snapshot from a Project, using the already created snapshots it possible. @@ -246,8 +251,8 @@ module Snapshots = let sourceFiles = // alist because order matters for the F# Compiler project.SourceFiles |> AList.ofList - |> AList.map Utils.normalizePath |> AList.map (fun sourcePath -> + let sourcePath = Utils.normalizePath sourcePath aval { // prefer in-memory files over on-disk files