Skip to content

Commit

Permalink
Merge pull request fsprojects#922 from 1eyewonder/seq-optimizations
Browse files Browse the repository at this point in the history
Minor Seq Optimizations
  • Loading branch information
nojaf authored Jun 21, 2024
2 parents 1ce0fae + 6b8d5c9 commit 2efb355
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 89 deletions.
2 changes: 1 addition & 1 deletion .config/dotnet-tools.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
]
},
"fsharp-analyzers": {
"version": "0.24.0",
"version": "0.26.0",
"commands": [
"fsharp-analyzers"
]
Expand Down
4 changes: 2 additions & 2 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageVersion Include="Ionide.KeepAChangelog.Tasks" Version="0.1.8" />
<PackageVersion Include="FSharp.Analyzers.Build" Version="0.3.0" />
<PackageVersion Include="G-Research.FSharp.Analyzers" Version="0.8.0" />
<PackageVersion Include="Ionide.Analyzers" Version="0.8.0" />
<PackageVersion Include="G-Research.FSharp.Analyzers" Version="0.10.0" />
<PackageVersion Include="Ionide.Analyzers" Version="0.11.0" />
</ItemGroup>
</Project>
7 changes: 5 additions & 2 deletions src/Common/StringParsing.fs
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,11 @@ module String =
let removeSpaces (lines: string list) =
let spaces =
lines
|> Seq.filter (String.IsNullOrWhiteSpace >> not)
|> Seq.map (fun line -> line |> Seq.takeWhile Char.IsWhiteSpace |> Seq.length)
|> Seq.choose (fun line ->
if String.IsNullOrWhiteSpace line |> not then
line |> Seq.takeWhile Char.IsWhiteSpace |> Seq.length |> Some
else
None)
|> fun xs -> if Seq.isEmpty xs then 0 else Seq.min xs

lines
Expand Down
149 changes: 78 additions & 71 deletions src/FSharp.Formatting.ApiDocs/GenerateModel.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1282,8 +1282,9 @@ module internal TypeFormatter =
n

curriedArgs
|> List.map (List.map (fun x -> formatArgNameAndType (counter ()) x |> fst))
|> List.map (fun argTuple ->
|> List.map (fun args ->
let argTuple = args |> List.map (formatArgNameAndType (counter ()) >> fst)

match argTuple with
| [] -> !! "()"
| [ argName ] when argName = "()" -> !! "()"
Expand Down Expand Up @@ -2217,10 +2218,9 @@ module internal SymbolReader =
}

/// Returns whether the link is not included in the document defined links
let linkNotDefined (doc: LiterateDocument) (link: string) =
let linkDefined (doc: LiterateDocument) (link: string) =
[ link; link.Replace("\r\n", ""); link.Replace("\r\n", " "); link.Replace("\n", ""); link.Replace("\n", " ") ]
|> Seq.map (fun key -> not (doc.DefinedLinks.ContainsKey(key)))
|> Seq.reduce (fun a c -> a && c)
|> List.exists (fun key -> doc.DefinedLinks.ContainsKey(key))

/// Returns a tuple of the undefined link and its Cref if it exists
let getTypeLink (ctx: ReadingContext) undefinedLink =
Expand Down Expand Up @@ -2264,8 +2264,11 @@ module internal SymbolReader =
do
replacedParagraphs
|> Seq.collect collectParagraphIndirectLinks
|> Seq.filter (linkNotDefined doc)
|> Seq.map (getTypeLink ctx)
|> Seq.choose (fun line ->
if linkDefined doc line then
None
else
getTypeLink ctx line |> Some)
|> Seq.iter (addLinkToType doc)

doc.With(paragraphs = replacedParagraphs)
Expand All @@ -2275,15 +2278,12 @@ module internal SymbolReader =

let text =
lines
|> List.filter (
findCommand
>> (function
|> List.choose (fun line ->
match findCommand line with
| Some(k, v) ->
cmds.[k] <- v
false
| _ -> true)
)
|> List.map fst
None
| _ -> fst line |> Some)
|> String.concat "\n"

let doc =
Expand Down Expand Up @@ -2421,8 +2421,7 @@ module internal SymbolReader =

let readChildren ctx (entities: FSharpEntity seq) reader cond =
entities
|> Seq.filter (fun v -> checkAccess ctx v.Accessibility)
|> Seq.filter cond
|> Seq.filter (fun v -> checkAccess ctx v.Accessibility && cond v)
|> Seq.sortBy (fun (c: FSharpEntity) -> c.DisplayName)
|> Seq.choose (reader ctx)
|> List.ofSeq
Expand All @@ -2448,23 +2447,28 @@ module internal SymbolReader =

let readAllMembers ctx entityUrl kind (members: FSharpMemberOrFunctionOrValue seq) =
members
|> Seq.filter (fun v -> checkAccess ctx v.Accessibility)
|> Seq.filter (fun v ->
not v.IsCompilerGenerated
&& not v.IsPropertyGetterMethod
&& not v.IsPropertySetterMethod
&& not v.IsEventAddMethod
&& not v.IsEventRemoveMethod)
|> Seq.choose (tryReadMember ctx entityUrl kind)
|> Seq.choose (fun v ->
if
checkAccess ctx v.Accessibility
&& not v.IsCompilerGenerated
&& not v.IsPropertyGetterMethod
&& not v.IsPropertySetterMethod
&& not v.IsEventAddMethod
&& not v.IsEventRemoveMethod
then
tryReadMember ctx entityUrl kind v
else
None)
|> List.ofSeq
|> collectNamespaceDocs

let readMembers ctx entityUrl kind (entity: FSharpEntity) cond =
entity.MembersFunctionsAndValues
|> Seq.filter (fun v -> checkAccess ctx v.Accessibility)
|> Seq.filter (fun v -> not v.IsCompilerGenerated)
|> Seq.filter cond
|> Seq.choose (tryReadMember ctx entityUrl kind)
|> Seq.choose (fun v ->
if checkAccess ctx v.Accessibility && not v.IsCompilerGenerated && cond v then
tryReadMember ctx entityUrl kind v
else
None)
|> List.ofSeq
|> collectNamespaceDocs

Expand All @@ -2485,47 +2489,51 @@ module internal SymbolReader =
let readUnionCases ctx entityUrl (typ: FSharpEntity) =
typ.UnionCases
|> List.ofSeq
|> List.filter (fun v -> checkAccess ctx v.Accessibility)
|> List.choose (fun case ->
readCommentsInto case ctx case.XmlDocSig (fun cat catidx exclude _ comment ->
let details = readUnionCase ctx typ case

ApiDocMember(
case.Name,
readAttributes case.Attributes,
entityUrl,
ApiDocMemberKind.UnionCase,
cat,
catidx,
exclude,
details,
comment,
case,
ctx.WarnOnMissingDocs
)))
if checkAccess ctx case.Accessibility |> not then
None
else
readCommentsInto case ctx case.XmlDocSig (fun cat catidx exclude _ comment ->
let details = readUnionCase ctx typ case

ApiDocMember(
case.Name,
readAttributes case.Attributes,
entityUrl,
ApiDocMemberKind.UnionCase,
cat,
catidx,
exclude,
details,
comment,
case,
ctx.WarnOnMissingDocs
)))
|> collectNamespaceDocs

let readRecordFields ctx entityUrl (typ: FSharpEntity) =
typ.FSharpFields
|> List.ofSeq
|> List.filter (fun field -> not field.IsCompilerGenerated)
|> List.choose (fun field ->
readCommentsInto field ctx field.XmlDocSig (fun cat catidx exclude _ comment ->
let details = readFSharpField ctx field

ApiDocMember(
field.Name,
readAttributes (Seq.append field.FieldAttributes field.PropertyAttributes),
entityUrl,
ApiDocMemberKind.RecordField,
cat,
catidx,
exclude,
details,
comment,
field,
ctx.WarnOnMissingDocs
)))
if field.IsCompilerGenerated then
None
else
readCommentsInto field ctx field.XmlDocSig (fun cat catidx exclude _ comment ->
let details = readFSharpField ctx field

ApiDocMember(
field.Name,
readAttributes (Seq.append field.FieldAttributes field.PropertyAttributes),
entityUrl,
ApiDocMemberKind.RecordField,
cat,
catidx,
exclude,
details,
comment,
field,
ctx.WarnOnMissingDocs
)))
|> collectNamespaceDocs

