Source file path_segment.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
open! Core
open! Bonsai_web
open! Bonsai.Let_syntax

module Style = struct
  include
    [%css
    stylesheet
      {|
        .segment {
          box-sizing: border-box;
          display: block;
          vertical-align: top;
          line-height: 18px;
          color: var(--foreground);
          background-color: var(--background);
          border-radius: 4px;
          white-space: var(--whitespace);
        }

        .segment:hover {
          text-decoration: underline;
          text-decoration-color: var(--underline);
        }

        .segment.limit-width {
          max-width: 120px;
          overflow: hidden;
          text-overflow: ellipsis;
          white-space: nowrap;
        }

        .leaf-segment-indent {
          margin-left: 20px;
        }
      |}]

  let segment ~layout ~contracted ~underline ~faded ~foreground ~background =
    let foreground =
      if faded
      then Skyline_theme_v1.fade foreground (Percent.of_percentage 70.)
      else foreground
    in
    let underline =
      if underline then Css_gen.Color.to_string_css Skyline_theme_v1.border else "unset"
    in
    let whitespace =
      match layout with
      | Layout.Tree -> "normal"
      | Layout.List -> "nowrap"
    in
    Vdom.Attr.many
      [ Variables.set
          ~foreground:(Css_gen.Color.to_string_css foreground)
          ~background:(Css_gen.Color.to_string_css background)
          ~underline
          ~whitespace
          ()
      ; segment
      ; (if contracted then limit_width else Vdom.Attr.empty)
      ]
  ;;

  let squishy_text =
    Css_gen.(
      flex_item ~shrink:10. ~grow:1. ()
      @> overflow `Hidden
      @> create ~field:"text-overflow" ~value:"ellipsis"
      @> white_space `Nowrap)
    |> Vdom.Attr.style
  ;;
end

type t =
  { layout : Layout.t
  ; parent_path_length : int
  ; matching_indices : int array option
  ; intent :
      [ `Primary of
        [ `Background of Css_gen.Color.t | `Foreground of Css_gen.Color.t | `None ]
      | `Secondary of Css_gen.Color.t option
      ]
  ; segments : string Nonempty_list.t
  ; indent_tree_leaf : bool
  }

let highlight_matching_sections_in_substring ~matching_indices ~pos substring =
  Skyline_fuzzy_match_v1.matching_sections_in_substring ~matching_indices ~pos ~substring
  |> List.map ~f:(function
    | `Matching, part ->
      Skyline_text_v1.span ~intent:Skyline_theme_v1.accent ~style:Bold part
    | `Not_matching, part -> Skyline_text_v1.span part)
;;

let single_path_segment
  ~layout
  ~matching_indices
  ~parent_path_length
  ~contracted
  ~intent
  ~faded
  segment
  =
  let foreground, background =
    match intent with
    | `Primary (`Foreground intent) | `Secondary (Some intent) ->
      intent, `Name "transparent"
    | `Primary (`Background intent) -> Skyline_theme_v1.background, intent
    | `Primary `None | `Secondary None -> Skyline_theme_v1.primary, `Name "transparent"
  in
  let highlighted_name =
    match matching_indices with
    | Some matching_indices ->
      (* This is subtly broken for items that have multiple children, since the common
         parent may not match (and so get [None] when we compute) matching indices for a
         given row. (It's subtle enough that we decided to ignore the issue for now
         though.) *)
      highlight_matching_sections_in_substring
        ~matching_indices
        ~pos:parent_path_length
        segment
    | None -> [ Skyline_text_v1.span segment ]
  in
  Vdom.Node.span
    ~attrs:
      [ Style.segment ~layout ~contracted ~underline:true ~faded ~foreground ~background
      ; Vdom.Attr.title segment
      ]
    highlighted_name
;;

let tree_path_segments
  { layout; parent_path_length; matching_indices; intent; segments; indent_tree_leaf }
  =
  let rec loop ~parent_path_length (segments : string Nonempty_list.t) =
    match segments with
    | [ primary_segment ] ->
      let faded =
        match intent with
        | `Primary _ -> false
        | `Secondary _ -> true
      in
      [ single_path_segment
          ~matching_indices
          ~parent_path_length
          ~contracted:false
          ~intent
          ~faded
          ~layout
          primary_segment
      ]
    | merged_segment :: hd :: tl ->
      let merged_segment =
        single_path_segment
          ~matching_indices
          ~parent_path_length:(parent_path_length + 1 + String.length merged_segment)
          ~contracted:true
          ~intent
          ~faded:true
          ~layout
          merged_segment
      in
      merged_segment :: Skyline_divider_v1.slash :: loop ~parent_path_length (hd :: tl)
  in
  Skyline_flex_v1.row
    ~wrap:Wrap
    ~align:Center
    ~justify:Flex_start
    ~attrs:(if indent_tree_leaf then [ Style.leaf_segment_indent ] else [])
    (loop segments ~parent_path_length)
;;

let list_path_segments
  { layout
  ; parent_path_length = _
  ; matching_indices
  ; intent
  ; segments
  ; indent_tree_leaf = _
  }
  =
  let item_name = Nonempty_list.last segments in
  let item_prefix =
    let rec loop (segments : string Nonempty_list.t) =
      match segments with
      | [ _ ] -> []
      | item :: hd :: tl -> item :: loop (hd :: tl)
    in
    String.concat ~sep:"/" (loop segments)
  in
  let fade_full_path =
    match intent with
    | `Primary _ -> false
    | `Secondary _ -> true
  in
  Skyline_flex_v1.row
    ~gap:(`Px 4)
    ~align:Baseline
    ~justify:Flex_start
    ~attrs:[ Vdom.Attr.style (Css_gen.max_width (`Percent (Percent.of_percentage 80.))) ]
    [ single_path_segment
        ~matching_indices
        ~parent_path_length:(String.length item_prefix)
        ~contracted:false
        ~intent
        ~faded:fade_full_path
        ~layout
        item_name
    ; Skyline_text_v1.span
        ~secondary:true
        ~size:Small
        ~attrs:[ Style.squishy_text; Vdom.Attr.title item_prefix ]
        item_prefix
    ]
;;

let component segment =
  match segment.layout with
  | Tree -> tree_path_segments segment
  | List -> list_path_segments segment
;;