test_ins_complete.vim 96 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112
  1. " Test for insert completion
  2. source screendump.vim
  3. source check.vim
  4. source vim9.vim
  5. " Test for insert expansion
  6. func Test_ins_complete()
  7. edit test_ins_complete.vim
  8. " The files in the current directory interferes with the files
  9. " used by this test. So use a separate directory for the test.
  10. call mkdir('Xdir')
  11. cd Xdir
  12. set ff=unix
  13. call writefile(["test11\t36Gepeto\t/Tag/",
  14. \ "asd\ttest11file\t36G",
  15. \ "Makefile\tto\trun"], 'Xtestfile')
  16. call writefile(['', 'start of testfile',
  17. \ 'ru',
  18. \ 'run1',
  19. \ 'run2',
  20. \ 'STARTTEST',
  21. \ 'ENDTEST',
  22. \ 'end of testfile'], 'Xtestdata')
  23. set ff&
  24. enew!
  25. edit Xtestdata
  26. new
  27. call append(0, ['#include "Xtestfile"', ''])
  28. call cursor(2, 1)
  29. set cot=
  30. set cpt=.,w
  31. " add-expands (word from next line) from other window
  32. exe "normal iru\<C-N>\<C-N>\<C-X>\<C-N>\<Esc>\<C-A>"
  33. call assert_equal('run1 run3', getline('.'))
  34. " add-expands (current buffer first)
  35. exe "normal o\<C-P>\<C-X>\<C-N>"
  36. call assert_equal('run3 run3', getline('.'))
  37. " Local expansion, ends in an empty line (unless it becomes a global
  38. " expansion)
  39. exe "normal o\<C-X>\<C-P>\<C-P>\<C-P>\<C-P>\<C-P>"
  40. call assert_equal('', getline('.'))
  41. " starts Local and switches to global add-expansion
  42. exe "normal o\<C-X>\<C-P>\<C-P>\<C-X>\<C-X>\<C-N>\<C-X>\<C-N>\<C-N>"
  43. call assert_equal('run1 run2', getline('.'))
  44. set cpt=.,\ ,w,i
  45. " i-add-expands and switches to local
  46. exe "normal OM\<C-N>\<C-X>\<C-N>\<C-X>\<C-N>\<C-X>\<C-X>\<C-X>\<C-P>"
  47. call assert_equal("Makefile\tto\trun3", getline('.'))
  48. " add-expands lines (it would end in an empty line if it didn't ignore
  49. " itself)
  50. exe "normal o\<C-X>\<C-L>\<C-X>\<C-L>\<C-P>\<C-P>"
  51. call assert_equal("Makefile\tto\trun3", getline('.'))
  52. call assert_equal("Makefile\tto\trun3", getline(line('.') - 1))
  53. set cpt=kXtestfile
  54. " checks k-expansion, and file expansion (use Xtest11 instead of test11,
  55. " because TEST11.OUT may match first on DOS)
  56. write Xtest11.one
  57. write Xtest11.two
  58. exe "normal o\<C-N>\<Esc>IX\<Esc>A\<C-X>\<C-F>\<C-N>"
  59. call assert_equal('Xtest11.two', getline('.'))
  60. " use CTRL-X CTRL-F to complete Xtest11.one, remove it and then use CTRL-X
  61. " CTRL-F again to verify this doesn't cause trouble.
  62. exe "normal oXt\<C-X>\<C-F>\<BS>\<BS>\<BS>\<BS>\<BS>\<BS>\<BS>\<BS>\<C-X>\<C-F>"
  63. call assert_equal('Xtest11.one', getline('.'))
  64. normal ddk
  65. " Test for expanding a non-existing filename
  66. exe "normal oa1b2X3Y4\<C-X>\<C-F>"
  67. call assert_equal('a1b2X3Y4', getline('.'))
  68. normal ddk
  69. set cpt=w
  70. " checks make_cyclic in other window
  71. exe "normal oST\<C-N>\<C-P>\<C-P>\<C-P>\<C-P>"
  72. call assert_equal('STARTTEST', getline('.'))
  73. set cpt=u nohid
  74. " checks unloaded buffer expansion
  75. only
  76. exe "normal oEN\<C-N>"
  77. call assert_equal('ENDTEST', getline('.'))
  78. " checks adding mode abortion
  79. exe "normal ounl\<C-N>\<C-X>\<C-X>\<C-P>"
  80. call assert_equal('unless', getline('.'))
  81. set cpt=t,d def=^\\k* tags=Xtestfile notagbsearch
  82. " tag expansion, define add-expansion interrupted
  83. exe "normal o\<C-X>\<C-]>\<C-X>\<C-D>\<C-X>\<C-D>\<C-X>\<C-X>\<C-D>\<C-X>\<C-D>\<C-X>\<C-D>\<C-X>\<C-D>"
  84. call assert_equal('test11file 36Gepeto /Tag/ asd', getline('.'))
  85. " t-expansion
  86. exe "normal oa\<C-N>\<Esc>"
  87. call assert_equal('asd', getline('.'))
  88. %bw!
  89. call delete('Xtestfile')
  90. call delete('Xtest11.one')
  91. call delete('Xtest11.two')
  92. call delete('Xtestdata')
  93. set cpt& cot& def& tags& tagbsearch& hidden&
  94. cd ..
  95. call delete('Xdir', 'rf')
  96. endfunc
  97. func Test_ins_complete_invalid_byte()
  98. if has('unix') && executable('base64')
  99. " this weird command was causing an illegal memory access
  100. call writefile(['bm9ybTlvMDCAMM4Dbw4OGA4ODg=='], 'Xinvalid64')
  101. call system('base64 -d Xinvalid64 > Xinvalid')
  102. call writefile(['qa!'], 'Xexit')
  103. call RunVim([], [], " -i NONE -n -X -Z -e -m -s -S Xinvalid -S Xexit")
  104. call delete('Xinvalid64')
  105. call delete('Xinvalid')
  106. call delete('Xexit')
  107. endif
  108. endfunc
  109. func Test_omni_dash()
  110. func Omni(findstart, base)
  111. if a:findstart
  112. return 5
  113. else
  114. echom a:base
  115. return ['-help', '-v']
  116. endif
  117. endfunc
  118. set omnifunc=Omni
  119. new
  120. exe "normal Gofind -\<C-x>\<C-o>"
  121. call assert_equal("find -help", getline('$'))
  122. bwipe!
  123. delfunc Omni
  124. set omnifunc=
  125. endfunc
  126. func Test_omni_throw()
  127. let g:CallCount = 0
  128. func Omni(findstart, base)
  129. let g:CallCount += 1
  130. if a:findstart
  131. throw "he he he"
  132. endif
  133. endfunc
  134. set omnifunc=Omni
  135. new
  136. try
  137. exe "normal ifoo\<C-x>\<C-o>"
  138. call assert_false(v:true, 'command should have failed')
  139. catch
  140. call assert_exception('he he he')
  141. call assert_equal(1, g:CallCount)
  142. endtry
  143. bwipe!
  144. delfunc Omni
  145. unlet g:CallCount
  146. set omnifunc=
  147. endfunc
  148. func Test_completefunc_args()
  149. let s:args = []
  150. func! CompleteFunc(findstart, base)
  151. let s:args += [[a:findstart, empty(a:base)]]
  152. endfunc
  153. new
  154. set completefunc=CompleteFunc
  155. call feedkeys("i\<C-X>\<C-U>\<Esc>", 'x')
  156. call assert_equal([1, 1], s:args[0])
  157. call assert_equal(0, s:args[1][0])
  158. set completefunc=
  159. let s:args = []
  160. set omnifunc=CompleteFunc
  161. call feedkeys("i\<C-X>\<C-O>\<Esc>", 'x')
  162. call assert_equal([1, 1], s:args[0])
  163. call assert_equal(0, s:args[1][0])
  164. set omnifunc=
  165. bwipe!
  166. unlet s:args
  167. delfunc CompleteFunc
  168. endfunc
  169. func s:CompleteDone_CompleteFuncNone( findstart, base )
  170. throw 'skipped: Nvim does not support v:none'
  171. if a:findstart
  172. return 0
  173. endif
  174. return v:none
  175. endfunc
  176. func s:CompleteDone_CompleteFuncDict( findstart, base )
  177. if a:findstart
  178. return 0
  179. endif
  180. return {
  181. \ 'words': [
  182. \ {
  183. \ 'word': 'aword',
  184. \ 'abbr': 'wrd',
  185. \ 'menu': 'extra text',
  186. \ 'info': 'words are cool',
  187. \ 'kind': 'W',
  188. \ 'user_data': ['one', 'two']
  189. \ }
  190. \ ]
  191. \ }
  192. endfunc
  193. func s:CompleteDone_CheckCompletedItemNone()
  194. let s:called_completedone = 1
  195. endfunc
  196. func s:CompleteDone_CheckCompletedItemDict(pre)
  197. call assert_equal( 'aword', v:completed_item[ 'word' ] )
  198. call assert_equal( 'wrd', v:completed_item[ 'abbr' ] )
  199. call assert_equal( 'extra text', v:completed_item[ 'menu' ] )
  200. call assert_equal( 'words are cool', v:completed_item[ 'info' ] )
  201. call assert_equal( 'W', v:completed_item[ 'kind' ] )
  202. call assert_equal( ['one', 'two'], v:completed_item[ 'user_data' ] )
  203. if a:pre
  204. call assert_equal('function', complete_info().mode)
  205. endif
  206. let s:called_completedone = 1
  207. endfunc
  208. func Test_CompleteDoneNone()
  209. throw 'skipped: Nvim does not support v:none'
  210. au CompleteDone * :call <SID>CompleteDone_CheckCompletedItemNone()
  211. let oldline = join(map(range(&columns), 'nr2char(screenchar(&lines-1, v:val+1))'), '')
  212. set completefunc=<SID>CompleteDone_CompleteFuncNone
  213. execute "normal a\<C-X>\<C-U>\<C-Y>"
  214. set completefunc&
  215. let newline = join(map(range(&columns), 'nr2char(screenchar(&lines-1, v:val+1))'), '')
  216. call assert_true(s:called_completedone)
  217. call assert_equal(oldline, newline)
  218. let s:called_completedone = 0
  219. au! CompleteDone
  220. endfunc
  221. func Test_CompleteDone_vevent_keys()
  222. func OnDone()
  223. let g:complete_word = get(v:event, 'complete_word', v:null)
  224. let g:complete_type = get(v:event, 'complete_type', v:null)
  225. endfunction
  226. autocmd CompleteDone * :call OnDone()
  227. func CompleteFunc(findstart, base)
  228. if a:findstart
  229. return col(".")
  230. endif
  231. return [#{word: "foo"}, #{word: "bar"}]
  232. endfunc
  233. set omnifunc=CompleteFunc
  234. set completefunc=CompleteFunc
  235. set completeopt+=menuone
  236. new
  237. call feedkeys("A\<C-X>\<C-O>\<Esc>", 'tx')
  238. call assert_equal('', g:complete_word)
  239. call assert_equal('omni', g:complete_type)
  240. call feedkeys("S\<C-X>\<C-O>\<C-Y>\<Esc>", 'tx')
  241. call assert_equal('foo', g:complete_word)
  242. call assert_equal('omni', g:complete_type)
  243. call feedkeys("S\<C-X>\<C-O>\<C-N>\<C-Y>\<Esc>0", 'tx')
  244. call assert_equal('bar', g:complete_word)
  245. call assert_equal('omni', g:complete_type)
  246. call feedkeys("Shello vim visual v\<C-X>\<C-N>\<ESC>", 'tx')
  247. call assert_equal('', g:complete_word)
  248. call assert_equal('keyword', g:complete_type)
  249. call feedkeys("Shello vim visual v\<C-X>\<C-N>\<C-Y>", 'tx')
  250. call assert_equal('vim', g:complete_word)
  251. call assert_equal('keyword', g:complete_type)
  252. call feedkeys("Shello vim visual v\<C-X>\<C-N>\<C-Y>", 'tx')
  253. call assert_equal('vim', g:complete_word)
  254. call assert_equal('keyword', g:complete_type)
  255. call feedkeys("Shello vim\<CR>completion test\<CR>\<C-X>\<C-l>\<C-Y>", 'tx')
  256. call assert_equal('completion test', g:complete_word)
  257. call assert_equal('whole_line', g:complete_type)
  258. call feedkeys("S\<C-X>\<C-U>\<C-Y>", 'tx')
  259. call assert_equal('foo', g:complete_word)
  260. call assert_equal('function', g:complete_type)
  261. inoremap <buffer> <f3> <cmd>call complete(1, ["red", "blue"])<cr>
  262. call feedkeys("S\<f3>\<C-Y>", 'tx')
  263. call assert_equal('red', g:complete_word)
  264. call assert_equal('eval', g:complete_type)
  265. call feedkeys("S\<C-X>\<C-V>\<C-Y>", 'tx')
  266. call assert_equal('!', g:complete_word)
  267. call assert_equal('cmdline', g:complete_type)
  268. call writefile([''], 'foo_test', 'D')
  269. call feedkeys("Sfoo\<C-X>\<C-F>\<C-Y>\<Esc>", 'tx')
  270. call assert_equal('foo_test', g:complete_word)
  271. call assert_equal('files', g:complete_type)
  272. call writefile(['hello help'], 'test_case.txt', 'D')
  273. set dictionary=test_case.txt
  274. call feedkeys("ggdGSh\<C-X>\<C-K>\<C-Y>\<Esc>", 'tx')
  275. call assert_equal('hello', g:complete_word)
  276. call assert_equal('dictionary', g:complete_type)
  277. set spell spelllang=en_us
  278. call feedkeys("STheatre\<C-X>s\<C-Y>\<Esc>", 'tx')
  279. call assert_equal('Theater', g:complete_word)
  280. call assert_equal('spell', g:complete_type)
  281. bwipe!
  282. set completeopt& omnifunc& completefunc& spell& spelllang& dictionary&
  283. autocmd! CompleteDone
  284. delfunc OnDone
  285. delfunc CompleteFunc
  286. unlet g:complete_word
  287. unlet g:complete_type
  288. endfunc
  289. func Test_CompleteDoneDict()
  290. au CompleteDonePre * :call <SID>CompleteDone_CheckCompletedItemDict(1)
  291. au CompleteDone * :call <SID>CompleteDone_CheckCompletedItemDict(0)
  292. set completefunc=<SID>CompleteDone_CompleteFuncDict
  293. execute "normal a\<C-X>\<C-U>\<C-Y>"
  294. set completefunc&
  295. call assert_equal(['one', 'two'], v:completed_item[ 'user_data' ])
  296. call assert_true(s:called_completedone)
  297. let s:called_completedone = 0
  298. au! CompleteDone
  299. endfunc
  300. func s:CompleteDone_CompleteFuncDictNoUserData(findstart, base)
  301. if a:findstart
  302. return 0
  303. endif
  304. return {
  305. \ 'words': [
  306. \ {
  307. \ 'word': 'aword',
  308. \ 'abbr': 'wrd',
  309. \ 'menu': 'extra text',
  310. \ 'info': 'words are cool',
  311. \ 'kind': 'W',
  312. \ }
  313. \ ]
  314. \ }
  315. endfunc
  316. func s:CompleteDone_CheckCompletedItemDictNoUserData()
  317. call assert_equal( 'aword', v:completed_item[ 'word' ] )
  318. call assert_equal( 'wrd', v:completed_item[ 'abbr' ] )
  319. call assert_equal( 'extra text', v:completed_item[ 'menu' ] )
  320. call assert_equal( 'words are cool', v:completed_item[ 'info' ] )
  321. call assert_equal( 'W', v:completed_item[ 'kind' ] )
  322. call assert_equal( '', v:completed_item[ 'user_data' ] )
  323. let s:called_completedone = 1
  324. endfunc
  325. func Test_CompleteDoneDictNoUserData()
  326. au CompleteDone * :call <SID>CompleteDone_CheckCompletedItemDictNoUserData()
  327. set completefunc=<SID>CompleteDone_CompleteFuncDictNoUserData
  328. execute "normal a\<C-X>\<C-U>\<C-Y>"
  329. set completefunc&
  330. call assert_equal('', v:completed_item[ 'user_data' ])
  331. call assert_true(s:called_completedone)
  332. let s:called_completedone = 0
  333. au! CompleteDone
  334. endfunc
  335. func s:CompleteDone_CompleteFuncList(findstart, base)
  336. if a:findstart
  337. return 0
  338. endif
  339. return [ 'aword' ]
  340. endfunc
  341. func s:CompleteDone_CheckCompletedItemList()
  342. call assert_equal( 'aword', v:completed_item[ 'word' ] )
  343. call assert_equal( '', v:completed_item[ 'abbr' ] )
  344. call assert_equal( '', v:completed_item[ 'menu' ] )
  345. call assert_equal( '', v:completed_item[ 'info' ] )
  346. call assert_equal( '', v:completed_item[ 'kind' ] )
  347. call assert_equal( '', v:completed_item[ 'user_data' ] )
  348. let s:called_completedone = 1
  349. endfunc
  350. func Test_CompleteDoneList()
  351. au CompleteDone * :call <SID>CompleteDone_CheckCompletedItemList()
  352. set completefunc=<SID>CompleteDone_CompleteFuncList
  353. execute "normal a\<C-X>\<C-U>\<C-Y>"
  354. set completefunc&
  355. call assert_equal('', v:completed_item[ 'user_data' ])
  356. call assert_true(s:called_completedone)
  357. let s:called_completedone = 0
  358. au! CompleteDone
  359. endfunc
  360. func Test_CompleteDone_undo()
  361. au CompleteDone * call append(0, "prepend1")
  362. new
  363. call setline(1, ["line1", "line2"])
  364. call feedkeys("Go\<C-X>\<C-N>\<CR>\<ESC>", "tx")
  365. call assert_equal(["prepend1", "line1", "line2", "line1", ""],
  366. \ getline(1, '$'))
  367. undo
  368. call assert_equal(["line1", "line2"], getline(1, '$'))
  369. bwipe!
  370. au! CompleteDone
  371. endfunc
  372. func Test_CompleteDone_modify()
  373. let value = {
  374. \ 'word': '',
  375. \ 'abbr': '',
  376. \ 'menu': '',
  377. \ 'info': '',
  378. \ 'kind': '',
  379. \ 'user_data': '',
  380. \ }
  381. let v:completed_item = value
  382. call assert_equal(value, v:completed_item)
  383. endfunc
  384. func CompleteTest(findstart, query)
  385. if a:findstart
  386. return col('.')
  387. endif
  388. return ['matched']
  389. endfunc
  390. func Test_completefunc_info()
  391. new
  392. set completeopt=menuone
  393. set completefunc=CompleteTest
  394. call feedkeys("i\<C-X>\<C-U>\<C-R>\<C-R>=string(complete_info())\<CR>\<ESC>", "tx")
  395. call assert_equal("matched{'pum_visible': 1, 'mode': 'function', 'selected': 0, 'items': [{'word': 'matched', 'menu': '', 'user_data': '', 'info': '', 'kind': '', 'abbr': ''}]}", getline(1))
  396. bwipe!
  397. set completeopt&
  398. set completefunc&
  399. endfunc
  400. func CompleteInfoUserDefinedFn(findstart, query)
  401. " User defined function (i_CTRL-X_CTRL-U)
  402. if a:findstart
  403. return col('.')
  404. endif
  405. return [{'word': 'foo'}, {'word': 'bar'}, {'word': 'baz'}, {'word': 'qux'}]
  406. endfunc
  407. func CompleteInfoTestUserDefinedFn(mvmt, idx, noselect)
  408. new
  409. if a:noselect
  410. set completeopt=menuone,popup,noinsert,noselect
  411. else
  412. set completeopt=menu,preview
  413. endif
  414. set completefunc=CompleteInfoUserDefinedFn
  415. call feedkeys("i\<C-X>\<C-U>" . a:mvmt . "\<C-R>\<C-R>=string(complete_info())\<CR>\<ESC>", "tx")
  416. let completed = a:idx != -1 ? ['foo', 'bar', 'baz', 'qux']->get(a:idx) : ''
  417. call assert_equal(completed. "{'pum_visible': 1, 'mode': 'function', 'selected': " . a:idx . ", 'items': [" .
  418. \ "{'word': 'foo', 'menu': '', 'user_data': '', 'info': '', 'kind': '', 'abbr': ''}, " .
  419. \ "{'word': 'bar', 'menu': '', 'user_data': '', 'info': '', 'kind': '', 'abbr': ''}, " .
  420. \ "{'word': 'baz', 'menu': '', 'user_data': '', 'info': '', 'kind': '', 'abbr': ''}, " .
  421. \ "{'word': 'qux', 'menu': '', 'user_data': '', 'info': '', 'kind': '', 'abbr': ''}" .
  422. \ "]}", getline(1))
  423. bwipe!
  424. set completeopt&
  425. set completefunc&
  426. endfunc
  427. func Test_complete_info_user_defined_fn()
  428. " forward
  429. call CompleteInfoTestUserDefinedFn("\<C-N>\<C-N>", 1, v:true)
  430. call CompleteInfoTestUserDefinedFn("\<C-N>\<C-N>\<C-N>", 2, v:true)
  431. call CompleteInfoTestUserDefinedFn("\<C-N>\<C-N>", 2, v:false)
  432. call CompleteInfoTestUserDefinedFn("\<C-N>\<C-N>\<C-N>", 3, v:false)
  433. call CompleteInfoTestUserDefinedFn("\<C-N>\<C-N>\<C-N>\<C-N>", -1, v:false)
  434. " backward
  435. call CompleteInfoTestUserDefinedFn("\<C-P>\<C-P>", 2, v:true)
  436. call CompleteInfoTestUserDefinedFn("\<C-P>\<C-P>\<C-P>", 1, v:true)
  437. call CompleteInfoTestUserDefinedFn("\<C-P>\<C-P>\<C-P>\<C-P>\<C-P>", -1, v:true)
  438. call CompleteInfoTestUserDefinedFn("\<C-P>\<C-P>", 3, v:false)
  439. call CompleteInfoTestUserDefinedFn("\<C-P>\<C-P>\<C-P>", 2, v:false)
  440. " forward backward
  441. call CompleteInfoTestUserDefinedFn("\<C-N>\<C-N>\<C-N>\<C-P>", 1, v:true)
  442. call CompleteInfoTestUserDefinedFn("\<C-N>\<C-N>\<C-P>", 0, v:true)
  443. call CompleteInfoTestUserDefinedFn("\<C-N>\<C-N>\<C-N>\<C-P>", 2, v:false)
  444. call CompleteInfoTestUserDefinedFn("\<C-N>\<C-N>\<C-N>\<C-N>\<C-P>", 3, v:false)
  445. call CompleteInfoTestUserDefinedFn("\<C-N>\<C-N>\<C-P>", 1, v:false)
  446. " backward forward
  447. call CompleteInfoTestUserDefinedFn("\<C-P>\<C-P>\<C-P>\<C-P>\<C-P>\<C-N>", 0, v:true)
  448. call CompleteInfoTestUserDefinedFn("\<C-P>\<C-P>\<C-P>\<C-N>", 2, v:true)
  449. call CompleteInfoTestUserDefinedFn("\<C-P>\<C-P>\<C-P>\<C-P>\<C-P>\<C-N>", 1, v:false)
  450. call CompleteInfoTestUserDefinedFn("\<C-P>\<C-P>\<C-P>\<C-N>", 3, v:false)
  451. call CompleteInfoTestUserDefinedFn("\<C-P>\<C-N>\<C-N>", 1, v:false)
  452. endfunc
  453. " Test that mouse scrolling/movement should not interrupt completion.
  454. func Test_mouse_scroll_move_during_completion()
  455. new
  456. com! -buffer TestCommand1 echo 'TestCommand1'
  457. com! -buffer TestCommand2 echo 'TestCommand2'
  458. call setline(1, ['', '', '', '', ''])
  459. call cursor(5, 1)
  460. " Without completion menu scrolling can move text.
  461. set completeopt-=menu wrap
  462. call feedkeys("ccT\<C-X>\<C-V>\<ScrollWheelDown>\<C-V>", 'tx')
  463. call assert_equal('TestCommand2', getline('.'))
  464. call assert_notequal(1, winsaveview().topline)
  465. call feedkeys("ccT\<C-X>\<C-V>\<ScrollWheelUp>\<C-V>", 'tx')
  466. call assert_equal('TestCommand2', getline('.'))
  467. call assert_equal(1, winsaveview().topline)
  468. set nowrap
  469. call feedkeys("ccT\<C-X>\<C-V>\<ScrollWheelRight>\<C-V>", 'tx')
  470. call assert_equal('TestCommand2', getline('.'))
  471. call assert_notequal(0, winsaveview().leftcol)
  472. call feedkeys("ccT\<C-X>\<C-V>\<ScrollWheelLeft>\<C-V>", 'tx')
  473. call assert_equal('TestCommand2', getline('.'))
  474. call assert_equal(0, winsaveview().leftcol)
  475. call feedkeys("ccT\<C-X>\<C-V>\<MouseMove>\<C-V>", 'tx')
  476. call assert_equal('TestCommand2', getline('.'))
  477. " With completion menu scrolling cannot move text.
  478. set completeopt+=menu wrap
  479. call feedkeys("ccT\<C-X>\<C-V>\<ScrollWheelDown>\<C-V>", 'tx')
  480. call assert_equal('TestCommand2', getline('.'))
  481. call assert_equal(1, winsaveview().topline)
  482. call feedkeys("ccT\<C-X>\<C-V>\<ScrollWheelUp>\<C-V>", 'tx')
  483. call assert_equal('TestCommand2', getline('.'))
  484. call assert_equal(1, winsaveview().topline)
  485. set nowrap
  486. call feedkeys("ccT\<C-X>\<C-V>\<ScrollWheelRight>\<C-V>", 'tx')
  487. call assert_equal('TestCommand2', getline('.'))
  488. call assert_equal(0, winsaveview().leftcol)
  489. call feedkeys("ccT\<C-X>\<C-V>\<ScrollWheelLeft>\<C-V>", 'tx')
  490. call assert_equal('TestCommand2', getline('.'))
  491. call assert_equal(0, winsaveview().leftcol)
  492. call feedkeys("ccT\<C-X>\<C-V>\<MouseMove>\<C-V>", 'tx')
  493. call assert_equal('TestCommand2', getline('.'))
  494. bwipe!
  495. set completeopt& wrap&
  496. endfunc
  497. " Check that when using feedkeys() typeahead does not interrupt searching for
  498. " completions.
  499. func Test_compl_feedkeys()
  500. new
  501. set completeopt=menuone,noselect
  502. call feedkeys("ajump ju\<C-X>\<C-N>\<C-P>\<ESC>", "tx")
  503. call assert_equal("jump jump", getline(1))
  504. bwipe!
  505. set completeopt&
  506. endfunc
  507. func s:ComplInCmdwin_GlobalCompletion(a, l, p)
  508. return 'global'
  509. endfunc
  510. func s:ComplInCmdwin_LocalCompletion(a, l, p)
  511. return 'local'
  512. endfunc
  513. func Test_compl_in_cmdwin()
  514. set wildmenu wildchar=<Tab>
  515. com! -nargs=1 -complete=command GetInput let input = <q-args>
  516. com! -buffer TestCommand echo 'TestCommand'
  517. let w:test_winvar = 'winvar'
  518. let b:test_bufvar = 'bufvar'
  519. " User-defined commands
  520. let input = ''
  521. call feedkeys("q:iGetInput T\<C-x>\<C-v>\<CR>", 'tx!')
  522. call assert_equal('TestCommand', input)
  523. let input = ''
  524. call feedkeys("q::GetInput T\<Tab>\<CR>:q\<CR>", 'tx!')
  525. call assert_equal('T', input)
  526. com! -nargs=1 -complete=var GetInput let input = <q-args>
  527. " Window-local variables
  528. let input = ''
  529. call feedkeys("q:iGetInput w:test_\<C-x>\<C-v>\<CR>", 'tx!')
  530. call assert_equal('w:test_winvar', input)
  531. let input = ''
  532. call feedkeys("q::GetInput w:test_\<Tab>\<CR>:q\<CR>", 'tx!')
  533. call assert_equal('w:test_', input)
  534. " Buffer-local variables
  535. let input = ''
  536. call feedkeys("q:iGetInput b:test_\<C-x>\<C-v>\<CR>", 'tx!')
  537. call assert_equal('b:test_bufvar', input)
  538. let input = ''
  539. call feedkeys("q::GetInput b:test_\<Tab>\<CR>:q\<CR>", 'tx!')
  540. call assert_equal('b:test_', input)
  541. " Argument completion of buffer-local command
  542. func s:ComplInCmdwin_GlobalCompletionList(a, l, p)
  543. return ['global']
  544. endfunc
  545. func s:ComplInCmdwin_LocalCompletionList(a, l, p)
  546. return ['local']
  547. endfunc
  548. func s:ComplInCmdwin_CheckCompletion(arg)
  549. call assert_equal('local', a:arg)
  550. endfunc
  551. com! -nargs=1 -complete=custom,<SID>ComplInCmdwin_GlobalCompletion
  552. \ TestCommand call s:ComplInCmdwin_CheckCompletion(<q-args>)
  553. com! -buffer -nargs=1 -complete=custom,<SID>ComplInCmdwin_LocalCompletion
  554. \ TestCommand call s:ComplInCmdwin_CheckCompletion(<q-args>)
  555. call feedkeys("q:iTestCommand \<Tab>\<CR>", 'tx!')
  556. com! -nargs=1 -complete=customlist,<SID>ComplInCmdwin_GlobalCompletionList
  557. \ TestCommand call s:ComplInCmdwin_CheckCompletion(<q-args>)
  558. com! -buffer -nargs=1 -complete=customlist,<SID>ComplInCmdwin_LocalCompletionList
  559. \ TestCommand call s:ComplInCmdwin_CheckCompletion(<q-args>)
  560. call feedkeys("q:iTestCommand \<Tab>\<CR>", 'tx!')
  561. func! s:ComplInCmdwin_CheckCompletion(arg)
  562. call assert_equal('global', a:arg)
  563. endfunc
  564. new
  565. call feedkeys("q:iTestCommand \<Tab>\<CR>", 'tx!')
  566. quit
  567. delfunc s:ComplInCmdwin_GlobalCompletion
  568. delfunc s:ComplInCmdwin_LocalCompletion
  569. delfunc s:ComplInCmdwin_GlobalCompletionList
  570. delfunc s:ComplInCmdwin_LocalCompletionList
  571. delfunc s:ComplInCmdwin_CheckCompletion
  572. delcom -buffer TestCommand
  573. delcom TestCommand
  574. delcom GetInput
  575. unlet w:test_winvar
  576. unlet b:test_bufvar
  577. set wildmenu& wildchar&
  578. endfunc
  579. " Test for insert path completion with completeslash option
  580. func Test_ins_completeslash()
  581. CheckMSWindows
  582. call mkdir('Xdir')
  583. let orig_shellslash = &shellslash
  584. set cpt&
  585. new
  586. set noshellslash
  587. set completeslash=
  588. exe "normal oXd\<C-X>\<C-F>"
  589. call assert_equal('Xdir\', getline('.'))
  590. set completeslash=backslash
  591. exe "normal oXd\<C-X>\<C-F>"
  592. call assert_equal('Xdir\', getline('.'))
  593. set completeslash=slash
  594. exe "normal oXd\<C-X>\<C-F>"
  595. call assert_equal('Xdir/', getline('.'))
  596. set shellslash
  597. set completeslash=
  598. exe "normal oXd\<C-X>\<C-F>"
  599. call assert_equal('Xdir/', getline('.'))
  600. set completeslash=backslash
  601. exe "normal oXd\<C-X>\<C-F>"
  602. call assert_equal('Xdir\', getline('.'))
  603. set completeslash=slash
  604. exe "normal oXd\<C-X>\<C-F>"
  605. call assert_equal('Xdir/', getline('.'))
  606. %bw!
  607. call delete('Xdir', 'rf')
  608. set noshellslash
  609. set completeslash=slash
  610. call assert_true(stridx(globpath(&rtp, 'syntax/*.vim', 1, 1)[0], '\') != -1)
  611. let &shellslash = orig_shellslash
  612. set completeslash=
  613. endfunc
  614. func Test_pum_stopped_by_timer()
  615. CheckScreendump
  616. let lines =<< trim END
  617. call setline(1, ['hello', 'hullo', 'heeee', ''])
  618. func StartCompl()
  619. call timer_start(100, { -> execute('stopinsert') })
  620. call feedkeys("Gah\<C-N>")
  621. endfunc
  622. END
  623. call writefile(lines, 'Xpumscript')
  624. let buf = RunVimInTerminal('-S Xpumscript', #{rows: 12})
  625. call term_sendkeys(buf, ":call StartCompl()\<CR>")
  626. call TermWait(buf, 200)
  627. call term_sendkeys(buf, "k")
  628. call VerifyScreenDump(buf, 'Test_pum_stopped_by_timer', {})
  629. call StopVimInTerminal(buf)
  630. call delete('Xpumscript')
  631. endfunc
  632. func Test_complete_stopinsert_startinsert()
  633. nnoremap <F2> <Cmd>startinsert<CR>
  634. inoremap <F2> <Cmd>stopinsert<CR>
  635. " This just checks if this causes an error
  636. call feedkeys("i\<C-X>\<C-N>\<F2>\<F2>", 'x')
  637. nunmap <F2>
  638. iunmap <F2>
  639. endfunc
  640. func Test_pum_with_folds_two_tabs()
  641. CheckScreendump
  642. let lines =<< trim END
  643. set fdm=marker
  644. call setline(1, ['" x {{{1', '" a some text'])
  645. call setline(3, range(&lines)->map({_, val -> '" a' .. val}))
  646. norm! zm
  647. tab sp
  648. call feedkeys('2Gzv', 'xt')
  649. call feedkeys("0fa", 'xt')
  650. END
  651. call writefile(lines, 'Xpumscript')
  652. let buf = RunVimInTerminal('-S Xpumscript', #{rows: 10})
  653. call TermWait(buf, 50)
  654. call term_sendkeys(buf, "a\<C-N>")
  655. call VerifyScreenDump(buf, 'Test_pum_with_folds_two_tabs', {})
  656. call term_sendkeys(buf, "\<Esc>")
  657. call StopVimInTerminal(buf)
  658. call delete('Xpumscript')
  659. endfunc
  660. func Test_pum_with_preview_win()
  661. CheckScreendump
  662. let lines =<< trim END
  663. funct Omni_test(findstart, base)
  664. if a:findstart
  665. return col(".") - 1
  666. endif
  667. return [#{word: "one", info: "1info"}, #{word: "two", info: "2info"}, #{word: "three", info: "3info"}]
  668. endfunc
  669. set omnifunc=Omni_test
  670. set completeopt+=longest
  671. END
  672. call writefile(lines, 'Xpreviewscript')
  673. let buf = RunVimInTerminal('-S Xpreviewscript', #{rows: 12})
  674. call term_sendkeys(buf, "Gi\<C-X>\<C-O>")
  675. call TermWait(buf, 200)
  676. call term_sendkeys(buf, "\<C-N>")
  677. call VerifyScreenDump(buf, 'Test_pum_with_preview_win', {})
  678. call term_sendkeys(buf, "\<Esc>")
  679. call StopVimInTerminal(buf)
  680. call delete('Xpreviewscript')
  681. endfunc
  682. func Test_scrollbar_on_wide_char()
  683. CheckScreendump
  684. let lines =<< trim END
  685. call setline(1, ['a', ' 啊啊啊',
  686. \ ' 哦哦哦',
  687. \ ' 呃呃呃'])
  688. call setline(5, range(10)->map({i, v -> 'aa' .. v .. 'bb'}))
  689. END
  690. call writefile(lines, 'Xwidescript')
  691. let buf = RunVimInTerminal('-S Xwidescript', #{rows: 10})
  692. call term_sendkeys(buf, "A\<C-N>")
  693. call VerifyScreenDump(buf, 'Test_scrollbar_on_wide_char', {})
  694. call StopVimInTerminal(buf)
  695. call delete('Xwidescript')
  696. endfunc
  697. " Test for inserting the tag search pattern in insert mode
  698. func Test_ins_compl_tag_sft()
  699. call writefile([
  700. \ "!_TAG_FILE_ENCODING\tutf-8\t//",
  701. \ "first\tXfoo\t/^int first() {}$/",
  702. \ "second\tXfoo\t/^int second() {}$/",
  703. \ "third\tXfoo\t/^int third() {}$/"],
  704. \ 'Xtags')
  705. set tags=Xtags
  706. let code =<< trim [CODE]
  707. int first() {}
  708. int second() {}
  709. int third() {}
  710. [CODE]
  711. call writefile(code, 'Xfoo')
  712. enew
  713. set showfulltag
  714. exe "normal isec\<C-X>\<C-]>\<C-N>\<CR>"
  715. call assert_equal('int second() {}', getline(1))
  716. set noshowfulltag
  717. call delete('Xtags')
  718. call delete('Xfoo')
  719. set tags&
  720. %bwipe!
  721. endfunc
  722. " Test for 'completefunc' deleting text
  723. func Test_completefunc_error()
  724. new
  725. " delete text when called for the first time
  726. func CompleteFunc(findstart, base)
  727. if a:findstart == 1
  728. normal dd
  729. return col('.') - 1
  730. endif
  731. return ['a', 'b']
  732. endfunc
  733. set completefunc=CompleteFunc
  734. call setline(1, ['', 'abcd', ''])
  735. call assert_fails('exe "normal 2G$a\<C-X>\<C-U>"', 'E565:')
  736. " delete text when called for the second time
  737. func CompleteFunc2(findstart, base)
  738. if a:findstart == 1
  739. return col('.') - 1
  740. endif
  741. normal dd
  742. return ['a', 'b']
  743. endfunc
  744. set completefunc=CompleteFunc2
  745. call setline(1, ['', 'abcd', ''])
  746. call assert_fails('exe "normal 2G$a\<C-X>\<C-U>"', 'E565:')
  747. " Jump to a different window from the complete function
  748. func CompleteFunc3(findstart, base)
  749. if a:findstart == 1
  750. return col('.') - 1
  751. endif
  752. wincmd p
  753. return ['a', 'b']
  754. endfunc
  755. set completefunc=CompleteFunc3
  756. new
  757. call assert_fails('exe "normal a\<C-X>\<C-U>"', 'E565:')
  758. close!
  759. set completefunc&
  760. delfunc CompleteFunc
  761. delfunc CompleteFunc2
  762. delfunc CompleteFunc3
  763. close!
  764. endfunc
  765. " Test for returning non-string values from 'completefunc'
  766. func Test_completefunc_invalid_data()
  767. new
  768. func! CompleteFunc(findstart, base)
  769. if a:findstart == 1
  770. return col('.') - 1
  771. endif
  772. return [{}, '', 'moon']
  773. endfunc
  774. set completefunc=CompleteFunc
  775. exe "normal i\<C-X>\<C-U>"
  776. call assert_equal('moon', getline(1))
  777. set completefunc&
  778. close!
  779. endfunc
  780. " Test for errors in using complete() function
  781. func Test_complete_func_error()
  782. call assert_fails('call complete(1, ["a"])', 'E785:')
  783. func ListColors()
  784. call complete(col('.'), "blue")
  785. endfunc
  786. call assert_fails('exe "normal i\<C-R>=ListColors()\<CR>"', 'E474:')
  787. func ListMonths()
  788. call complete(col('.'), test_null_list())
  789. endfunc
  790. " Nvim allows a NULL list
  791. " call assert_fails('exe "normal i\<C-R>=ListMonths()\<CR>"', 'E474:')
  792. delfunc ListColors
  793. delfunc ListMonths
  794. call assert_fails('call complete_info({})', 'E714:')
  795. call assert_equal([], complete_info(['items']).items)
  796. endfunc
  797. " Test for recursively starting completion mode using complete()
  798. func Test_recursive_complete_func()
  799. func ListColors()
  800. call complete(5, ["red", "blue"])
  801. return ''
  802. endfunc
  803. new
  804. call setline(1, ['a1', 'a2'])
  805. set complete=.
  806. exe "normal Goa\<C-X>\<C-L>\<C-R>=ListColors()\<CR>\<C-N>"
  807. call assert_equal('a2blue', getline(3))
  808. delfunc ListColors
  809. bw!
  810. endfunc
  811. " Test for using complete() with completeopt+=longest
  812. func Test_complete_with_longest()
  813. new
  814. inoremap <buffer> <f3> <cmd>call complete(1, ["iaax", "iaay", "iaaz"])<cr>
  815. " default: insert first match
  816. set completeopt&
  817. call setline(1, ['i'])
  818. exe "normal Aa\<f3>\<esc>"
  819. call assert_equal('iaax', getline(1))
  820. " with longest: insert longest prefix
  821. set completeopt+=longest
  822. call setline(1, ['i'])
  823. exe "normal Aa\<f3>\<esc>"
  824. call assert_equal('iaa', getline(1))
  825. set completeopt&
  826. bwipe!
  827. endfunc
  828. " Test for buffer-local value of 'completeopt'
  829. func Test_completeopt_buffer_local()
  830. set completeopt=menu
  831. new
  832. call setline(1, ['foofoo', 'foobar', 'foobaz', ''])
  833. call assert_equal('', &l:completeopt)
  834. call assert_equal('menu', &completeopt)
  835. call assert_equal('menu', &g:completeopt)
  836. setlocal bufhidden=hide
  837. enew
  838. call setline(1, ['foofoo', 'foobar', 'foobaz', ''])
  839. call assert_equal('', &l:completeopt)
  840. call assert_equal('menu', &completeopt)
  841. call assert_equal('menu', &g:completeopt)
  842. setlocal completeopt+=fuzzy,noinsert
  843. call assert_equal('menu,fuzzy,noinsert', &l:completeopt)
  844. call assert_equal('menu,fuzzy,noinsert', &completeopt)
  845. call assert_equal('menu', &g:completeopt)
  846. call feedkeys("Gccf\<C-X>\<C-N>bz\<C-Y>", 'tnix')
  847. call assert_equal('foobaz', getline('.'))
  848. setlocal completeopt=
  849. call assert_equal('', &l:completeopt)
  850. call assert_equal('menu', &completeopt)
  851. call assert_equal('menu', &g:completeopt)
  852. call feedkeys("Gccf\<C-X>\<C-N>\<C-Y>", 'tnix')
  853. call assert_equal('foofoo', getline('.'))
  854. setlocal completeopt+=longest
  855. call assert_equal('menu,longest', &l:completeopt)
  856. call assert_equal('menu,longest', &completeopt)
  857. call assert_equal('menu', &g:completeopt)
  858. call feedkeys("Gccf\<C-X>\<C-N>\<C-X>\<C-Z>", 'tnix')
  859. call assert_equal('foo', getline('.'))
  860. setlocal bufhidden=hide
  861. buffer #
  862. call assert_equal('', &l:completeopt)
  863. call assert_equal('menu', &completeopt)
  864. call assert_equal('menu', &g:completeopt)
  865. call feedkeys("Gccf\<C-X>\<C-N>\<C-Y>", 'tnix')
  866. call assert_equal('foofoo', getline('.'))
  867. setlocal completeopt+=fuzzy,noinsert
  868. call assert_equal('menu,fuzzy,noinsert', &l:completeopt)
  869. call assert_equal('menu,fuzzy,noinsert', &completeopt)
  870. call assert_equal('menu', &g:completeopt)
  871. call feedkeys("Gccf\<C-X>\<C-N>bz\<C-Y>", 'tnix')
  872. call assert_equal('foobaz', getline('.'))
  873. buffer #
  874. call assert_equal('menu,longest', &l:completeopt)
  875. call assert_equal('menu,longest', &completeopt)
  876. call assert_equal('menu', &g:completeopt)
  877. call feedkeys("Gccf\<C-X>\<C-N>\<C-X>\<C-Z>", 'tnix')
  878. call assert_equal('foo', getline('.'))
  879. setlocal bufhidden=wipe
  880. buffer! #
  881. bwipe!
  882. call assert_equal('', &l:completeopt)
  883. call assert_equal('menu', &completeopt)
  884. call assert_equal('menu', &g:completeopt)
  885. new | only
  886. call setline(1, ['foofoo', 'foobar', 'foobaz', ''])
  887. set completeopt&
  888. setlocal completeopt=menu,fuzzy,noinsert
  889. setglobal completeopt=menu,longest
  890. call assert_equal('menu,fuzzy,noinsert', &completeopt)
  891. call assert_equal('menu,fuzzy,noinsert', &l:completeopt)
  892. call assert_equal('menu,longest', &g:completeopt)
  893. call feedkeys("Gccf\<C-X>\<C-N>bz\<C-Y>", 'tnix')
  894. call assert_equal('foobaz', getline('.'))
  895. setlocal bufhidden=wipe
  896. new | only!
  897. call setline(1, ['foofoo', 'foobar', 'foobaz', ''])
  898. call assert_equal('menu,longest', &completeopt)
  899. call assert_equal('menu,longest', &g:completeopt)
  900. call assert_equal('', &l:completeopt)
  901. call feedkeys("Gccf\<C-X>\<C-N>\<C-X>\<C-Z>", 'tnix')
  902. call assert_equal('foo', getline('.'))
  903. bwipe!
  904. new | only
  905. call setline(1, ['foofoo', 'foobar', 'foobaz', ''])
  906. set completeopt&
  907. setlocal completeopt=menu,fuzzy,noinsert
  908. set completeopt=menu,longest
  909. call assert_equal('menu,longest', &completeopt)
  910. call assert_equal('menu,longest', &g:completeopt)
  911. call assert_equal('', &l:completeopt)
  912. call feedkeys("Gccf\<C-X>\<C-N>\<C-X>\<C-Z>", 'tnix')
  913. call assert_equal('foo', getline('.'))
  914. setlocal bufhidden=wipe
  915. new | only!
  916. call setline(1, ['foofoo', 'foobar', 'foobaz', ''])
  917. call assert_equal('menu,longest', &completeopt)
  918. call assert_equal('menu,longest', &g:completeopt)
  919. call assert_equal('', &l:completeopt)
  920. call feedkeys("Gccf\<C-X>\<C-N>\<C-X>\<C-Z>", 'tnix')
  921. call assert_equal('foo', getline('.'))
  922. bwipe!
  923. set completeopt&
  924. endfunc
  925. " Test for completing words following a completed word in a line
  926. func Test_complete_wrapscan()
  927. " complete words from another buffer
  928. new
  929. call setline(1, ['one two', 'three four'])
  930. new
  931. setlocal complete=w
  932. call feedkeys("itw\<C-N>\<C-X>\<C-N>\<C-X>\<C-N>\<C-X>\<C-N>", 'xt')
  933. call assert_equal('two three four', getline(1))
  934. close!
  935. " complete words from the current buffer
  936. setlocal complete=.
  937. %d
  938. call setline(1, ['one two', ''])
  939. call cursor(2, 1)
  940. call feedkeys("ion\<C-N>\<C-X>\<C-N>\<C-X>\<C-N>\<C-X>\<C-N>", 'xt')
  941. call assert_equal('one two one two', getline(2))
  942. close!
  943. endfunc
  944. " Test for completing special characters
  945. func Test_complete_special_chars()
  946. new
  947. call setline(1, 'int .*[-\^$ func float')
  948. call feedkeys("oin\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>", 'xt')
  949. call assert_equal('int .*[-\^$ func float', getline(2))
  950. close!
  951. endfunc
  952. " Test for completion when text is wrapped across lines.
  953. func Test_complete_across_line()
  954. new
  955. call setline(1, ['red green blue', 'one two three'])
  956. setlocal textwidth=20
  957. exe "normal 2G$a re\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>"
  958. call assert_equal(['one two three red', 'green blue one'], getline(2, '$'))
  959. close!
  960. endfunc
  961. " Test for completing words with a '.' at the end of a word.
  962. func Test_complete_joinspaces()
  963. new
  964. call setline(1, ['one two.', 'three. four'])
  965. set joinspaces
  966. exe "normal Goon\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>"
  967. call assert_equal("one two. three. four", getline(3))
  968. set joinspaces&
  969. bw!
  970. endfunc
  971. " Test for using CTRL-L to add one character when completing matching
  972. func Test_complete_add_onechar()
  973. new
  974. call setline(1, ['wool', 'woodwork'])
  975. call feedkeys("Gowoo\<C-P>\<C-P>\<C-P>\<C-L>f", 'xt')
  976. call assert_equal('woof', getline(3))
  977. " use 'ignorecase' and backspace to erase characters from the prefix string
  978. " and then add letters using CTRL-L
  979. %d
  980. set ignorecase backspace=2
  981. setlocal complete=.
  982. call setline(1, ['workhorse', 'workload'])
  983. normal Go
  984. exe "normal aWOR\<C-P>\<bs>\<bs>\<bs>\<bs>\<bs>\<bs>\<C-L>\<C-L>\<C-L>"
  985. call assert_equal('workh', getline(3))
  986. set ignorecase& backspace&
  987. close!
  988. endfunc
  989. " Test for using CTRL-X CTRL-L to complete whole lines lines
  990. func Test_complete_wholeline()
  991. new
  992. " complete one-line
  993. call setline(1, ['a1', 'a2'])
  994. exe "normal ggoa\<C-X>\<C-L>"
  995. call assert_equal(['a1', 'a1', 'a2'], getline(1, '$'))
  996. " go to the next match (wrapping around the buffer)
  997. exe "normal 2GCa\<C-X>\<C-L>\<C-N>"
  998. call assert_equal(['a1', 'a', 'a2'], getline(1, '$'))
  999. " go to the next match
  1000. exe "normal 2GCa\<C-X>\<C-L>\<C-N>\<C-N>"
  1001. call assert_equal(['a1', 'a2', 'a2'], getline(1, '$'))
  1002. exe "normal 2GCa\<C-X>\<C-L>\<C-N>\<C-N>\<C-N>"
  1003. call assert_equal(['a1', 'a1', 'a2'], getline(1, '$'))
  1004. " repeat the test using CTRL-L
  1005. " go to the next match (wrapping around the buffer)
  1006. exe "normal 2GCa\<C-X>\<C-L>\<C-L>"
  1007. call assert_equal(['a1', 'a2', 'a2'], getline(1, '$'))
  1008. " go to the next match
  1009. exe "normal 2GCa\<C-X>\<C-L>\<C-L>\<C-L>"
  1010. call assert_equal(['a1', 'a', 'a2'], getline(1, '$'))
  1011. exe "normal 2GCa\<C-X>\<C-L>\<C-L>\<C-L>\<C-L>"
  1012. call assert_equal(['a1', 'a1', 'a2'], getline(1, '$'))
  1013. %d
  1014. " use CTRL-X CTRL-L to add one more line
  1015. call setline(1, ['a1', 'b1'])
  1016. setlocal complete=.
  1017. exe "normal ggOa\<C-X>\<C-L>\<C-X>\<C-L>\<C-X>\<C-L>"
  1018. call assert_equal(['a1', 'b1', '', 'a1', 'b1'], getline(1, '$'))
  1019. bw!
  1020. endfunc
  1021. " Test insert completion with 'cindent' (adjust the indent)
  1022. func Test_complete_with_cindent()
  1023. new
  1024. setlocal cindent
  1025. call setline(1, ['if (i == 1)', " j = 2;"])
  1026. exe "normal Go{\<CR>i\<C-X>\<C-L>\<C-X>\<C-L>\<CR>}"
  1027. call assert_equal(['{', "\tif (i == 1)", "\t\tj = 2;", '}'], getline(3, '$'))
  1028. %d
  1029. call setline(1, ['when while', '{', ''])
  1030. setlocal cinkeys+==while
  1031. exe "normal Giwh\<C-P> "
  1032. call assert_equal("\twhile ", getline('$'))
  1033. close!
  1034. endfunc
  1035. " Test for <CTRL-X> <CTRL-V> completion. Complete commands and functions
  1036. func Test_complete_cmdline()
  1037. new
  1038. exe "normal icaddb\<C-X>\<C-V>"
  1039. call assert_equal('caddbuffer', getline(1))
  1040. exe "normal ocall getqf\<C-X>\<C-V>"
  1041. call assert_equal('call getqflist(', getline(2))
  1042. exe "normal oabcxyz(\<C-X>\<C-V>"
  1043. call assert_equal('abcxyz(', getline(3))
  1044. com! -buffer TestCommand1 echo 'TestCommand1'
  1045. com! -buffer TestCommand2 echo 'TestCommand2'
  1046. write TestCommand1Test
  1047. write TestCommand2Test
  1048. " Test repeating <CTRL-X> <CTRL-V> and switching to another CTRL-X mode
  1049. exe "normal oT\<C-X>\<C-V>\<C-X>\<C-V>\<C-X>\<C-F>\<Esc>"
  1050. call assert_equal('TestCommand2Test', getline(4))
  1051. call delete('TestCommand1Test')
  1052. call delete('TestCommand2Test')
  1053. delcom TestCommand1
  1054. delcom TestCommand2
  1055. close!
  1056. endfunc
  1057. " Test for <CTRL-X> <CTRL-Z> stopping completion without changing the match
  1058. func Test_complete_stop()
  1059. new
  1060. func Save_mode1()
  1061. let g:mode1 = mode(1)
  1062. return ''
  1063. endfunc
  1064. func Save_mode2()
  1065. let g:mode2 = mode(1)
  1066. return ''
  1067. endfunc
  1068. inoremap <F1> <C-R>=Save_mode1()<CR>
  1069. inoremap <F2> <C-R>=Save_mode2()<CR>
  1070. call setline(1, ['aaa bbb ccc '])
  1071. exe "normal A\<C-N>\<C-P>\<F1>\<C-X>\<C-Z>\<F2>\<Esc>"
  1072. call assert_equal('ic', g:mode1)
  1073. call assert_equal('i', g:mode2)
  1074. call assert_equal('aaa bbb ccc ', getline(1))
  1075. exe "normal A\<C-N>\<Down>\<F1>\<C-X>\<C-Z>\<F2>\<Esc>"
  1076. call assert_equal('ic', g:mode1)
  1077. call assert_equal('i', g:mode2)
  1078. call assert_equal('aaa bbb ccc aaa', getline(1))
  1079. set completeopt+=noselect
  1080. exe "normal A \<C-N>\<Down>\<Down>\<C-L>\<C-L>\<F1>\<C-X>\<C-Z>\<F2>\<Esc>"
  1081. call assert_equal('ic', g:mode1)
  1082. call assert_equal('i', g:mode2)
  1083. call assert_equal('aaa bbb ccc aaa bb', getline(1))
  1084. set completeopt&
  1085. exe "normal A d\<C-N>\<F1>\<C-X>\<C-Z>\<F2>\<Esc>"
  1086. call assert_equal('ic', g:mode1)
  1087. call assert_equal('i', g:mode2)
  1088. call assert_equal('aaa bbb ccc aaa bb d', getline(1))
  1089. com! -buffer TestCommand1 echo 'TestCommand1'
  1090. com! -buffer TestCommand2 echo 'TestCommand2'
  1091. exe "normal oT\<C-X>\<C-V>\<C-X>\<C-V>\<F1>\<C-X>\<C-Z>\<F2>\<Esc>"
  1092. call assert_equal('ic', g:mode1)
  1093. call assert_equal('i', g:mode2)
  1094. call assert_equal('TestCommand2', getline(2))
  1095. delcom TestCommand1
  1096. delcom TestCommand2
  1097. unlet g:mode1
  1098. unlet g:mode2
  1099. iunmap <F1>
  1100. iunmap <F2>
  1101. delfunc Save_mode1
  1102. delfunc Save_mode2
  1103. close!
  1104. endfunc
  1105. " Test for typing CTRL-R in insert completion mode to insert a register
  1106. " content.
  1107. func Test_complete_reginsert()
  1108. new
  1109. call setline(1, ['a1', 'a12', 'a123', 'a1234'])
  1110. " if a valid CTRL-X mode key is returned from <C-R>=, then it should be
  1111. " processed. Otherwise, CTRL-X mode should be stopped and the key should be
  1112. " inserted.
  1113. exe "normal Goa\<C-P>\<C-R>=\"\\<C-P>\"\<CR>"
  1114. call assert_equal('a123', getline(5))
  1115. let @r = "\<C-P>\<C-P>"
  1116. exe "normal GCa\<C-P>\<C-R>r"
  1117. call assert_equal('a12', getline(5))
  1118. exe "normal GCa\<C-P>\<C-R>=\"x\"\<CR>"
  1119. call assert_equal('a1234x', getline(5))
  1120. bw!
  1121. endfunc
  1122. func Test_issue_7021()
  1123. CheckMSWindows
  1124. let orig_shellslash = &shellslash
  1125. set noshellslash
  1126. set completeslash=slash
  1127. call assert_false(expand('~') =~ '/')
  1128. let &shellslash = orig_shellslash
  1129. set completeslash=
  1130. endfunc
  1131. " Test for 'longest' setting in 'completeopt' with latin1 and utf-8 encodings
  1132. func Test_complete_longest_match()
  1133. " for e in ['latin1', 'utf-8']
  1134. for e in ['utf-8']
  1135. exe 'set encoding=' .. e
  1136. new
  1137. set complete=.
  1138. set completeopt=menu,longest
  1139. call setline(1, ['pfx_a1', 'pfx_a12', 'pfx_a123', 'pfx_b1'])
  1140. exe "normal Gopfx\<C-P>"
  1141. call assert_equal('pfx_', getline(5))
  1142. bw!
  1143. endfor
  1144. " Test for completing additional words with longest match set
  1145. new
  1146. call setline(1, ['abc1', 'abd2'])
  1147. exe "normal Goab\<C-P>\<C-X>\<C-P>"
  1148. call assert_equal('ab', getline(3))
  1149. bw!
  1150. set complete& completeopt&
  1151. endfunc
  1152. " Test for removing the first displayed completion match and selecting the
  1153. " match just before that.
  1154. func Test_complete_erase_firstmatch()
  1155. new
  1156. call setline(1, ['a12', 'a34', 'a56'])
  1157. set complete=.
  1158. exe "normal Goa\<C-P>\<BS>\<BS>3\<CR>"
  1159. call assert_equal('a34', getline('$'))
  1160. set complete&
  1161. bw!
  1162. endfunc
  1163. " Test for completing words from unloaded buffers
  1164. func Test_complete_from_unloadedbuf()
  1165. call writefile(['abc'], "Xfile1")
  1166. call writefile(['def'], "Xfile2")
  1167. edit Xfile1
  1168. edit Xfile2
  1169. new | close
  1170. enew
  1171. bunload Xfile1 Xfile2
  1172. set complete=u
  1173. " complete from an unloaded buffer
  1174. exe "normal! ia\<C-P>"
  1175. call assert_equal('abc', getline(1))
  1176. exe "normal! od\<C-P>"
  1177. call assert_equal('def', getline(2))
  1178. set complete&
  1179. %bw!
  1180. call delete("Xfile1")
  1181. call delete("Xfile2")
  1182. endfunc
  1183. " Test for completing whole lines from unloaded buffers
  1184. func Test_complete_wholeline_unloadedbuf()
  1185. call writefile(['a line1', 'a line2', 'a line3'], "Xfile1")
  1186. edit Xfile1
  1187. enew
  1188. set complete=u
  1189. exe "normal! ia\<C-X>\<C-L>\<C-P>"
  1190. call assert_equal('a line2', getline(1))
  1191. %d
  1192. " completing from an unlisted buffer should fail
  1193. bdel Xfile1
  1194. exe "normal! ia\<C-X>\<C-L>\<C-P>"
  1195. call assert_equal('a', getline(1))
  1196. set complete&
  1197. %bw!
  1198. call delete("Xfile1")
  1199. endfunc
  1200. " Test for completing words from unlisted buffers
  1201. func Test_complete_from_unlistedbuf()
  1202. call writefile(['abc'], "Xfile1")
  1203. call writefile(['def'], "Xfile2")
  1204. edit Xfile1
  1205. edit Xfile2
  1206. new | close
  1207. bdel Xfile1 Xfile2
  1208. set complete=U
  1209. " complete from an unlisted buffer
  1210. exe "normal! ia\<C-P>"
  1211. call assert_equal('abc', getline(1))
  1212. exe "normal! od\<C-P>"
  1213. call assert_equal('def', getline(2))
  1214. set complete&
  1215. %bw!
  1216. call delete("Xfile1")
  1217. call delete("Xfile2")
  1218. endfunc
  1219. " Test for completing whole lines from unlisted buffers
  1220. func Test_complete_wholeline_unlistedbuf()
  1221. call writefile(['a line1', 'a line2', 'a line3'], "Xfile1")
  1222. edit Xfile1
  1223. enew
  1224. set complete=U
  1225. " completing from an unloaded buffer should fail
  1226. exe "normal! ia\<C-X>\<C-L>\<C-P>"
  1227. call assert_equal('a', getline(1))
  1228. %d
  1229. bdel Xfile1
  1230. exe "normal! ia\<C-X>\<C-L>\<C-P>"
  1231. call assert_equal('a line2', getline(1))
  1232. set complete&
  1233. %bw!
  1234. call delete("Xfile1")
  1235. endfunc
  1236. " Test for adding a multibyte character using CTRL-L in completion mode
  1237. func Test_complete_mbyte_char_add()
  1238. new
  1239. set complete=.
  1240. call setline(1, 'abė')
  1241. exe "normal! oa\<C-P>\<BS>\<BS>\<C-L>\<C-L>"
  1242. call assert_equal('abė', getline(2))
  1243. " Test for a leader with multibyte character
  1244. %d
  1245. call setline(1, 'abėĕ')
  1246. exe "normal! oabė\<C-P>"
  1247. call assert_equal('abėĕ', getline(2))
  1248. bw!
  1249. endfunc
  1250. " Test for using <C-X><C-P> for local expansion even if 'complete' is set to
  1251. " not to complete matches from the local buffer. Also test using multiple
  1252. " <C-X> to cancel the current completion mode.
  1253. func Test_complete_local_expansion()
  1254. new
  1255. set complete=t
  1256. call setline(1, ['abc', 'def'])
  1257. exe "normal! Go\<C-X>\<C-P>"
  1258. call assert_equal("def", getline(3))
  1259. exe "normal! Go\<C-P>"
  1260. call assert_equal("", getline(4))
  1261. exe "normal! Go\<C-X>\<C-N>"
  1262. call assert_equal("abc", getline(5))
  1263. exe "normal! Go\<C-N>"
  1264. call assert_equal("", getline(6))
  1265. " use multiple <C-X> to cancel the previous completion mode
  1266. exe "normal! Go\<C-P>\<C-X>\<C-P>"
  1267. call assert_equal("", getline(7))
  1268. exe "normal! Go\<C-P>\<C-X>\<C-X>\<C-P>"
  1269. call assert_equal("", getline(8))
  1270. exe "normal! Go\<C-P>\<C-X>\<C-X>\<C-X>\<C-P>"
  1271. call assert_equal("abc", getline(9))
  1272. " interrupt the current completion mode
  1273. set completeopt=menu,noinsert
  1274. exe "normal! Go\<C-X>\<C-F>\<C-X>\<C-X>\<C-P>\<C-Y>"
  1275. call assert_equal("abc", getline(10))
  1276. " when only one <C-X> is used to interrupt, do normal expansion
  1277. exe "normal! Go\<C-X>\<C-F>\<C-X>\<C-P>"
  1278. call assert_equal("", getline(11))
  1279. set completeopt&
  1280. " using two <C-X> in non-completion mode and restarting the same mode
  1281. exe "normal! God\<C-X>\<C-X>\<C-P>\<C-X>\<C-X>\<C-P>\<C-Y>"
  1282. call assert_equal("def", getline(12))
  1283. " test for adding a match from the original empty text
  1284. %d
  1285. call setline(1, 'abc def g')
  1286. exe "normal! o\<C-X>\<C-P>\<C-N>\<C-X>\<C-P>"
  1287. call assert_equal('def', getline(2))
  1288. exe "normal! 0C\<C-X>\<C-N>\<C-P>\<C-X>\<C-N>"
  1289. call assert_equal('abc', getline(2))
  1290. bw!
  1291. endfunc
  1292. " Test for undoing changes after a insert-mode completion
  1293. func Test_complete_undo()
  1294. new
  1295. set complete=.
  1296. " undo with 'ignorecase'
  1297. call setline(1, ['ABOVE', 'BELOW'])
  1298. set ignorecase
  1299. exe "normal! Goab\<C-G>u\<C-P>"
  1300. call assert_equal("ABOVE", getline(3))
  1301. undo
  1302. call assert_equal("ab", getline(3))
  1303. set ignorecase&
  1304. %d
  1305. " undo with longest match
  1306. set completeopt=menu,longest
  1307. call setline(1, ['above', 'about'])
  1308. exe "normal! Goa\<C-G>u\<C-P>"
  1309. call assert_equal("abo", getline(3))
  1310. undo
  1311. call assert_equal("a", getline(3))
  1312. set completeopt&
  1313. %d
  1314. " undo for line completion
  1315. call setline(1, ['above that change', 'below that change'])
  1316. exe "normal! Goabove\<C-G>u\<C-X>\<C-L>"
  1317. call assert_equal("above that change", getline(3))
  1318. undo
  1319. call assert_equal("above", getline(3))
  1320. bw!
  1321. endfunc
  1322. " Test for completing a very long word
  1323. func Test_complete_long_word()
  1324. set complete&
  1325. new
  1326. call setline(1, repeat('x', 950) .. ' one two three')
  1327. exe "normal! Gox\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>"
  1328. call assert_equal(repeat('x', 950) .. ' one two three', getline(2))
  1329. %d
  1330. " should fail when more than 950 characters are in a word
  1331. call setline(1, repeat('x', 951) .. ' one two three')
  1332. exe "normal! Gox\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>\<C-X>\<C-P>"
  1333. call assert_equal(repeat('x', 951), getline(2))
  1334. " Test for adding a very long word to an existing completion
  1335. %d
  1336. call setline(1, ['abc', repeat('x', 1016) .. '012345'])
  1337. exe "normal! Goab\<C-P>\<C-X>\<C-P>"
  1338. call assert_equal('abc ' .. repeat('x', 1016) .. '0123', getline(3))
  1339. bw!
  1340. endfunc
  1341. " Test for some fields in the complete items used by complete()
  1342. func Test_complete_items()
  1343. func CompleteItems(idx)
  1344. let items = [[#{word: "one", dup: 1, user_data: 'u1'}, #{word: "one", dup: 1, user_data: 'u2'}],
  1345. \ [#{word: "one", dup: 0, user_data: 'u3'}, #{word: "one", dup: 0, user_data: 'u4'}],
  1346. \ [#{word: "one", icase: 1, user_data: 'u7'}, #{word: "oNE", icase: 1, user_data: 'u8'}],
  1347. \ [#{user_data: 'u9'}],
  1348. \ [#{word: "", user_data: 'u10'}],
  1349. \ [#{word: "", empty: 1, user_data: 'u11'}]]
  1350. call complete(col('.'), items[a:idx])
  1351. return ''
  1352. endfunc
  1353. new
  1354. exe "normal! i\<C-R>=CompleteItems(0)\<CR>\<C-N>\<C-Y>"
  1355. call assert_equal('u2', v:completed_item.user_data)
  1356. call assert_equal('one', getline(1))
  1357. exe "normal! o\<C-R>=CompleteItems(1)\<CR>\<C-Y>"
  1358. call assert_equal('u3', v:completed_item.user_data)
  1359. call assert_equal('one', getline(2))
  1360. exe "normal! o\<C-R>=CompleteItems(1)\<CR>\<C-N>"
  1361. call assert_equal('', getline(3))
  1362. set completeopt=menu,noinsert
  1363. exe "normal! o\<C-R>=CompleteItems(2)\<CR>one\<C-N>\<C-Y>"
  1364. call assert_equal('oNE', getline(4))
  1365. call assert_equal('u8', v:completed_item.user_data)
  1366. set completeopt&
  1367. exe "normal! o\<C-R>=CompleteItems(3)\<CR>"
  1368. call assert_equal('', getline(5))
  1369. exe "normal! o\<C-R>=CompleteItems(4)\<CR>"
  1370. call assert_equal('', getline(6))
  1371. exe "normal! o\<C-R>=CompleteItems(5)\<CR>"
  1372. call assert_equal('', getline(7))
  1373. call assert_equal('u11', v:completed_item.user_data)
  1374. " pass invalid argument to complete()
  1375. let cmd = "normal! o\<C-R>=complete(1, [[]])\<CR>"
  1376. call assert_fails('exe cmd', 'E730:')
  1377. bw!
  1378. delfunc CompleteItems
  1379. endfunc
  1380. " Test for the "refresh" item in the dict returned by an insert completion
  1381. " function
  1382. func Test_complete_item_refresh_always()
  1383. let g:CallCount = 0
  1384. func! Tcomplete(findstart, base)
  1385. if a:findstart
  1386. " locate the start of the word
  1387. let line = getline('.')
  1388. let start = col('.') - 1
  1389. while start > 0 && line[start - 1] =~ '\a'
  1390. let start -= 1
  1391. endwhile
  1392. return start
  1393. else
  1394. let g:CallCount += 1
  1395. let res = ["update1", "update12", "update123"]
  1396. return #{words: res, refresh: 'always'}
  1397. endif
  1398. endfunc
  1399. new
  1400. set completeopt=menu,longest
  1401. set completefunc=Tcomplete
  1402. exe "normal! iup\<C-X>\<C-U>\<BS>\<BS>\<BS>\<BS>\<BS>"
  1403. call assert_equal('up', getline(1))
  1404. call assert_equal(6, g:CallCount)
  1405. set completeopt&
  1406. set completefunc&
  1407. bw!
  1408. delfunc Tcomplete
  1409. endfunc
  1410. " Test for completing from a thesaurus file without read permission
  1411. func Test_complete_unreadable_thesaurus_file()
  1412. CheckUnix
  1413. CheckNotRoot
  1414. call writefile(['about', 'above'], 'Xfile')
  1415. call setfperm('Xfile', '---r--r--')
  1416. new
  1417. set complete=sXfile
  1418. exe "normal! ia\<C-P>"
  1419. call assert_equal('a', getline(1))
  1420. bw!
  1421. call delete('Xfile')
  1422. set complete&
  1423. endfunc
  1424. " Test to ensure 'Scanning...' messages are not recorded in messages history
  1425. func Test_z1_complete_no_history()
  1426. new
  1427. messages clear
  1428. let currmess = execute('messages')
  1429. setlocal dictionary=README.txt
  1430. exe "normal owh\<C-X>\<C-K>"
  1431. exe "normal owh\<C-N>"
  1432. call assert_equal(currmess, execute('messages'))
  1433. bwipe!
  1434. endfunc
  1435. " A mapping is not used for the key after CTRL-X.
  1436. func Test_no_mapping_for_ctrl_x_key()
  1437. new
  1438. inoremap <buffer> <C-K> <Cmd>let was_mapped = 'yes'<CR>
  1439. setlocal dictionary=README.txt
  1440. call feedkeys("aexam\<C-X>\<C-K> ", 'xt')
  1441. call assert_equal('example ', getline(1))
  1442. call assert_false(exists('was_mapped'))
  1443. bwipe!
  1444. endfunc
  1445. " Test for different ways of setting the 'completefunc' option
  1446. func Test_completefunc_callback()
  1447. func CompleteFunc1(callnr, findstart, base)
  1448. call add(g:CompleteFunc1Args, [a:callnr, a:findstart, a:base])
  1449. return a:findstart ? 0 : []
  1450. endfunc
  1451. func CompleteFunc2(findstart, base)
  1452. call add(g:CompleteFunc2Args, [a:findstart, a:base])
  1453. return a:findstart ? 0 : []
  1454. endfunc
  1455. let lines =<< trim END
  1456. #" Test for using a global function name
  1457. LET &completefunc = 'g:CompleteFunc2'
  1458. new
  1459. call setline(1, 'global')
  1460. LET g:CompleteFunc2Args = []
  1461. call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
  1462. call assert_equal([[1, ''], [0, 'global']], g:CompleteFunc2Args)
  1463. bw!
  1464. #" Test for using a function()
  1465. set completefunc=function('g:CompleteFunc1',\ [10])
  1466. new
  1467. call setline(1, 'one')
  1468. LET g:CompleteFunc1Args = []
  1469. call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
  1470. call assert_equal([[10, 1, ''], [10, 0, 'one']], g:CompleteFunc1Args)
  1471. bw!
  1472. #" Using a funcref variable to set 'completefunc'
  1473. VAR Fn = function('g:CompleteFunc1', [11])
  1474. LET &completefunc = Fn
  1475. new
  1476. call setline(1, 'two')
  1477. LET g:CompleteFunc1Args = []
  1478. call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
  1479. call assert_equal([[11, 1, ''], [11, 0, 'two']], g:CompleteFunc1Args)
  1480. bw!
  1481. #" Using string(funcref_variable) to set 'completefunc'
  1482. LET Fn = function('g:CompleteFunc1', [12])
  1483. LET &completefunc = string(Fn)
  1484. new
  1485. call setline(1, 'two')
  1486. LET g:CompleteFunc1Args = []
  1487. call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
  1488. call assert_equal([[12, 1, ''], [12, 0, 'two']], g:CompleteFunc1Args)
  1489. bw!
  1490. #" Test for using a funcref()
  1491. set completefunc=funcref('g:CompleteFunc1',\ [13])
  1492. new
  1493. call setline(1, 'three')
  1494. LET g:CompleteFunc1Args = []
  1495. call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
  1496. call assert_equal([[13, 1, ''], [13, 0, 'three']], g:CompleteFunc1Args)
  1497. bw!
  1498. #" Using a funcref variable to set 'completefunc'
  1499. LET Fn = funcref('g:CompleteFunc1', [14])
  1500. LET &completefunc = Fn
  1501. new
  1502. call setline(1, 'four')
  1503. LET g:CompleteFunc1Args = []
  1504. call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
  1505. call assert_equal([[14, 1, ''], [14, 0, 'four']], g:CompleteFunc1Args)
  1506. bw!
  1507. #" Using a string(funcref_variable) to set 'completefunc'
  1508. LET Fn = funcref('g:CompleteFunc1', [15])
  1509. LET &completefunc = string(Fn)
  1510. new
  1511. call setline(1, 'four')
  1512. LET g:CompleteFunc1Args = []
  1513. call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
  1514. call assert_equal([[15, 1, ''], [15, 0, 'four']], g:CompleteFunc1Args)
  1515. bw!
  1516. #" Test for using a lambda function with set
  1517. VAR optval = "LSTART a, b LMIDDLE CompleteFunc1(16, a, b) LEND"
  1518. LET optval = substitute(optval, ' ', '\\ ', 'g')
  1519. exe "set completefunc=" .. optval
  1520. new
  1521. call setline(1, 'five')
  1522. LET g:CompleteFunc1Args = []
  1523. call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
  1524. call assert_equal([[16, 1, ''], [16, 0, 'five']], g:CompleteFunc1Args)
  1525. bw!
  1526. #" Set 'completefunc' to a lambda expression
  1527. LET &completefunc = LSTART a, b LMIDDLE CompleteFunc1(17, a, b) LEND
  1528. new
  1529. call setline(1, 'six')
  1530. LET g:CompleteFunc1Args = []
  1531. call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
  1532. call assert_equal([[17, 1, ''], [17, 0, 'six']], g:CompleteFunc1Args)
  1533. bw!
  1534. #" Set 'completefunc' to string(lambda_expression)
  1535. LET &completefunc = 'LSTART a, b LMIDDLE CompleteFunc1(18, a, b) LEND'
  1536. new
  1537. call setline(1, 'six')
  1538. LET g:CompleteFunc1Args = []
  1539. call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
  1540. call assert_equal([[18, 1, ''], [18, 0, 'six']], g:CompleteFunc1Args)
  1541. bw!
  1542. #" Set 'completefunc' to a variable with a lambda expression
  1543. VAR Lambda = LSTART a, b LMIDDLE CompleteFunc1(19, a, b) LEND
  1544. LET &completefunc = Lambda
  1545. new
  1546. call setline(1, 'seven')
  1547. LET g:CompleteFunc1Args = []
  1548. call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
  1549. call assert_equal([[19, 1, ''], [19, 0, 'seven']], g:CompleteFunc1Args)
  1550. bw!
  1551. #" Set 'completefunc' to a string(variable with a lambda expression)
  1552. LET Lambda = LSTART a, b LMIDDLE CompleteFunc1(20, a, b) LEND
  1553. LET &completefunc = string(Lambda)
  1554. new
  1555. call setline(1, 'seven')
  1556. LET g:CompleteFunc1Args = []
  1557. call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
  1558. call assert_equal([[20, 1, ''], [20, 0, 'seven']], g:CompleteFunc1Args)
  1559. bw!
  1560. #" Test for using a lambda function with incorrect return value
  1561. LET Lambda = LSTART a, b LMIDDLE strlen(a) LEND
  1562. LET &completefunc = Lambda
  1563. new
  1564. call setline(1, 'eight')
  1565. call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
  1566. bw!
  1567. #" Test for clearing the 'completefunc' option
  1568. set completefunc=''
  1569. set completefunc&
  1570. call assert_fails("set completefunc=function('abc')", "E700:")
  1571. call assert_fails("set completefunc=funcref('abc')", "E700:")
  1572. #" set 'completefunc' to a non-existing function
  1573. set completefunc=CompleteFunc2
  1574. call setline(1, 'five')
  1575. call assert_fails("set completefunc=function('NonExistingFunc')", 'E700:')
  1576. call assert_fails("LET &completefunc = function('NonExistingFunc')", 'E700:')
  1577. LET g:CompleteFunc2Args = []
  1578. call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
  1579. call assert_equal([[1, ''], [0, 'five']], g:CompleteFunc2Args)
  1580. bw!
  1581. END
  1582. call CheckLegacyAndVim9Success(lines)
  1583. " Test for using a script-local function name
  1584. func s:CompleteFunc3(findstart, base)
  1585. call add(g:CompleteFunc3Args, [a:findstart, a:base])
  1586. return a:findstart ? 0 : []
  1587. endfunc
  1588. set completefunc=s:CompleteFunc3
  1589. new
  1590. call setline(1, 'script1')
  1591. let g:CompleteFunc3Args = []
  1592. call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
  1593. call assert_equal([[1, ''], [0, 'script1']], g:CompleteFunc3Args)
  1594. bw!
  1595. let &completefunc = 's:CompleteFunc3'
  1596. new
  1597. call setline(1, 'script2')
  1598. let g:CompleteFunc3Args = []
  1599. call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
  1600. call assert_equal([[1, ''], [0, 'script2']], g:CompleteFunc3Args)
  1601. bw!
  1602. delfunc s:CompleteFunc3
  1603. " In Vim9 script s: can be omitted
  1604. let lines =<< trim END
  1605. vim9script
  1606. var CompleteFunc4Args = []
  1607. def CompleteFunc4(findstart: bool, base: string): any
  1608. add(CompleteFunc4Args, [findstart, base])
  1609. return findstart ? 0 : []
  1610. enddef
  1611. set completefunc=CompleteFunc4
  1612. new
  1613. setline(1, 'script1')
  1614. feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
  1615. assert_equal([[1, ''], [0, 'script1']], CompleteFunc4Args)
  1616. bw!
  1617. END
  1618. call CheckScriptSuccess(lines)
  1619. " invalid return value
  1620. let &completefunc = {a -> 'abc'}
  1621. call feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
  1622. " Using Vim9 lambda expression in legacy context should fail
  1623. set completefunc=(a,\ b)\ =>\ CompleteFunc1(21,\ a,\ b)
  1624. new | only
  1625. let g:CompleteFunc1Args = []
  1626. call assert_fails('call feedkeys("A\<C-X>\<C-U>\<Esc>", "x")', 'E117:')
  1627. call assert_equal([], g:CompleteFunc1Args)
  1628. " set 'completefunc' to a partial with dict. This used to cause a crash.
  1629. func SetCompleteFunc()
  1630. let params = {'complete': function('g:DictCompleteFunc')}
  1631. let &completefunc = params.complete
  1632. endfunc
  1633. func g:DictCompleteFunc(_) dict
  1634. endfunc
  1635. call SetCompleteFunc()
  1636. new
  1637. call SetCompleteFunc()
  1638. bw
  1639. call test_garbagecollect_now()
  1640. new
  1641. set completefunc=
  1642. wincmd w
  1643. set completefunc=
  1644. %bw!
  1645. delfunc g:DictCompleteFunc
  1646. delfunc SetCompleteFunc
  1647. " Vim9 tests
  1648. let lines =<< trim END
  1649. vim9script
  1650. def Vim9CompleteFunc(callnr: number, findstart: number, base: string): any
  1651. add(g:Vim9completeFuncArgs, [callnr, findstart, base])
  1652. return findstart ? 0 : []
  1653. enddef
  1654. # Test for using a def function with completefunc
  1655. set completefunc=function('Vim9CompleteFunc',\ [60])
  1656. new | only
  1657. setline(1, 'one')
  1658. g:Vim9completeFuncArgs = []
  1659. feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
  1660. assert_equal([[60, 1, ''], [60, 0, 'one']], g:Vim9completeFuncArgs)
  1661. bw!
  1662. # Test for using a global function name
  1663. &completefunc = g:CompleteFunc2
  1664. new | only
  1665. setline(1, 'two')
  1666. g:CompleteFunc2Args = []
  1667. feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
  1668. assert_equal([[1, ''], [0, 'two']], g:CompleteFunc2Args)
  1669. bw!
  1670. # Test for using a script-local function name
  1671. def LocalCompleteFunc(findstart: number, base: string): any
  1672. add(g:LocalCompleteFuncArgs, [findstart, base])
  1673. return findstart ? 0 : []
  1674. enddef
  1675. &completefunc = LocalCompleteFunc
  1676. new | only
  1677. setline(1, 'three')
  1678. g:LocalCompleteFuncArgs = []
  1679. feedkeys("A\<C-X>\<C-U>\<Esc>", 'x')
  1680. assert_equal([[1, ''], [0, 'three']], g:LocalCompleteFuncArgs)
  1681. bw!
  1682. END
  1683. call CheckScriptSuccess(lines)
  1684. " cleanup
  1685. set completefunc&
  1686. delfunc CompleteFunc1
  1687. delfunc CompleteFunc2
  1688. unlet g:CompleteFunc1Args g:CompleteFunc2Args
  1689. %bw!
  1690. endfunc
  1691. " Test for different ways of setting the 'omnifunc' option
  1692. func Test_omnifunc_callback()
  1693. func OmniFunc1(callnr, findstart, base)
  1694. call add(g:OmniFunc1Args, [a:callnr, a:findstart, a:base])
  1695. return a:findstart ? 0 : []
  1696. endfunc
  1697. func OmniFunc2(findstart, base)
  1698. call add(g:OmniFunc2Args, [a:findstart, a:base])
  1699. return a:findstart ? 0 : []
  1700. endfunc
  1701. let lines =<< trim END
  1702. #" Test for using a function name
  1703. LET &omnifunc = 'g:OmniFunc2'
  1704. new
  1705. call setline(1, 'zero')
  1706. LET g:OmniFunc2Args = []
  1707. call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
  1708. call assert_equal([[1, ''], [0, 'zero']], g:OmniFunc2Args)
  1709. bw!
  1710. #" Test for using a function()
  1711. set omnifunc=function('g:OmniFunc1',\ [10])
  1712. new
  1713. call setline(1, 'one')
  1714. LET g:OmniFunc1Args = []
  1715. call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
  1716. call assert_equal([[10, 1, ''], [10, 0, 'one']], g:OmniFunc1Args)
  1717. bw!
  1718. #" Using a funcref variable to set 'omnifunc'
  1719. VAR Fn = function('g:OmniFunc1', [11])
  1720. LET &omnifunc = Fn
  1721. new
  1722. call setline(1, 'two')
  1723. LET g:OmniFunc1Args = []
  1724. call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
  1725. call assert_equal([[11, 1, ''], [11, 0, 'two']], g:OmniFunc1Args)
  1726. bw!
  1727. #" Using a string(funcref_variable) to set 'omnifunc'
  1728. LET Fn = function('g:OmniFunc1', [12])
  1729. LET &omnifunc = string(Fn)
  1730. new
  1731. call setline(1, 'two')
  1732. LET g:OmniFunc1Args = []
  1733. call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
  1734. call assert_equal([[12, 1, ''], [12, 0, 'two']], g:OmniFunc1Args)
  1735. bw!
  1736. #" Test for using a funcref()
  1737. set omnifunc=funcref('g:OmniFunc1',\ [13])
  1738. new
  1739. call setline(1, 'three')
  1740. LET g:OmniFunc1Args = []
  1741. call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
  1742. call assert_equal([[13, 1, ''], [13, 0, 'three']], g:OmniFunc1Args)
  1743. bw!
  1744. #" Use let to set 'omnifunc' to a funcref
  1745. LET Fn = funcref('g:OmniFunc1', [14])
  1746. LET &omnifunc = Fn
  1747. new
  1748. call setline(1, 'four')
  1749. LET g:OmniFunc1Args = []
  1750. call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
  1751. call assert_equal([[14, 1, ''], [14, 0, 'four']], g:OmniFunc1Args)
  1752. bw!
  1753. #" Using a string(funcref) to set 'omnifunc'
  1754. LET Fn = funcref("g:OmniFunc1", [15])
  1755. LET &omnifunc = string(Fn)
  1756. new
  1757. call setline(1, 'four')
  1758. LET g:OmniFunc1Args = []
  1759. call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
  1760. call assert_equal([[15, 1, ''], [15, 0, 'four']], g:OmniFunc1Args)
  1761. bw!
  1762. #" Test for using a lambda function with set
  1763. VAR optval = "LSTART a, b LMIDDLE OmniFunc1(16, a, b) LEND"
  1764. LET optval = substitute(optval, ' ', '\\ ', 'g')
  1765. exe "set omnifunc=" .. optval
  1766. new
  1767. call setline(1, 'five')
  1768. LET g:OmniFunc1Args = []
  1769. call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
  1770. call assert_equal([[16, 1, ''], [16, 0, 'five']], g:OmniFunc1Args)
  1771. bw!
  1772. #" Set 'omnifunc' to a lambda expression
  1773. LET &omnifunc = LSTART a, b LMIDDLE OmniFunc1(17, a, b) LEND
  1774. new
  1775. call setline(1, 'six')
  1776. LET g:OmniFunc1Args = []
  1777. call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
  1778. call assert_equal([[17, 1, ''], [17, 0, 'six']], g:OmniFunc1Args)
  1779. bw!
  1780. #" Set 'omnifunc' to a string(lambda_expression)
  1781. LET &omnifunc = 'LSTART a, b LMIDDLE OmniFunc1(18, a, b) LEND'
  1782. new
  1783. call setline(1, 'six')
  1784. LET g:OmniFunc1Args = []
  1785. call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
  1786. call assert_equal([[18, 1, ''], [18, 0, 'six']], g:OmniFunc1Args)
  1787. bw!
  1788. #" Set 'omnifunc' to a variable with a lambda expression
  1789. VAR Lambda = LSTART a, b LMIDDLE OmniFunc1(19, a, b) LEND
  1790. LET &omnifunc = Lambda
  1791. new
  1792. call setline(1, 'seven')
  1793. LET g:OmniFunc1Args = []
  1794. call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
  1795. call assert_equal([[19, 1, ''], [19, 0, 'seven']], g:OmniFunc1Args)
  1796. bw!
  1797. #" Set 'omnifunc' to a string(variable with a lambda expression)
  1798. LET Lambda = LSTART a, b LMIDDLE OmniFunc1(20, a, b) LEND
  1799. LET &omnifunc = string(Lambda)
  1800. new
  1801. call setline(1, 'seven')
  1802. LET g:OmniFunc1Args = []
  1803. call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
  1804. call assert_equal([[20, 1, ''], [20, 0, 'seven']], g:OmniFunc1Args)
  1805. bw!
  1806. #" Test for using a lambda function with incorrect return value
  1807. LET Lambda = LSTART a, b LMIDDLE strlen(a) LEND
  1808. LET &omnifunc = Lambda
  1809. new
  1810. call setline(1, 'eight')
  1811. call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
  1812. bw!
  1813. #" Test for clearing the 'omnifunc' option
  1814. set omnifunc=''
  1815. set omnifunc&
  1816. call assert_fails("set omnifunc=function('abc')", "E700:")
  1817. call assert_fails("set omnifunc=funcref('abc')", "E700:")
  1818. #" set 'omnifunc' to a non-existing function
  1819. set omnifunc=OmniFunc2
  1820. call setline(1, 'nine')
  1821. call assert_fails("set omnifunc=function('NonExistingFunc')", 'E700:')
  1822. call assert_fails("LET &omnifunc = function('NonExistingFunc')", 'E700:')
  1823. LET g:OmniFunc2Args = []
  1824. call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
  1825. call assert_equal([[1, ''], [0, 'nine']], g:OmniFunc2Args)
  1826. bw!
  1827. END
  1828. call CheckLegacyAndVim9Success(lines)
  1829. " Test for using a script-local function name
  1830. func s:OmniFunc3(findstart, base)
  1831. call add(g:OmniFunc3Args, [a:findstart, a:base])
  1832. return a:findstart ? 0 : []
  1833. endfunc
  1834. set omnifunc=s:OmniFunc3
  1835. new
  1836. call setline(1, 'script1')
  1837. let g:OmniFunc3Args = []
  1838. call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
  1839. call assert_equal([[1, ''], [0, 'script1']], g:OmniFunc3Args)
  1840. bw!
  1841. let &omnifunc = 's:OmniFunc3'
  1842. new
  1843. call setline(1, 'script2')
  1844. let g:OmniFunc3Args = []
  1845. call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
  1846. call assert_equal([[1, ''], [0, 'script2']], g:OmniFunc3Args)
  1847. bw!
  1848. delfunc s:OmniFunc3
  1849. " invalid return value
  1850. let &omnifunc = {a -> 'abc'}
  1851. call feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
  1852. " Using Vim9 lambda expression in legacy context should fail
  1853. set omnifunc=(a,\ b)\ =>\ OmniFunc1(21,\ a,\ b)
  1854. new | only
  1855. let g:OmniFunc1Args = []
  1856. call assert_fails('call feedkeys("A\<C-X>\<C-O>\<Esc>", "x")', 'E117:')
  1857. call assert_equal([], g:OmniFunc1Args)
  1858. " set 'omnifunc' to a partial with dict. This used to cause a crash.
  1859. func SetOmniFunc()
  1860. let params = {'omni': function('g:DictOmniFunc')}
  1861. let &omnifunc = params.omni
  1862. endfunc
  1863. func g:DictOmniFunc(_) dict
  1864. endfunc
  1865. call SetOmniFunc()
  1866. new
  1867. call SetOmniFunc()
  1868. bw
  1869. call test_garbagecollect_now()
  1870. new
  1871. set omnifunc=
  1872. wincmd w
  1873. set omnifunc=
  1874. %bw!
  1875. delfunc g:DictOmniFunc
  1876. delfunc SetOmniFunc
  1877. " Vim9 tests
  1878. let lines =<< trim END
  1879. vim9script
  1880. def Vim9omniFunc(callnr: number, findstart: number, base: string): any
  1881. add(g:Vim9omniFunc_Args, [callnr, findstart, base])
  1882. return findstart ? 0 : []
  1883. enddef
  1884. # Test for using a def function with omnifunc
  1885. set omnifunc=function('Vim9omniFunc',\ [60])
  1886. new | only
  1887. setline(1, 'one')
  1888. g:Vim9omniFunc_Args = []
  1889. feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
  1890. assert_equal([[60, 1, ''], [60, 0, 'one']], g:Vim9omniFunc_Args)
  1891. bw!
  1892. # Test for using a global function name
  1893. &omnifunc = g:OmniFunc2
  1894. new | only
  1895. setline(1, 'two')
  1896. g:OmniFunc2Args = []
  1897. feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
  1898. assert_equal([[1, ''], [0, 'two']], g:OmniFunc2Args)
  1899. bw!
  1900. # Test for using a script-local function name
  1901. def LocalOmniFunc(findstart: number, base: string): any
  1902. add(g:LocalOmniFuncArgs, [findstart, base])
  1903. return findstart ? 0 : []
  1904. enddef
  1905. &omnifunc = LocalOmniFunc
  1906. new | only
  1907. setline(1, 'three')
  1908. g:LocalOmniFuncArgs = []
  1909. feedkeys("A\<C-X>\<C-O>\<Esc>", 'x')
  1910. assert_equal([[1, ''], [0, 'three']], g:LocalOmniFuncArgs)
  1911. bw!
  1912. END
  1913. call CheckScriptSuccess(lines)
  1914. " cleanup
  1915. set omnifunc&
  1916. delfunc OmniFunc1
  1917. delfunc OmniFunc2
  1918. unlet g:OmniFunc1Args g:OmniFunc2Args
  1919. %bw!
  1920. endfunc
  1921. " Test for different ways of setting the 'thesaurusfunc' option
  1922. func Test_thesaurusfunc_callback()
  1923. func TsrFunc1(callnr, findstart, base)
  1924. call add(g:TsrFunc1Args, [a:callnr, a:findstart, a:base])
  1925. return a:findstart ? 0 : []
  1926. endfunc
  1927. func TsrFunc2(findstart, base)
  1928. call add(g:TsrFunc2Args, [a:findstart, a:base])
  1929. return a:findstart ? 0 : ['sunday']
  1930. endfunc
  1931. let lines =<< trim END
  1932. #" Test for using a function name
  1933. LET &thesaurusfunc = 'g:TsrFunc2'
  1934. new
  1935. call setline(1, 'zero')
  1936. LET g:TsrFunc2Args = []
  1937. call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
  1938. call assert_equal([[1, ''], [0, 'zero']], g:TsrFunc2Args)
  1939. bw!
  1940. #" Test for using a function()
  1941. set thesaurusfunc=function('g:TsrFunc1',\ [10])
  1942. new
  1943. call setline(1, 'one')
  1944. LET g:TsrFunc1Args = []
  1945. call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
  1946. call assert_equal([[10, 1, ''], [10, 0, 'one']], g:TsrFunc1Args)
  1947. bw!
  1948. #" Using a funcref variable to set 'thesaurusfunc'
  1949. VAR Fn = function('g:TsrFunc1', [11])
  1950. LET &thesaurusfunc = Fn
  1951. new
  1952. call setline(1, 'two')
  1953. LET g:TsrFunc1Args = []
  1954. call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
  1955. call assert_equal([[11, 1, ''], [11, 0, 'two']], g:TsrFunc1Args)
  1956. bw!
  1957. #" Using a string(funcref_variable) to set 'thesaurusfunc'
  1958. LET Fn = function('g:TsrFunc1', [12])
  1959. LET &thesaurusfunc = string(Fn)
  1960. new
  1961. call setline(1, 'two')
  1962. LET g:TsrFunc1Args = []
  1963. call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
  1964. call assert_equal([[12, 1, ''], [12, 0, 'two']], g:TsrFunc1Args)
  1965. bw!
  1966. #" Test for using a funcref()
  1967. set thesaurusfunc=funcref('g:TsrFunc1',\ [13])
  1968. new
  1969. call setline(1, 'three')
  1970. LET g:TsrFunc1Args = []
  1971. call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
  1972. call assert_equal([[13, 1, ''], [13, 0, 'three']], g:TsrFunc1Args)
  1973. bw!
  1974. #" Using a funcref variable to set 'thesaurusfunc'
  1975. LET Fn = funcref('g:TsrFunc1', [14])
  1976. LET &thesaurusfunc = Fn
  1977. new
  1978. call setline(1, 'four')
  1979. LET g:TsrFunc1Args = []
  1980. call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
  1981. call assert_equal([[14, 1, ''], [14, 0, 'four']], g:TsrFunc1Args)
  1982. bw!
  1983. #" Using a string(funcref_variable) to set 'thesaurusfunc'
  1984. LET Fn = funcref('g:TsrFunc1', [15])
  1985. LET &thesaurusfunc = string(Fn)
  1986. new
  1987. call setline(1, 'four')
  1988. LET g:TsrFunc1Args = []
  1989. call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
  1990. call assert_equal([[15, 1, ''], [15, 0, 'four']], g:TsrFunc1Args)
  1991. bw!
  1992. #" Test for using a lambda function
  1993. VAR optval = "LSTART a, b LMIDDLE TsrFunc1(16, a, b) LEND"
  1994. LET optval = substitute(optval, ' ', '\\ ', 'g')
  1995. exe "set thesaurusfunc=" .. optval
  1996. new
  1997. call setline(1, 'five')
  1998. LET g:TsrFunc1Args = []
  1999. call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
  2000. call assert_equal([[16, 1, ''], [16, 0, 'five']], g:TsrFunc1Args)
  2001. bw!
  2002. #" Test for using a lambda function with set
  2003. LET &thesaurusfunc = LSTART a, b LMIDDLE TsrFunc1(17, a, b) LEND
  2004. new
  2005. call setline(1, 'six')
  2006. LET g:TsrFunc1Args = []
  2007. call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
  2008. call assert_equal([[17, 1, ''], [17, 0, 'six']], g:TsrFunc1Args)
  2009. bw!
  2010. #" Set 'thesaurusfunc' to a string(lambda expression)
  2011. LET &thesaurusfunc = 'LSTART a, b LMIDDLE TsrFunc1(18, a, b) LEND'
  2012. new
  2013. call setline(1, 'six')
  2014. LET g:TsrFunc1Args = []
  2015. call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
  2016. call assert_equal([[18, 1, ''], [18, 0, 'six']], g:TsrFunc1Args)
  2017. bw!
  2018. #" Set 'thesaurusfunc' to a variable with a lambda expression
  2019. VAR Lambda = LSTART a, b LMIDDLE TsrFunc1(19, a, b) LEND
  2020. LET &thesaurusfunc = Lambda
  2021. new
  2022. call setline(1, 'seven')
  2023. LET g:TsrFunc1Args = []
  2024. call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
  2025. call assert_equal([[19, 1, ''], [19, 0, 'seven']], g:TsrFunc1Args)
  2026. bw!
  2027. #" Set 'thesaurusfunc' to a string(variable with a lambda expression)
  2028. LET Lambda = LSTART a, b LMIDDLE TsrFunc1(20, a, b) LEND
  2029. LET &thesaurusfunc = string(Lambda)
  2030. new
  2031. call setline(1, 'seven')
  2032. LET g:TsrFunc1Args = []
  2033. call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
  2034. call assert_equal([[20, 1, ''], [20, 0, 'seven']], g:TsrFunc1Args)
  2035. bw!
  2036. #" Test for using a lambda function with incorrect return value
  2037. LET Lambda = LSTART a, b LMIDDLE strlen(a) LEND
  2038. LET &thesaurusfunc = Lambda
  2039. new
  2040. call setline(1, 'eight')
  2041. call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
  2042. bw!
  2043. #" Test for clearing the 'thesaurusfunc' option
  2044. set thesaurusfunc=''
  2045. set thesaurusfunc&
  2046. call assert_fails("set thesaurusfunc=function('abc')", "E700:")
  2047. call assert_fails("set thesaurusfunc=funcref('abc')", "E700:")
  2048. #" set 'thesaurusfunc' to a non-existing function
  2049. set thesaurusfunc=TsrFunc2
  2050. call setline(1, 'ten')
  2051. call assert_fails("set thesaurusfunc=function('NonExistingFunc')", 'E700:')
  2052. call assert_fails("LET &thesaurusfunc = function('NonExistingFunc')", 'E700:')
  2053. LET g:TsrFunc2Args = []
  2054. call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
  2055. call assert_equal([[1, ''], [0, 'ten']], g:TsrFunc2Args)
  2056. bw!
  2057. #" Use a buffer-local value and a global value
  2058. set thesaurusfunc&
  2059. setlocal thesaurusfunc=function('g:TsrFunc1',\ [22])
  2060. call setline(1, 'sun')
  2061. LET g:TsrFunc1Args = []
  2062. call feedkeys("A\<C-X>\<C-T>\<Esc>", "x")
  2063. call assert_equal('sun', getline(1))
  2064. call assert_equal([[22, 1, ''], [22, 0, 'sun']], g:TsrFunc1Args)
  2065. new
  2066. call setline(1, 'sun')
  2067. LET g:TsrFunc1Args = []
  2068. call feedkeys("A\<C-X>\<C-T>\<Esc>", "x")
  2069. call assert_equal('sun', getline(1))
  2070. call assert_equal([], g:TsrFunc1Args)
  2071. set thesaurusfunc=function('g:TsrFunc1',\ [23])
  2072. wincmd w
  2073. call setline(1, 'sun')
  2074. LET g:TsrFunc1Args = []
  2075. call feedkeys("A\<C-X>\<C-T>\<Esc>", "x")
  2076. call assert_equal('sun', getline(1))
  2077. call assert_equal([[22, 1, ''], [22, 0, 'sun']], g:TsrFunc1Args)
  2078. :%bw!
  2079. END
  2080. call CheckLegacyAndVim9Success(lines)
  2081. " Test for using a script-local function name
  2082. func s:TsrFunc3(findstart, base)
  2083. call add(g:TsrFunc3Args, [a:findstart, a:base])
  2084. return a:findstart ? 0 : []
  2085. endfunc
  2086. set tsrfu=s:TsrFunc3
  2087. new
  2088. call setline(1, 'script1')
  2089. let g:TsrFunc3Args = []
  2090. call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
  2091. call assert_equal([[1, ''], [0, 'script1']], g:TsrFunc3Args)
  2092. bw!
  2093. let &tsrfu = 's:TsrFunc3'
  2094. new
  2095. call setline(1, 'script2')
  2096. let g:TsrFunc3Args = []
  2097. call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
  2098. call assert_equal([[1, ''], [0, 'script2']], g:TsrFunc3Args)
  2099. bw!
  2100. new | only
  2101. set thesaurusfunc=
  2102. setlocal thesaurusfunc=NoSuchFunc
  2103. setglobal thesaurusfunc=s:TsrFunc3
  2104. call assert_equal('NoSuchFunc', &thesaurusfunc)
  2105. call assert_equal('NoSuchFunc', &l:thesaurusfunc)
  2106. call assert_equal('s:TsrFunc3', &g:thesaurusfunc)
  2107. new | only
  2108. call assert_equal('s:TsrFunc3', &thesaurusfunc)
  2109. call assert_equal('s:TsrFunc3', &g:thesaurusfunc)
  2110. call assert_equal('', &l:thesaurusfunc)
  2111. call setline(1, 'script1')
  2112. let g:TsrFunc3Args = []
  2113. call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
  2114. call assert_equal([[1, ''], [0, 'script1']], g:TsrFunc3Args)
  2115. bw!
  2116. new | only
  2117. set thesaurusfunc=
  2118. setlocal thesaurusfunc=NoSuchFunc
  2119. set thesaurusfunc=s:TsrFunc3
  2120. call assert_equal('s:TsrFunc3', &thesaurusfunc)
  2121. call assert_equal('s:TsrFunc3', &g:thesaurusfunc)
  2122. call assert_equal('', &l:thesaurusfunc)
  2123. call setline(1, 'script1')
  2124. let g:TsrFunc3Args = []
  2125. call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
  2126. call assert_equal([[1, ''], [0, 'script1']], g:TsrFunc3Args)
  2127. setlocal bufhidden=wipe
  2128. new | only!
  2129. call assert_equal('s:TsrFunc3', &thesaurusfunc)
  2130. call assert_equal('s:TsrFunc3', &g:thesaurusfunc)
  2131. call assert_equal('', &l:thesaurusfunc)
  2132. call setline(1, 'script1')
  2133. let g:TsrFunc3Args = []
  2134. call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
  2135. call assert_equal([[1, ''], [0, 'script1']], g:TsrFunc3Args)
  2136. bw!
  2137. delfunc s:TsrFunc3
  2138. " invalid return value
  2139. let &thesaurusfunc = {a -> 'abc'}
  2140. call feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
  2141. " Using Vim9 lambda expression in legacy context should fail
  2142. set thesaurusfunc=(a,\ b)\ =>\ TsrFunc1(21,\ a,\ b)
  2143. new | only
  2144. let g:TsrFunc1Args = []
  2145. call assert_fails('call feedkeys("A\<C-X>\<C-T>\<Esc>", "x")', 'E117:')
  2146. call assert_equal([], g:TsrFunc1Args)
  2147. bw!
  2148. " set 'thesaurusfunc' to a partial with dict. This used to cause a crash.
  2149. func SetTsrFunc()
  2150. let params = {'thesaurus': function('g:DictTsrFunc')}
  2151. let &thesaurusfunc = params.thesaurus
  2152. endfunc
  2153. func g:DictTsrFunc(_) dict
  2154. endfunc
  2155. call SetTsrFunc()
  2156. new
  2157. call SetTsrFunc()
  2158. bw
  2159. call test_garbagecollect_now()
  2160. new
  2161. set thesaurusfunc=
  2162. wincmd w
  2163. %bw!
  2164. delfunc SetTsrFunc
  2165. " set buffer-local 'thesaurusfunc' to a partial with dict. This used to
  2166. " cause a crash.
  2167. func SetLocalTsrFunc()
  2168. let params = {'thesaurus': function('g:DictTsrFunc')}
  2169. let &l:thesaurusfunc = params.thesaurus
  2170. endfunc
  2171. call SetLocalTsrFunc()
  2172. call test_garbagecollect_now()
  2173. call SetLocalTsrFunc()
  2174. set thesaurusfunc=
  2175. bw!
  2176. delfunc g:DictTsrFunc
  2177. delfunc SetLocalTsrFunc
  2178. " Vim9 tests
  2179. let lines =<< trim END
  2180. vim9script
  2181. def Vim9tsrFunc(callnr: number, findstart: number, base: string): any
  2182. add(g:Vim9tsrFunc_Args, [callnr, findstart, base])
  2183. return findstart ? 0 : []
  2184. enddef
  2185. # Test for using a def function with thesaurusfunc
  2186. set thesaurusfunc=function('Vim9tsrFunc',\ [60])
  2187. new | only
  2188. setline(1, 'one')
  2189. g:Vim9tsrFunc_Args = []
  2190. feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
  2191. assert_equal([[60, 1, ''], [60, 0, 'one']], g:Vim9tsrFunc_Args)
  2192. bw!
  2193. # Test for using a global function name
  2194. &thesaurusfunc = g:TsrFunc2
  2195. new | only
  2196. setline(1, 'two')
  2197. g:TsrFunc2Args = []
  2198. feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
  2199. assert_equal([[1, ''], [0, 'two']], g:TsrFunc2Args)
  2200. bw!
  2201. # Test for using a script-local function name
  2202. def LocalTsrFunc(findstart: number, base: string): any
  2203. add(g:LocalTsrFuncArgs, [findstart, base])
  2204. return findstart ? 0 : []
  2205. enddef
  2206. &thesaurusfunc = LocalTsrFunc
  2207. new | only
  2208. setline(1, 'three')
  2209. g:LocalTsrFuncArgs = []
  2210. feedkeys("A\<C-X>\<C-T>\<Esc>", 'x')
  2211. assert_equal([[1, ''], [0, 'three']], g:LocalTsrFuncArgs)
  2212. bw!
  2213. END
  2214. call CheckScriptSuccess(lines)
  2215. " cleanup
  2216. set thesaurusfunc&
  2217. delfunc TsrFunc1
  2218. delfunc TsrFunc2
  2219. unlet g:TsrFunc1Args g:TsrFunc2Args
  2220. %bw!
  2221. endfunc
  2222. func FooBarComplete(findstart, base)
  2223. if a:findstart
  2224. return col('.') - 1
  2225. else
  2226. return ["Foo", "Bar", "}"]
  2227. endif
  2228. endfunc
  2229. func Test_complete_smartindent()
  2230. new
  2231. setlocal smartindent completefunc=FooBarComplete
  2232. exe "norm! o{\<cr>\<c-x>\<c-u>\<c-p>}\<cr>\<esc>"
  2233. let result = getline(1,'$')
  2234. call assert_equal(['', '{','}',''], result)
  2235. bw!
  2236. delfunction! FooBarComplete
  2237. endfunc
  2238. func Test_complete_overrun()
  2239. " this was going past the end of the copied text
  2240. new
  2241. sil norm si”0s0 
  2242. bwipe!
  2243. endfunc
  2244. func Test_infercase_very_long_line()
  2245. " this was truncating the line when inferring case
  2246. new
  2247. let longLine = "blah "->repeat(300)
  2248. let verylongLine = "blah "->repeat(400)
  2249. call setline(1, verylongLine)
  2250. call setline(2, longLine)
  2251. set ic infercase
  2252. exe "normal 2Go\<C-X>\<C-L>\<Esc>"
  2253. call assert_equal(longLine, getline(3))
  2254. " check that the too long text is NUL terminated
  2255. %del
  2256. norm o
  2257. norm 1987ax
  2258. exec "norm ox\<C-X>\<C-L>"
  2259. call assert_equal(repeat('x', 1987), getline(3))
  2260. bwipe!
  2261. set noic noinfercase
  2262. endfunc
  2263. func Test_ins_complete_add()
  2264. " this was reading past the end of allocated memory
  2265. new
  2266. norm o
  2267. norm 7o€€
  2268. sil! norm o
  2269. bwipe!
  2270. endfunc
  2271. func Test_ins_complete_end_of_line()
  2272. " this was reading past the end of the line
  2273. new
  2274. norm 8o€ý 
  2275. sil! norm o
  2276. bwipe!
  2277. endfunc
  2278. func s:Tagfunc(t,f,o)
  2279. bwipe!
  2280. return []
  2281. endfunc
  2282. " This was using freed memory, since 'complete' was in a wiped out buffer.
  2283. " Also using a window that was closed.
  2284. func Test_tagfunc_wipes_out_buffer()
  2285. new
  2286. set complete=.,t,w,b,u,i
  2287. se tagfunc=s:Tagfunc
  2288. sil norm i
  2289. bwipe!
  2290. endfunc
  2291. func Test_ins_complete_popup_position()
  2292. CheckScreendump
  2293. let lines =<< trim END
  2294. vim9script
  2295. set nowrap
  2296. setline(1, ['one', 'two', 'this is line ', 'four'])
  2297. prop_type_add('test', {highlight: 'Error'})
  2298. prop_add(3, 0, {
  2299. text_align: 'above',
  2300. text: 'The quick brown fox jumps over the lazy dog',
  2301. type: 'test'
  2302. })
  2303. END
  2304. call writefile(lines, 'XinsPopup', 'D')
  2305. let buf = RunVimInTerminal('-S XinsPopup', #{rows: 10})
  2306. call term_sendkeys(buf, "3GA\<C-N>")
  2307. call VerifyScreenDump(buf, 'Test_ins_complete_popup_position_1', {})
  2308. call StopVimInTerminal(buf)
  2309. endfunc
  2310. func GetCompleteInfo()
  2311. let g:compl_info = complete_info()
  2312. return ''
  2313. endfunc
  2314. func Test_completion_restart()
  2315. new
  2316. set complete=. completeopt=menuone backspace=2
  2317. call setline(1, 'workhorse workhorse')
  2318. exe "normal $a\<C-N>\<BS>\<BS>\<C-R>=GetCompleteInfo()\<CR>"
  2319. call assert_equal(1, len(g:compl_info['items']))
  2320. call assert_equal('workhorse', g:compl_info['items'][0]['word'])
  2321. set complete& completeopt& backspace&
  2322. bwipe!
  2323. endfunc
  2324. func Test_complete_info_index()
  2325. new
  2326. call setline(1, ["aaa", "bbb", "ccc", "ddd", "eee", "fff"])
  2327. inoremap <buffer><F5> <C-R>=GetCompleteInfo()<CR>
  2328. " Ensure 'index' in complete_info() is coherent with the 'items' array.
  2329. set completeopt=menu,preview
  2330. " Search forward
  2331. call feedkeys("Go\<C-X>\<C-N>\<F5>\<Esc>_dd", 'tx')
  2332. call assert_equal("aaa", g:compl_info['items'][g:compl_info['selected']]['word'])
  2333. call assert_equal(6 , len(g:compl_info['items']))
  2334. call feedkeys("Go\<C-X>\<C-N>\<C-N>\<F5>\<Esc>_dd", 'tx')
  2335. call assert_equal("bbb", g:compl_info['items'][g:compl_info['selected']]['word'])
  2336. call assert_equal(6 , len(g:compl_info['items']))
  2337. call feedkeys("Go\<C-X>\<C-N>\<C-N>\<C-N>\<F5>\<Esc>_dd", 'tx')
  2338. call assert_equal("ccc", g:compl_info['items'][g:compl_info['selected']]['word'])
  2339. call assert_equal(6 , len(g:compl_info['items']))
  2340. call feedkeys("Go\<C-X>\<C-N>\<C-N>\<C-N>\<C-N>\<F5>\<Esc>_dd", 'tx')
  2341. call assert_equal("ddd", g:compl_info['items'][g:compl_info['selected']]['word'])
  2342. call assert_equal(6 , len(g:compl_info['items']))
  2343. call feedkeys("Go\<C-X>\<C-N>\<C-N>\<C-N>\<C-N>\<C-N>\<F5>\<Esc>_dd", 'tx')
  2344. call assert_equal("eee", g:compl_info['items'][g:compl_info['selected']]['word'])
  2345. call assert_equal(6 , len(g:compl_info['items']))
  2346. call feedkeys("Go\<C-X>\<C-N>\<C-N>\<C-N>\<C-N>\<C-N>\<C-N>\<F5>\<Esc>_dd", 'tx')
  2347. call assert_equal("fff", g:compl_info['items'][g:compl_info['selected']]['word'])
  2348. call assert_equal(6 , len(g:compl_info['items']))
  2349. " Search forward: unselected item
  2350. call feedkeys("Go\<C-X>\<C-N>\<C-N>\<C-N>\<C-N>\<C-N>\<C-N>\<C-N>\<F5>\<Esc>_dd", 'tx')
  2351. call assert_equal(6 , len(g:compl_info['items']))
  2352. call assert_equal(-1 , g:compl_info['selected'])
  2353. " Search backward
  2354. call feedkeys("Go\<C-X>\<C-P>\<F5>\<Esc>_dd", 'tx')
  2355. call assert_equal("fff", g:compl_info['items'][g:compl_info['selected']]['word'])
  2356. call assert_equal(6 , len(g:compl_info['items']))
  2357. call feedkeys("Go\<C-X>\<C-P>\<C-P>\<F5>\<Esc>_dd", 'tx')
  2358. call assert_equal("eee", g:compl_info['items'][g:compl_info['selected']]['word'])
  2359. call assert_equal(6 , len(g:compl_info['items']))
  2360. call feedkeys("Go\<C-X>\<C-P>\<C-P>\<C-P>\<F5>\<Esc>_dd", 'tx')
  2361. call assert_equal("ddd", g:compl_info['items'][g:compl_info['selected']]['word'])
  2362. call assert_equal(6 , len(g:compl_info['items']))
  2363. call feedkeys("Go\<C-X>\<C-P>\<C-P>\<C-P>\<C-P>\<F5>\<Esc>_dd", 'tx')
  2364. call assert_equal("ccc", g:compl_info['items'][g:compl_info['selected']]['word'])
  2365. call assert_equal(6 , len(g:compl_info['items']))
  2366. call feedkeys("Go\<C-X>\<C-P>\<C-P>\<C-P>\<C-P>\<C-P>\<F5>\<Esc>_dd", 'tx')
  2367. call assert_equal("bbb", g:compl_info['items'][g:compl_info['selected']]['word'])
  2368. call assert_equal(6 , len(g:compl_info['items']))
  2369. call feedkeys("Go\<C-X>\<C-P>\<C-P>\<C-P>\<C-P>\<C-P>\<C-P>\<F5>\<Esc>_dd", 'tx')
  2370. call assert_equal("aaa", g:compl_info['items'][g:compl_info['selected']]['word'])
  2371. call assert_equal(6 , len(g:compl_info['items']))
  2372. " search backwards: unselected item
  2373. call feedkeys("Go\<C-X>\<C-P>\<C-P>\<C-P>\<C-P>\<C-P>\<C-P>\<C-P>\<F5>\<Esc>_dd", 'tx')
  2374. call assert_equal(6 , len(g:compl_info['items']))
  2375. call assert_equal(-1 , g:compl_info['selected'])
  2376. " switch direction: forwards, then backwards
  2377. call feedkeys("Go\<C-X>\<C-N>\<C-P>\<C-P>\<F5>\<Esc>_dd", 'tx')
  2378. call assert_equal("fff", g:compl_info['items'][g:compl_info['selected']]['word'])
  2379. call assert_equal(6 , len(g:compl_info['items']))
  2380. " switch direction: forwards, then backwards, then forwards again
  2381. call feedkeys("Go\<C-X>\<C-N>\<C-P>\<C-P>\<F5>\<Esc>_dd", 'tx')
  2382. call feedkeys("Go\<C-X>\<C-N>\<C-N>\<C-P>\<C-P>\<C-N>\<F5>\<Esc>_dd", 'tx')
  2383. call assert_equal("aaa", g:compl_info['items'][g:compl_info['selected']]['word'])
  2384. call assert_equal(6 , len(g:compl_info['items']))
  2385. " switch direction: backwards, then forwards
  2386. call feedkeys("Go\<C-X>\<C-P>\<C-N>\<C-N>\<F5>\<Esc>_dd", 'tx')
  2387. call assert_equal("aaa", g:compl_info['items'][g:compl_info['selected']]['word'])
  2388. call assert_equal(6 , len(g:compl_info['items']))
  2389. " switch direction: backwards, then forwards, then backwards again
  2390. call feedkeys("Go\<C-X>\<C-P>\<C-P>\<C-N>\<C-N>\<C-P>\<F5>\<Esc>_dd", 'tx')
  2391. call assert_equal("fff", g:compl_info['items'][g:compl_info['selected']]['word'])
  2392. call assert_equal(6 , len(g:compl_info['items']))
  2393. " Add 'noselect', check that 'selected' is -1 when nothing is selected.
  2394. set completeopt+=noselect
  2395. " Search forward.
  2396. call feedkeys("Go\<C-X>\<C-N>\<F5>\<Esc>_dd", 'tx')
  2397. call assert_equal(-1, g:compl_info['selected'])
  2398. " Search backward.
  2399. call feedkeys("Go\<C-X>\<C-P>\<F5>\<Esc>_dd", 'tx')
  2400. call assert_equal(-1, g:compl_info['selected'])
  2401. call feedkeys("Go\<C-X>\<C-N>\<C-P>\<F5>\<Esc>_dd", 'tx')
  2402. call assert_equal(5, g:compl_info['selected'])
  2403. call assert_equal(6 , len(g:compl_info['items']))
  2404. call assert_equal("fff", g:compl_info['items'][g:compl_info['selected']]['word'])
  2405. call feedkeys("Go\<C-X>\<C-N>\<C-N>\<C-N>\<C-N>\<C-N>\<C-N>\<C-N>\<C-N>\<C-N>\<F5>\<Esc>_dd", 'tx')
  2406. call assert_equal("aaa", g:compl_info['items'][g:compl_info['selected']]['word'])
  2407. call assert_equal(6 , len(g:compl_info['items']))
  2408. call feedkeys("Go\<C-X>\<C-N>\<F5>\<Esc>_dd", 'tx')
  2409. call assert_equal(-1, g:compl_info['selected'])
  2410. call assert_equal(6 , len(g:compl_info['items']))
  2411. set completeopt&
  2412. bwipe!
  2413. endfunc
  2414. func Test_complete_changed_complete_info()
  2415. CheckRunVimInTerminal
  2416. " this used to crash vim, see #13929
  2417. let lines =<< trim END
  2418. set completeopt=menuone
  2419. autocmd CompleteChanged * call complete_info(['items'])
  2420. call feedkeys("iii\<cr>\<c-p>")
  2421. END
  2422. call writefile(lines, 'Xsegfault', 'D')
  2423. let buf = RunVimInTerminal('-S Xsegfault', #{rows: 5})
  2424. call WaitForAssert({-> assert_match('^ii', term_getline(buf, 1))}, 1000)
  2425. call StopVimInTerminal(buf)
  2426. endfunc
  2427. func Test_completefunc_first_call_complete_add()
  2428. new
  2429. func Complete(findstart, base) abort
  2430. if a:findstart
  2431. let col = col('.')
  2432. call complete_add('#')
  2433. return col - 1
  2434. else
  2435. return []
  2436. endif
  2437. endfunc
  2438. set completeopt=longest completefunc=Complete
  2439. " This used to cause heap-buffer-overflow
  2440. call assert_fails('call feedkeys("ifoo#\<C-X>\<C-U>", "xt")', 'E840:')
  2441. delfunc Complete
  2442. set completeopt& completefunc&
  2443. bwipe!
  2444. endfunc
  2445. func Test_complete_fuzzy_match()
  2446. func OnPumChange()
  2447. let g:item = get(v:event, 'completed_item', {})
  2448. let g:word = get(g:item, 'word', v:null)
  2449. let g:abbr = get(g:item, 'abbr', v:null)
  2450. let g:selected = get(complete_info(['selected']), 'selected')
  2451. endfunction
  2452. augroup AAAAA_Group
  2453. au!
  2454. autocmd CompleteChanged * :call OnPumChange()
  2455. augroup END
  2456. func Omni_test(findstart, base)
  2457. if a:findstart
  2458. return col(".")
  2459. endif
  2460. return [#{word: "foo"}, #{word: "foobar"}, #{word: "fooBaz"}, #{word: "foobala"}, #{word: "你好吗"}, #{word: "我好"}]
  2461. endfunc
  2462. new
  2463. set omnifunc=Omni_test
  2464. set completeopt+=noinsert,fuzzy
  2465. call feedkeys("Gi\<C-x>\<C-o>", 'tx')
  2466. call assert_equal('foo', g:word)
  2467. call feedkeys("S\<C-x>\<C-o>fb", 'tx')
  2468. call assert_equal('fooBaz', g:word)
  2469. call feedkeys("S\<C-x>\<C-o>fa", 'tx')
  2470. call assert_equal('foobar', g:word)
  2471. " select next
  2472. call feedkeys("S\<C-x>\<C-o>fb\<C-n>", 'tx')
  2473. call assert_equal('foobar', g:word)
  2474. " can cyclically select next
  2475. call feedkeys("S\<C-x>\<C-o>fb\<C-n>\<C-n>\<C-n>", 'tx')
  2476. call assert_equal(v:null, g:word)
  2477. " select prev
  2478. call feedkeys("S\<C-x>\<C-o>fb\<C-p>", 'tx')
  2479. call assert_equal(v:null, g:word)
  2480. " can cyclically select prev
  2481. call feedkeys("S\<C-x>\<C-o>fb\<C-p>\<C-p>\<C-p>\<C-p>", 'tx')
  2482. call assert_equal('fooBaz', g:word)
  2483. func Comp()
  2484. call complete(col('.'), ["fooBaz", "foobar", "foobala"])
  2485. return ''
  2486. endfunc
  2487. call feedkeys("i\<C-R>=Comp()\<CR>", 'tx')
  2488. call assert_equal('fooBaz', g:word)
  2489. " respect noselect
  2490. set completeopt+=noselect
  2491. call feedkeys("S\<C-x>\<C-o>fb", 'tx')
  2492. call assert_equal(v:null, g:word)
  2493. call feedkeys("S\<C-x>\<C-o>fb\<C-n>", 'tx')
  2494. call assert_equal('fooBaz', g:word)
  2495. " avoid breaking default completion behavior
  2496. set completeopt=fuzzy,menu
  2497. call setline(1, ['hello help hero h'])
  2498. " Use "!" flag of feedkeys() so that ex_normal_busy is not set and
  2499. " ins_compl_check_keys() is not skipped.
  2500. " Add a "0" after the <Esc> to avoid waiting for an escape sequence.
  2501. call feedkeys("A\<C-X>\<C-N>\<Esc>0", 'tx!')
  2502. call assert_equal('hello help hero hello', getline('.'))
  2503. set completeopt+=noinsert
  2504. call setline(1, ['hello help hero h'])
  2505. call feedkeys("A\<C-X>\<C-N>\<Esc>0", 'tx!')
  2506. call assert_equal('hello help hero h', getline('.'))
  2507. " issue #15526
  2508. set completeopt=fuzzy,menuone,menu,noselect
  2509. call setline(1, ['Text', 'ToText', ''])
  2510. call cursor(2, 1)
  2511. call feedkeys("STe\<C-X>\<C-N>x\<CR>\<Esc>0", 'tx!')
  2512. call assert_equal('Tex', getline('.'))
  2513. " test case for nosort option
  2514. set cot=menuone,menu,noinsert,fuzzy,nosort
  2515. " "fooBaz" should have a higher score when the leader is "fb".
  2516. " With "nosort", "foobar" should still be shown first in the popup menu.
  2517. call feedkeys("S\<C-x>\<C-o>fb", 'tx')
  2518. call assert_equal('foobar', g:word)
  2519. call feedkeys("S\<C-x>\<C-o>好", 'tx')
  2520. call assert_equal("你好吗", g:word)
  2521. set cot+=noselect
  2522. call feedkeys("S\<C-x>\<C-o>好", 'tx')
  2523. call assert_equal(v:null, g:word)
  2524. call feedkeys("S\<C-x>\<C-o>好\<C-N>", 'tx')
  2525. call assert_equal('你好吗', g:word)
  2526. " "nosort" shouldn't enable fuzzy filtering when "fuzzy" isn't present.
  2527. set cot=menuone,noinsert,nosort
  2528. call feedkeys("S\<C-x>\<C-o>fooB\<C-Y>", 'tx')
  2529. call assert_equal('fooBaz', getline('.'))
  2530. set cot=menuone,fuzzy,nosort
  2531. func CompAnother()
  2532. call complete(col('.'), [#{word: "do" }, #{word: "echo"}, #{word: "for (${1:expr1}, ${2:expr2}, ${3:expr3}) {\n\t$0\n}", abbr: "for" }, #{word: "foo"}])
  2533. return ''
  2534. endfunc
  2535. call feedkeys("i\<C-R>=CompAnother()\<CR>\<C-N>\<C-N>", 'tx')
  2536. call assert_equal("for", g:abbr)
  2537. call assert_equal(2, g:selected)
  2538. set cot+=noinsert
  2539. call feedkeys("i\<C-R>=CompAnother()\<CR>f", 'tx')
  2540. call assert_equal("for", g:abbr)
  2541. call assert_equal(2, g:selected)
  2542. " clean up
  2543. set omnifunc=
  2544. bw!
  2545. set complete& completeopt&
  2546. autocmd! AAAAA_Group
  2547. augroup! AAAAA_Group
  2548. delfunc OnPumChange
  2549. delfunc Omni_test
  2550. delfunc Comp
  2551. delfunc CompAnother
  2552. unlet g:item
  2553. unlet g:word
  2554. unlet g:selected
  2555. unlet g:abbr
  2556. endfunc
  2557. " Check that tie breaking is stable for completeopt+=fuzzy (which should
  2558. " behave the same on different platforms).
  2559. func Test_complete_fuzzy_match_tie()
  2560. new
  2561. set completeopt+=fuzzy,noselect
  2562. call setline(1, ['aaabbccc', 'aaabbCCC', 'aaabbcccc', 'aaabbCCCC', ''])
  2563. call feedkeys("Gcc\<C-X>\<C-N>ab\<C-N>\<C-Y>", 'tx')
  2564. call assert_equal('aaabbccc', getline('.'))
  2565. call feedkeys("Gcc\<C-X>\<C-N>ab\<C-N>\<C-N>\<C-Y>", 'tx')
  2566. call assert_equal('aaabbCCC', getline('.'))
  2567. call feedkeys("Gcc\<C-X>\<C-N>ab\<C-N>\<C-N>\<C-N>\<C-Y>", 'tx')
  2568. call assert_equal('aaabbcccc', getline('.'))
  2569. call feedkeys("Gcc\<C-X>\<C-N>ab\<C-N>\<C-N>\<C-N>\<C-N>\<C-Y>", 'tx')
  2570. call assert_equal('aaabbCCCC', getline('.'))
  2571. bwipe!
  2572. set completeopt&
  2573. endfunc
  2574. func Test_complete_info_matches()
  2575. let g:what = ['matches']
  2576. func ShownInfo()
  2577. let g:compl_info = complete_info(g:what)
  2578. return ''
  2579. endfunc
  2580. set completeopt+=noinsert
  2581. new
  2582. call setline(1, ['aaa', 'aab', 'aba', 'abb'])
  2583. inoremap <buffer><F5> <C-R>=ShownInfo()<CR>
  2584. call feedkeys("Go\<C-X>\<C-N>\<F5>\<Esc>dd", 'tx')
  2585. call assert_equal([
  2586. \ {'word': 'aaa', 'menu': '', 'user_data': '', 'info': '', 'kind': '', 'abbr': ''},
  2587. \ {'word': 'aab', 'menu': '', 'user_data': '', 'info': '', 'kind': '', 'abbr': ''},
  2588. \ {'word': 'aba', 'menu': '', 'user_data': '', 'info': '', 'kind': '', 'abbr': ''},
  2589. \ {'word': 'abb', 'menu': '', 'user_data': '', 'info': '', 'kind': '', 'abbr': ''},
  2590. \], g:compl_info['matches'])
  2591. call feedkeys("Goa\<C-X>\<C-N>b\<F5>\<Esc>dd", 'tx')
  2592. call assert_equal([
  2593. \ {'word': 'aba', 'menu': '', 'user_data': '', 'info': '', 'kind': '', 'abbr': ''},
  2594. \ {'word': 'abb', 'menu': '', 'user_data': '', 'info': '', 'kind': '', 'abbr': ''},
  2595. \], g:compl_info['matches'])
  2596. " items and matches both in what
  2597. let g:what = ['items', 'matches']
  2598. call feedkeys("Goa\<C-X>\<C-N>b\<F5>\<Esc>dd", 'tx')
  2599. call assert_equal([
  2600. \ {'word': 'aaa', 'menu': '', 'user_data': '', 'match': v:false, 'info': '', 'kind': '', 'abbr': ''},
  2601. \ {'word': 'aab', 'menu': '', 'user_data': '', 'match': v:false, 'info': '', 'kind': '', 'abbr': ''},
  2602. \ {'word': 'aba', 'menu': '', 'user_data': '', 'match': v:true, 'info': '', 'kind': '', 'abbr': ''},
  2603. \ {'word': 'abb', 'menu': '', 'user_data': '', 'match': v:true, 'info': '', 'kind': '', 'abbr': ''},
  2604. \], g:compl_info['items'])
  2605. call assert_false(has_key(g:compl_info, 'matches'))
  2606. bw!
  2607. unlet g:what
  2608. delfunc ShownInfo
  2609. set cot&
  2610. endfunc
  2611. func Test_complete_info_completed()
  2612. func ShownInfo()
  2613. let g:compl_info = complete_info(['completed'])
  2614. return ''
  2615. endfunc
  2616. set completeopt+=noinsert
  2617. new
  2618. call setline(1, ['aaa', 'aab', 'aba', 'abb'])
  2619. inoremap <buffer><F5> <C-R>=ShownInfo()<CR>
  2620. call feedkeys("Go\<C-X>\<C-N>\<F5>\<Esc>dd", 'tx')
  2621. call assert_equal({'word': 'aaa', 'menu': '', 'user_data': '', 'info': '', 'kind': '', 'abbr': ''}, g:compl_info['completed'])
  2622. call feedkeys("Go\<C-X>\<C-N>\<C-N>\<F5>\<Esc>dd", 'tx')
  2623. call assert_equal({'word': 'aab', 'menu': '', 'user_data': '', 'info': '', 'kind': '', 'abbr': ''}, g:compl_info['completed'])
  2624. call feedkeys("Go\<C-X>\<C-N>\<C-N>\<C-N>\<C-N>\<F5>\<Esc>dd", 'tx')
  2625. call assert_equal({'word': 'abb', 'menu': '', 'user_data': '', 'info': '', 'kind': '', 'abbr': ''}, g:compl_info['completed'])
  2626. set completeopt+=noselect
  2627. call feedkeys("Go\<C-X>\<C-N>\<F5>\<Esc>dd", 'tx')
  2628. call assert_equal({}, g:compl_info)
  2629. bw!
  2630. delfunc ShownInfo
  2631. set cot&
  2632. endfunc
  2633. function Test_completeopt_preinsert()
  2634. func Omni_test(findstart, base)
  2635. if a:findstart
  2636. return col(".")
  2637. endif
  2638. return [#{word: "fobar"}, #{word: "foobar"}, #{word: "你的"}, #{word: "你好世界"}]
  2639. endfunc
  2640. set omnifunc=Omni_test
  2641. set completeopt=menu,menuone,preinsert
  2642. new
  2643. call feedkeys("S\<C-X>\<C-O>f", 'tx')
  2644. call assert_equal("fobar", getline('.'))
  2645. call feedkeys("\<C-E>\<ESC>", 'tx')
  2646. call feedkeys("S\<C-X>\<C-O>foo", 'tx')
  2647. call assert_equal("foobar", getline('.'))
  2648. call feedkeys("\<C-E>\<ESC>", 'tx')
  2649. call feedkeys("S\<C-X>\<C-O>foo\<BS>\<BS>\<BS>", 'tx')
  2650. call assert_equal("", getline('.'))
  2651. call feedkeys("\<C-E>\<ESC>", 'tx')
  2652. " delete a character and input new leader
  2653. call feedkeys("S\<C-X>\<C-O>foo\<BS>b", 'tx')
  2654. call assert_equal("fobar", getline('.'))
  2655. call feedkeys("\<C-E>\<ESC>", 'tx')
  2656. " delete preinsert when prepare completion
  2657. call feedkeys("S\<C-X>\<C-O>f\<Space>", 'tx')
  2658. call assert_equal("f ", getline('.'))
  2659. call feedkeys("\<C-E>\<ESC>", 'tx')
  2660. call feedkeys("S\<C-X>\<C-O>你", 'tx')
  2661. call assert_equal("你的", getline('.'))
  2662. call feedkeys("\<C-E>\<ESC>", 'tx')
  2663. call feedkeys("S\<C-X>\<C-O>你好", 'tx')
  2664. call assert_equal("你好世界", getline('.'))
  2665. call feedkeys("\<C-E>\<ESC>", 'tx')
  2666. call feedkeys("Shello wo\<Left>\<Left>\<Left>\<C-X>\<C-O>f", 'tx')
  2667. call assert_equal("hello fobar wo", getline('.'))
  2668. call feedkeys("\<C-E>\<ESC>", 'tx')
  2669. call feedkeys("Shello wo\<Left>\<Left>\<Left>\<C-X>\<C-O>f\<BS>", 'tx')
  2670. call assert_equal("hello wo", getline('.'))
  2671. call feedkeys("\<C-E>\<ESC>", 'tx')
  2672. call feedkeys("Shello wo\<Left>\<Left>\<Left>\<C-X>\<C-O>foo", 'tx')
  2673. call assert_equal("hello foobar wo", getline('.'))
  2674. call feedkeys("\<C-E>\<ESC>", 'tx')
  2675. call feedkeys("Shello wo\<Left>\<Left>\<Left>\<C-X>\<C-O>foo\<BS>b", 'tx')
  2676. call assert_equal("hello fobar wo", getline('.'))
  2677. call feedkeys("\<C-E>\<ESC>", 'tx')
  2678. " confirm
  2679. call feedkeys("S\<C-X>\<C-O>f\<C-Y>", 'tx')
  2680. call assert_equal("fobar", getline('.'))
  2681. call assert_equal(5, col('.'))
  2682. " cancel
  2683. call feedkeys("S\<C-X>\<C-O>fo\<C-E>", 'tx')
  2684. call assert_equal("fo", getline('.'))
  2685. call assert_equal(2, col('.'))
  2686. call feedkeys("S hello hero\<CR>h\<C-X>\<C-N>", 'tx')
  2687. call assert_equal("hello", getline('.'))
  2688. call assert_equal(1, col('.'))
  2689. call feedkeys("Sh\<C-X>\<C-N>\<C-Y>", 'tx')
  2690. call assert_equal("hello", getline('.'))
  2691. call assert_equal(5, col('.'))
  2692. " delete preinsert part
  2693. call feedkeys("S\<C-X>\<C-O>fo ", 'tx')
  2694. call assert_equal("fo ", getline('.'))
  2695. call assert_equal(3, col('.'))
  2696. call feedkeys("She\<C-X>\<C-N>\<C-U>", 'tx')
  2697. call assert_equal("", getline('.'))
  2698. call assert_equal(1, col('.'))
  2699. call feedkeys("She\<C-X>\<C-N>\<C-W>", 'tx')
  2700. call assert_equal("", getline('.'))
  2701. call assert_equal(1, col('.'))
  2702. " whole line
  2703. call feedkeys("Shello hero\<CR>\<C-X>\<C-L>", 'tx')
  2704. call assert_equal("hello hero", getline('.'))
  2705. call assert_equal(1, col('.'))
  2706. call feedkeys("Shello hero\<CR>he\<C-X>\<C-L>", 'tx')
  2707. call assert_equal("hello hero", getline('.'))
  2708. call assert_equal(2, col('.'))
  2709. call feedkeys("Shello hero\<CR>h\<C-X>\<C-N>er", 'tx')
  2710. call assert_equal("hero", getline('.'))
  2711. call assert_equal(3, col('.'))
  2712. " can not work with fuzzy
  2713. set cot+=fuzzy
  2714. call feedkeys("S\<C-X>\<C-O>", 'tx')
  2715. call assert_equal("fobar", getline('.'))
  2716. call assert_equal(5, col('.'))
  2717. " test for fuzzy and noinsert
  2718. set cot+=noinsert
  2719. call feedkeys("S\<C-X>\<C-O>fb", 'tx')
  2720. call assert_equal("fb", getline('.'))
  2721. call assert_equal(2, col('.'))
  2722. call feedkeys("S\<C-X>\<C-O>你", 'tx')
  2723. call assert_equal("你", getline('.'))
  2724. call assert_equal(1, col('.'))
  2725. call feedkeys("S\<C-X>\<C-O>fb\<C-Y>", 'tx')
  2726. call assert_equal("fobar", getline('.'))
  2727. call assert_equal(5, col('.'))
  2728. bw!
  2729. set cot&
  2730. set omnifunc&
  2731. delfunc Omni_test
  2732. endfunc
  2733. " vim: shiftwidth=2 sts=2 expandtab nofoldenable