let readStaticParams ctx entityUrl (typ: FSharpEntity) =
Expand Down Expand Up @@ -2585,11 +2593,12 @@ module internal SymbolReader =
if isNull nameAttr then
None
else
Some(nameAttr.Value, p.Value))
|> Seq.iter (fun (name, xmlDoc) ->
let xmlDocSig = getFSharpStaticParamXmlSig typ name
let xmlDocSig = getFSharpStaticParamXmlSig typ nameAttr.Value

registerXmlDoc ctx xmlDocSig (Security.SecurityElement.Escape xmlDoc) |> ignore)
registerXmlDoc ctx xmlDocSig (Security.SecurityElement.Escape p.Value)
|> ignore
|> Some)
|> ignore

let rec readType (ctx: ReadingContext) (typ: FSharpEntity) =
if typ.IsProvided && typ.XmlDoc <> FSharpXmlDoc.None then
Expand Down Expand Up @@ -2617,17 +2626,15 @@ module internal SymbolReader =

let ivals, svals =
getMembers typ
|> List.ofSeq
|> List.filter (fun v ->
|> Seq.filter (fun v ->
checkAccess ctx v.Accessibility
&& not v.IsCompilerGenerated
&& not v.IsOverrideOrExplicitInterfaceImplementation)
|> List.filter (fun v ->
not v.IsCompilerGenerated
&& not v.IsOverrideOrExplicitInterfaceImplementation
&& not v.IsEventAddMethod
&& not v.IsEventRemoveMethod
&& not v.IsPropertyGetterMethod
&& not v.IsPropertySetterMethod)
|> List.ofSeq
|> List.partition (fun v -> v.IsInstanceMember)

let cvals, svals = svals |> List.partition (fun v -> v.CompiledName = ".ctor")
Expand Down
13 changes: 9 additions & 4 deletions src/FSharp.Formatting.Common/Templating.fs
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,15 @@ type FrontMatterFile =
// Allow empty lines in frontmatter
let isBlankLine = String.IsNullOrWhiteSpace line
isBlankLine || line.Contains(":"))
|> Seq.filter (String.IsNullOrWhiteSpace >> not)
|> Seq.map (fun line ->
let parts = line.Split(":")
parts.[0].ToLowerInvariant(), parts.[1])
|> Seq.choose (fun line ->
if String.IsNullOrWhiteSpace line |> not then
let parts = line.Split(":") |> Array.toList

match parts with
| first :: second :: _ -> Some(first.ToLowerInvariant(), second)
| _ -> None
else
None)
|> Map.ofSeq

