From ae8780aac7a8ee7c67af598fe2090527e13e2818 Mon Sep 17 00:00:00 2001 From: krymtkts Date: Sun, 26 Jan 2025 18:20:35 +0900 Subject: [PATCH 1/6] Specify return type as `obj seq` for `unwrap`, --- src/pocof/Data.fs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/pocof/Data.fs b/src/pocof/Data.fs index e6d663f..57263c7 100644 --- a/src/pocof/Data.fs +++ b/src/pocof/Data.fs @@ -146,10 +146,10 @@ module Data = | Obj(o) -> o[prop] | Dict(d) -> d[prop] - let unwrap (entries: Entry seq) = + let unwrap (entries: Entry seq) : obj seq = entries |> Seq.map (function - | Entry.Dict(dct) -> dct :> obj + | Entry.Dict(dct) -> dct | Entry.Obj(o) -> o) let (|Found|_|) aType excludes name = From c156305e13f93cdcc4bc895be5a57a111fac3f69 Mon Sep 17 00:00:00 2001 From: krymtkts Date: Sun, 26 Jan 2025 18:21:59 +0900 Subject: [PATCH 2/6] Refactor `Found` active pattern to use case-insensitive string comparison. --- src/pocof/Data.fs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/pocof/Data.fs b/src/pocof/Data.fs index 57263c7..7f8d0a6 100644 --- a/src/pocof/Data.fs +++ b/src/pocof/Data.fs @@ -154,14 +154,16 @@ module Data = let (|Found|_|) aType excludes name = FSharpType.GetUnionCases aType - |> Array.filter (fun u -> Set.contains u.Name excludes |> not) - |> Array.tryFind (fun u -> u.Name |> String.lower = name) + |> (fun arr -> + match Set.isEmpty excludes with + | true -> arr + | _ -> arr |> Array.filter (fun u -> Set.contains u.Name excludes |> not)) + |> Array.tryFind _.Name.Equals(name, StringComparison.CurrentCultureIgnoreCase) let private tryFromStringExcludes<'a> (excludes: Set) s = - let name = String.lower s let aType = typeof<'a> - match name with + match s with | Found aType excludes u -> Ok <| (FSharpValue.MakeUnion(u, [||]) :?> 'a) | _ -> Error <| $"Unknown %s{aType.Name} '%s{s}'." From 24ce5acd0ac83f8fc08d4c6a10f403c2b8f03e36 Mon Sep 17 00:00:00 2001 From: krymtkts Date: Sun, 26 Jan 2025 19:59:54 +0900 Subject: [PATCH 3/6] Make `ToString` method for `QueryCondition` a function in `QueryCondition` module. --- src/pocof.Test/Data.fs | 5 ++++- src/pocof/Data.fs | 13 ++++++------- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/src/pocof.Test/Data.fs b/src/pocof.Test/Data.fs index d0213f3..039e8be 100644 --- a/src/pocof.Test/Data.fs +++ b/src/pocof.Test/Data.fs @@ -235,7 +235,10 @@ module ``QueryState toString`` = | Matcher.Like -> $"""{if data.Invert then "not" else ""}like""" | Matcher.Match -> $"""{if data.Invert then "not" else ""}match""" - data |> string |> shouldEqual $"{c}{m} {data.Operator}" |> Prop.collect data + data + |> QueryCondition.toString + |> shouldEqual $"{c}{m} {data.Operator}" + |> Prop.collect data module initConfig = [] diff --git a/src/pocof/Data.fs b/src/pocof/Data.fs index 7f8d0a6..5d97400 100644 --- a/src/pocof/Data.fs +++ b/src/pocof/Data.fs @@ -411,21 +411,20 @@ module Data = CaseSensitive: bool Invert: bool } - override __.ToString() = - [ match __.CaseSensitive with + module QueryCondition = + let toString (condition: QueryCondition) = + [ match condition.CaseSensitive with | true -> "c" | _ -> "" // NOTE: use ToString to avoid extra branches when calculating coverages. - match __.Matcher, __.Invert with + match condition.Matcher, condition.Invert with | Matcher.Eq, true -> "ne" | m, true -> "not" + m.ToString() | m, _ -> m.ToString() " " - __.Operator.ToString() ] + condition.Operator.ToString() ] |> String.concat "" - [] - module QueryCondition = let rotateMatcher (condition: QueryCondition) = { condition with Matcher = @@ -459,7 +458,7 @@ module Data = module InternalState = let queryInfo (state: InternalState) (count: int) = - $" %O{state.QueryCondition} [%d{count}]" + $" %s{state.QueryCondition |> QueryCondition.toString} [%d{count}]" let getX promptLength (state: InternalState) = promptLength + state.QueryState.Cursor - state.QueryState.WindowBeginningCursor From ebd36a8c754ab67f70af14a4760774e6547fd68d Mon Sep 17 00:00:00 2001 From: krymtkts Date: Sun, 26 Jan 2025 22:18:25 +0900 Subject: [PATCH 4/6] Add `StructAttribute` to small discriminated unions. --- src/pocof/Data.fs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/pocof/Data.fs b/src/pocof/Data.fs index 5d97400..3a4080a 100644 --- a/src/pocof/Data.fs +++ b/src/pocof/Data.fs @@ -227,6 +227,7 @@ module Data = [] [] + [] type Matcher = | Eq | Like @@ -240,6 +241,7 @@ module Data = [] [] + [] type Operator = | And | Or @@ -252,6 +254,7 @@ module Data = [] [] + [] type Layout = | TopDown | TopDownHalf @@ -271,6 +274,7 @@ module Data = [] [] + [] type Refresh = | Required | NotRequired @@ -291,6 +295,7 @@ module Data = [] [] + [] type InputMode = | Input // Note: positive number is the backward selection from the cursor. negative number is the forward selection from the cursor. From 253faa3cf815d65631413c67ba3c911528c8f7e5 Mon Sep 17 00:00:00 2001 From: krymtkts Date: Fri, 31 Jan 2025 09:59:57 +0900 Subject: [PATCH 5/6] Refactor string case handling to use `_.Property` shorthand. --- src/pocof.Test/Data.fs | 2 +- src/pocof/Data.fs | 6 ++---- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/src/pocof.Test/Data.fs b/src/pocof.Test/Data.fs index 039e8be..b3ec408 100644 --- a/src/pocof.Test/Data.fs +++ b/src/pocof.Test/Data.fs @@ -116,7 +116,7 @@ let randomCase (s: string) = let duNames<'U> = FSharpType.GetUnionCases(typeof<'U>) |> Seq.map _.Name let randomCases (name: string) = - [ name; String.lower name; String.upper name; randomCase name ] + [ name; name.ToLower(); name.ToUpper(); randomCase name ] let toIgnoreCaseSet (x: string seq) = HashSet(x, StringComparer.InvariantCultureIgnoreCase) diff --git a/src/pocof/Data.fs b/src/pocof/Data.fs index 3a4080a..4343ec2 100644 --- a/src/pocof/Data.fs +++ b/src/pocof/Data.fs @@ -52,8 +52,6 @@ module LanguageExtension = let split (separator: string) (s: string) = Regex.Split(s, separator) module String = - let lower (s: string) = s.ToLower() - let upper (s: string) = s.ToUpper() let startsWith (value: string) (s: string) = s.StartsWith(value) let startsWithIgnoreCase (value: string) (s: string) = @@ -233,7 +231,7 @@ module Data = | Like | Match - override __.ToString() = toString __ |> String.lower + override __.ToString() = toString __ |> _.ToLower() [] module Matcher = @@ -246,7 +244,7 @@ module Data = | And | Or - override __.ToString() = toString __ |> String.lower + override __.ToString() = toString __ |> _.ToLower() [] module Operator = From 088775745a6136fd566b78c83d91f85b34f08fc1 Mon Sep 17 00:00:00 2001 From: krymtkts Date: Fri, 31 Jan 2025 10:16:31 +0900 Subject: [PATCH 6/6] Refactor string trimming to use `_.Property` shorthand. --- src/pocof/Data.fs | 1 - src/pocof/Query.fs | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/src/pocof/Data.fs b/src/pocof/Data.fs index 4343ec2..beb8172 100644 --- a/src/pocof/Data.fs +++ b/src/pocof/Data.fs @@ -63,7 +63,6 @@ module LanguageExtension = s.Split(separators, StringSplitOptions.None) let equals (opt: StringComparison) (value: string) (s: string) = s.Equals(value, opt) - let trim (s: string) = s.Trim() let replace (oldValue: string) (newValue: string) (s: string) = s.Replace(oldValue, newValue) let fromIndex (index: int) (s: string) = s.Substring(index) let upToIndex (index: int) (s: string) = s.Substring(0, index) diff --git a/src/pocof/Query.fs b/src/pocof/Query.fs index e76ef1e..9ddc600 100644 --- a/src/pocof/Query.fs +++ b/src/pocof/Query.fs @@ -84,7 +84,7 @@ module Query = | _ -> let is = prepareTest condition - query |> String.trim |> Regex.split @"\s+" |> List.ofSeq |> parseQuery is [] + query |> _.Trim() |> Regex.split @"\s+" |> List.ofSeq |> parseQuery is [] let private prepareNotification (query: string) (condition: QueryCondition) = match condition.Matcher with