test_ins_complete.vim 85 KB

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