match
Expand Down
10 changes: 4 additions & 6 deletions src/FSharp.Formatting.Common/YaafFSharpScripting.fs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ module internal CompilerServiceExtensions =

options.OtherOptions
|> Array.filter (fun path -> path.StartsWith("-r:", StringComparison.Ordinal))
|> Array.filter (fun path -> path.StartsWith("-r:", StringComparison.Ordinal))
//|> Seq.choose (fun path -> if path.StartsWith "-r:" then path.Substring 3 |> Some else None)
//|> Seq.map (fun path -> path.Replace("\\\\", "\\"))
|> Array.toList)
Expand Down Expand Up @@ -180,11 +179,11 @@ module internal CompilerServiceExtensions =
|| libDirs
|> List.exists (fun lib ->
Directory.EnumerateFiles(lib)
|> Seq.filter (fun file -> Path.GetExtension file =? ".dll")
|> Seq.filter (fun file ->
// If we find a FSharp.Core in a lib path, we check if is suited for us...
Path.GetFileNameWithoutExtension file <>? "FSharp.Core"
|| (tryCheckFsCore file |> Option.isSome))
Path.GetExtension file =? ".dll"
&& (Path.GetFileNameWithoutExtension file <>? "FSharp.Core"
|| (tryCheckFsCore file |> Option.isSome)))
|> Seq.toList
|> isAssembly asm)

Expand Down Expand Up @@ -279,10 +278,9 @@ module internal CompilerServiceExtensions =

let findReferences libDir =
Directory.EnumerateFiles(libDir, "*.dll")
|> Seq.map Path.GetFullPath
// Filter files already referenced directly
|> Seq.filter (fun file ->
let fileName = Path.GetFileName file
let fileName = Path.GetFullPath file |> Path.GetFileName

dllFiles
|> List.exists (fun (dllFile: string) -> Path.GetFileName dllFile =? fileName)
Expand Down
9 changes: 6 additions & 3 deletions src/fsdocs-tool/BuildCommand.fs
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,12 @@ type internal DocContent
(subFolderFullPath = rootOutputFolderFullPath)

let allCultures =
System.Globalization.CultureInfo.GetCultures(System.Globalization.CultureTypes.AllCultures)
|> Array.map (fun x -> x.TwoLetterISOLanguageName)
|> Array.filter (fun x -> x.Length = 2)
CultureInfo.GetCultures(CultureTypes.AllCultures)
|> Array.choose (fun x ->
if x.TwoLetterISOLanguageName.Length <> 2 then
None
else
Some x.TwoLetterISOLanguageName)
|> Array.distinct

let makeMarkdownLinkResolver
Expand Down

0 comments on commit 2efb355

Please sign in to comment.