@@ -16,69 +16,98 @@ import m from 'mithril';
16
16
import { isString } from '../../base/object_utils' ;
17
17
import { Icons } from '../../base/semantic_icons' ;
18
18
import { exists } from '../../base/utils' ;
19
- import { ArgNode , convertArgsToTree , Key } from './slice_args_parser' ;
20
19
import { Anchor } from '../../widgets/anchor' ;
21
20
import { MenuItem , PopupMenu } from '../../widgets/menu' ;
22
21
import { TreeNode } from '../../widgets/tree' ;
23
- import { Arg } from '../sql_utils/args' ;
22
+ import { Args , ArgsDict , ArgValue } from '../sql_utils/args' ;
24
23
import { Trace } from '../../public/trace' ;
25
24
26
25
// Renders slice arguments (key/value pairs) as a subtree.
27
26
export function renderArguments (
28
27
trace : Trace ,
29
- args : ReadonlyArray < Arg > ,
30
- extraMenuItems ?: ( arg : Arg ) => m . Children ,
28
+ args : ArgsDict ,
29
+ extraMenuItems ?: ( key : string , arg : ArgValue ) => m . Children ,
31
30
) : m . Children {
32
- if ( args . length > 0 ) {
33
- const tree = convertArgsToTree ( args ) ;
34
- return renderArgTreeNodes ( trace , tree , extraMenuItems ) ;
35
- } else {
36
- return undefined ;
31
+ if ( hasArgs ( args ) ) {
32
+ return Object . entries ( args ) . map ( ( [ key , value ] ) =>
33
+ renderArgsTree ( trace , key , key , value , extraMenuItems ) ,
34
+ ) ;
37
35
}
36
+ return undefined ;
38
37
}
39
38
40
- export function hasArgs ( args ?: Arg [ ] ) : args is Arg [ ] {
41
- return exists ( args ) && args . length > 0 ;
39
+ export function hasArgs ( args ?: ArgsDict ) : args is ArgsDict {
40
+ return exists ( args ) && Object . keys ( args ) . length > 0 ;
42
41
}
43
42
44
- function renderArgTreeNodes (
43
+ function renderArgsTree (
45
44
trace : Trace ,
46
- args : ArgNode < Arg > [ ] ,
47
- extraMenuItems ?: ( arg : Arg ) => m . Children ,
45
+ key : string ,
46
+ fullKey : string ,
47
+ args : Args ,
48
+ extraMenuItems ?: ( path : string , arg : ArgValue ) => m . Children ,
48
49
) : m . Children {
49
- return args . map ( ( arg ) => {
50
- const { key, value, children} = arg ;
51
- if ( children && children . length === 1 ) {
52
- // If we only have one child, collapse into self and combine keys
53
- const child = children [ 0 ] ;
54
- const compositeArg = {
55
- ...child ,
56
- key : stringifyKey ( key , child . key ) ,
57
- } ;
58
- return renderArgTreeNodes ( trace , [ compositeArg ] , extraMenuItems ) ;
59
- } else {
60
- return m (
61
- TreeNode ,
62
- {
63
- left : renderArgKey ( stringifyKey ( key ) , value , extraMenuItems ) ,
64
- right : exists ( value ) && renderArgValue ( value ) ,
65
- summary : children && renderSummary ( children ) ,
66
- } ,
67
- children && renderArgTreeNodes ( trace , children , extraMenuItems ) ,
50
+ if ( args instanceof Array ) {
51
+ return m (
52
+ TreeNode ,
53
+ {
54
+ left : key ,
55
+ summary : renderArraySummary ( args ) ,
56
+ } ,
57
+ args . map ( ( value , index ) =>
58
+ renderArgsTree (
59
+ trace ,
60
+ `[${ index } ]` ,
61
+ `${ fullKey } [${ index } ]` ,
62
+ value ,
63
+ extraMenuItems ,
64
+ ) ,
65
+ ) ,
66
+ ) ;
67
+ }
68
+ if ( args !== null && typeof args === 'object' ) {
69
+ if ( Object . keys ( args ) . length === 1 ) {
70
+ const [ [ childName , value ] ] = Object . entries ( args ) ;
71
+ return renderArgsTree (
72
+ trace ,
73
+ `${ key } .${ childName } ` ,
74
+ `${ fullKey } .${ childName } ` ,
75
+ value ,
76
+ extraMenuItems ,
68
77
) ;
69
78
}
79
+ return m (
80
+ TreeNode ,
81
+ {
82
+ left : key ,
83
+ summary : renderDictSummary ( args ) ,
84
+ } ,
85
+ Object . entries ( args ) . map ( ( [ childName , child ] ) =>
86
+ renderArgsTree (
87
+ trace ,
88
+ childName ,
89
+ `${ fullKey } .${ childName } ` ,
90
+ child ,
91
+ extraMenuItems ,
92
+ ) ,
93
+ ) ,
94
+ ) ;
95
+ }
96
+ return m ( TreeNode , {
97
+ left : renderArgKey ( key , fullKey , args , extraMenuItems ) ,
98
+ right : renderArgValue ( args ) ,
70
99
} ) ;
71
100
}
72
101
73
102
function renderArgKey (
74
103
key : string ,
75
- value : Arg | undefined ,
76
- extraMenuItems ?: ( arg : Arg ) => m . Children ,
104
+ fullKey : string ,
105
+ value : ArgValue ,
106
+ extraMenuItems ?: ( path : string , arg : ArgValue ) => m . Children ,
77
107
) : m . Children {
78
108
if ( value === undefined ) {
79
109
return key ;
80
110
} else {
81
- const { key : fullKey } = value ;
82
111
return m (
83
112
PopupMenu ,
84
113
{ trigger : m ( Anchor , { icon : Icons . ContextMenu } , key ) } ,
@@ -87,44 +116,33 @@ function renderArgKey(
87
116
icon : 'content_copy' ,
88
117
onclick : ( ) => navigator . clipboard . writeText ( fullKey ) ,
89
118
} ) ,
90
- extraMenuItems ?.( value ) ,
119
+ extraMenuItems ?.( fullKey , value ) ,
91
120
) ;
92
121
}
93
122
}
94
123
95
- function renderArgValue ( { displayValue } : Arg ) : m . Children {
96
- if ( isWebLink ( displayValue ) ) {
97
- return renderWebLink ( displayValue ) ;
124
+ function renderArgValue ( value : ArgValue ) : m . Children {
125
+ if ( isWebLink ( value ) ) {
126
+ return renderWebLink ( value ) ;
98
127
} else {
99
- return `${ displayValue } ` ;
128
+ return `${ value } ` ;
100
129
}
101
130
}
102
131
103
- function renderSummary ( children : ArgNode < Arg > [ ] ) : m . Children {
104
- const summary = children
105
- . slice ( 0 , 2 )
106
- . map ( ( { key} ) => key )
107
- . join ( ', ' ) ;
108
- const remaining = children . length - 2 ;
132
+ function renderArraySummary ( children : Args [ ] ) : m . Children {
133
+ return `[ ... (${ children . length } items ]` ;
134
+ }
135
+
136
+ function renderDictSummary ( children : ArgsDict ) : m . Children {
137
+ const summary = Object . keys ( children ) . slice ( 0 , 2 ) . join ( ', ' ) ;
138
+ const remaining = Object . keys ( children ) . length - 2 ;
109
139
if ( remaining > 0 ) {
110
140
return `{${ summary } , ... (${ remaining } more items)}` ;
111
141
} else {
112
142
return `{${ summary } }` ;
113
143
}
114
144
}
115
145
116
- function stringifyKey ( ...key : Key [ ] ) : string {
117
- return key
118
- . map ( ( element , index ) => {
119
- if ( typeof element === 'number' ) {
120
- return `[${ element } ]` ;
121
- } else {
122
- return ( index === 0 ? '' : '.' ) + element ;
123
- }
124
- } )
125
- . join ( '' ) ;
126
- }
127
-
128
146
function isWebLink ( value : unknown ) : value is string {
129
147
return (
130
148
isString ( value ) &&
0 commit comments