@@ -103,6 +103,118 @@ func Get_expected_props()
103103 \ ]
104104endfunc
105105
106+ func Test_prop_find ()
107+ new
108+ call setline (1 , [' one one one' , ' twotwo' , ' three' , ' fourfour' , ' five' , ' sixsix' ])
109+
110+ " Add two text props on lines 1 and 5, and one spanning lines 2 to 4.
111+ call prop_type_add (' prop_name' , {' highlight' : ' Directory' })
112+ call prop_add (1 , 5 , {' type' : ' prop_name' , ' id' : 10 , ' length' : 3 })
113+ call prop_add (2 , 4 , {' type' : ' prop_name' , ' id' : 11 , ' end_lnum' : 4 , ' end_col' : 9 })
114+ call prop_add (5 , 4 , {' type' : ' prop_name' , ' id' : 12 , ' length' : 1 })
115+
116+ let expected = [
117+ \ {' lnum' : 1 , ' col' : 5 , ' length' : 3 , ' id' : 10 , ' type' : ' prop_name' , ' start' : 1 , ' end' : 1 },
118+ \ {' lnum' : 2 , ' col' : 4 , ' id' : 11 , ' type' : ' prop_name' , ' start' : 1 , ' end' : 0 },
119+ \ {' lnum' : 5 , ' col' : 4 , ' length' : 1 , ' id' : 12 , ' type' : ' prop_name' , ' start' : 1 , ' end' : 1 }
120+ \ ]
121+
122+ " Starting at line 5 col 1 this should find the prop at line 5 col 4.
123+ call cursor (5 ,1 )
124+ let result = prop_find ({' type' : ' prop_name' }, ' f' )
125+ call assert_equal (expected[2 ], result)
126+
127+ " With skipstart left at false (default), this should find the prop at line
128+ " 5 col 4.
129+ let result = prop_find ({' type' : ' prop_name' , ' lnum' : 5 , ' col' : 4 }, ' b' )
130+ call assert_equal (expected[2 ], result)
131+
132+ " With skipstart set to true, this should skip the prop at line 5 col 4.
133+ let result = prop_find ({' type' : ' prop_name' , ' lnum' : 5 , ' col' : 4 , ' skipstart' : 1 }, ' b' )
134+ unlet result.length
135+ call assert_equal (expected[1 ], result)
136+
137+ " Search backwards from line 1 col 10 to find the prop on the same line.
138+ let result = prop_find ({' type' : ' prop_name' , ' lnum' : 1 , ' col' : 10 }, ' b' )
139+ call assert_equal (expected[0 ], result)
140+
141+ " with skipstart set to false, if the start position is anywhere between the
142+ " start and end lines of a text prop (searching forward or backward), the
143+ " result should be the prop on the first line (the line with 'start' set to 1).
144+ call cursor (3 ,1 )
145+ let result = prop_find ({' type' : ' prop_name' }, ' f' )
146+ unlet result.length
147+ call assert_equal (expected[1 ], result)
148+ let result = prop_find ({' type' : ' prop_name' }, ' b' )
149+ unlet result.length
150+ call assert_equal (expected[1 ], result)
151+
152+ " with skipstart set to true, if the start position is anywhere between the
153+ " start and end lines of a text prop (searching forward or backward), all lines
154+ " of the prop will be skipped.
155+ let result = prop_find ({' type' : ' prop_name' , ' skipstart' : 1 }, ' b' )
156+ call assert_equal (expected[0 ], result)
157+ let result = prop_find ({' type' : ' prop_name' , ' skipstart' : 1 }, ' f' )
158+ call assert_equal (expected[2 ], result)
159+
160+ " Use skipstart to search through all props with type name 'prop_name'.
161+ " First forward...
162+ let lnum = 1
163+ let col = 1
164+ let i = 0
165+ for exp in expected
166+ let result = prop_find ({' type' : ' prop_name' , ' lnum' : lnum, ' col' : col , ' skipstart' : 1 }, ' f' )
167+ if ! has_key (exp , " length" )
168+ unlet result.length
169+ endif
170+ call assert_equal (exp , result)
171+ let lnum = result.lnum
172+ let col = result.col
173+ let i = i + 1
174+ endfor
175+
176+ " ...then backwards.
177+ let lnum = 6
178+ let col = 4
179+ let i = 2
180+ while i >= 0
181+ let result = prop_find ({' type' : ' prop_name' , ' lnum' : lnum, ' col' : col , ' skipstart' : 1 }, ' b' )
182+ if ! has_key (expected[i ], " length" )
183+ unlet result.length
184+ endif
185+ call assert_equal (expected[i ], result)
186+ let lnum = result.lnum
187+ let col = result.col
188+ let i = i - 1
189+ endwhile
190+
191+ " Starting from line 6 col 1 search backwards for prop with id 10.
192+ call cursor (6 ,1 )
193+ let result = prop_find ({' id' : 10 , ' skipstart' : 1 }, ' b' )
194+ call assert_equal (expected[0 ], result)
195+
196+ " Starting from line 1 col 1 search forwards for prop with id 12.
197+ call cursor (1 ,1 )
198+ let result = prop_find ({' id' : 12 }, ' f' )
199+ call assert_equal (expected[2 ], result)
200+
201+ " Search for a prop with an unknown id.
202+ let result = prop_find ({' id' : 999 }, ' f' )
203+ call assert_equal ({}, result)
204+
205+ " Search backwards from the proceeding position of the prop with id 11
206+ " (at line num 2 col 4). This should return an empty dict.
207+ let result = prop_find ({' id' : 11 , ' lnum' : 2 , ' col' : 3 }, ' b' )
208+ call assert_equal ({}, result)
209+
210+ " When lnum is given and col is omitted, use column 1.
211+ let result = prop_find ({' type' : ' prop_name' , ' lnum' : 1 }, ' f' )
212+ call assert_equal (expected[0 ], result)
213+
214+ call prop_clear (1 ,6 )
215+ call prop_type_delete (' prop_name' )
216+ endfunc
217+
106218func Test_prop_add ()
107219 new
108220 call AddPropTypes ()
0 commit comments