blob: 3d429f3a98ed56a5b1bdae7f4e306c3635f4aff3 [file] [log] [blame]
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02001" Test for the gf and gF (goto file) commands
Bram Moolenaar53f16732016-09-07 20:46:39 +02002
3" This is a test if a URL is recognized by "gf", with the cursor before and
4" after the "://". Also test ":\\".
Bram Moolenaar2a79ed22017-05-24 09:51:39 +02005func Test_gf_url()
Bram Moolenaar53f16732016-09-07 20:46:39 +02006 enew!
7 call append(0, [
8 \ "first test for URL://machine.name/tmp/vimtest2a and other text",
9 \ "second test for URL://machine.name/tmp/vimtest2b. And other text",
10 \ "third test for URL:\\\\machine.name\\vimtest2c and other text",
Bram Moolenaar9e3dfc62017-12-25 14:29:18 +010011 \ "fourth test for URL:\\\\machine.name\\tmp\\vimtest2d, and other text",
12 \ "fifth test for URL://machine.name/tmp?q=vim&opt=yes and other text",
Bram Moolenaarcbef8e12019-03-09 12:32:56 +010013 \ "sixth test for URL://machine.name:1234?q=vim and other text",
Bram Moolenaar53f16732016-09-07 20:46:39 +020014 \ ])
15 call cursor(1,1)
16 call search("^first")
17 call search("tmp")
18 call assert_equal("URL://machine.name/tmp/vimtest2a", expand("<cfile>"))
19 call search("^second")
20 call search("URL")
21 call assert_equal("URL://machine.name/tmp/vimtest2b", expand("<cfile>"))
Bram Moolenaar424bcae2022-01-31 14:59:41 +000022 set isf=@,48-57,/,.,-,_,+,,,$,~,\
Bram Moolenaar53f16732016-09-07 20:46:39 +020023 call search("^third")
24 call search("name")
25 call assert_equal("URL:\\\\machine.name\\vimtest2c", expand("<cfile>"))
26 call search("^fourth")
27 call search("URL")
28 call assert_equal("URL:\\\\machine.name\\tmp\\vimtest2d", expand("<cfile>"))
29
Bram Moolenaar9e3dfc62017-12-25 14:29:18 +010030 call search("^fifth")
31 call search("URL")
32 call assert_equal("URL://machine.name/tmp?q=vim&opt=yes", expand("<cfile>"))
33
Bram Moolenaarcbef8e12019-03-09 12:32:56 +010034 call search("^sixth")
35 call search("URL")
36 call assert_equal("URL://machine.name:1234?q=vim", expand("<cfile>"))
37
Bram Moolenaarbdd2c292020-06-22 21:34:30 +020038 %d
39 call setline(1, "demo://remote_file")
40 wincmd f
41 call assert_equal('demo://remote_file', @%)
42 call assert_equal(2, winnr('$'))
43 close!
44
Bram Moolenaar53f16732016-09-07 20:46:39 +020045 set isf&vim
46 enew!
Bram Moolenaar2a79ed22017-05-24 09:51:39 +020047endfunc
48
49func Test_gF()
50 new
51 call setline(1, ['111', '222', '333', '444'])
Bram Moolenaarb18b4962022-09-02 21:55:50 +010052 w! Xgffile
Bram Moolenaar2a79ed22017-05-24 09:51:39 +020053 close
54 new
Bram Moolenaar712598f2017-05-24 10:42:37 +020055 set isfname-=:
Bram Moolenaarb18b4962022-09-02 21:55:50 +010056 call setline(1, ['one', 'Xgffile:3', 'three'])
Bram Moolenaar2a79ed22017-05-24 09:51:39 +020057 2
58 call assert_fails('normal gF', 'E37:')
59 call assert_equal(2, getcurpos()[1])
Bram Moolenaarb18b4962022-09-02 21:55:50 +010060 w! Xgffile2
Bram Moolenaar2a79ed22017-05-24 09:51:39 +020061 normal gF
Bram Moolenaarb18b4962022-09-02 21:55:50 +010062 call assert_equal('Xgffile', bufname('%'))
Bram Moolenaar2a79ed22017-05-24 09:51:39 +020063 call assert_equal(3, getcurpos()[1])
64
Bram Moolenaar64e74c92019-12-22 15:38:06 +010065 enew!
Bram Moolenaarb18b4962022-09-02 21:55:50 +010066 call setline(1, ['one', 'the Xgffile line 2, and more', 'three'])
67 w! Xgffile2
Bram Moolenaar64e74c92019-12-22 15:38:06 +010068 normal 2GfX
69 normal gF
Bram Moolenaarb18b4962022-09-02 21:55:50 +010070 call assert_equal('Xgffile', bufname('%'))
Bram Moolenaar64e74c92019-12-22 15:38:06 +010071 call assert_equal(2, getcurpos()[1])
72
Bram Moolenaar5d3c9f82020-06-26 20:41:39 +020073 " jumping to the file/line with CTRL-W_F
74 %bw!
75 edit Xfile1
Bram Moolenaarb18b4962022-09-02 21:55:50 +010076 call setline(1, ['one', 'Xgffile:4', 'three'])
Bram Moolenaar5d3c9f82020-06-26 20:41:39 +020077 exe "normal 2G\<C-W>F"
Bram Moolenaarb18b4962022-09-02 21:55:50 +010078 call assert_equal('Xgffile', bufname('%'))
Bram Moolenaar5d3c9f82020-06-26 20:41:39 +020079 call assert_equal(4, getcurpos()[1])
80
Bram Moolenaar712598f2017-05-24 10:42:37 +020081 set isfname&
Bram Moolenaarb18b4962022-09-02 21:55:50 +010082 call delete('Xgffile')
83 call delete('Xgffile2')
Bram Moolenaar5d3c9f82020-06-26 20:41:39 +020084 %bw!
Bram Moolenaar2a79ed22017-05-24 09:51:39 +020085endfunc
Bram Moolenaarf0ab01f2019-05-06 22:00:00 +020086
87" Test for invoking 'gf' on a ${VAR} variable
88func Test_gf()
Bram Moolenaar424bcae2022-01-31 14:59:41 +000089 set isfname=@,48-57,/,.,-,_,+,,,$,:,~,{,}
Bram Moolenaarf0ab01f2019-05-06 22:00:00 +020090
Bram Moolenaar572a4432022-09-28 21:07:03 +010091 call writefile(["Test for gf command"], "Xtest1", 'D')
Bram Moolenaarf0ab01f2019-05-06 22:00:00 +020092 if has("unix")
Bram Moolenaar572a4432022-09-28 21:07:03 +010093 call writefile([" ${CDIR}/Xtest1"], "Xtestgf", 'D')
Bram Moolenaarf0ab01f2019-05-06 22:00:00 +020094 else
Bram Moolenaar572a4432022-09-28 21:07:03 +010095 call writefile([" $TDIR/Xtest1"], "Xtestgf", 'D')
Bram Moolenaarf0ab01f2019-05-06 22:00:00 +020096 endif
97 new Xtestgf
98 if has("unix")
99 let $CDIR = "."
100 /CDIR
101 else
102 if has("amiga")
103 let $TDIR = "/testdir"
104 else
105 let $TDIR = "."
106 endif
107 /TDIR
108 endif
109
110 normal gf
111 call assert_equal('Xtest1', fnamemodify(bufname(''), ":t"))
Bram Moolenaarf0ab01f2019-05-06 22:00:00 +0200112
Bram Moolenaar572a4432022-09-28 21:07:03 +0100113 close!
Bram Moolenaarf0ab01f2019-05-06 22:00:00 +0200114endfunc
Bram Moolenaar0208b6b2019-11-08 21:49:48 +0100115
116func Test_gf_visual()
Bram Moolenaar572a4432022-09-28 21:07:03 +0100117 call writefile(['one', 'two', 'three', 'four'], "Xtest_gf_visual", 'D')
Bram Moolenaar0208b6b2019-11-08 21:49:48 +0100118 new
119 call setline(1, 'XXXtest_gf_visualXXX')
120 set hidden
121
122 " Visually select Xtest_gf_visual and use gf to go to that file
123 norm! ttvtXgf
124 call assert_equal('Xtest_gf_visual', bufname('%'))
125
Bram Moolenaarbdd2c292020-06-22 21:34:30 +0200126 " if multiple lines are selected, then gf should fail
127 call setline(1, ["one", "two"])
128 normal VGgf
129 call assert_equal('Xtest_gf_visual', @%)
130
Bram Moolenaarefd5d8a2020-09-14 19:11:45 +0200131 " following line number is used for gF
132 bwipe!
133 new
134 call setline(1, 'XXXtest_gf_visual:3XXX')
135 norm! 0ttvt:gF
136 call assert_equal('Xtest_gf_visual', bufname('%'))
137 call assert_equal(3, getcurpos()[1])
138
John Drouhard95fca122022-08-01 11:38:17 +0100139 " do not include the NUL at the end
Bram Moolenaar572a4432022-09-28 21:07:03 +0100140 call writefile(['x'], 'Xvisual', 'D')
Bram Moolenaar395bd1f2022-05-14 21:29:44 +0100141 let save_enc = &enc
142 for enc in ['latin1', 'utf-8']
143 exe "set enc=" .. enc
144 new
Bram Moolenaar572a4432022-09-28 21:07:03 +0100145 call setline(1, 'Xvisual')
Bram Moolenaar395bd1f2022-05-14 21:29:44 +0100146 set nomodified
147 exe "normal \<C-V>$gf"
Bram Moolenaar572a4432022-09-28 21:07:03 +0100148 call assert_equal('Xvisual', bufname())
Bram Moolenaar395bd1f2022-05-14 21:29:44 +0100149 bwipe!
150 endfor
151 let &enc = save_enc
Bram Moolenaar395bd1f2022-05-14 21:29:44 +0100152
Bram Moolenaarefd5d8a2020-09-14 19:11:45 +0200153 " line number in visual area is used for file name
154 if has('unix')
155 bwipe!
Bram Moolenaar572a4432022-09-28 21:07:03 +0100156 call writefile([], "Xtest_gf_visual:3", 'D')
Bram Moolenaarefd5d8a2020-09-14 19:11:45 +0200157 new
158 call setline(1, 'XXXtest_gf_visual:3XXX')
159 norm! 0ttvtXgF
160 call assert_equal('Xtest_gf_visual:3', bufname('%'))
Bram Moolenaarefd5d8a2020-09-14 19:11:45 +0200161 endif
162
Bram Moolenaar0208b6b2019-11-08 21:49:48 +0100163 bwipe!
Bram Moolenaar0208b6b2019-11-08 21:49:48 +0100164 set hidden&
165endfunc
166
167func Test_gf_error()
168 new
169 call assert_fails('normal gf', 'E446:')
170 call assert_fails('normal gF', 'E446:')
171 call setline(1, '/doesnotexist')
172 call assert_fails('normal gf', 'E447:')
173 call assert_fails('normal gF', 'E447:')
Bram Moolenaar1671f442020-03-10 07:48:13 +0100174 call assert_fails('normal [f', 'E447:')
175
176 " gf is not allowed when text is locked
177 au InsertCharPre <buffer> normal! gF<CR>
Bram Moolenaarff06f282020-04-21 22:01:14 +0200178 let caught_e565 = 0
Bram Moolenaar1671f442020-03-10 07:48:13 +0100179 try
180 call feedkeys("ix\<esc>", 'xt')
Bram Moolenaarff06f282020-04-21 22:01:14 +0200181 catch /^Vim\%((\a\+)\)\=:E565/ " catch E565
182 let caught_e565 = 1
Bram Moolenaar1671f442020-03-10 07:48:13 +0100183 endtry
Bram Moolenaarff06f282020-04-21 22:01:14 +0200184 call assert_equal(1, caught_e565)
Bram Moolenaar1671f442020-03-10 07:48:13 +0100185 au! InsertCharPre
186
Bram Moolenaar0208b6b2019-11-08 21:49:48 +0100187 bwipe!
Bram Moolenaard1ad99b2020-10-04 16:16:54 +0200188
189 " gf is not allowed when buffer is locked
190 new
191 augroup Test_gf
192 au!
193 au OptionSet diff norm! gf
194 augroup END
195 call setline(1, ['Xfile1', 'line2', 'line3', 'line4'])
196 call test_override('starting', 1)
197 call assert_fails('diffthis', 'E788:')
198 call test_override('starting', 0)
199 augroup Test_gf
200 au!
201 augroup END
202 bw!
Bram Moolenaar0208b6b2019-11-08 21:49:48 +0100203endfunc
Bram Moolenaar1671f442020-03-10 07:48:13 +0100204
Bram Moolenaarbdd2c292020-06-22 21:34:30 +0200205" If a file is not found by 'gf', then 'includeexpr' should be used to locate
206" the file.
207func Test_gf_includeexpr()
208 new
209 let g:Inc_fname = ''
210 func IncFunc()
211 let g:Inc_fname = v:fname
212 return v:fname
213 endfunc
214 setlocal includeexpr=IncFunc()
215 call setline(1, 'somefile.java')
216 call assert_fails('normal gf', 'E447:')
217 call assert_equal('somefile.java', g:Inc_fname)
218 close!
219 delfunc IncFunc
220endfunc
221
Yegappan Lakshmanan8bb65f22021-12-26 10:51:39 +0000222" Test for using a script-local function for 'includeexpr'
223func Test_includeexpr_scriptlocal_func()
224 func! s:IncludeFunc()
225 let g:IncludeFname = v:fname
226 return ''
227 endfunc
228 set includeexpr=s:IncludeFunc()
229 call assert_equal(expand('<SID>') .. 'IncludeFunc()', &includeexpr)
230 new | only
231 call setline(1, 'TestFile1')
232 let g:IncludeFname = ''
233 call assert_fails('normal! gf', 'E447:')
234 call assert_equal('TestFile1', g:IncludeFname)
235 bw!
236 set includeexpr=<SID>IncludeFunc()
237 call assert_equal(expand('<SID>') .. 'IncludeFunc()', &includeexpr)
238 new | only
239 call setline(1, 'TestFile2')
240 let g:IncludeFname = ''
241 call assert_fails('normal! gf', 'E447:')
242 call assert_equal('TestFile2', g:IncludeFname)
243 set includeexpr&
244 delfunc s:IncludeFunc
245 bw!
246endfunc
247
John Drouhard95fca122022-08-01 11:38:17 +0100248" Check that expanding directories can handle more than 255 entries.
249func Test_gf_subdirs_wildcard()
250 let cwd = getcwd()
251 let dir = 'Xtestgf_dir'
Bram Moolenaar572a4432022-09-28 21:07:03 +0100252 call mkdir(dir, 'R')
John Drouhard95fca122022-08-01 11:38:17 +0100253 call chdir(dir)
254 for i in range(300)
255 call mkdir(i)
256 call writefile([], i .. '/' .. i, 'S')
257 endfor
258 set path=./**
259
260 new | only
261 call setline(1, '99')
262 w! Xtest1
263 normal gf
264 call assert_equal('99', fnamemodify(bufname(''), ":t"))
265
266 call chdir(cwd)
John Drouhard95fca122022-08-01 11:38:17 +0100267 set path&
268endfunc
269
Bram Moolenaar1671f442020-03-10 07:48:13 +0100270" vim: shiftwidth=2 sts=2 expandtab