Skip to content

Commit 2205fb3

Browse files
authored
Minor Seq Optimizations (#1308)
* Seq optimizations * Added variable for code readability
1 parent 8d6120c commit 2205fb3

File tree

8 files changed

+50
-37
lines changed

8 files changed

+50
-37
lines changed

src/FsAutoComplete.Core/CodeGeneration.fs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -759,8 +759,7 @@ module CodeGenerationUtils =
759759
let duplicatedMembers =
760760
missingMembers
761761
|> Seq.countBy (fun (m, insts) -> m.DisplayName, insts |> Seq.length)
762-
|> Seq.filter (snd >> (<) 1)
763-
|> Seq.map (fst >> fst)
762+
|> Seq.choose (fun (m, insts) -> if insts > 1 then fst m |> Some else None)
764763
|> Set.ofSeq
765764

766765
let getReturnType v = snd (getArgTypes ctx v)

src/FsAutoComplete.Core/DocumentationFormatter.fs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -721,16 +721,18 @@ module DocumentationFormatter =
721721

722722
let types =
723723
fse.NestedEntities
724-
|> Seq.filter (fun ne ->
724+
|> Seq.choose (fun ne ->
725725
let isCompilerGenerated =
726726
ne.Attributes
727727
|> Seq.tryFind (fun attribute -> attribute.AttributeType.CompiledName = "CompilerGeneratedAttribute")
728728
|> Option.isSome
729729

730-
not ne.IsNamespace && not isCompilerGenerated)
731-
|> Seq.map (fun ne ->
732-
(typeName ne)
733-
++ fst (formatShowDocumentationLink ne.DisplayName ne.XmlDocSig ne.Assembly.SimpleName))
730+
if not ne.IsNamespace && not isCompilerGenerated then
731+
(typeName ne)
732+
++ fst (formatShowDocumentationLink ne.DisplayName ne.XmlDocSig ne.Assembly.SimpleName)
733+
|> Some
734+
else
735+
None)
734736
|> Seq.toArray
735737

736738
{ Constructors = constructors

src/FsAutoComplete.Core/InlayHints.fs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -954,14 +954,12 @@ let provideHints
954954

955955
let parms =
956956
appliedArgRanges
957-
|> Array.ofList
958-
|> Array.mapi (fun i v ->
957+
|> List.indexed
958+
|> List.choose (fun (i, v) ->
959959
if i < definitionArgs.Length then
960960
Some(definitionArgs.[i], v)
961961
else
962962
None)
963-
|> Array.filter Option.isSome
964-
|> Array.map Option.get
965963

966964
for (definitionArg, appliedArgRange) in parms do
967965
let! appliedArgText = text[appliedArgRange]

src/FsAutoComplete.Core/SignatureFormatter.fs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -593,11 +593,13 @@ module SignatureFormatter =
593593
let enumTip () =
594594
$" ={nl} |"
595595
++ (fse.FSharpFields
596-
|> Seq.filter (fun f -> not f.IsCompilerGenerated)
597-
|> Seq.map (fun field ->
598-
match field.LiteralValue with
599-
| Some lv -> field.Name + " = " + (string lv)
600-
| None -> field.Name)
596+
|> Seq.choose (fun field ->
597+
if field.IsCompilerGenerated then
598+
None
599+
else
600+
match field.LiteralValue with
601+
| Some lv -> field.Name + " = " + (string lv) |> Some
602+
| None -> Some field.Name)
601603
|> String.concat $"{nl} | ")
602604

603605
let unionTip () =

src/FsAutoComplete/CodeFixes/AddMissingXmlDocumentation.fs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,11 @@ let fix (getParseResultsForFile: GetParseResultsForFile) : CodeFix =
9696
| parameters ->
9797
parameters
9898
|> List.concat
99-
|> List.filter (fun (parameter, _) ->
100-
docLines
101-
|> List.exists (fun c -> c.Contains($"<param name=\"%s{parameter}\">"))
102-
|> not)
103-
|> List.mapi (fun _index parameter -> parameterSection parameter)
99+
|> List.choose (fun (p, o) ->
100+
let hasParam =
101+
docLines |> List.exists (fun c -> c.Contains($"<param name=\"%s{p}\">"))
102+
103+
if hasParam then None else parameterSection (p, o) |> Some)
104104

105105
match indexForParams with
106106
| None -> List.append docLines missingParams
@@ -117,11 +117,12 @@ let fix (getParseResultsForFile: GetParseResultsForFile) : CodeFix =
117117
| [] -> []
118118
| generics ->
119119
generics
120-
|> List.filter (fun generic ->
121-
docLines
122-
|> List.exists (fun c -> c.Contains($"<typeparam name=\"'%s{generic}\">"))
123-
|> not)
124-
|> List.mapi (fun _index generic -> genericArg generic)
120+
|> List.choose (fun generic ->
121+
let hasTypeParams =
122+
docLines
123+
|> List.exists (fun c -> c.Contains($"<typeparam name=\"'%s{generic}\">"))
124+
125+
if hasTypeParams then None else genericArg generic |> Some)
125126

126127
match indexForTypeParams with
127128
| None -> List.append withAdded missingTypeParams

src/FsAutoComplete/CodeFixes/AdjustConstant.fs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -756,8 +756,11 @@ module CommonFixes =
756756

757757
let ty =
758758
assemblies
759-
|> Seq.filter (isSystemAssembly)
760-
|> Seq.tryPick (fun system -> system.Contents.FindEntityByPath [ "System"; name ])
759+
|> Seq.tryPick (fun system ->
760+
if isSystemAssembly system then
761+
system.Contents.FindEntityByPath [ "System"; name ]
762+
else
763+
None)
761764
|> Option.map (fun ent -> ent.AsType())
762765

763766
// Note: `ty` should never be `None`: we're only looking up standard dotnet types -- which should always be available.

src/FsAutoComplete/LspServers/AdaptiveServerState.fs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -970,8 +970,11 @@ type AdaptiveState
970970
| MSBuildAllProjects v ->
971971
yield!
972972
v
973-
|> Array.filter (fun x -> x.EndsWith(".props", StringComparison.Ordinal) && isWithinObjFolder x)
974-
|> Array.map (Utils.normalizePath >> projectFileChanges)
973+
|> Array.choose (fun x ->
974+
if x.EndsWith(".props", StringComparison.Ordinal) && isWithinObjFolder x then
975+
Utils.normalizePath x |> projectFileChanges |> Some
976+
else
977+
None)
975978
| _ -> () ]
976979

977980
HashMap.ofList

src/FsAutoComplete/LspServers/ProjectWorkspace.fs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -185,11 +185,15 @@ module Snapshots =
185185
)
186186

187187
loadedProjectsA
188-
|> AMap.filter (fun k _ ->
189-
project.ReferencedProjects
190-
|> List.exists (fun x -> normalizePath x.ProjectFileName = k))
191-
|> AMap.map (fun _ proj ->
192-
if proj.ProjectFileName.EndsWith ".fsproj" then
188+
|> AMap.choose (fun localPath proj ->
189+
let loadedProjectNotReferenced =
190+
project.ReferencedProjects
191+
|> List.exists (fun x -> normalizePath x.ProjectFileName = localPath)
192+
|> not
193+
194+
if loadedProjectNotReferenced then
195+
None
196+
else if proj.ProjectFileName.EndsWith ".fsproj" then
193197

194198
let resolvedTargetPath =
195199
aval {
@@ -205,10 +209,11 @@ module Snapshots =
205209
sourceTextFactory
206210
(createReferences cachedSnapshots inMemorySourceFiles sourceTextFactory loadedProjectsA)
207211
|> createReferencedProjectsFSharpReference resolvedTargetPath
212+
|> Some
208213

209214
else
210215
// TODO: Find if this needs to be adaptive or if `getStamp` in a PEReference will be enough to break thru the caching in FCS
211-
loadFromDotnetDll proj |> AVal.constant)
216+
loadFromDotnetDll proj |> AVal.constant |> Some)
212217
|> AMap.toASetValues
213218

214219
/// <summary>Creates a snapshot from a Project, using the already created snapshots it possible.</summary>
@@ -246,8 +251,8 @@ module Snapshots =
246251
let sourceFiles = // alist because order matters for the F# Compiler
247252
project.SourceFiles
248253
|> AList.ofList
249-
|> AList.map Utils.normalizePath
250254
|> AList.map (fun sourcePath ->
255+
let sourcePath = Utils.normalizePath sourcePath
251256

252257
aval {
253258
// prefer in-memory files over on-disk files

0 commit comments

Comments
 (0)