File: vim9_expr.vim

package info (click to toggle)
vim 2%3A9.1.1230-2
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 88,180 kB
  • sloc: ansic: 416,989; cpp: 6,324; makefile: 4,448; java: 2,226; sh: 1,861; perl: 1,419; python: 960; awk: 730; lisp: 501; cs: 458; objc: 369; xml: 247; sed: 8; csh: 6
file content (163 lines) | stat: -rw-r--r-- 2,803 bytes parent folder | download
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
vim9script
# Vim9-script expressions


# Dictionary

echo {}
echo {   foo: 21 * 2 }
echo { -foo-: 21 * 2 }
echo {    42: 21 * 2 }
echo { 'foo': 21 * 2 }
echo { "foo": 21 * 2 }

echo { foo: {   bar: 21 * 2 } }
echo { foo: { -bar-: 21 * 2 } }
echo { foo: {    42: 21 * 2 } }
echo { foo: { 'bar': 21 * 2 } }
echo { foo: { "bar": 21 * 2 } }

echo { -foo-: {   bar: 21 * 2 } }
echo { -foo-: { -bar-: 21 * 2 } }
echo { -foo-: {    42: 21 * 2 } }
echo { -foo-: { 'bar': 21 * 2 } }
echo { -foo-: { "bar": 21 * 2 } }

echo { 42: {   bar: 21 * 2 } }
echo { 42: { -bar-: 21 * 2 } }
echo { 42: {    42: 21 * 2 } }
echo { 42: { 'bar': 21 * 2 } }
echo { 42: { "bar": 21 * 2 } }

echo { 'foo': {   bar: 21 * 2 } }
echo { 'foo': { -bar-: 21 * 2 } }
echo { 'foo': {    42: 21 * 2 } }
echo { 'foo': { "bar": 21 * 2 } }
echo { 'foo': { 'bar': 21 * 2 } }

echo { "foo": {   bar: 21 * 2 } }
echo { "foo": { -bar-: 21 * 2 } }
echo { "foo": {    42: 21 * 2 } }
echo { "foo": { 'bar': 21 * 2 } }
echo { "foo": { "bar": 21 * 2 } }

echo {
  # comment
  foo: {
    bar: 21 * 2
  }
}

# match as keys not scope dictionaries
echo { b: 42, w: 42, t: 42, g: 42, l: 42, s: 42, a: 42, v: 42  }

# Operators

# Ternary

echo expr ? expr : expr

echo lnum == 1 ? "top" : lnum
echo lnum == 1 ? "top" : lnum == 1000 ? "last" : lnum

echo lnum == 1
	? "top"
	: lnum == 1000
		? "last"
		: lnum
echo lnum == 1 ?
	"top" :
	lnum == 1000 ?
		"last" :
		lnum

echo true ? true : false
echo 1 ? 1 : 0
echo "foo" ? "foo" : "bar"
echo foo ? foo : bar
echo g:foo ? g:foo : g:bar
echo $FOO ? $FOO : $BAR
echo True() ? True() : False()
echo @a ? @a : @b
echo (true) ? (true) : (false)
echo (1) ? (1) : (0)

# Falsy

echo expr ?? expr

echo theList ?? 'list is empty'
echo GetName() ?? 'unknown'

echo theList
      \ ?? 'list is empty'
echo theList ??
      \ 'list is empty'

echo true ?? true
echo 1 ?? 1
echo "foo" ?? "foo"
echo foo ?? foo
echo g:foo ?? g:foo
echo $FOO ?? $FOO
echo True() ?? True()
echo @a ?? @a
echo (true) ?? (true)
echo (1) ?? (1)


# Function calls

Foo(true, false, null)


# Command {expr} arguments

if true
  echo true
elseif false
  echo false
endif

while true
  break
endwhile

def Foo(): bool
  return true
enddef


# Issue #14423 (vim.vim: Opt out of vimSearch*)

:?truthy
const truthy: number = false
    ? (0
    )
    : (1
    )
echo truthy

def Foo()
  :?truthy
  const truthy: number = false
      ? (0
      )
      : (1
      )
  echo truthy
enddef


# Issue #16227 (Vimscript ternary expression highlighting)

var foo = 'foo'                         # comment
var bar = foo == 'foo' ? 'bar' : 'baz'
var baz = foo == 'foo'
            \ ? 'baz'
            \ : 'bar'
var qux = foo == 'foo'
            ? 'qux'                     # comment
            : 'qux'                     # comment
echo qux ?? 'quux'