| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046 |
- import assert from "node:assert";
- import { describe, it } from "node:test";
- import { stripVTControlCharacters } from "node:util";
- import { type AutocompleteProvider, CombinedAutocompleteProvider } from "../src/autocomplete.ts";
- import { Editor, wordWrapLine } from "../src/components/editor.ts";
- import { TUI } from "../src/tui.ts";
- import { visibleWidth } from "../src/utils.ts";
- import { defaultEditorTheme } from "./test-themes.ts";
- import { VirtualTerminal } from "./virtual-terminal.ts";
- /** Create a TUI with a virtual terminal for testing */
- function createTestTUI(cols = 80, rows = 24): TUI {
- return new TUI(new VirtualTerminal(cols, rows));
- }
- /** Standard applyCompletion that replaces prefix with item.value */
- function applyCompletion(
- lines: string[],
- cursorLine: number,
- cursorCol: number,
- item: { value: string },
- prefix: string,
- ): { lines: string[]; cursorLine: number; cursorCol: number } {
- const line = lines[cursorLine] || "";
- const before = line.slice(0, cursorCol - prefix.length);
- const after = line.slice(cursorCol);
- const newLines = [...lines];
- newLines[cursorLine] = before + item.value + after;
- return {
- lines: newLines,
- cursorLine,
- cursorCol: cursorCol - prefix.length + item.value.length,
- };
- }
- async function flushAutocomplete(): Promise<void> {
- await Promise.resolve();
- await new Promise((resolve) => setImmediate(resolve));
- }
- describe("Editor component", () => {
- describe("Prompt history navigation", () => {
- it("does nothing on Up arrow when history is empty", () => {
- const editor = new Editor(createTestTUI(), defaultEditorTheme);
- editor.handleInput("\x1b[A"); // Up arrow
- assert.strictEqual(editor.getText(), "");
- });
- it("shows most recent history entry on Up arrow when editor is empty", () => {
- const editor = new Editor(createTestTUI(), defaultEditorTheme);
- editor.addToHistory("first prompt");
- editor.addToHistory("second prompt");
- editor.handleInput("\x1b[A"); // Up arrow
- assert.strictEqual(editor.getText(), "second prompt");
- });
- it("cycles through history entries on repeated Up arrow", () => {
- const editor = new Editor(createTestTUI(), defaultEditorTheme);
- editor.addToHistory("first");
- editor.addToHistory("second");
- editor.addToHistory("third");
- editor.handleInput("\x1b[A"); // Up - shows "third"
- assert.strictEqual(editor.getText(), "third");
- editor.handleInput("\x1b[A"); // Up - shows "second"
- assert.strictEqual(editor.getText(), "second");
- editor.handleInput("\x1b[A"); // Up - shows "first"
- assert.strictEqual(editor.getText(), "first");
- editor.handleInput("\x1b[A"); // Up - stays at "first" (oldest)
- assert.strictEqual(editor.getText(), "first");
- });
- it("restores draft on Down arrow after browsing history", () => {
- const editor = new Editor(createTestTUI(), defaultEditorTheme);
- editor.addToHistory("prompt");
- editor.setText("draft");
- editor.handleInput("\x1b[D");
- editor.handleInput("\x1b[D");
- editor.handleInput("\x1b[A"); // Up - shows "prompt"
- assert.strictEqual(editor.getText(), "prompt");
- editor.handleInput("\x1b[B"); // Down - restores draft
- assert.strictEqual(editor.getText(), "draft");
- assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 3 });
- });
- it("navigates forward through history with Down arrow", () => {
- const editor = new Editor(createTestTUI(), defaultEditorTheme);
- editor.addToHistory("first");
- editor.addToHistory("second");
- editor.addToHistory("third");
- editor.setText("draft");
- // Go to oldest
- editor.handleInput("\x1b[A"); // third
- editor.handleInput("\x1b[A"); // second
- editor.handleInput("\x1b[A"); // first
- // Navigate back
- editor.handleInput("\x1b[B"); // second
- assert.strictEqual(editor.getText(), "second");
- editor.handleInput("\x1b[B"); // third
- assert.strictEqual(editor.getText(), "third");
- editor.handleInput("\x1b[B"); // draft
- assert.strictEqual(editor.getText(), "draft");
- });
- it("exits history mode when typing a character", () => {
- const editor = new Editor(createTestTUI(), defaultEditorTheme);
- editor.addToHistory("old prompt");
- editor.handleInput("\x1b[A"); // Up - shows "old prompt"
- editor.handleInput("x"); // Type a character - exits history mode
- assert.strictEqual(editor.getText(), "xold prompt");
- });
- it("exits history mode on setText", () => {
- const editor = new Editor(createTestTUI(), defaultEditorTheme);
- editor.addToHistory("first");
- editor.addToHistory("second");
- editor.handleInput("\x1b[A"); // Up - shows "second"
- editor.setText(""); // External clear
- // Up should start fresh from most recent
- editor.handleInput("\x1b[A");
- assert.strictEqual(editor.getText(), "second");
- });
- it("does not add empty strings to history", () => {
- const editor = new Editor(createTestTUI(), defaultEditorTheme);
- editor.addToHistory("");
- editor.addToHistory(" ");
- editor.addToHistory("valid");
- editor.handleInput("\x1b[A");
- assert.strictEqual(editor.getText(), "valid");
- // Should not have more entries
- editor.handleInput("\x1b[A");
- assert.strictEqual(editor.getText(), "valid");
- });
- it("does not add consecutive duplicates to history", () => {
- const editor = new Editor(createTestTUI(), defaultEditorTheme);
- editor.addToHistory("same");
- editor.addToHistory("same");
- editor.addToHistory("same");
- editor.handleInput("\x1b[A"); // "same"
- assert.strictEqual(editor.getText(), "same");
- editor.handleInput("\x1b[A"); // stays at "same" (only one entry)
- assert.strictEqual(editor.getText(), "same");
- });
- it("allows non-consecutive duplicates in history", () => {
- const editor = new Editor(createTestTUI(), defaultEditorTheme);
- editor.addToHistory("first");
- editor.addToHistory("second");
- editor.addToHistory("first"); // Not consecutive, should be added
- editor.handleInput("\x1b[A"); // "first"
- assert.strictEqual(editor.getText(), "first");
- editor.handleInput("\x1b[A"); // "second"
- assert.strictEqual(editor.getText(), "second");
- editor.handleInput("\x1b[A"); // "first" (older one)
- assert.strictEqual(editor.getText(), "first");
- });
- it("uses cursor movement instead of history when editor has content", () => {
- const editor = new Editor(createTestTUI(), defaultEditorTheme);
- editor.addToHistory("history item");
- editor.setText("line1\nline2");
- // Cursor is at end of line2, Up should move to line1
- editor.handleInput("\x1b[A"); // Up - cursor movement
- // Insert character to verify cursor position
- editor.handleInput("X");
- // X should be inserted in line1, not replace with history
- assert.strictEqual(editor.getText(), "line1X\nline2");
- });
- it("limits history to 100 entries", () => {
- const editor = new Editor(createTestTUI(), defaultEditorTheme);
- // Add 105 entries
- for (let i = 0; i < 105; i++) {
- editor.addToHistory(`prompt ${i}`);
- }
- // Navigate to oldest
- for (let i = 0; i < 100; i++) {
- editor.handleInput("\x1b[A");
- }
- // Should be at entry 5 (oldest kept), not entry 0
- assert.strictEqual(editor.getText(), "prompt 5");
- // One more Up should not change anything
- editor.handleInput("\x1b[A");
- assert.strictEqual(editor.getText(), "prompt 5");
- });
- it("places cursor at start after browsing history upward", () => {
- const editor = new Editor(createTestTUI(), defaultEditorTheme);
- editor.addToHistory("older entry");
- editor.addToHistory("line1\nline2\nline3");
- editor.handleInput("\x1b[A"); // Up - shows multi-line entry at start
- assert.strictEqual(editor.getText(), "line1\nline2\nline3");
- assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 0 });
- editor.handleInput("\x1b[A"); // Up again - immediately navigates to older entry
- assert.strictEqual(editor.getText(), "older entry");
- assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 0 });
- });
- it("places cursor at end after browsing history downward", () => {
- const editor = new Editor(createTestTUI(), defaultEditorTheme);
- editor.addToHistory("older entry");
- editor.addToHistory("line1\nline2\nline3");
- editor.addToHistory("newer entry");
- editor.handleInput("\x1b[A"); // newer entry
- editor.handleInput("\x1b[A"); // multi-line entry
- editor.handleInput("\x1b[A"); // older entry
- editor.handleInput("\x1b[B"); // Down - shows multi-line entry at end
- assert.strictEqual(editor.getText(), "line1\nline2\nline3");
- assert.deepStrictEqual(editor.getCursor(), { line: 2, col: 5 });
- editor.handleInput("\x1b[B"); // Down again - immediately navigates to newer entry
- assert.strictEqual(editor.getText(), "newer entry");
- });
- it("allows opposite-direction cursor movement within multi-line history entry", () => {
- const editor = new Editor(createTestTUI(), defaultEditorTheme);
- editor.addToHistory("line1\nline2\nline3");
- editor.handleInput("\x1b[A"); // Up - shows entry at start
- assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 0 });
- editor.handleInput("\x1b[B"); // Down - cursor moves to line2
- assert.strictEqual(editor.getText(), "line1\nline2\nline3");
- assert.deepStrictEqual(editor.getCursor(), { line: 1, col: 0 });
- editor.handleInput("\x1b[A"); // Up - cursor moves back to line1
- assert.strictEqual(editor.getText(), "line1\nline2\nline3");
- assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 0 });
- });
- });
- describe("public state accessors", () => {
- it("returns cursor position", () => {
- const editor = new Editor(createTestTUI(), defaultEditorTheme);
- assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 0 });
- editor.handleInput("a");
- editor.handleInput("b");
- editor.handleInput("c");
- assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 3 });
- editor.handleInput("\x1b[D"); // Left
- assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 2 });
- });
- it("returns lines as a defensive copy", () => {
- const editor = new Editor(createTestTUI(), defaultEditorTheme);
- editor.setText("a\nb");
- const lines = editor.getLines();
- assert.deepStrictEqual(lines, ["a", "b"]);
- lines[0] = "mutated";
- assert.deepStrictEqual(editor.getLines(), ["a", "b"]);
- });
- });
- describe("Backslash+Enter newline workaround", () => {
- it("inserts backslash immediately (no buffering)", () => {
- const editor = new Editor(createTestTUI(), defaultEditorTheme);
- editor.handleInput("\\");
- // Backslash should be visible immediately, not buffered
- assert.strictEqual(editor.getText(), "\\");
- });
- it("converts standalone backslash to newline on Enter", () => {
- const editor = new Editor(createTestTUI(), defaultEditorTheme);
- editor.handleInput("\\");
- editor.handleInput("\r");
- assert.strictEqual(editor.getText(), "\n");
- });
- it("inserts backslash normally when followed by other characters", () => {
- const editor = new Editor(createTestTUI(), defaultEditorTheme);
- editor.handleInput("\\");
- editor.handleInput("x");
- assert.strictEqual(editor.getText(), "\\x");
- });
- it("does not trigger newline when backslash is not immediately before cursor", () => {
- const editor = new Editor(createTestTUI(), defaultEditorTheme);
- let submitted = false;
- editor.onSubmit = () => {
- submitted = true;
- };
- editor.handleInput("\\");
- editor.handleInput("x");
- editor.handleInput("\r");
- // Should submit, not insert newline (backslash not at cursor)
- assert.strictEqual(submitted, true);
- });
- it("only removes one backslash when multiple are present", () => {
- const editor = new Editor(createTestTUI(), defaultEditorTheme);
- editor.handleInput("\\");
- editor.handleInput("\\");
- editor.handleInput("\\");
- assert.strictEqual(editor.getText(), "\\\\\\");
- editor.handleInput("\r");
- // Only the last backslash is removed, newline inserted
- assert.strictEqual(editor.getText(), "\\\\\n");
- });
- });
- describe("Kitty CSI-u handling", () => {
- it("ignores printable CSI-u sequences with unsupported modifiers", () => {
- const editor = new Editor(createTestTUI(), defaultEditorTheme);
- editor.handleInput("\x1b[99;9u");
- assert.strictEqual(editor.getText(), "");
- });
- it("inserts shifted CSI-u letters as text", () => {
- const editor = new Editor(createTestTUI(), defaultEditorTheme);
- editor.handleInput("\x1b[69;2u");
- assert.strictEqual(editor.getText(), "E");
- });
- it("inserts shifted xterm modifyOtherKeys letters as text", () => {
- const editor = new Editor(createTestTUI(), defaultEditorTheme);
- editor.handleInput("\x1b[27;2;69~");
- assert.strictEqual(editor.getText(), "E");
- });
- });
- describe("Unicode text editing behavior", () => {
- it("inserts mixed ASCII, umlauts, and emojis as literal text", () => {
- const editor = new Editor(createTestTUI(), defaultEditorTheme);
- editor.handleInput("H");
- editor.handleInput("e");
- editor.handleInput("l");
- editor.handleInput("l");
- editor.handleInput("o");
- editor.handleInput(" ");
- editor.handleInput("ä");
- editor.handleInput("ö");
- editor.handleInput("ü");
- editor.handleInput(" ");
- editor.handleInput("😀");
- const text = editor.getText();
- assert.strictEqual(text, "Hello äöü 😀");
- });
- it("deletes single-code-unit unicode characters (umlauts) with Backspace", () => {
- const editor = new Editor(createTestTUI(), defaultEditorTheme);
- editor.handleInput("ä");
- editor.handleInput("ö");
- editor.handleInput("ü");
- // Delete the last character (ü)
- editor.handleInput("\x7f"); // Backspace
- const text = editor.getText();
- assert.strictEqual(text, "äö");
- });
- it("deletes multi-code-unit emojis with single Backspace", () => {
- const editor = new Editor(createTestTUI(), defaultEditorTheme);
- editor.handleInput("😀");
- editor.handleInput("👍");
- // Delete the last emoji (👍) - single backspace deletes whole grapheme cluster
- editor.handleInput("\x7f"); // Backspace
- const text = editor.getText();
- assert.strictEqual(text, "😀");
- });
- it("inserts characters at the correct position after cursor movement over umlauts", () => {
- const editor = new Editor(createTestTUI(), defaultEditorTheme);
- editor.handleInput("ä");
- editor.handleInput("ö");
- editor.handleInput("ü");
- // Move cursor left twice
- editor.handleInput("\x1b[D"); // Left arrow
- editor.handleInput("\x1b[D"); // Left arrow
- // Insert 'x' in the middle
- editor.handleInput("x");
- const text = editor.getText();
- assert.strictEqual(text, "äxöü");
- });
- it("moves cursor across multi-code-unit emojis with single arrow key", () => {
- const editor = new Editor(createTestTUI(), defaultEditorTheme);
- editor.handleInput("😀");
- editor.handleInput("👍");
- editor.handleInput("🎉");
- // Move cursor left over last emoji (🎉) - single arrow moves over whole grapheme
- editor.handleInput("\x1b[D"); // Left arrow
- // Move cursor left over second emoji (👍)
- editor.handleInput("\x1b[D");
- // Insert 'x' between first and second emoji
- editor.handleInput("x");
- const text = editor.getText();
- assert.strictEqual(text, "😀x👍🎉");
- });
- it("preserves umlauts across line breaks", () => {
- const editor = new Editor(createTestTUI(), defaultEditorTheme);
- editor.handleInput("ä");
- editor.handleInput("ö");
- editor.handleInput("ü");
- editor.handleInput("\n"); // new line
- editor.handleInput("Ä");
- editor.handleInput("Ö");
- editor.handleInput("Ü");
- const text = editor.getText();
- assert.strictEqual(text, "äöü\nÄÖÜ");
- });
- it("replaces the entire document with unicode text via setText (paste simulation)", () => {
- const editor = new Editor(createTestTUI(), defaultEditorTheme);
- // Simulate bracketed paste / programmatic replacement
- editor.setText("Hällö Wörld! 😀 äöüÄÖÜß");
- const text = editor.getText();
- assert.strictEqual(text, "Hällö Wörld! 😀 äöüÄÖÜß");
- });
- it("moves cursor to document start on Ctrl+A and inserts at the beginning", () => {
- const editor = new Editor(createTestTUI(), defaultEditorTheme);
- editor.handleInput("a");
- editor.handleInput("b");
- editor.handleInput("\x01"); // Ctrl+A (move to start)
- editor.handleInput("x"); // Insert at start
- const text = editor.getText();
- assert.strictEqual(text, "xab");
- });
- it("deletes words correctly with Ctrl+W and Alt+Backspace", () => {
- const editor = new Editor(createTestTUI(), defaultEditorTheme);
- // Basic word deletion
- editor.setText("foo bar baz");
- editor.handleInput("\x17"); // Ctrl+W
- assert.strictEqual(editor.getText(), "foo bar ");
- // Trailing whitespace
- editor.setText("foo bar ");
- editor.handleInput("\x17");
- assert.strictEqual(editor.getText(), "foo ");
- // Punctuation run
- editor.setText("foo bar...");
- editor.handleInput("\x17");
- assert.strictEqual(editor.getText(), "foo bar");
- // ASCII punctuation inside Intl word-like segments preserves old boundaries
- editor.setText("foo.bar");
- editor.handleInput("\x17");
- assert.strictEqual(editor.getText(), "foo.");
- editor.setText("foo:bar");
- editor.handleInput("\x17");
- assert.strictEqual(editor.getText(), "foo:");
- // Delete across multiple lines
- editor.setText("line one\nline two");
- editor.handleInput("\x17");
- assert.strictEqual(editor.getText(), "line one\nline ");
- // Delete empty line (merge)
- editor.setText("line one\n");
- editor.handleInput("\x17");
- assert.strictEqual(editor.getText(), "line one");
- // Grapheme safety (emoji as a word)
- editor.setText("foo 😀😀 bar");
- editor.handleInput("\x17");
- assert.strictEqual(editor.getText(), "foo 😀😀 ");
- editor.handleInput("\x17");
- assert.strictEqual(editor.getText(), "foo ");
- // Alt+Backspace
- editor.setText("foo bar");
- editor.handleInput("\x1b\x7f"); // Alt+Backspace (legacy)
- assert.strictEqual(editor.getText(), "foo ");
- });
- it("navigates words correctly with Ctrl+Left/Right", () => {
- const editor = new Editor(createTestTUI(), defaultEditorTheme);
- editor.setText("foo bar... baz");
- // Cursor at end
- // Move left over baz
- editor.handleInput("\x1b[1;5D"); // Ctrl+Left
- assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 11 }); // after '...'
- // Move left over punctuation
- editor.handleInput("\x1b[1;5D"); // Ctrl+Left
- assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 7 }); // after 'bar'
- // Move left over bar
- editor.handleInput("\x1b[1;5D"); // Ctrl+Left
- assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 4 }); // after 'foo '
- // Move right over bar
- editor.handleInput("\x1b[1;5C"); // Ctrl+Right
- assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 7 }); // at end of 'bar'
- // Move right over punctuation run
- editor.handleInput("\x1b[1;5C"); // Ctrl+Right
- assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 10 }); // after '...'
- // Move right skips space and lands after baz
- editor.handleInput("\x1b[1;5C"); // Ctrl+Right
- assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 14 }); // end of line
- // Test forward from start with leading whitespace
- editor.setText(" foo bar");
- editor.handleInput("\x01"); // Ctrl+A to go to start
- editor.handleInput("\x1b[1;5C"); // Ctrl+Right
- assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 6 }); // after 'foo'
- // ASCII punctuation inside Intl word-like segments preserves old boundaries
- editor.setText("foo.bar baz");
- editor.handleInput("\x1b[1;5D"); // Ctrl+Left over baz
- assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 8 });
- editor.handleInput("\x1b[1;5D"); // Ctrl+Left over bar
- assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 4 });
- editor.handleInput("\x1b[1;5D"); // Ctrl+Left over .
- assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 3 });
- editor.handleInput("\x01"); // Ctrl+A
- editor.handleInput("\x1b[1;5C"); // Ctrl+Right over foo
- assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 3 });
- editor.handleInput("\x1b[1;5C"); // Ctrl+Right over .
- assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 4 });
- editor.handleInput("\x1b[1;5C"); // Ctrl+Right over bar
- assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 7 });
- });
- it("stops at fullwidth Chinese punctuation (issue #4972)", () => {
- const editor = new Editor(createTestTUI(), defaultEditorTheme);
- // 你好,世界 = 你好(0-2) ,(2-3) 世界(3-5)
- editor.setText("你好,世界");
- // Cursor at end (col 5)
- // Move left over 世界
- editor.handleInput("\x1b[1;5D"); // Ctrl+Left
- assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 3 }); // after ,
- // Move left over ,
- editor.handleInput("\x1b[1;5D"); // Ctrl+Left
- assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 2 }); // after 你好
- // Move left over 你好
- editor.handleInput("\x1b[1;5D"); // Ctrl+Left
- assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 0 }); // start
- // Move right over 你好
- editor.handleInput("\x1b[1;5C"); // Ctrl+Right
- assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 2 }); // after 你好
- // Move right over ,
- editor.handleInput("\x1b[1;5C"); // Ctrl+Right
- assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 3 }); // after ,
- // Move right over 世界
- editor.handleInput("\x1b[1;5C"); // Ctrl+Right
- assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 5 }); // end
- });
- it("handles mixed CJK and ASCII word movement", () => {
- const editor = new Editor(createTestTUI(), defaultEditorTheme);
- // "hello你好,world世界" = hello(0-5) 你好(5-7) ,(7-8) world(8-13) 世界(13-15)
- editor.setText("hello你好,world世界");
- // Cursor at end (col 15)
- // Move left over 世界
- editor.handleInput("\x1b[1;5D"); // Ctrl+Left
- assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 13 }); // after 'world'
- // Move left over world
- editor.handleInput("\x1b[1;5D"); // Ctrl+Left
- assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 8 }); // after ,
- // Move left over ,
- editor.handleInput("\x1b[1;5D"); // Ctrl+Left
- assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 7 }); // after 你好
- // Move left over 你好
- editor.handleInput("\x1b[1;5D"); // Ctrl+Left
- assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 5 }); // after 'hello'
- // Move left over hello
- editor.handleInput("\x1b[1;5D"); // Ctrl+Left
- assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 0 }); // start
- // Forward from start
- editor.handleInput("\x1b[1;5C"); // Ctrl+Right
- assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 5 }); // after 'hello'
- editor.handleInput("\x1b[1;5C"); // Ctrl+Right
- assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 7 }); // after 你好
- editor.handleInput("\x1b[1;5C"); // Ctrl+Right
- assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 8 }); // after ,
- editor.handleInput("\x1b[1;5C"); // Ctrl+Right
- assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 13 }); // after 'world'
- editor.handleInput("\x1b[1;5C"); // Ctrl+Right
- assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 15 }); // end
- });
- });
- describe("Grapheme-aware text wrapping", () => {
- it("wraps lines correctly when text contains wide emojis", () => {
- const editor = new Editor(createTestTUI(), defaultEditorTheme);
- const width = 20;
- // ✅ is 2 columns wide, so "Hello ✅ World" is 14 columns
- editor.setText("Hello ✅ World");
- const lines = editor.render(width);
- // All content lines (between borders) should fit within width
- for (let i = 1; i < lines.length - 1; i++) {
- const lineWidth = visibleWidth(lines[i]!);
- assert.strictEqual(lineWidth, width, `Line ${i} has width ${lineWidth}, expected ${width}`);
- }
- });
- it("wraps long text with emojis at correct positions", () => {
- const editor = new Editor(createTestTUI(), defaultEditorTheme);
- const width = 10;
- // Each ✅ is 2 columns. "✅✅✅✅✅" = 10 columns, fits exactly
- // "✅✅✅✅✅✅" = 12 columns, needs wrap
- editor.setText("✅✅✅✅✅✅");
- const lines = editor.render(width);
- // Should have 2 content lines (plus 2 border lines)
- // First line: 5 emojis (10 cols), second line: 1 emoji (2 cols) + padding
- for (let i = 1; i < lines.length - 1; i++) {
- const lineWidth = visibleWidth(lines[i]!);
- assert.strictEqual(lineWidth, width, `Line ${i} has width ${lineWidth}, expected ${width}`);
- }
- });
- it("renders isolated Thai and Lao AM clusters without width drift", () => {
- for (const text of ["ำabc", "ຳabc"]) {
- const editor = new Editor(createTestTUI(), defaultEditorTheme);
- const width = 8;
- editor.setText(text);
- for (const line of editor.render(width)) {
- assert.strictEqual(visibleWidth(line), width, `line width drift for ${JSON.stringify(text)}: ${line}`);
- }
- }
- });
- it("wraps CJK characters correctly (each is 2 columns wide)", () => {
- const editor = new Editor(createTestTUI(), defaultEditorTheme);
- const width = 10 + 1; // +1 col reserved for cursor
- // Each CJK char is 2 columns. "日本語テスト" = 6 chars = 12 columns
- editor.setText("日本語テスト");
- const lines = editor.render(width);
- for (let i = 1; i < lines.length - 1; i++) {
- const lineWidth = visibleWidth(lines[i]!);
- assert.strictEqual(lineWidth, width, `Line ${i} has width ${lineWidth}, expected ${width}`);
- }
- // Verify content split correctly
- const contentLines = lines.slice(1, -1).map((l) => stripVTControlCharacters(l).trim());
- assert.strictEqual(contentLines.length, 2);
- assert.strictEqual(contentLines[0], "日本語テス"); // 5 chars = 10 columns
- assert.strictEqual(contentLines[1], "ト"); // 1 char = 2 columns (+ padding)
- });
- it("handles mixed ASCII and wide characters in wrapping", () => {
- const editor = new Editor(createTestTUI(), defaultEditorTheme);
- const width = 15 + 1; // +1 col reserved for cursor
- // "Test ✅ OK 日本" = 4 + 1 + 2 + 1 + 2 + 1 + 4 = 15 columns (fits in width-1=15)
- editor.setText("Test ✅ OK 日本");
- const lines = editor.render(width);
- // Should fit in one content line
- const contentLines = lines.slice(1, -1);
- assert.strictEqual(contentLines.length, 1);
- const lineWidth = visibleWidth(contentLines[0]!);
- assert.strictEqual(lineWidth, width);
- });
- it("renders cursor correctly on wide characters", () => {
- const editor = new Editor(createTestTUI(), defaultEditorTheme);
- const width = 20;
- editor.setText("A✅B");
- // Cursor should be at end (after B)
- const lines = editor.render(width);
- // The cursor (reverse video space) should be visible
- const contentLine = lines[1]!;
- assert.ok(contentLine.includes("\x1b[7m"), "Should have reverse video cursor");
- // Line should still be correct width
- assert.strictEqual(visibleWidth(contentLine), width);
- });
- it("does not exceed terminal width with emoji at wrap boundary", () => {
- const editor = new Editor(createTestTUI(), defaultEditorTheme);
- const width = 11;
- // "0123456789✅" = 10 ASCII + 2-wide emoji = 12 columns
- // Should wrap before the emoji since it would exceed width
- editor.setText("0123456789✅");
- const lines = editor.render(width);
- for (let i = 1; i < lines.length - 1; i++) {
- const lineWidth = visibleWidth(lines[i]!);
- assert.ok(lineWidth <= width, `Line ${i} has width ${lineWidth}, exceeds max ${width}`);
- }
- });
- it("shows cursor at end of line before wrap, wraps on next char", () => {
- const width = 10;
- for (const paddingX of [0, 1]) {
- const editor = new Editor(createTestTUI(width + paddingX), defaultEditorTheme, { paddingX });
- // Type 9 chars → fills layoutWidth exactly, cursor at end on same line
- for (const ch of "aaaaaaaaa") editor.handleInput(ch);
- let lines = editor.render(width + paddingX);
- let contentLines = lines.slice(1, -1);
- assert.strictEqual(contentLines.length, 1, "Should be 1 content line before wrap");
- assert.ok(contentLines[0]!.endsWith("\x1b[7m \x1b[0m"), "Cursor should be at end of line");
- // Type 1 more → text wraps to second line
- editor.handleInput("a");
- lines = editor.render(width + paddingX);
- contentLines = lines.slice(1, -1);
- assert.strictEqual(contentLines.length, 2, "Should wrap to 2 content lines");
- }
- });
- });
- describe("Word wrapping", () => {
- it("wraps at word boundaries instead of mid-word", () => {
- const editor = new Editor(createTestTUI(), defaultEditorTheme);
- const width = 40;
- editor.setText("Hello world this is a test of word wrapping functionality");
- const lines = editor.render(width);
- // Get content lines (between borders)
- const contentLines = lines.slice(1, -1).map((l) => stripVTControlCharacters(l).trim());
- // Should NOT break mid-word
- // Line 1 should end with a complete word
- assert.ok(!contentLines[0]!.endsWith("-"), "Line should not end with hyphen (mid-word break)");
- // Each content line should be complete words
- for (const line of contentLines) {
- // Words at end of line should be complete (no partial words)
- const lastChar = line.trimEnd().slice(-1);
- assert.ok(lastChar === "" || /[\w.,!?;:]/.test(lastChar), `Line ends unexpectedly with: "${lastChar}"`);
- }
- });
- it("does not start lines with leading whitespace after word wrap", () => {
- const editor = new Editor(createTestTUI(), defaultEditorTheme);
- const width = 20;
- editor.setText("Word1 Word2 Word3 Word4 Word5 Word6");
- const lines = editor.render(width);
- // Get content lines (between borders)
- const contentLines = lines.slice(1, -1);
- // No line should start with whitespace (except for padding at the end)
- for (let i = 0; i < contentLines.length; i++) {
- const line = stripVTControlCharacters(contentLines[i]!);
- const trimmedStart = line.trimStart();
- // The line should either be all padding or start with a word character
- if (trimmedStart.length > 0) {
- assert.ok(!/^\s+\S/.test(line.trimEnd()), `Line ${i} starts with unexpected whitespace before content`);
- }
- }
- });
- it("breaks long words (URLs) at character level", () => {
- const editor = new Editor(createTestTUI(), defaultEditorTheme);
- const width = 30;
- editor.setText("Check https://example.com/very/long/path/that/exceeds/width here");
- const lines = editor.render(width);
- // All lines should fit within width
- for (let i = 1; i < lines.length - 1; i++) {
- const lineWidth = visibleWidth(lines[i]!);
- assert.strictEqual(lineWidth, width, `Line ${i} has width ${lineWidth}, expected ${width}`);
- }
- });
- it("preserves multiple spaces within words on same line", () => {
- const editor = new Editor(createTestTUI(), defaultEditorTheme);
- const width = 50;
- editor.setText("Word1 Word2 Word3");
- const lines = editor.render(width);
- const contentLine = stripVTControlCharacters(lines[1]!).trim();
- // Multiple spaces should be preserved
- assert.ok(contentLine.includes("Word1 Word2"), "Multiple spaces should be preserved");
- });
- it("handles empty string", () => {
- const editor = new Editor(createTestTUI(), defaultEditorTheme);
- const width = 40;
- editor.setText("");
- const lines = editor.render(width);
- // Should have border + empty content + border
- assert.strictEqual(lines.length, 3);
- });
- it("handles single word that fits exactly", () => {
- const editor = new Editor(createTestTUI(), defaultEditorTheme);
- const width = 10 + 1; // +1 col reserved for cursor
- editor.setText("1234567890");
- const lines = editor.render(width);
- // Should have exactly 3 lines (top border, content, bottom border)
- assert.strictEqual(lines.length, 3);
- const contentLine = stripVTControlCharacters(lines[1]!);
- assert.ok(contentLine.includes("1234567890"), "Content should contain the word");
- });
- it("wraps word to next line when it ends exactly at terminal width", () => {
- // "hello " (6) + "world" (5) = 11, but "world" is non-whitespace ending at width.
- // Thus, wrap it to next line. The trailing space stays with "hello" on line 1
- const chunks = wordWrapLine("hello world test", 11);
- assert.strictEqual(chunks.length, 2);
- assert.strictEqual(chunks[0]!.text, "hello ");
- assert.strictEqual(chunks[1]!.text, "world test");
- });
- it("keeps whitespace at terminal width boundary on same line", () => {
- // "hello world " is exactly 12 chars (including trailing space)
- // The space at position 12 should stay on the first line
- const chunks = wordWrapLine("hello world test", 12);
- assert.strictEqual(chunks.length, 2);
- assert.strictEqual(chunks[0]!.text, "hello world ");
- assert.strictEqual(chunks[1]!.text, "test");
- });
- it("handles unbreakable word filling width exactly followed by space", () => {
- const chunks = wordWrapLine("aaaaaaaaaaaa aaaa", 12);
- assert.strictEqual(chunks.length, 2);
- assert.strictEqual(chunks[0]!.text, "aaaaaaaaaaaa");
- assert.strictEqual(chunks[1]!.text, " aaaa");
- });
- it("wraps word to next line when it fits width but not remaining space", () => {
- const chunks = wordWrapLine(" aaaaaaaaaaaa", 12);
- assert.strictEqual(chunks.length, 2);
- assert.strictEqual(chunks[0]!.text, " ");
- assert.strictEqual(chunks[1]!.text, "aaaaaaaaaaaa");
- });
- it("keeps word with multi-space and following word together when they fit", () => {
- const chunks = wordWrapLine("Lorem ipsum dolor sit amet, consectetur", 30);
- assert.strictEqual(chunks.length, 2);
- assert.strictEqual(chunks[0]!.text, "Lorem ipsum dolor sit ");
- assert.strictEqual(chunks[1]!.text, "amet, consectetur");
- });
- it("keeps word with multi-space and following word when they fill width exactly", () => {
- const chunks = wordWrapLine("Lorem ipsum dolor sit amet, consectetur", 30);
- assert.strictEqual(chunks.length, 2);
- assert.strictEqual(chunks[0]!.text, "Lorem ipsum dolor sit ");
- assert.strictEqual(chunks[1]!.text, "amet, consectetur");
- });
- it("splits when word plus multi-space plus word exceeds width", () => {
- const chunks = wordWrapLine("Lorem ipsum dolor sit amet, consectetur", 30);
- assert.strictEqual(chunks.length, 3);
- assert.strictEqual(chunks[0]!.text, "Lorem ipsum dolor sit ");
- assert.strictEqual(chunks[1]!.text, "amet, ");
- assert.strictEqual(chunks[2]!.text, "consectetur");
- });
- it("breaks long whitespace at line boundary", () => {
- const chunks = wordWrapLine("Lorem ipsum dolor sit amet, consectetur", 30);
- assert.strictEqual(chunks.length, 3);
- assert.strictEqual(chunks[0]!.text, "Lorem ipsum dolor sit ");
- assert.strictEqual(chunks[1]!.text, "amet, ");
- assert.strictEqual(chunks[2]!.text, "consectetur");
- });
- it("breaks long whitespace at line boundary 2", () => {
- const chunks = wordWrapLine("Lorem ipsum dolor sit amet, consectetur", 30);
- assert.strictEqual(chunks.length, 3);
- assert.strictEqual(chunks[0]!.text, "Lorem ipsum dolor sit ");
- assert.strictEqual(chunks[1]!.text, "amet, ");
- assert.strictEqual(chunks[2]!.text, " consectetur");
- });
- it("breaks whitespace spanning full lines", () => {
- const chunks = wordWrapLine("Lorem ipsum dolor sit amet, consectetur", 30);
- assert.strictEqual(chunks.length, 3);
- assert.strictEqual(chunks[0]!.text, "Lorem ipsum dolor sit ");
- assert.strictEqual(chunks[1]!.text, "amet, ");
- assert.strictEqual(chunks[2]!.text, " consectetur");
- });
- it("force-breaks when wide char after word boundary wrap still overflows", () => {
- // " " (1) + "a"*186 (186) + "你" (2) = 189 visible width
- // maxWidth = 187: backtracking to the space would leave 186 + 2 = 188 > 187,
- // so the algorithm must force-break before the wide char instead.
- const line = ` ${"a".repeat(186)}你`;
- const chunks = wordWrapLine(line, 187);
- for (const chunk of chunks) {
- assert.ok(
- visibleWidth(chunk.text) <= 187,
- `chunk "${chunk.text.slice(0, 20)}..." has visible width ${visibleWidth(chunk.text)}, expected <= 187`,
- );
- }
- // Verify no content is lost
- const reconstructed = chunks.map((c) => line.slice(c.startIndex, c.endIndex)).join("");
- assert.strictEqual(reconstructed, line);
- });
- it("splits oversized atomic segment across multiple chunks", () => {
- // Simulate a paste marker wider than maxWidth by passing pre-segmented data
- const marker = "[paste #1 +20 lines]"; // 21 chars
- const line = `A${marker}B`;
- const segments: Intl.SegmentData[] = [
- { segment: "A", index: 0, input: line },
- { segment: marker, index: 1, input: line },
- { segment: "B", index: 1 + marker.length, input: line },
- ];
- const chunks = wordWrapLine(line, 10, segments);
- // Every chunk must fit within maxWidth
- for (const chunk of chunks) {
- assert.ok(
- visibleWidth(chunk.text) <= 10,
- `chunk "${chunk.text}" has visible width ${visibleWidth(chunk.text)}, expected <= 10`,
- );
- }
- // Verify no content is lost
- const reconstructed = chunks.map((c) => line.slice(c.startIndex, c.endIndex)).join("");
- assert.strictEqual(reconstructed, line);
- });
- it("splits oversized atomic segment at start of line", () => {
- const marker = "[paste #1 +20 lines]"; // 21 chars
- const line = `${marker}B`;
- const segments: Intl.SegmentData[] = [
- { segment: marker, index: 0, input: line },
- { segment: "B", index: marker.length, input: line },
- ];
- const chunks = wordWrapLine(line, 10, segments);
- for (const chunk of chunks) {
- assert.ok(visibleWidth(chunk.text) <= 10);
- }
- // "B" ends up on the last line (either alone or with the marker tail)
- assert.strictEqual(chunks[chunks.length - 1]!.text.includes("B"), true);
- const reconstructed = chunks.map((c) => line.slice(c.startIndex, c.endIndex)).join("");
- assert.strictEqual(reconstructed, line);
- });
- it("splits oversized atomic segment at end of line", () => {
- const marker = "[paste #1 +20 lines]"; // 21 chars
- const line = `A${marker}`;
- const segments: Intl.SegmentData[] = [
- { segment: "A", index: 0, input: line },
- { segment: marker, index: 1, input: line },
- ];
- const chunks = wordWrapLine(line, 10, segments);
- for (const chunk of chunks) {
- assert.ok(visibleWidth(chunk.text) <= 10);
- }
- assert.strictEqual(chunks[0]!.text, "A");
- const reconstructed = chunks.map((c) => line.slice(c.startIndex, c.endIndex)).join("");
- assert.strictEqual(reconstructed, line);
- });
- it("splits consecutive oversized atomic segments", () => {
- const m1 = "[paste #1 +20 lines]"; // 21 chars
- const m2 = "[paste #2 +30 lines]"; // 21 chars
- const line = `${m1}${m2}`;
- const segments: Intl.SegmentData[] = [
- { segment: m1, index: 0, input: line },
- { segment: m2, index: m1.length, input: line },
- ];
- const chunks = wordWrapLine(line, 10, segments);
- for (const chunk of chunks) {
- assert.ok(
- visibleWidth(chunk.text) <= 10,
- `chunk "${chunk.text}" has visible width ${visibleWidth(chunk.text)}, expected <= 10`,
- );
- }
- const reconstructed = chunks.map((c) => line.slice(c.startIndex, c.endIndex)).join("");
- assert.strictEqual(reconstructed, line);
- });
- it("wraps normally after oversized atomic segment", () => {
- const marker = "[paste #1 +20 lines]"; // 21 chars
- const line = `${marker} hello world`;
- const segments: Intl.SegmentData[] = [
- { segment: marker, index: 0, input: line },
- { segment: " ", index: marker.length, input: line },
- { segment: "h", index: marker.length + 1, input: line },
- { segment: "e", index: marker.length + 2, input: line },
- { segment: "l", index: marker.length + 3, input: line },
- { segment: "l", index: marker.length + 4, input: line },
- { segment: "o", index: marker.length + 5, input: line },
- { segment: " ", index: marker.length + 6, input: line },
- { segment: "w", index: marker.length + 7, input: line },
- { segment: "o", index: marker.length + 8, input: line },
- { segment: "r", index: marker.length + 9, input: line },
- { segment: "l", index: marker.length + 10, input: line },
- { segment: "d", index: marker.length + 11, input: line },
- ];
- const chunks = wordWrapLine(line, 10, segments);
- // All chunks must fit
- for (const chunk of chunks) {
- assert.ok(
- visibleWidth(chunk.text) <= 10,
- `chunk "${chunk.text}" has visible width ${visibleWidth(chunk.text)}, expected <= 10`,
- );
- }
- // Last chunk should contain "world" (normal wrapping resumes)
- assert.strictEqual(chunks[chunks.length - 1]!.text, "world");
- const reconstructed = chunks.map((c) => line.slice(c.startIndex, c.endIndex)).join("");
- assert.strictEqual(reconstructed, line);
- });
- });
- describe("Kill ring", () => {
- it("Ctrl+W saves deleted text to kill ring and Ctrl+Y yanks it", () => {
- const editor = new Editor(createTestTUI(), defaultEditorTheme);
- editor.setText("foo bar baz");
- editor.handleInput("\x17"); // Ctrl+W - deletes "baz"
- assert.strictEqual(editor.getText(), "foo bar ");
- // Move to beginning and yank
- editor.handleInput("\x01"); // Ctrl+A
- editor.handleInput("\x19"); // Ctrl+Y
- assert.strictEqual(editor.getText(), "bazfoo bar ");
- });
- it("Ctrl+U saves deleted text to kill ring", () => {
- const editor = new Editor(createTestTUI(), defaultEditorTheme);
- editor.setText("hello world");
- // Move cursor to middle
- editor.handleInput("\x01"); // Ctrl+A (start)
- editor.handleInput("\x1b[C"); // Right 5 times
- editor.handleInput("\x1b[C");
- editor.handleInput("\x1b[C");
- editor.handleInput("\x1b[C");
- editor.handleInput("\x1b[C");
- editor.handleInput("\x1b[C"); // After "hello "
- editor.handleInput("\x15"); // Ctrl+U - deletes "hello "
- assert.strictEqual(editor.getText(), "world");
- editor.handleInput("\x19"); // Ctrl+Y
- assert.strictEqual(editor.getText(), "hello world");
- });
- it("Ctrl+K saves deleted text to kill ring", () => {
- const editor = new Editor(createTestTUI(), defaultEditorTheme);
- editor.setText("hello world");
- editor.handleInput("\x01"); // Ctrl+A (start)
- editor.handleInput("\x0b"); // Ctrl+K - deletes "hello world"
- assert.strictEqual(editor.getText(), "");
- editor.handleInput("\x19"); // Ctrl+Y
- assert.strictEqual(editor.getText(), "hello world");
- });
- it("Ctrl+Y does nothing when kill ring is empty", () => {
- const editor = new Editor(createTestTUI(), defaultEditorTheme);
- editor.setText("test");
- editor.handleInput("\x19"); // Ctrl+Y
- assert.strictEqual(editor.getText(), "test");
- });
- it("Alt+Y cycles through kill ring after Ctrl+Y", () => {
- const editor = new Editor(createTestTUI(), defaultEditorTheme);
- // Create kill ring with multiple entries
- editor.setText("first");
- editor.handleInput("\x17"); // Ctrl+W - deletes "first"
- editor.setText("second");
- editor.handleInput("\x17"); // Ctrl+W - deletes "second"
- editor.setText("third");
- editor.handleInput("\x17"); // Ctrl+W - deletes "third"
- // Kill ring now has: [first, second, third]
- assert.strictEqual(editor.getText(), "");
- editor.handleInput("\x19"); // Ctrl+Y - yanks "third" (most recent)
- assert.strictEqual(editor.getText(), "third");
- editor.handleInput("\x1by"); // Alt+Y - cycles to "second"
- assert.strictEqual(editor.getText(), "second");
- editor.handleInput("\x1by"); // Alt+Y - cycles to "first"
- assert.strictEqual(editor.getText(), "first");
- editor.handleInput("\x1by"); // Alt+Y - cycles back to "third"
- assert.strictEqual(editor.getText(), "third");
- });
- it("Alt+Y does nothing if not preceded by yank", () => {
- const editor = new Editor(createTestTUI(), defaultEditorTheme);
- editor.setText("test");
- editor.handleInput("\x17"); // Ctrl+W - deletes "test"
- editor.setText("other");
- // Type something to break the yank chain
- editor.handleInput("x");
- assert.strictEqual(editor.getText(), "otherx");
- // Alt+Y should do nothing
- editor.handleInput("\x1by"); // Alt+Y
- assert.strictEqual(editor.getText(), "otherx");
- });
- it("Alt+Y does nothing if kill ring has ≤1 entry", () => {
- const editor = new Editor(createTestTUI(), defaultEditorTheme);
- editor.setText("only");
- editor.handleInput("\x17"); // Ctrl+W - deletes "only"
- editor.handleInput("\x19"); // Ctrl+Y - yanks "only"
- assert.strictEqual(editor.getText(), "only");
- editor.handleInput("\x1by"); // Alt+Y - should do nothing (only 1 entry)
- assert.strictEqual(editor.getText(), "only");
- });
- it("consecutive Ctrl+W accumulates into one kill ring entry", () => {
- const editor = new Editor(createTestTUI(), defaultEditorTheme);
- editor.setText("one two three");
- editor.handleInput("\x17"); // Ctrl+W - deletes "three"
- editor.handleInput("\x17"); // Ctrl+W - deletes "two " (prepended)
- editor.handleInput("\x17"); // Ctrl+W - deletes "one " (prepended)
- assert.strictEqual(editor.getText(), "");
- // Should be one combined entry
- editor.handleInput("\x19"); // Ctrl+Y
- assert.strictEqual(editor.getText(), "one two three");
- });
- it("Ctrl+U accumulates multiline deletes including newlines", () => {
- const editor = new Editor(createTestTUI(), defaultEditorTheme);
- // Start with multiline text, cursor at end
- editor.setText("line1\nline2\nline3");
- // Cursor is at end of line3 (line 2, col 5)
- // Delete "line3"
- editor.handleInput("\x15"); // Ctrl+U
- assert.strictEqual(editor.getText(), "line1\nline2\n");
- // Delete newline (at start of empty line 2, merges with line1)
- editor.handleInput("\x15"); // Ctrl+U
- assert.strictEqual(editor.getText(), "line1\nline2");
- // Delete "line2"
- editor.handleInput("\x15"); // Ctrl+U
- assert.strictEqual(editor.getText(), "line1\n");
- // Delete newline
- editor.handleInput("\x15"); // Ctrl+U
- assert.strictEqual(editor.getText(), "line1");
- // Delete "line1"
- editor.handleInput("\x15"); // Ctrl+U
- assert.strictEqual(editor.getText(), "");
- // All deletions accumulated into one entry: "line1\nline2\nline3"
- editor.handleInput("\x19"); // Ctrl+Y
- assert.strictEqual(editor.getText(), "line1\nline2\nline3");
- });
- it("backward deletions prepend, forward deletions append during accumulation", () => {
- const editor = new Editor(createTestTUI(), defaultEditorTheme);
- editor.setText("prefix|suffix");
- // Position cursor at |
- editor.handleInput("\x01"); // Ctrl+A
- for (let i = 0; i < 6; i++) editor.handleInput("\x1b[C"); // Move right 6 times
- editor.handleInput("\x0b"); // Ctrl+K - deletes "suffix" (forward)
- editor.handleInput("\x0b"); // Ctrl+K - deletes "|" (forward, appended)
- assert.strictEqual(editor.getText(), "prefix");
- editor.handleInput("\x19"); // Ctrl+Y
- assert.strictEqual(editor.getText(), "prefix|suffix");
- });
- it("non-delete actions break kill accumulation", () => {
- const editor = new Editor(createTestTUI(), defaultEditorTheme);
- // Delete "baz", then type "x" to break accumulation, then delete "x"
- editor.setText("foo bar baz");
- editor.handleInput("\x17"); // Ctrl+W - deletes "baz"
- assert.strictEqual(editor.getText(), "foo bar ");
- editor.handleInput("x"); // Typing breaks accumulation
- assert.strictEqual(editor.getText(), "foo bar x");
- editor.handleInput("\x17"); // Ctrl+W - deletes "x" (separate entry, not accumulated)
- assert.strictEqual(editor.getText(), "foo bar ");
- // Yank most recent - should be "x", not "xbaz"
- editor.handleInput("\x19"); // Ctrl+Y
- assert.strictEqual(editor.getText(), "foo bar x");
- // Cycle to previous - should be "baz" (separate entry)
- editor.handleInput("\x1by"); // Alt+Y
- assert.strictEqual(editor.getText(), "foo bar baz");
- });
- it("non-yank actions break Alt+Y chain", () => {
- const editor = new Editor(createTestTUI(), defaultEditorTheme);
- editor.setText("first");
- editor.handleInput("\x17"); // Ctrl+W
- editor.setText("second");
- editor.handleInput("\x17"); // Ctrl+W
- editor.setText("");
- editor.handleInput("\x19"); // Ctrl+Y - yanks "second"
- assert.strictEqual(editor.getText(), "second");
- editor.handleInput("x"); // Type breaks yank chain
- assert.strictEqual(editor.getText(), "secondx");
- editor.handleInput("\x1by"); // Alt+Y - should do nothing
- assert.strictEqual(editor.getText(), "secondx");
- });
- it("kill ring rotation persists after cycling", () => {
- const editor = new Editor(createTestTUI(), defaultEditorTheme);
- editor.setText("first");
- editor.handleInput("\x17"); // deletes "first"
- editor.setText("second");
- editor.handleInput("\x17"); // deletes "second"
- editor.setText("third");
- editor.handleInput("\x17"); // deletes "third"
- editor.setText("");
- // Ring: [first, second, third]
- editor.handleInput("\x19"); // Ctrl+Y - yanks "third"
- editor.handleInput("\x1by"); // Alt+Y - cycles to "second", ring rotates
- // Now ring is: [third, first, second]
- assert.strictEqual(editor.getText(), "second");
- // Do something else
- editor.handleInput("x");
- editor.setText("");
- // New yank should get "second" (now at end after rotation)
- editor.handleInput("\x19"); // Ctrl+Y
- assert.strictEqual(editor.getText(), "second");
- });
- it("consecutive deletions across lines coalesce into one entry", () => {
- const editor = new Editor(createTestTUI(), defaultEditorTheme);
- // "1\n2\n3" with cursor at end, delete everything with Ctrl+W
- editor.setText("1\n2\n3");
- editor.handleInput("\x17"); // Ctrl+W - deletes "3"
- assert.strictEqual(editor.getText(), "1\n2\n");
- editor.handleInput("\x17"); // Ctrl+W - deletes newline (merge with prev line)
- assert.strictEqual(editor.getText(), "1\n2");
- editor.handleInput("\x17"); // Ctrl+W - deletes "2"
- assert.strictEqual(editor.getText(), "1\n");
- editor.handleInput("\x17"); // Ctrl+W - deletes newline
- assert.strictEqual(editor.getText(), "1");
- editor.handleInput("\x17"); // Ctrl+W - deletes "1"
- assert.strictEqual(editor.getText(), "");
- // All deletions should have accumulated into one entry
- editor.handleInput("\x19"); // Ctrl+Y
- assert.strictEqual(editor.getText(), "1\n2\n3");
- });
- it("Ctrl+K at line end deletes newline and coalesces", () => {
- const editor = new Editor(createTestTUI(), defaultEditorTheme);
- // "ab" on line 1, "cd" on line 2, cursor at end of line 1
- editor.setText("");
- editor.handleInput("a");
- editor.handleInput("b");
- editor.handleInput("\n");
- editor.handleInput("c");
- editor.handleInput("d");
- // Move to end of first line
- editor.handleInput("\x1b[A"); // Up arrow
- editor.handleInput("\x05"); // Ctrl+E - end of line
- // Now at end of "ab", Ctrl+K should delete newline (merge with "cd")
- editor.handleInput("\x0b"); // Ctrl+K - deletes newline
- assert.strictEqual(editor.getText(), "abcd");
- // Continue deleting
- editor.handleInput("\x0b"); // Ctrl+K - deletes "cd"
- assert.strictEqual(editor.getText(), "ab");
- // Both deletions should accumulate
- editor.handleInput("\x19"); // Ctrl+Y
- assert.strictEqual(editor.getText(), "ab\ncd");
- });
- it("handles yank in middle of text", () => {
- const editor = new Editor(createTestTUI(), defaultEditorTheme);
- editor.setText("word");
- editor.handleInput("\x17"); // Ctrl+W - deletes "word"
- editor.setText("hello world");
- // Move to middle (after "hello ")
- editor.handleInput("\x01"); // Ctrl+A
- for (let i = 0; i < 6; i++) editor.handleInput("\x1b[C");
- editor.handleInput("\x19"); // Ctrl+Y
- assert.strictEqual(editor.getText(), "hello wordworld");
- });
- it("handles yank-pop in middle of text", () => {
- const editor = new Editor(createTestTUI(), defaultEditorTheme);
- // Create two kill ring entries
- editor.setText("FIRST");
- editor.handleInput("\x17"); // Ctrl+W - deletes "FIRST"
- editor.setText("SECOND");
- editor.handleInput("\x17"); // Ctrl+W - deletes "SECOND"
- // Ring: ["FIRST", "SECOND"]
- // Set up "hello world" and position cursor after "hello "
- editor.setText("hello world");
- editor.handleInput("\x01"); // Ctrl+A - go to start of line
- for (let i = 0; i < 6; i++) editor.handleInput("\x1b[C"); // Move right 6
- // Yank "SECOND" in the middle
- editor.handleInput("\x19"); // Ctrl+Y
- assert.strictEqual(editor.getText(), "hello SECONDworld");
- // Yank-pop replaces "SECOND" with "FIRST"
- editor.handleInput("\x1by"); // Alt+Y
- assert.strictEqual(editor.getText(), "hello FIRSTworld");
- });
- it("multiline yank and yank-pop in middle of text", () => {
- const editor = new Editor(createTestTUI(), defaultEditorTheme);
- // Create single-line entry
- editor.setText("SINGLE");
- editor.handleInput("\x17"); // Ctrl+W - deletes "SINGLE"
- // Create multiline entry via consecutive Ctrl+U
- editor.setText("A\nB");
- editor.handleInput("\x15"); // Ctrl+U - deletes "B"
- editor.handleInput("\x15"); // Ctrl+U - deletes newline
- editor.handleInput("\x15"); // Ctrl+U - deletes "A"
- // Ring: ["SINGLE", "A\nB"]
- // Insert in middle of "hello world"
- editor.setText("hello world");
- editor.handleInput("\x01"); // Ctrl+A
- for (let i = 0; i < 6; i++) editor.handleInput("\x1b[C");
- // Yank multiline "A\nB"
- editor.handleInput("\x19"); // Ctrl+Y
- assert.strictEqual(editor.getText(), "hello A\nBworld");
- // Yank-pop replaces with "SINGLE"
- editor.handleInput("\x1by"); // Alt+Y
- assert.strictEqual(editor.getText(), "hello SINGLEworld");
- });
- it("Alt+D deletes word forward and saves to kill ring", () => {
- const editor = new Editor(createTestTUI(), defaultEditorTheme);
- editor.setText("hello world test");
- editor.handleInput("\x01"); // Ctrl+A - go to start
- editor.handleInput("\x1bd"); // Alt+D - deletes "hello"
- assert.strictEqual(editor.getText(), " world test");
- editor.handleInput("\x1bd"); // Alt+D - deletes " world" (skips whitespace, then word)
- assert.strictEqual(editor.getText(), " test");
- // Yank should get accumulated text
- editor.handleInput("\x19"); // Ctrl+Y
- assert.strictEqual(editor.getText(), "hello world test");
- });
- it("Alt+D at end of line deletes newline", () => {
- const editor = new Editor(createTestTUI(), defaultEditorTheme);
- editor.setText("line1\nline2");
- // Move to start of document, then to end of first line
- editor.handleInput("\x1b[A"); // Up arrow - go to first line
- editor.handleInput("\x05"); // Ctrl+E - end of line
- editor.handleInput("\x1bd"); // Alt+D - deletes newline (merges lines)
- assert.strictEqual(editor.getText(), "line1line2");
- editor.handleInput("\x19"); // Ctrl+Y
- assert.strictEqual(editor.getText(), "line1\nline2");
- });
- });
- describe("Undo", () => {
- it("does nothing when undo stack is empty", () => {
- const editor = new Editor(createTestTUI(), defaultEditorTheme);
- editor.handleInput("\x1b[45;5u"); // Ctrl+- (undo)
- assert.strictEqual(editor.getText(), "");
- });
- it("coalesces consecutive word characters into one undo unit", () => {
- const editor = new Editor(createTestTUI(), defaultEditorTheme);
- editor.handleInput("h");
- editor.handleInput("e");
- editor.handleInput("l");
- editor.handleInput("l");
- editor.handleInput("o");
- editor.handleInput(" ");
- editor.handleInput("w");
- editor.handleInput("o");
- editor.handleInput("r");
- editor.handleInput("l");
- editor.handleInput("d");
- assert.strictEqual(editor.getText(), "hello world");
- // Undo removes " world" (space captured state before it, so we restore to "hello")
- editor.handleInput("\x1b[45;5u"); // Ctrl+- (undo)
- assert.strictEqual(editor.getText(), "hello");
- // Undo removes "hello"
- editor.handleInput("\x1b[45;5u"); // Ctrl+- (undo)
- assert.strictEqual(editor.getText(), "");
- });
- it("undoes spaces one at a time", () => {
- const editor = new Editor(createTestTUI(), defaultEditorTheme);
- editor.handleInput("h");
- editor.handleInput("e");
- editor.handleInput("l");
- editor.handleInput("l");
- editor.handleInput("o");
- editor.handleInput(" ");
- editor.handleInput(" ");
- assert.strictEqual(editor.getText(), "hello ");
- editor.handleInput("\x1b[45;5u"); // Ctrl+- (undo) - removes second " "
- assert.strictEqual(editor.getText(), "hello ");
- editor.handleInput("\x1b[45;5u"); // Ctrl+- (undo) - removes first " "
- assert.strictEqual(editor.getText(), "hello");
- editor.handleInput("\x1b[45;5u"); // Ctrl+- (undo) - removes "hello"
- assert.strictEqual(editor.getText(), "");
- });
- it("undoes newlines and signals next word to capture state", () => {
- const editor = new Editor(createTestTUI(), defaultEditorTheme);
- editor.handleInput("h");
- editor.handleInput("e");
- editor.handleInput("l");
- editor.handleInput("l");
- editor.handleInput("o");
- editor.handleInput("\n");
- editor.handleInput("w");
- editor.handleInput("o");
- editor.handleInput("r");
- editor.handleInput("l");
- editor.handleInput("d");
- assert.strictEqual(editor.getText(), "hello\nworld");
- editor.handleInput("\x1b[45;5u"); // Ctrl+- (undo)
- assert.strictEqual(editor.getText(), "hello\n");
- editor.handleInput("\x1b[45;5u"); // Ctrl+- (undo)
- assert.strictEqual(editor.getText(), "hello");
- editor.handleInput("\x1b[45;5u"); // Ctrl+- (undo)
- assert.strictEqual(editor.getText(), "");
- });
- it("undoes backspace", () => {
- const editor = new Editor(createTestTUI(), defaultEditorTheme);
- editor.handleInput("h");
- editor.handleInput("e");
- editor.handleInput("l");
- editor.handleInput("l");
- editor.handleInput("o");
- editor.handleInput("\x7f"); // Backspace
- assert.strictEqual(editor.getText(), "hell");
- editor.handleInput("\x1b[45;5u"); // Ctrl+- (undo)
- assert.strictEqual(editor.getText(), "hello");
- });
- it("undoes forward delete", () => {
- const editor = new Editor(createTestTUI(), defaultEditorTheme);
- editor.handleInput("h");
- editor.handleInput("e");
- editor.handleInput("l");
- editor.handleInput("l");
- editor.handleInput("o");
- editor.handleInput("\x01"); // Ctrl+A - go to start
- editor.handleInput("\x1b[C"); // Right arrow
- editor.handleInput("\x1b[3~"); // Delete key
- assert.strictEqual(editor.getText(), "hllo");
- editor.handleInput("\x1b[45;5u"); // Ctrl+- (undo)
- assert.strictEqual(editor.getText(), "hello");
- });
- it("undoes Ctrl+W (delete word backward)", () => {
- const editor = new Editor(createTestTUI(), defaultEditorTheme);
- editor.handleInput("h");
- editor.handleInput("e");
- editor.handleInput("l");
- editor.handleInput("l");
- editor.handleInput("o");
- editor.handleInput(" ");
- editor.handleInput("w");
- editor.handleInput("o");
- editor.handleInput("r");
- editor.handleInput("l");
- editor.handleInput("d");
- assert.strictEqual(editor.getText(), "hello world");
- editor.handleInput("\x17"); // Ctrl+W
- assert.strictEqual(editor.getText(), "hello ");
- editor.handleInput("\x1b[45;5u"); // Ctrl+- (undo)
- assert.strictEqual(editor.getText(), "hello world");
- });
- it("undoes Ctrl+K (delete to line end)", () => {
- const editor = new Editor(createTestTUI(), defaultEditorTheme);
- editor.handleInput("h");
- editor.handleInput("e");
- editor.handleInput("l");
- editor.handleInput("l");
- editor.handleInput("o");
- editor.handleInput(" ");
- editor.handleInput("w");
- editor.handleInput("o");
- editor.handleInput("r");
- editor.handleInput("l");
- editor.handleInput("d");
- editor.handleInput("\x01"); // Ctrl+A - go to start
- for (let i = 0; i < 6; i++) editor.handleInput("\x1b[C"); // Move right 6 times
- editor.handleInput("\x0b"); // Ctrl+K
- assert.strictEqual(editor.getText(), "hello ");
- editor.handleInput("\x1b[45;5u"); // Ctrl+- (undo)
- assert.strictEqual(editor.getText(), "hello world");
- editor.handleInput("|");
- assert.strictEqual(editor.getText(), "hello |world");
- });
- it("undoes Ctrl+U (delete to line start)", () => {
- const editor = new Editor(createTestTUI(), defaultEditorTheme);
- editor.handleInput("h");
- editor.handleInput("e");
- editor.handleInput("l");
- editor.handleInput("l");
- editor.handleInput("o");
- editor.handleInput(" ");
- editor.handleInput("w");
- editor.handleInput("o");
- editor.handleInput("r");
- editor.handleInput("l");
- editor.handleInput("d");
- editor.handleInput("\x01"); // Ctrl+A - go to start
- for (let i = 0; i < 6; i++) editor.handleInput("\x1b[C"); // Move right 6 times
- editor.handleInput("\x15"); // Ctrl+U
- assert.strictEqual(editor.getText(), "world");
- editor.handleInput("\x1b[45;5u"); // Ctrl+- (undo)
- assert.strictEqual(editor.getText(), "hello world");
- });
- it("undoes yank", () => {
- const editor = new Editor(createTestTUI(), defaultEditorTheme);
- editor.handleInput("h");
- editor.handleInput("e");
- editor.handleInput("l");
- editor.handleInput("l");
- editor.handleInput("o");
- editor.handleInput(" ");
- editor.handleInput("\x17"); // Ctrl+W - delete "hello "
- editor.handleInput("\x19"); // Ctrl+Y - yank
- assert.strictEqual(editor.getText(), "hello ");
- editor.handleInput("\x1b[45;5u"); // Ctrl+- (undo)
- assert.strictEqual(editor.getText(), "");
- });
- it("undoes single-line paste atomically", () => {
- const editor = new Editor(createTestTUI(), defaultEditorTheme);
- editor.setText("hello world");
- editor.handleInput("\x01"); // Ctrl+A - go to start
- for (let i = 0; i < 5; i++) editor.handleInput("\x1b[C"); // Move right 5 (after "hello", before space)
- // Simulate bracketed paste of "beep boop"
- editor.handleInput("\x1b[200~beep boop\x1b[201~");
- assert.strictEqual(editor.getText(), "hellobeep boop world");
- // Single undo should restore entire pre-paste state
- editor.handleInput("\x1b[45;5u"); // Ctrl+- (undo)
- assert.strictEqual(editor.getText(), "hello world");
- editor.handleInput("|");
- assert.strictEqual(editor.getText(), "hello| world");
- });
- it("does not trigger autocomplete during single-line paste", () => {
- const editor = new Editor(createTestTUI(), defaultEditorTheme);
- let suggestionCalls = 0;
- const mockProvider: AutocompleteProvider = {
- getSuggestions: async () => {
- suggestionCalls += 1;
- return null;
- },
- applyCompletion,
- };
- editor.setAutocompleteProvider(mockProvider);
- editor.handleInput("\x1b[200~look at @node_modules/react/index.js please\x1b[201~");
- assert.strictEqual(editor.getText(), "look at @node_modules/react/index.js please");
- assert.strictEqual(suggestionCalls, 0);
- assert.strictEqual(editor.isShowingAutocomplete(), false);
- });
- it("decodes CSI-u Ctrl+letter sequences inside bracketed paste (tmux popup)", () => {
- const editor = new Editor(createTestTUI(), defaultEditorTheme);
- // tmux popups with extended-keys-format=csi-u re-encode \n in pastes as
- // \x1b[106;5u (Ctrl+J). Without decoding, the per-char filter strips ESC
- // and leaks "[106;5u" between lines. See issue #3599.
- editor.handleInput("\x1b[200~line1\x1b[106;5uline2\x1b[106;5uline3\x1b[201~");
- assert.strictEqual(editor.getText(), "line1\nline2\nline3");
- });
- it("undoes multi-line paste atomically", () => {
- const editor = new Editor(createTestTUI(), defaultEditorTheme);
- editor.setText("hello world");
- editor.handleInput("\x01"); // Ctrl+A - go to start
- for (let i = 0; i < 5; i++) editor.handleInput("\x1b[C"); // Move right 5 (after "hello", before space)
- // Simulate bracketed paste of multi-line text
- editor.handleInput("\x1b[200~line1\nline2\nline3\x1b[201~");
- assert.strictEqual(editor.getText(), "helloline1\nline2\nline3 world");
- // Single undo should restore entire pre-paste state
- editor.handleInput("\x1b[45;5u"); // Ctrl+- (undo)
- assert.strictEqual(editor.getText(), "hello world");
- editor.handleInput("|");
- assert.strictEqual(editor.getText(), "hello| world");
- });
- it("undoes insertTextAtCursor atomically", () => {
- const editor = new Editor(createTestTUI(), defaultEditorTheme);
- editor.setText("hello world");
- editor.handleInput("\x01"); // Ctrl+A - go to start
- for (let i = 0; i < 5; i++) editor.handleInput("\x1b[C"); // Move right 5 (after "hello", before space)
- // Programmatic insertion (e.g., clipboard image path)
- editor.insertTextAtCursor("/tmp/image.png");
- assert.strictEqual(editor.getText(), "hello/tmp/image.png world");
- // Single undo should restore entire pre-insert state
- editor.handleInput("\x1b[45;5u"); // Ctrl+- (undo)
- assert.strictEqual(editor.getText(), "hello world");
- editor.handleInput("|");
- assert.strictEqual(editor.getText(), "hello| world");
- });
- it("insertTextAtCursor handles multiline text", () => {
- const editor = new Editor(createTestTUI(), defaultEditorTheme);
- editor.setText("hello world");
- editor.handleInput("\x01"); // Ctrl+A - go to start
- for (let i = 0; i < 5; i++) editor.handleInput("\x1b[C"); // Move right 5 (after "hello", before space)
- // Insert multiline text
- editor.insertTextAtCursor("line1\nline2\nline3");
- assert.strictEqual(editor.getText(), "helloline1\nline2\nline3 world");
- // Cursor should be at end of inserted text (after "line3", before " world")
- const cursor = editor.getCursor();
- assert.strictEqual(cursor.line, 2);
- assert.strictEqual(cursor.col, 5); // "line3".length
- // Single undo should restore entire pre-insert state
- editor.handleInput("\x1b[45;5u"); // Ctrl+- (undo)
- assert.strictEqual(editor.getText(), "hello world");
- });
- it("insertTextAtCursor normalizes CRLF and CR line endings", () => {
- const editor = new Editor(createTestTUI(), defaultEditorTheme);
- editor.setText("");
- // Insert text with CRLF
- editor.insertTextAtCursor("a\r\nb\r\nc");
- assert.strictEqual(editor.getText(), "a\nb\nc");
- editor.handleInput("\x1b[45;5u"); // Undo
- assert.strictEqual(editor.getText(), "");
- // Insert text with CR only
- editor.insertTextAtCursor("x\ry\rz");
- assert.strictEqual(editor.getText(), "x\ny\nz");
- });
- it("undoes setText to empty string", () => {
- const editor = new Editor(createTestTUI(), defaultEditorTheme);
- editor.handleInput("h");
- editor.handleInput("e");
- editor.handleInput("l");
- editor.handleInput("l");
- editor.handleInput("o");
- editor.handleInput(" ");
- editor.handleInput("w");
- editor.handleInput("o");
- editor.handleInput("r");
- editor.handleInput("l");
- editor.handleInput("d");
- assert.strictEqual(editor.getText(), "hello world");
- editor.setText("");
- assert.strictEqual(editor.getText(), "");
- editor.handleInput("\x1b[45;5u"); // Ctrl+- (undo)
- assert.strictEqual(editor.getText(), "hello world");
- });
- it("clears undo stack on submit", () => {
- const editor = new Editor(createTestTUI(), defaultEditorTheme);
- let submitted = "";
- editor.onSubmit = (text) => {
- submitted = text;
- };
- editor.handleInput("h");
- editor.handleInput("e");
- editor.handleInput("l");
- editor.handleInput("l");
- editor.handleInput("o");
- editor.handleInput("\r"); // Enter - submit
- assert.strictEqual(submitted, "hello");
- assert.strictEqual(editor.getText(), "");
- // Undo should do nothing - stack was cleared
- editor.handleInput("\x1b[45;5u"); // Ctrl+- (undo)
- assert.strictEqual(editor.getText(), "");
- });
- it("exits history browsing mode on undo", () => {
- const editor = new Editor(createTestTUI(), defaultEditorTheme);
- // Add "hello" to history
- editor.addToHistory("hello");
- assert.strictEqual(editor.getText(), "");
- // Type "world"
- editor.handleInput("w");
- editor.handleInput("o");
- editor.handleInput("r");
- editor.handleInput("l");
- editor.handleInput("d");
- assert.strictEqual(editor.getText(), "world");
- // Ctrl+W - delete word
- editor.handleInput("\x17"); // Ctrl+W
- assert.strictEqual(editor.getText(), "");
- // Press Up - enter history browsing, shows "hello"
- editor.handleInput("\x1b[A"); // Up arrow
- assert.strictEqual(editor.getText(), "hello");
- // Undo should restore to "" (state before entering history browsing)
- editor.handleInput("\x1b[45;5u"); // Ctrl+- (undo)
- assert.strictEqual(editor.getText(), "");
- // Undo again should restore to "world" (state before Ctrl+W)
- editor.handleInput("\x1b[45;5u"); // Ctrl+- (undo)
- assert.strictEqual(editor.getText(), "world");
- });
- it("undo restores to pre-history state even after multiple history navigations", () => {
- const editor = new Editor(createTestTUI(), defaultEditorTheme);
- // Add history entries
- editor.addToHistory("first");
- editor.addToHistory("second");
- editor.addToHistory("third");
- // Type something
- editor.handleInput("c");
- editor.handleInput("u");
- editor.handleInput("r");
- editor.handleInput("r");
- editor.handleInput("e");
- editor.handleInput("n");
- editor.handleInput("t");
- assert.strictEqual(editor.getText(), "current");
- // Clear editor
- editor.handleInput("\x17"); // Ctrl+W
- assert.strictEqual(editor.getText(), "");
- // Navigate through history multiple times
- editor.handleInput("\x1b[A"); // Up - "third"
- assert.strictEqual(editor.getText(), "third");
- editor.handleInput("\x1b[A"); // Up - "second"
- assert.strictEqual(editor.getText(), "second");
- editor.handleInput("\x1b[A"); // Up - "first"
- assert.strictEqual(editor.getText(), "first");
- // Undo should go back to "" (state before we started browsing), not intermediate states
- editor.handleInput("\x1b[45;5u"); // Ctrl+- (undo)
- assert.strictEqual(editor.getText(), "");
- // Another undo goes back to "current"
- editor.handleInput("\x1b[45;5u"); // Ctrl+- (undo)
- assert.strictEqual(editor.getText(), "current");
- });
- it("cursor movement starts new undo unit", () => {
- const editor = new Editor(createTestTUI(), defaultEditorTheme);
- editor.handleInput("h");
- editor.handleInput("e");
- editor.handleInput("l");
- editor.handleInput("l");
- editor.handleInput("o");
- editor.handleInput(" ");
- editor.handleInput("w");
- editor.handleInput("o");
- editor.handleInput("r");
- editor.handleInput("l");
- editor.handleInput("d");
- assert.strictEqual(editor.getText(), "hello world");
- // Move cursor left 5 (to after "hello ")
- for (let i = 0; i < 5; i++) editor.handleInput("\x1b[D");
- // Type "lol" in the middle
- editor.handleInput("l");
- editor.handleInput("o");
- editor.handleInput("l");
- assert.strictEqual(editor.getText(), "hello lolworld");
- // Undo should restore to "hello world" (before inserting "lol")
- editor.handleInput("\x1b[45;5u"); // Ctrl+- (undo)
- assert.strictEqual(editor.getText(), "hello world");
- editor.handleInput("|");
- assert.strictEqual(editor.getText(), "hello |world");
- });
- it("no-op delete operations do not push undo snapshots", () => {
- const editor = new Editor(createTestTUI(), defaultEditorTheme);
- editor.handleInput("h");
- editor.handleInput("e");
- editor.handleInput("l");
- editor.handleInput("l");
- editor.handleInput("o");
- assert.strictEqual(editor.getText(), "hello");
- // Delete word on empty - multiple times (should be no-ops)
- editor.handleInput("\x17"); // Ctrl+W - deletes "hello"
- assert.strictEqual(editor.getText(), "");
- editor.handleInput("\x17"); // Ctrl+W - no-op (nothing to delete)
- editor.handleInput("\x17"); // Ctrl+W - no-op
- // Single undo should restore "hello"
- editor.handleInput("\x1b[45;5u"); // Ctrl+- (undo)
- assert.strictEqual(editor.getText(), "hello");
- });
- it("undoes autocomplete", async () => {
- const editor = new Editor(createTestTUI(), defaultEditorTheme);
- // Create a mock autocomplete provider
- const mockProvider: AutocompleteProvider = {
- getSuggestions: async (lines, _cursorLine, cursorCol) => {
- const text = lines[0] || "";
- const prefix = text.slice(0, cursorCol);
- if (prefix === "di") {
- return {
- items: [{ value: "dist/", label: "dist/" }],
- prefix: "di",
- };
- }
- return null;
- },
- applyCompletion,
- };
- editor.setAutocompleteProvider(mockProvider);
- // Type "di"
- editor.handleInput("d");
- editor.handleInput("i");
- assert.strictEqual(editor.getText(), "di");
- // Press Tab to trigger autocomplete
- editor.handleInput("\t");
- await flushAutocomplete();
- assert.strictEqual(editor.getText(), "dist/");
- assert.strictEqual(editor.isShowingAutocomplete(), false);
- // Undo should restore to "di"
- editor.handleInput("\x1b[45;5u"); // Ctrl+- (undo)
- assert.strictEqual(editor.getText(), "di");
- });
- });
- describe("Autocomplete", () => {
- it("auto-applies single force-file suggestion without showing menu", async () => {
- const editor = new Editor(createTestTUI(), defaultEditorTheme);
- const mockProvider: AutocompleteProvider = {
- getSuggestions: async (lines, _cursorLine, cursorCol, options) => {
- if (!options.force) {
- return null;
- }
- const text = lines[0] || "";
- const prefix = text.slice(0, cursorCol);
- if (prefix === "Work") {
- return {
- items: [{ value: "Workspace/", label: "Workspace/" }],
- prefix: "Work",
- };
- }
- return null;
- },
- applyCompletion,
- };
- editor.setAutocompleteProvider(mockProvider);
- // Type "Work"
- editor.handleInput("W");
- editor.handleInput("o");
- editor.handleInput("r");
- editor.handleInput("k");
- assert.strictEqual(editor.getText(), "Work");
- // Press Tab - should auto-apply without showing menu
- editor.handleInput("\t");
- await flushAutocomplete();
- assert.strictEqual(editor.getText(), "Workspace/");
- assert.strictEqual(editor.isShowingAutocomplete(), false);
- // Undo should restore to "Work"
- editor.handleInput("\x1b[45;5u"); // Ctrl+- (undo)
- assert.strictEqual(editor.getText(), "Work");
- });
- it("shows menu when force-file has multiple suggestions", async () => {
- const editor = new Editor(createTestTUI(), defaultEditorTheme);
- const mockProvider: AutocompleteProvider = {
- getSuggestions: async (lines, _cursorLine, cursorCol, options) => {
- if (!options.force) {
- return null;
- }
- const text = lines[0] || "";
- const prefix = text.slice(0, cursorCol);
- if (prefix === "src") {
- return {
- items: [
- { value: "src/", label: "src/" },
- { value: "src.txt", label: "src.txt" },
- ],
- prefix: "src",
- };
- }
- return null;
- },
- applyCompletion,
- };
- editor.setAutocompleteProvider(mockProvider);
- // Type "src"
- editor.handleInput("s");
- editor.handleInput("r");
- editor.handleInput("c");
- assert.strictEqual(editor.getText(), "src");
- // Press Tab - should show menu because there are multiple suggestions
- editor.handleInput("\t");
- await flushAutocomplete();
- assert.strictEqual(editor.getText(), "src");
- assert.strictEqual(editor.isShowingAutocomplete(), true);
- // Press Tab again to accept first suggestion
- editor.handleInput("\t");
- assert.strictEqual(editor.getText(), "src/");
- assert.strictEqual(editor.isShowingAutocomplete(), false);
- });
- it("keeps suggestions open when typing in force mode (Tab-triggered)", async () => {
- const editor = new Editor(createTestTUI(), defaultEditorTheme);
- const allFiles = [
- { value: "readme.md", label: "readme.md" },
- { value: "package.json", label: "package.json" },
- { value: "src/", label: "src/" },
- { value: "dist/", label: "dist/" },
- ];
- const mockProvider: AutocompleteProvider = {
- getSuggestions: async (lines, _cursorLine, cursorCol, options) => {
- const text = lines[0] || "";
- const prefix = text.slice(0, cursorCol);
- const shouldMatch = options.force || prefix.includes("/") || prefix.startsWith(".");
- if (!shouldMatch) {
- return null;
- }
- const filtered = allFiles.filter((f) => f.value.toLowerCase().startsWith(prefix.toLowerCase()));
- if (filtered.length > 0) {
- return { items: filtered, prefix };
- }
- return null;
- },
- applyCompletion,
- };
- editor.setAutocompleteProvider(mockProvider);
- // Press Tab on empty prompt - should show all files (force mode)
- editor.handleInput("\t");
- await flushAutocomplete();
- assert.strictEqual(editor.isShowingAutocomplete(), true);
- // Type "r" - should narrow to "readme.md" (force mode keeps suggestions open)
- editor.handleInput("r");
- await flushAutocomplete();
- assert.strictEqual(editor.getText(), "r");
- assert.strictEqual(editor.isShowingAutocomplete(), true);
- // Type "e" - should still show "readme.md"
- editor.handleInput("e");
- await flushAutocomplete();
- assert.strictEqual(editor.getText(), "re");
- assert.strictEqual(editor.isShowingAutocomplete(), true);
- // Accept with Tab
- editor.handleInput("\t");
- assert.strictEqual(editor.getText(), "readme.md");
- assert.strictEqual(editor.isShowingAutocomplete(), false);
- });
- it("debounces @ autocomplete while typing", async () => {
- const editor = new Editor(createTestTUI(), defaultEditorTheme);
- let suggestionCalls = 0;
- const mockProvider: AutocompleteProvider = {
- getSuggestions: async (lines, _cursorLine, cursorCol) => {
- suggestionCalls += 1;
- const text = (lines[0] || "").slice(0, cursorCol);
- return {
- items: [{ value: "@main.ts", label: "main.ts" }],
- prefix: text,
- };
- },
- applyCompletion,
- };
- editor.setAutocompleteProvider(mockProvider);
- editor.handleInput("@");
- editor.handleInput("m");
- editor.handleInput("a");
- editor.handleInput("i");
- assert.strictEqual(suggestionCalls, 0);
- assert.strictEqual(editor.isShowingAutocomplete(), false);
- await new Promise((resolve) => setTimeout(resolve, 50));
- await flushAutocomplete();
- assert.strictEqual(suggestionCalls, 1);
- assert.strictEqual(editor.isShowingAutocomplete(), true);
- });
- it("re-queries the autocomplete picker when the cursor moves back into the command name", async () => {
- // Regression for earendil-works/pi#5496: arrowing left out of a slash
- // command's argument region must re-query the picker, not leave the
- // stale argument list showing. Before the fix, moveCursor() never
- // called updateAutocomplete(), so `/cmd ` (argument menu) + Left kept
- // displaying the arguments against a `/cmd` prefix — and a Tab there
- // would concatenate the stale suggestion onto the partial command name.
- const editor = new Editor(createTestTUI(), defaultEditorTheme);
- const mockProvider: AutocompleteProvider = {
- getSuggestions: async (lines, _cursorLine, cursorCol) => {
- const before = (lines[0] || "").slice(0, cursorCol);
- if (!before.startsWith("/")) return null;
- // Past the command name (a space before the cursor): offer arguments.
- if (before.includes(" ")) {
- return {
- items: [
- { value: "repo", label: "repo" },
- { value: "message", label: "message" },
- { value: "help", label: "help" },
- ],
- prefix: before.slice(before.indexOf(" ") + 1),
- };
- }
- // Inside the command name: offer the command name only.
- return { items: [{ value: "cmd", label: "cmd" }], prefix: before };
- },
- applyCompletion,
- };
- editor.setAutocompleteProvider(mockProvider);
- // Type `/cmd ` so the picker ends up showing the argument list.
- for (const ch of "/cmd ") {
- editor.handleInput(ch);
- await flushAutocomplete();
- }
- assert.strictEqual(editor.getText(), "/cmd ");
- assert.strictEqual(editor.isShowingAutocomplete(), true);
- const atArg = editor
- .render(80)
- .map((l) => stripVTControlCharacters(l))
- .join("\n");
- assert.ok(atArg.includes("repo"), "argument menu should be visible at `/cmd `");
- // Arrow Left back into the command name (`/cmd`).
- editor.handleInput("\x1b[D");
- await flushAutocomplete();
- // The picker must have re-queried: the stale argument items are gone
- // (replaced by the command-name suggestion, or the picker closed).
- const afterMove = editor
- .render(80)
- .map((l) => stripVTControlCharacters(l))
- .join("\n");
- assert.ok(!afterMove.includes("repo"), "stale argument menu must not survive the cursor move");
- assert.ok(!afterMove.includes("message"), "stale argument menu must not survive the cursor move");
- });
- it("debounces # autocomplete while typing", async () => {
- const editor = new Editor(createTestTUI(), defaultEditorTheme);
- let suggestionCalls = 0;
- const mockProvider: AutocompleteProvider = {
- getSuggestions: async (lines, _cursorLine, cursorCol) => {
- suggestionCalls += 1;
- const text = (lines[0] || "").slice(0, cursorCol);
- return {
- items: [{ value: "#2983", label: "#2983" }],
- prefix: text,
- };
- },
- applyCompletion,
- };
- editor.setAutocompleteProvider(mockProvider);
- editor.handleInput("#");
- editor.handleInput("2");
- editor.handleInput("9");
- editor.handleInput("8");
- assert.strictEqual(suggestionCalls, 0);
- assert.strictEqual(editor.isShowingAutocomplete(), false);
- await new Promise((resolve) => setTimeout(resolve, 50));
- await flushAutocomplete();
- assert.strictEqual(suggestionCalls, 1);
- assert.strictEqual(editor.isShowingAutocomplete(), true);
- });
- it("debounces custom triggerCharacters autocomplete while typing", async () => {
- const editor = new Editor(createTestTUI(), defaultEditorTheme);
- let suggestionCalls = 0;
- editor.setAutocompleteProvider({
- triggerCharacters: ["$"],
- getSuggestions: async (lines, _cursorLine, cursorCol) => {
- suggestionCalls += 1;
- const prefix = (lines[0] || "").slice(0, cursorCol);
- return { items: [{ value: "$skill-name", label: "skill-name" }], prefix };
- },
- applyCompletion,
- });
- editor.handleInput("$");
- editor.handleInput("s");
- editor.handleInput("k");
- assert.strictEqual(suggestionCalls, 0);
- await new Promise((resolve) => setTimeout(resolve, 50));
- await flushAutocomplete();
- assert.strictEqual(suggestionCalls, 1);
- assert.strictEqual(editor.isShowingAutocomplete(), true);
- });
- it("resets custom triggerCharacters when provider changes", async () => {
- const editor = new Editor(createTestTUI(), defaultEditorTheme);
- let suggestionCalls = 0;
- editor.setAutocompleteProvider({
- triggerCharacters: ["$"],
- getSuggestions: async () => ({ items: [{ value: "$skill-name", label: "skill-name" }], prefix: "$" }),
- applyCompletion,
- });
- editor.setAutocompleteProvider({
- getSuggestions: async () => {
- suggestionCalls += 1;
- return { items: [{ value: "$skill-name", label: "skill-name" }], prefix: "$" };
- },
- applyCompletion,
- });
- editor.handleInput("$");
- editor.handleInput("s");
- await new Promise((resolve) => setTimeout(resolve, 50));
- await flushAutocomplete();
- assert.strictEqual(suggestionCalls, 0);
- assert.strictEqual(editor.isShowingAutocomplete(), false);
- });
- it("aborts active @ autocomplete when typing continues", async () => {
- const editor = new Editor(createTestTUI(), defaultEditorTheme);
- let aborts = 0;
- const mockProvider: AutocompleteProvider = {
- getSuggestions: async (_lines, _cursorLine, _cursorCol, options) => {
- return await new Promise((resolve) => {
- const timeout = setTimeout(() => {
- resolve({ items: [{ value: "@main.ts", label: "main.ts" }], prefix: "@main" });
- }, 500);
- options.signal.addEventListener(
- "abort",
- () => {
- aborts += 1;
- clearTimeout(timeout);
- resolve(null);
- },
- { once: true },
- );
- });
- },
- applyCompletion,
- };
- editor.setAutocompleteProvider(mockProvider);
- editor.handleInput("@");
- editor.handleInput("m");
- editor.handleInput("a");
- editor.handleInput("i");
- await new Promise((resolve) => setTimeout(resolve, 250));
- editor.handleInput("n");
- await new Promise((resolve) => setTimeout(resolve, 50));
- assert.strictEqual(aborts, 1);
- });
- it("hides autocomplete when backspacing slash command to empty", async () => {
- const editor = new Editor(createTestTUI(), defaultEditorTheme);
- // Mock provider with slash commands
- const mockProvider: AutocompleteProvider = {
- getSuggestions: async (lines, _cursorLine, cursorCol) => {
- const text = lines[0] || "";
- const prefix = text.slice(0, cursorCol);
- // Only return slash command suggestions when line starts with /
- if (prefix.startsWith("/")) {
- const commands = [
- { value: "/model", label: "model", description: "Change model" },
- { value: "/help", label: "help", description: "Show help" },
- ];
- const query = prefix.slice(1); // Remove leading /
- const filtered = commands.filter((c) => c.value.startsWith(query));
- if (filtered.length > 0) {
- return { items: filtered, prefix };
- }
- }
- return null;
- },
- applyCompletion,
- };
- editor.setAutocompleteProvider(mockProvider);
- // Type "/" - should show slash command suggestions
- editor.handleInput("/");
- await flushAutocomplete();
- assert.strictEqual(editor.getText(), "/");
- assert.strictEqual(editor.isShowingAutocomplete(), true);
- // Backspace to delete "/" - should hide autocomplete completely
- editor.handleInput("\x7f"); // Backspace
- await flushAutocomplete();
- assert.strictEqual(editor.getText(), "");
- assert.strictEqual(editor.isShowingAutocomplete(), false);
- });
- it("applies exact typed slash-argument value on Enter even when first item is highlighted", async () => {
- const editor = new Editor(createTestTUI(), defaultEditorTheme);
- // Mock provider for /argtest command with argument completions
- const mockProvider: AutocompleteProvider = {
- getSuggestions: async (lines, _cursorLine, cursorCol) => {
- const text = lines[0] || "";
- const beforeCursor = text.slice(0, cursorCol);
- // Check if we're in argument completion context: "/argtest <prefix>"
- const argtestMatch = beforeCursor.match(/^\/argtest\s+(\S+)$/);
- if (argtestMatch) {
- const argumentText = argtestMatch[1]!;
- const allArguments = [
- { value: "one", label: "one" },
- { value: "two", label: "two" },
- { value: "three", label: "three" },
- ];
- // Return all arguments that start with the typed prefix
- const filtered = allArguments.filter((arg) => arg.value.startsWith(argumentText));
- if (filtered.length > 0) {
- return { items: filtered, prefix: argumentText };
- }
- }
- return null;
- },
- applyCompletion,
- };
- editor.setAutocompleteProvider(mockProvider);
- // Type "/argtest two"
- editor.handleInput("/");
- editor.handleInput("a");
- editor.handleInput("r");
- editor.handleInput("g");
- editor.handleInput("t");
- editor.handleInput("e");
- editor.handleInput("s");
- editor.handleInput("t");
- editor.handleInput(" ");
- editor.handleInput("t");
- editor.handleInput("w");
- editor.handleInput("o");
- assert.strictEqual(editor.getText(), "/argtest two");
- await flushAutocomplete();
- assert.strictEqual(editor.isShowingAutocomplete(), true);
- // Press Enter - should apply the exact typed value "two", not the first item
- editor.handleInput("\r");
- // The exact typed value "two" should be retained
- assert.strictEqual(editor.getText(), "/argtest two");
- });
- it("selects first prefix match on Enter when typed arg is not exact match", async () => {
- const editor = new Editor(createTestTUI(), defaultEditorTheme);
- // Mock provider for /argtest command with argument completions
- const mockProvider: AutocompleteProvider = {
- getSuggestions: async (lines, _cursorLine, cursorCol) => {
- const text = lines[0] || "";
- const beforeCursor = text.slice(0, cursorCol);
- // Check if we're in argument completion context
- const argtestMatch = beforeCursor.match(/^\/argtest\s+(\S+)$/);
- if (argtestMatch) {
- const argumentText = argtestMatch[1]!;
- const allArguments = [
- { value: "two", label: "two" },
- { value: "three", label: "three" },
- { value: "twelve", label: "twelve" },
- ];
- // Return all items that start with the typed prefix
- const filtered = allArguments.filter((arg) => arg.value.startsWith(argumentText));
- if (filtered.length > 0) {
- return { items: filtered, prefix: argumentText };
- }
- }
- return null;
- },
- applyCompletion,
- };
- editor.setAutocompleteProvider(mockProvider);
- // Type "/argtest t" - filtered to [two, three, twelve], prefix "t" matches "two" first
- editor.handleInput("/");
- editor.handleInput("a");
- editor.handleInput("r");
- editor.handleInput("g");
- editor.handleInput("t");
- editor.handleInput("e");
- editor.handleInput("s");
- editor.handleInput("t");
- editor.handleInput(" ");
- editor.handleInput("t");
- await flushAutocomplete();
- assert.strictEqual(editor.isShowingAutocomplete(), true);
- // Press Enter - "t" prefix matches "two" (first in list), so "two" is applied
- editor.handleInput("\r");
- assert.strictEqual(editor.getText(), "/argtest two");
- });
- it("highlights unique prefix match as user types (before full exact match)", async () => {
- const editor = new Editor(createTestTUI(), defaultEditorTheme);
- // Mock provider that returns all items unfiltered (like real extensions do)
- const mockProvider: AutocompleteProvider = {
- getSuggestions: async (lines, _cursorLine, cursorCol) => {
- const text = lines[0] || "";
- const beforeCursor = text.slice(0, cursorCol);
- const argtestMatch = beforeCursor.match(/^\/argtest\s+(\S+)$/);
- if (argtestMatch) {
- const argumentText = argtestMatch[1]!;
- // Return all items - provider does not filter
- const allArguments = [
- { value: "one", label: "one" },
- { value: "two", label: "two" },
- { value: "three", label: "three" },
- ];
- return { items: allArguments, prefix: argumentText };
- }
- return null;
- },
- applyCompletion,
- };
- editor.setAutocompleteProvider(mockProvider);
- // Type "/argtest tw" - "tw" is a prefix of only "two"
- editor.handleInput("/");
- editor.handleInput("a");
- editor.handleInput("r");
- editor.handleInput("g");
- editor.handleInput("t");
- editor.handleInput("e");
- editor.handleInput("s");
- editor.handleInput("t");
- editor.handleInput(" ");
- editor.handleInput("t");
- editor.handleInput("w");
- assert.strictEqual(editor.getText(), "/argtest tw");
- await flushAutocomplete();
- assert.strictEqual(editor.isShowingAutocomplete(), true);
- // Press Enter - "tw" uniquely matches "two", so "two" should be applied
- editor.handleInput("\r");
- assert.strictEqual(editor.getText(), "/argtest two");
- });
- it("selects first prefix match when multiple items match", async () => {
- const editor = new Editor(createTestTUI(), defaultEditorTheme);
- // Mock provider that returns all items unfiltered
- const mockProvider: AutocompleteProvider = {
- getSuggestions: async (lines, _cursorLine, cursorCol) => {
- const text = lines[0] || "";
- const beforeCursor = text.slice(0, cursorCol);
- const argtestMatch = beforeCursor.match(/^\/argtest\s+(\S+)$/);
- if (argtestMatch) {
- const argumentText = argtestMatch[1]!;
- const allArguments = [
- { value: "one", label: "one" },
- { value: "two", label: "two" },
- { value: "three", label: "three" },
- ];
- return { items: allArguments, prefix: argumentText };
- }
- return null;
- },
- applyCompletion,
- };
- editor.setAutocompleteProvider(mockProvider);
- // Type "/argtest t" - "t" is a prefix of both "two" and "three"
- editor.handleInput("/");
- editor.handleInput("a");
- editor.handleInput("r");
- editor.handleInput("g");
- editor.handleInput("t");
- editor.handleInput("e");
- editor.handleInput("s");
- editor.handleInput("t");
- editor.handleInput(" ");
- editor.handleInput("t");
- await flushAutocomplete();
- assert.strictEqual(editor.isShowingAutocomplete(), true);
- // Press Enter - "t" matches "two" first, so "two" is selected
- editor.handleInput("\r");
- assert.strictEqual(editor.getText(), "/argtest two");
- });
- it("works for built-in-style command argument completion path (model-like)", async () => {
- const editor = new Editor(createTestTUI(), defaultEditorTheme);
- // Mock provider for /model command with model completions
- const mockProvider: AutocompleteProvider = {
- getSuggestions: async (lines, _cursorLine, cursorCol) => {
- const text = lines[0] || "";
- const beforeCursor = text.slice(0, cursorCol);
- // Check if we're in /model argument completion context
- // Use [^ ]+ to match any non-space characters (including hyphens)
- const modelMatch = beforeCursor.match(/^\/model\s+(\S+)$/);
- if (modelMatch) {
- const modelText = modelMatch[1]!;
- const allModels = [
- { value: "gpt-4o", label: "gpt-4o" },
- { value: "gpt-4o-mini", label: "gpt-4o-mini" },
- { value: "claude-sonnet", label: "claude-sonnet" },
- ];
- // Return all models that start with the typed prefix
- const filtered = allModels.filter((m) => m.value.startsWith(modelText));
- if (filtered.length > 0) {
- return { items: filtered, prefix: modelText };
- }
- }
- return null;
- },
- applyCompletion,
- };
- editor.setAutocompleteProvider(mockProvider);
- // Type "/model gpt-4o-mini" - exact match for second item in list
- editor.handleInput("/");
- editor.handleInput("m");
- editor.handleInput("o");
- editor.handleInput("d");
- editor.handleInput("e");
- editor.handleInput("l");
- editor.handleInput(" ");
- editor.handleInput("g");
- editor.handleInput("p");
- editor.handleInput("t");
- editor.handleInput("-");
- editor.handleInput("4");
- editor.handleInput("o");
- editor.handleInput("-");
- editor.handleInput("m");
- editor.handleInput("i");
- editor.handleInput("n");
- editor.handleInput("i");
- assert.strictEqual(editor.getText(), "/model gpt-4o-mini");
- await flushAutocomplete();
- assert.strictEqual(editor.isShowingAutocomplete(), true);
- // Press Enter - should retain exact typed value, not apply first highlighted item
- editor.handleInput("\r");
- // The exact typed value should be retained
- assert.strictEqual(editor.getText(), "/model gpt-4o-mini");
- });
- it("awaits async slash command argument completions", async () => {
- const editor = new Editor(createTestTUI(), defaultEditorTheme);
- const provider = new CombinedAutocompleteProvider(
- [
- {
- name: "load-skills",
- description: "Load skills",
- getArgumentCompletions: async (prefix) =>
- prefix.startsWith("s") ? [{ value: "skill-a", label: "skill-a" }] : null,
- },
- ],
- process.cwd(),
- );
- editor.setAutocompleteProvider(provider);
- editor.setText("/load-skills ");
- editor.handleInput("s");
- await flushAutocomplete();
- assert.strictEqual(editor.isShowingAutocomplete(), true);
- editor.handleInput("\t");
- assert.strictEqual(editor.getText(), "/load-skills skill-a");
- assert.strictEqual(editor.isShowingAutocomplete(), false);
- });
- it("ignores invalid slash command argument completion results", async () => {
- const editor = new Editor(createTestTUI(), defaultEditorTheme);
- const provider = new CombinedAutocompleteProvider(
- [
- {
- name: "load-skills",
- description: "Load skills",
- getArgumentCompletions: (() => "not-an-array") as unknown as (
- argumentPrefix: string,
- ) => Promise<{ value: string; label: string }[] | null>,
- },
- ],
- process.cwd(),
- );
- editor.setAutocompleteProvider(provider);
- editor.setText("/load-skills ");
- editor.handleInput("s");
- await flushAutocomplete();
- assert.strictEqual(editor.isShowingAutocomplete(), false);
- assert.strictEqual(editor.getText(), "/load-skills s");
- });
- it("does not show argument completions when command has no argument completer", async () => {
- const editor = new Editor(createTestTUI(), defaultEditorTheme);
- const provider = new CombinedAutocompleteProvider(
- [
- { name: "help", description: "Show help" },
- {
- name: "model",
- description: "Switch model",
- getArgumentCompletions: () => [{ value: "claude-opus", label: "claude-opus" }],
- },
- ],
- process.cwd(),
- );
- editor.setAutocompleteProvider(provider);
- editor.handleInput("/");
- editor.handleInput("h");
- editor.handleInput("e");
- await flushAutocomplete();
- assert.strictEqual(editor.isShowingAutocomplete(), true);
- editor.handleInput("\t");
- assert.strictEqual(editor.getText(), "/help ");
- assert.strictEqual(editor.isShowingAutocomplete(), false);
- });
- });
- describe("Character jump (Ctrl+])", () => {
- it("jumps forward to first occurrence of character on same line", () => {
- const editor = new Editor(createTestTUI(), defaultEditorTheme);
- editor.setText("hello world");
- editor.handleInput("\x01"); // Ctrl+A - go to start
- assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 0 });
- editor.handleInput("\x1d"); // Ctrl+] (legacy sequence for ctrl+])
- editor.handleInput("o"); // Jump to first 'o'
- assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 4 }); // 'o' in "hello"
- });
- it("jumps forward to next occurrence after cursor", () => {
- const editor = new Editor(createTestTUI(), defaultEditorTheme);
- editor.setText("hello world");
- editor.handleInput("\x01"); // Ctrl+A - go to start
- // Move cursor to the 'o' in "hello" (col 4)
- for (let i = 0; i < 4; i++) editor.handleInput("\x1b[C");
- assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 4 });
- editor.handleInput("\x1d"); // Ctrl+]
- editor.handleInput("o"); // Jump to next 'o' (in "world")
- assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 7 }); // 'o' in "world"
- });
- it("jumps forward across multiple lines", () => {
- const editor = new Editor(createTestTUI(), defaultEditorTheme);
- editor.setText("abc\ndef\nghi");
- // Cursor is at end (line 2, col 3). Move to line 0 via up arrows, then Ctrl+A
- editor.handleInput("\x1b[A"); // Up
- editor.handleInput("\x1b[A"); // Up - now on line 0
- editor.handleInput("\x01"); // Ctrl+A - go to start of line
- assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 0 });
- editor.handleInput("\x1d"); // Ctrl+]
- editor.handleInput("g"); // Jump to 'g' on line 3
- assert.deepStrictEqual(editor.getCursor(), { line: 2, col: 0 });
- });
- it("jumps backward to first occurrence before cursor on same line", () => {
- const editor = new Editor(createTestTUI(), defaultEditorTheme);
- editor.setText("hello world");
- // Cursor at end (col 11)
- assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 11 });
- editor.handleInput("\x1b\x1d"); // Ctrl+Alt+] (ESC followed by Ctrl+])
- editor.handleInput("o"); // Jump to last 'o' before cursor
- assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 7 }); // 'o' in "world"
- });
- it("jumps backward across multiple lines", () => {
- const editor = new Editor(createTestTUI(), defaultEditorTheme);
- editor.setText("abc\ndef\nghi");
- // Cursor at end of line 3
- assert.deepStrictEqual(editor.getCursor(), { line: 2, col: 3 });
- editor.handleInput("\x1b\x1d"); // Ctrl+Alt+]
- editor.handleInput("a"); // Jump to 'a' on line 1
- assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 0 });
- });
- it("does nothing when character is not found (forward)", () => {
- const editor = new Editor(createTestTUI(), defaultEditorTheme);
- editor.setText("hello world");
- editor.handleInput("\x01"); // Ctrl+A - go to start
- assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 0 });
- editor.handleInput("\x1d"); // Ctrl+]
- editor.handleInput("z"); // 'z' doesn't exist
- assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 0 }); // Cursor unchanged
- });
- it("does nothing when character is not found (backward)", () => {
- const editor = new Editor(createTestTUI(), defaultEditorTheme);
- editor.setText("hello world");
- // Cursor at end
- assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 11 });
- editor.handleInput("\x1b\x1d"); // Ctrl+Alt+]
- editor.handleInput("z"); // 'z' doesn't exist
- assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 11 }); // Cursor unchanged
- });
- it("is case-sensitive", () => {
- const editor = new Editor(createTestTUI(), defaultEditorTheme);
- editor.setText("Hello World");
- editor.handleInput("\x01"); // Ctrl+A - go to start
- assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 0 });
- // Search for lowercase 'h' - should not find it (only 'H' exists)
- editor.handleInput("\x1d"); // Ctrl+]
- editor.handleInput("h");
- assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 0 }); // Cursor unchanged
- // Search for uppercase 'W' - should find it
- editor.handleInput("\x1d"); // Ctrl+]
- editor.handleInput("W");
- assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 6 }); // 'W' in "World"
- });
- it("cancels jump mode when Ctrl+] is pressed again", () => {
- const editor = new Editor(createTestTUI(), defaultEditorTheme);
- editor.setText("hello world");
- editor.handleInput("\x01"); // Ctrl+A - go to start
- assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 0 });
- editor.handleInput("\x1d"); // Ctrl+] - enter jump mode
- editor.handleInput("\x1d"); // Ctrl+] again - cancel
- // Type 'o' normally - should insert, not jump
- editor.handleInput("o");
- assert.strictEqual(editor.getText(), "ohello world");
- });
- it("cancels jump mode on Escape and processes the Escape", () => {
- const editor = new Editor(createTestTUI(), defaultEditorTheme);
- editor.setText("hello world");
- editor.handleInput("\x01"); // Ctrl+A - go to start
- assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 0 });
- editor.handleInput("\x1d"); // Ctrl+] - enter jump mode
- editor.handleInput("\x1b"); // Escape - cancel jump mode
- // Cursor should be unchanged (Escape itself doesn't move cursor in editor)
- assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 0 });
- // Type 'o' normally - should insert, not jump
- editor.handleInput("o");
- assert.strictEqual(editor.getText(), "ohello world");
- });
- it("cancels backward jump mode when Ctrl+Alt+] is pressed again", () => {
- const editor = new Editor(createTestTUI(), defaultEditorTheme);
- editor.setText("hello world");
- // Cursor at end
- assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 11 });
- editor.handleInput("\x1b\x1d"); // Ctrl+Alt+] - enter backward jump mode
- editor.handleInput("\x1b\x1d"); // Ctrl+Alt+] again - cancel
- // Type 'o' normally - should insert, not jump
- editor.handleInput("o");
- assert.strictEqual(editor.getText(), "hello worldo");
- });
- it("searches for special characters", () => {
- const editor = new Editor(createTestTUI(), defaultEditorTheme);
- editor.setText("foo(bar) = baz;");
- editor.handleInput("\x01"); // Ctrl+A - go to start
- assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 0 });
- // Jump to '('
- editor.handleInput("\x1d"); // Ctrl+]
- editor.handleInput("(");
- assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 3 });
- // Jump to '='
- editor.handleInput("\x1d"); // Ctrl+]
- editor.handleInput("=");
- assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 9 });
- });
- it("handles empty text gracefully", () => {
- const editor = new Editor(createTestTUI(), defaultEditorTheme);
- editor.setText("");
- assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 0 });
- editor.handleInput("\x1d"); // Ctrl+]
- editor.handleInput("x");
- assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 0 }); // Cursor unchanged
- });
- it("resets lastAction when jumping", () => {
- const editor = new Editor(createTestTUI(), defaultEditorTheme);
- editor.setText("hello world");
- editor.handleInput("\x01"); // Ctrl+A - go to start
- // Type to set lastAction to "type-word"
- editor.handleInput("x");
- assert.strictEqual(editor.getText(), "xhello world");
- // Jump forward
- editor.handleInput("\x1d"); // Ctrl+]
- editor.handleInput("o");
- // Type more - should start a new undo unit (lastAction was reset)
- editor.handleInput("Y");
- assert.strictEqual(editor.getText(), "xhellYo world");
- // Undo should only undo "Y", not "x" as well
- editor.handleInput("\x1b[45;5u"); // Ctrl+- (undo)
- assert.strictEqual(editor.getText(), "xhello world");
- });
- });
- describe("Sticky column", () => {
- // Helper: position cursor at a specific line and column
- function positionCursor(editor: Editor, line: number, col: number): void {
- // Go to line 0 first
- for (let i = 0; i < 20; i++) editor.handleInput("\x1b[A");
- // Go to target line
- for (let i = 0; i < line; i++) editor.handleInput("\x1b[B");
- // Go to target col
- editor.handleInput("\x01"); // Ctrl+A
- for (let i = 0; i < col; i++) editor.handleInput("\x1b[C");
- }
- it("preserves target column when moving up through a shorter line", () => {
- const editor = new Editor(createTestTUI(), defaultEditorTheme);
- // Line 0: "2222222222x222" (x at col 10)
- // Line 1: "" (empty)
- // Line 2: "1111111111_111111111111" (_ at col 10)
- editor.setText("2222222222x222\n\n1111111111_111111111111");
- // Position cursor on _ (line 2, col 10)
- assert.deepStrictEqual(editor.getCursor(), { line: 2, col: 23 }); // At end
- editor.handleInput("\x01"); // Ctrl+A - go to start of line
- for (let i = 0; i < 10; i++) editor.handleInput("\x1b[C"); // Move right to col 10
- assert.deepStrictEqual(editor.getCursor(), { line: 2, col: 10 });
- // Press Up - should move to empty line (col clamped to 0)
- editor.handleInput("\x1b[A"); // Up arrow
- assert.deepStrictEqual(editor.getCursor(), { line: 1, col: 0 });
- // Press Up again - should move to line 0 at col 10 (on 'x')
- editor.handleInput("\x1b[A"); // Up arrow
- assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 10 });
- });
- it("preserves target column when moving down through a shorter line", () => {
- const editor = new Editor(createTestTUI(), defaultEditorTheme);
- editor.setText("1111111111_111\n\n2222222222x222222222222");
- // Position cursor on _ (line 0, col 10)
- editor.handleInput("\x1b[A"); // Up to line 1
- editor.handleInput("\x1b[A"); // Up to line 0
- editor.handleInput("\x01"); // Ctrl+A
- for (let i = 0; i < 10; i++) editor.handleInput("\x1b[C");
- assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 10 });
- // Press Down - should move to empty line (col clamped to 0)
- editor.handleInput("\x1b[B"); // Down arrow
- assert.deepStrictEqual(editor.getCursor(), { line: 1, col: 0 });
- // Press Down again - should move to line 2 at col 10 (on 'x')
- editor.handleInput("\x1b[B"); // Down arrow
- assert.deepStrictEqual(editor.getCursor(), { line: 2, col: 10 });
- });
- it("resets sticky column on horizontal movement (left arrow)", () => {
- const editor = new Editor(createTestTUI(), defaultEditorTheme);
- editor.setText("1234567890\n\n1234567890");
- // Start at line 2, col 5
- editor.handleInput("\x01"); // Ctrl+A
- for (let i = 0; i < 5; i++) editor.handleInput("\x1b[C");
- assert.deepStrictEqual(editor.getCursor(), { line: 2, col: 5 });
- // Move up through empty line
- editor.handleInput("\x1b[A"); // Up - line 1, col 0
- editor.handleInput("\x1b[A"); // Up - line 0, col 5 (sticky)
- assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 5 });
- // Move left - resets sticky column
- editor.handleInput("\x1b[D"); // Left
- assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 4 });
- // Move down twice
- editor.handleInput("\x1b[B"); // Down - line 1, col 0
- editor.handleInput("\x1b[B"); // Down - line 2, col 4 (new sticky from col 4)
- assert.deepStrictEqual(editor.getCursor(), { line: 2, col: 4 });
- });
- it("resets sticky column on horizontal movement (right arrow)", () => {
- const editor = new Editor(createTestTUI(), defaultEditorTheme);
- editor.setText("1234567890\n\n1234567890");
- // Start at line 0, col 5
- editor.handleInput("\x1b[A"); // Up to line 1
- editor.handleInput("\x1b[A"); // Up to line 0
- editor.handleInput("\x01"); // Ctrl+A
- for (let i = 0; i < 5; i++) editor.handleInput("\x1b[C");
- assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 5 });
- // Move down through empty line
- editor.handleInput("\x1b[B"); // Down - line 1, col 0
- editor.handleInput("\x1b[B"); // Down - line 2, col 5 (sticky)
- assert.deepStrictEqual(editor.getCursor(), { line: 2, col: 5 });
- // Move right - resets sticky column
- editor.handleInput("\x1b[C"); // Right
- assert.deepStrictEqual(editor.getCursor(), { line: 2, col: 6 });
- // Move up twice
- editor.handleInput("\x1b[A"); // Up - line 1, col 0
- editor.handleInput("\x1b[A"); // Up - line 0, col 6 (new sticky from col 6)
- assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 6 });
- });
- it("resets sticky column on typing", () => {
- const editor = new Editor(createTestTUI(), defaultEditorTheme);
- editor.setText("1234567890\n\n1234567890");
- // Start at line 2, col 8
- editor.handleInput("\x01"); // Ctrl+A
- for (let i = 0; i < 8; i++) editor.handleInput("\x1b[C");
- // Move up through empty line
- editor.handleInput("\x1b[A"); // Up
- editor.handleInput("\x1b[A"); // Up - line 0, col 8
- assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 8 });
- // Type a character - resets sticky column
- editor.handleInput("X");
- assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 9 });
- // Move down twice
- editor.handleInput("\x1b[B"); // Down - line 1, col 0
- editor.handleInput("\x1b[B"); // Down - line 2, col 9 (new sticky from col 9)
- assert.deepStrictEqual(editor.getCursor(), { line: 2, col: 9 });
- });
- it("resets sticky column on backspace", () => {
- const editor = new Editor(createTestTUI(), defaultEditorTheme);
- editor.setText("1234567890\n\n1234567890");
- // Start at line 2, col 8
- editor.handleInput("\x01"); // Ctrl+A
- for (let i = 0; i < 8; i++) editor.handleInput("\x1b[C");
- // Move up through empty line
- editor.handleInput("\x1b[A"); // Up
- editor.handleInput("\x1b[A"); // Up - line 0, col 8
- assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 8 });
- // Backspace - resets sticky column
- editor.handleInput("\x7f"); // Backspace
- assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 7 });
- // Move down twice
- editor.handleInput("\x1b[B"); // Down - line 1, col 0
- editor.handleInput("\x1b[B"); // Down - line 2, col 7 (new sticky from col 7)
- assert.deepStrictEqual(editor.getCursor(), { line: 2, col: 7 });
- });
- it("resets sticky column on Ctrl+A (move to line start)", () => {
- const editor = new Editor(createTestTUI(), defaultEditorTheme);
- editor.setText("1234567890\n\n1234567890");
- // Start at line 2, col 8
- editor.handleInput("\x01"); // Ctrl+A
- for (let i = 0; i < 8; i++) editor.handleInput("\x1b[C");
- // Move up - establishes sticky col 8
- editor.handleInput("\x1b[A"); // Up - line 1, col 0
- // Ctrl+A - resets sticky column to 0
- editor.handleInput("\x01"); // Ctrl+A
- assert.deepStrictEqual(editor.getCursor(), { line: 1, col: 0 });
- // Move up
- editor.handleInput("\x1b[A"); // Up - line 0, col 0 (new sticky from col 0)
- assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 0 });
- });
- it("resets sticky column on Ctrl+E (move to line end)", () => {
- const editor = new Editor(createTestTUI(), defaultEditorTheme);
- editor.setText("12345\n\n1234567890");
- // Start at line 2, col 3
- editor.handleInput("\x01"); // Ctrl+A
- for (let i = 0; i < 3; i++) editor.handleInput("\x1b[C");
- // Move up through empty line - establishes sticky col 3
- editor.handleInput("\x1b[A"); // Up - line 1, col 0
- editor.handleInput("\x1b[A"); // Up - line 0, col 3
- assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 3 });
- // Ctrl+E - resets sticky column to end
- editor.handleInput("\x05"); // Ctrl+E
- assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 5 });
- // Move down twice
- editor.handleInput("\x1b[B"); // Down - line 1, col 0
- editor.handleInput("\x1b[B"); // Down - line 2, col 5 (new sticky from col 5)
- assert.deepStrictEqual(editor.getCursor(), { line: 2, col: 5 });
- });
- it("resets sticky column on word movement (Ctrl+Left)", () => {
- const editor = new Editor(createTestTUI(), defaultEditorTheme);
- editor.setText("hello world\n\nhello world");
- // Start at end of line 2 (col 11)
- assert.deepStrictEqual(editor.getCursor(), { line: 2, col: 11 });
- // Move up through empty line - establishes sticky col 11
- editor.handleInput("\x1b[A"); // Up - line 1, col 0
- editor.handleInput("\x1b[A"); // Up - line 0, col 11
- assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 11 });
- // Ctrl+Left - word movement resets sticky column
- editor.handleInput("\x1b[1;5D"); // Ctrl+Left
- assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 6 }); // Before "world"
- // Move down twice
- editor.handleInput("\x1b[B"); // Down - line 1, col 0
- editor.handleInput("\x1b[B"); // Down - line 2, col 6 (new sticky from col 6)
- assert.deepStrictEqual(editor.getCursor(), { line: 2, col: 6 });
- });
- it("resets sticky column on word movement (Ctrl+Right)", () => {
- const editor = new Editor(createTestTUI(), defaultEditorTheme);
- editor.setText("hello world\n\nhello world");
- // Start at line 0, col 0
- editor.handleInput("\x1b[A"); // Up
- editor.handleInput("\x1b[A"); // Up
- editor.handleInput("\x01"); // Ctrl+A
- assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 0 });
- // Move down through empty line - establishes sticky col 0
- editor.handleInput("\x1b[B"); // Down - line 1, col 0
- editor.handleInput("\x1b[B"); // Down - line 2, col 0
- assert.deepStrictEqual(editor.getCursor(), { line: 2, col: 0 });
- // Ctrl+Right - word movement resets sticky column
- editor.handleInput("\x1b[1;5C"); // Ctrl+Right
- assert.deepStrictEqual(editor.getCursor(), { line: 2, col: 5 }); // After "hello"
- // Move up twice
- editor.handleInput("\x1b[A"); // Up - line 1, col 0
- editor.handleInput("\x1b[A"); // Up - line 0, col 5 (new sticky from col 5)
- assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 5 });
- });
- it("resets sticky column on undo", () => {
- const editor = new Editor(createTestTUI(), defaultEditorTheme);
- editor.setText("1234567890\n\n1234567890");
- // Go to line 0, col 8
- editor.handleInput("\x1b[A"); // Up to line 1
- editor.handleInput("\x1b[A"); // Up to line 0
- editor.handleInput("\x01"); // Ctrl+A
- for (let i = 0; i < 8; i++) editor.handleInput("\x1b[C");
- assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 8 });
- // Move down through empty line - establishes sticky col 8
- editor.handleInput("\x1b[B"); // Down - line 1, col 0
- editor.handleInput("\x1b[B"); // Down - line 2, col 8 (sticky)
- assert.deepStrictEqual(editor.getCursor(), { line: 2, col: 8 });
- // Type something to create undo state - this clears sticky and sets col to 9
- editor.handleInput("X");
- assert.strictEqual(editor.getText(), "1234567890\n\n12345678X90");
- assert.deepStrictEqual(editor.getCursor(), { line: 2, col: 9 });
- // Move up - establishes new sticky col 9
- editor.handleInput("\x1b[A"); // Up - line 1, col 0
- editor.handleInput("\x1b[A"); // Up - line 0, col 9
- assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 9 });
- // Undo - resets sticky column and restores cursor to line 2, col 8
- editor.handleInput("\x1b[45;5u"); // Ctrl+- (undo)
- assert.strictEqual(editor.getText(), "1234567890\n\n1234567890");
- assert.deepStrictEqual(editor.getCursor(), { line: 2, col: 8 });
- // Move up - should capture new sticky from restored col 8, not old col 9
- editor.handleInput("\x1b[A"); // Up - line 1, col 0
- editor.handleInput("\x1b[A"); // Up - line 0, col 8 (new sticky from restored position)
- assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 8 });
- });
- it("handles multiple consecutive up/down movements", () => {
- const editor = new Editor(createTestTUI(), defaultEditorTheme);
- editor.setText("1234567890\nab\ncd\nef\n1234567890");
- // Start at line 4, col 7
- editor.handleInput("\x01"); // Ctrl+A
- for (let i = 0; i < 7; i++) editor.handleInput("\x1b[C");
- assert.deepStrictEqual(editor.getCursor(), { line: 4, col: 7 });
- // Move up multiple times through short lines
- editor.handleInput("\x1b[A"); // Up - line 3, col 2 (clamped)
- editor.handleInput("\x1b[A"); // Up - line 2, col 2 (clamped)
- editor.handleInput("\x1b[A"); // Up - line 1, col 2 (clamped)
- editor.handleInput("\x1b[A"); // Up - line 0, col 7 (restored)
- assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 7 });
- // Move down multiple times - sticky should still be 7
- editor.handleInput("\x1b[B"); // Down - line 1, col 2
- editor.handleInput("\x1b[B"); // Down - line 2, col 2
- editor.handleInput("\x1b[B"); // Down - line 3, col 2
- editor.handleInput("\x1b[B"); // Down - line 4, col 7 (restored)
- assert.deepStrictEqual(editor.getCursor(), { line: 4, col: 7 });
- });
- it("moves correctly through wrapped visual lines without getting stuck", () => {
- const tui = createTestTUI(15, 24); // Narrow terminal
- const editor = new Editor(tui, defaultEditorTheme);
- // Line 0: short
- // Line 1: 30 chars = wraps to 3 visual lines at width 10 (after padding)
- editor.setText("short\n123456789012345678901234567890");
- editor.render(15); // This gives 14 layout width
- // Position at end of line 1 (col 30)
- assert.deepStrictEqual(editor.getCursor(), { line: 1, col: 30 });
- // Move up repeatedly - should traverse all visual lines of the wrapped text
- // and eventually reach line 0
- editor.handleInput("\x1b[A"); // Up - to previous visual line within line 1
- assert.strictEqual(editor.getCursor().line, 1);
- editor.handleInput("\x1b[A"); // Up - another visual line
- assert.strictEqual(editor.getCursor().line, 1);
- editor.handleInput("\x1b[A"); // Up - should reach line 0
- assert.strictEqual(editor.getCursor().line, 0);
- });
- it("handles setText resetting sticky column", () => {
- const editor = new Editor(createTestTUI(), defaultEditorTheme);
- editor.setText("1234567890\n\n1234567890");
- // Establish sticky column
- editor.handleInput("\x01"); // Ctrl+A
- for (let i = 0; i < 8; i++) editor.handleInput("\x1b[C");
- editor.handleInput("\x1b[A"); // Up
- // setText should reset sticky column
- editor.setText("abcdefghij\n\nabcdefghij");
- assert.deepStrictEqual(editor.getCursor(), { line: 2, col: 10 }); // At end
- // Move up - should capture new sticky from current position (10)
- editor.handleInput("\x1b[A"); // Up - line 1, col 0
- editor.handleInput("\x1b[A"); // Up - line 0, col 10
- assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 10 });
- });
- it("sets preferredVisualCol when pressing right at end of prompt (last line)", () => {
- const editor = new Editor(createTestTUI(), defaultEditorTheme);
- // Line 0: 20 chars with 'x' at col 10
- // Line 1: empty
- // Line 2: 10 chars ending with '_'
- editor.setText("111111111x1111111111\n\n333333333_");
- // Go to line 0, press Ctrl+E (end of line) - col 20
- editor.handleInput("\x1b[A"); // Up to line 1
- editor.handleInput("\x1b[A"); // Up to line 0
- editor.handleInput("\x05"); // Ctrl+E - move to end of line
- assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 20 });
- // Move down to line 2 - cursor clamped to col 10 (end of line)
- editor.handleInput("\x1b[B"); // Down to line 1, col 0
- editor.handleInput("\x1b[B"); // Down to line 2, col 10 (clamped)
- assert.deepStrictEqual(editor.getCursor(), { line: 2, col: 10 });
- // Press Right at end of prompt - nothing visible happens, but sets preferredVisualCol to 10
- editor.handleInput("\x1b[C"); // Right - can't move, but sets preferredVisualCol
- assert.deepStrictEqual(editor.getCursor(), { line: 2, col: 10 }); // Still at same position
- // Move up twice to line 0 - should use preferredVisualCol (10) to land on 'x'
- editor.handleInput("\x1b[A"); // Up to line 1, col 0
- editor.handleInput("\x1b[A"); // Up to line 0, col 10 (on 'x')
- assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 10 });
- });
- it("handles editor resizes when preferredVisualCol is on the same line", () => {
- // Create editor with wider terminal
- const tui = createTestTUI(80, 24);
- const editor = new Editor(tui, defaultEditorTheme);
- editor.setText("12345678901234567890\n\n12345678901234567890");
- // Start at line 2, col 15
- editor.handleInput("\x01"); // Ctrl+A
- for (let i = 0; i < 15; i++) editor.handleInput("\x1b[C");
- // Move up through empty line - establishes sticky col 15
- editor.handleInput("\x1b[A"); // Up
- editor.handleInput("\x1b[A"); // Up - line 0, col 15
- assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 15 });
- // Render with narrower width to simulate resize
- editor.render(12); // Width 12
- // Move down - sticky should be clamped to new width
- editor.handleInput("\x1b[B"); // Down - line 1
- editor.handleInput("\x1b[B"); // Down - line 2, col should be clamped
- assert.equal(editor.getCursor().col, 4);
- });
- it("handles editor resizes when preferredVisualCol is on a different line", () => {
- const tui = createTestTUI(80, 24);
- const editor = new Editor(tui, defaultEditorTheme);
- // Create a line that wraps into multiple visual lines at width 10
- // "12345678901234567890" = 20 chars, wraps to 2 visual lines at width 10
- editor.setText("short\n12345678901234567890");
- // Go to line 1, col 15
- editor.handleInput("\x01"); // Ctrl+A
- for (let i = 0; i < 15; i++) editor.handleInput("\x1b[C");
- assert.deepStrictEqual(editor.getCursor(), { line: 1, col: 15 });
- // Move up to establish sticky col 15
- editor.handleInput("\x1b[A"); // Up to line 0
- // Line 0 has only 5 chars, so cursor at col 5
- assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 5 });
- // Narrow the editor
- editor.render(10);
- // Move down - preferredVisualCol was 15, but width is 10
- // Should land on line 1, clamped to width (visual col 9, which is logical col 9)
- editor.handleInput("\x1b[B"); // Down to line 1
- assert.deepStrictEqual(editor.getCursor(), { line: 1, col: 8 });
- // Move up
- editor.handleInput("\x1b[A"); // Up - should go to line 0
- assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 5 }); // Line 0 only has 5 chars
- // Restore the original width
- editor.render(80);
- // Move down - preferredVisualCol was kept at 15
- editor.handleInput("\x1b[B"); // Down to line 1
- assert.deepStrictEqual(editor.getCursor(), { line: 1, col: 15 });
- });
- it("rewrapped lines: target fits current visual column", () => {
- const tui = createTestTUI(80, 24);
- const editor = new Editor(tui, defaultEditorTheme);
- editor.setText("abcdefghijklmnopqr\n123456789012345678");
- positionCursor(editor, 0, 18);
- assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 18 });
- // Narrow to width 10 (layoutWidth = 9).
- // Line 0 last segment has visual col max 9, line 1 first segment max 8
- editor.render(10);
- // Move down: cursor clamps to 8
- editor.handleInput("\x1b[B");
- assert.deepStrictEqual(editor.getCursor(), { line: 1, col: 8 });
- // Widen back. Move up, the current visual col wins
- editor.render(80);
- editor.handleInput("\x1b[A");
- assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 8 });
- // Preferred was cleared by the rewrapped branch
- editor.handleInput("\x1b[B");
- assert.deepStrictEqual(editor.getCursor(), { line: 1, col: 8 });
- });
- it("rewrapped lines: target shorter than current visual column", () => {
- const tui = createTestTUI(80, 24);
- const editor = new Editor(tui, defaultEditorTheme);
- editor.setText("abcdefghijklmnopqr\n123456789012345678\nab");
- positionCursor(editor, 0, 18);
- assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 18 });
- // Narrow to width 10 (layoutWidth = 9). Moving down clamps to col 8
- editor.render(10);
- editor.handleInput("\x1b[B");
- assert.deepStrictEqual(editor.getCursor(), { line: 1, col: 8 });
- // Widen the editor
- editor.render(80);
- // Move down to short line "ab".
- // preferredVisualCol is replaced with current visual col (8), cursor clamps to 2
- editor.handleInput("\x1b[B");
- assert.deepStrictEqual(editor.getCursor(), { line: 2, col: 2 });
- // Moving up restores to preferred col 8
- editor.handleInput("\x1b[A");
- assert.deepStrictEqual(editor.getCursor(), { line: 1, col: 8 });
- });
- });
- describe("Paste marker atomic behavior", () => {
- /** Helper: simulate a large paste that creates a marker */
- function pasteWithMarker(editor: Editor): string {
- const bigContent = "line\n".repeat(20).trimEnd(); // 20 lines
- editor.handleInput(`\x1b[200~${bigContent}\x1b[201~`);
- // The editor replaces large pastes with a marker like "[paste #1 +20 lines]"
- return editor.getText();
- }
- it("creates a paste marker for large pastes", () => {
- const editor = new Editor(createTestTUI(), defaultEditorTheme);
- const text = pasteWithMarker(editor);
- assert.match(text, /\[paste #\d+ \+\d+ lines\]/);
- });
- it("treats paste marker as single unit for right arrow", () => {
- const editor = new Editor(createTestTUI(), defaultEditorTheme);
- editor.handleInput("A");
- pasteWithMarker(editor);
- editor.handleInput("B");
- // Text: "A[paste #1 +20 lines]B", cursor at end
- // Go to start
- editor.handleInput("\x01"); // Ctrl+A
- assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 0 });
- // Right arrow: should move past "A"
- editor.handleInput("\x1b[C");
- assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 1 });
- // Right arrow: should skip the entire marker
- editor.handleInput("\x1b[C");
- const marker = editor.getText().match(/\[paste #\d+ \+\d+ lines\]/)![0];
- assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 1 + marker.length });
- // Right arrow: should move past "B"
- editor.handleInput("\x1b[C");
- assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 1 + marker.length + 1 });
- });
- it("treats paste marker as single unit for left arrow", () => {
- const editor = new Editor(createTestTUI(), defaultEditorTheme);
- editor.handleInput("A");
- pasteWithMarker(editor);
- editor.handleInput("B");
- // Cursor at end
- // Left arrow: past "B"
- editor.handleInput("\x1b[D");
- const text = editor.getText();
- const marker = text.match(/\[paste #\d+ \+\d+ lines\]/)![0];
- assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 1 + marker.length });
- // Left arrow: skip the entire marker
- editor.handleInput("\x1b[D");
- assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 1 });
- // Left arrow: past "A"
- editor.handleInput("\x1b[D");
- assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 0 });
- });
- it("treats paste marker as single unit for backspace", () => {
- const editor = new Editor(createTestTUI(), defaultEditorTheme);
- editor.handleInput("A");
- pasteWithMarker(editor);
- editor.handleInput("B");
- const text = editor.getText();
- const marker = text.match(/\[paste #\d+ \+\d+ lines\]/)![0];
- // Position cursor right after the marker (before "B")
- editor.handleInput("\x01"); // Ctrl+A
- // Move past "A" and the marker
- editor.handleInput("\x1b[C"); // past "A"
- editor.handleInput("\x1b[C"); // past marker
- assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 1 + marker.length });
- // Backspace: should delete the entire marker at once
- editor.handleInput("\x7f");
- assert.strictEqual(editor.getText(), "AB");
- assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 1 });
- });
- it("treats paste marker as single unit for forward delete", () => {
- const editor = new Editor(createTestTUI(), defaultEditorTheme);
- editor.handleInput("A");
- pasteWithMarker(editor);
- editor.handleInput("B");
- // Position cursor on "A" (col 0) then move right once to be just before marker
- editor.handleInput("\x01"); // Ctrl+A
- editor.handleInput("\x1b[C"); // past "A", now at col 1 (start of marker)
- // Forward delete: should delete the entire marker at once
- editor.handleInput("\x1b[3~"); // Delete key
- assert.strictEqual(editor.getText(), "AB");
- assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 1 });
- });
- it("treats paste marker as single unit for word movement", () => {
- const editor = new Editor(createTestTUI(), defaultEditorTheme);
- editor.handleInput("X");
- editor.handleInput(" ");
- pasteWithMarker(editor);
- editor.handleInput(" ");
- editor.handleInput("Y");
- // Text: "X [paste #1 +20 lines] Y"
- const text = editor.getText();
- const marker = text.match(/\[paste #\d+ \+\d+ lines\]/)![0];
- // Go to start
- editor.handleInput("\x01"); // Ctrl+A
- // Ctrl+Right: skip "X"
- editor.handleInput("\x1b[1;5C");
- assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 1 });
- // Ctrl+Right: skip whitespace + marker (marker treated as single non-ws, non-punct unit)
- editor.handleInput("\x1b[1;5C");
- assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 2 + marker.length });
- });
- it("undo restores marker after backspace deletion", () => {
- const editor = new Editor(createTestTUI(), defaultEditorTheme);
- editor.handleInput("A");
- pasteWithMarker(editor);
- editor.handleInput("B");
- const textBefore = editor.getText();
- // Position after marker
- editor.handleInput("\x01");
- editor.handleInput("\x1b[C"); // past A
- editor.handleInput("\x1b[C"); // past marker
- // Delete marker
- editor.handleInput("\x7f");
- assert.strictEqual(editor.getText(), "AB");
- // Undo
- editor.handleInput("\x1b[45;5u");
- assert.strictEqual(editor.getText(), textBefore);
- });
- it("handles multiple paste markers in same line", () => {
- const editor = new Editor(createTestTUI(), defaultEditorTheme);
- pasteWithMarker(editor);
- editor.handleInput(" ");
- pasteWithMarker(editor);
- const text = editor.getText();
- const markers = [...text.matchAll(/\[paste #\d+ \+\d+ lines\]/g)];
- assert.strictEqual(markers.length, 2);
- // Go to start
- editor.handleInput("\x01");
- // Right arrow: should skip first marker atomically
- editor.handleInput("\x1b[C");
- assert.deepStrictEqual(editor.getCursor(), { line: 0, col: markers[0]![0].length });
- // Right arrow: past space
- editor.handleInput("\x1b[C");
- assert.deepStrictEqual(editor.getCursor(), { line: 0, col: markers[0]![0].length + 1 });
- // Right arrow: should skip second marker atomically
- editor.handleInput("\x1b[C");
- assert.deepStrictEqual(editor.getCursor(), {
- line: 0,
- col: markers[0]![0].length + 1 + markers[1]![0].length,
- });
- });
- it("does not treat manually typed marker-like text as atomic (no valid paste ID)", () => {
- const editor = new Editor(createTestTUI(), defaultEditorTheme);
- // Type text that matches the pattern but was typed manually (no paste entry)
- const fakeMarker = "[paste #99 +5 lines]";
- for (const ch of fakeMarker) editor.handleInput(ch);
- assert.strictEqual(editor.getText(), fakeMarker);
- // No paste with ID 99 exists, so the marker is NOT treated atomically.
- // Right arrow should move one grapheme at a time.
- editor.handleInput("\x01"); // Ctrl+A
- editor.handleInput("\x1b[C"); // Right
- assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 1 }); // Just past "["
- });
- it("does not crash when paste marker is wider than terminal width", () => {
- // Reproduce: terminal width 8, paste marker "[paste #1 +47 lines]" (21 chars)
- const tui = createTestTUI();
- const editor = new Editor(tui, defaultEditorTheme);
- const bigContent = "line\n".repeat(47).trimEnd();
- editor.handleInput(`\x1b[200~${bigContent}\x1b[201~`);
- const text = editor.getText();
- const marker = text.match(/\[paste #\d+ \+\d+ lines\]/);
- assert.ok(marker, "paste marker should be created");
- assert.ok(visibleWidth(marker[0]) > 8, "marker should be wider than render width");
- // Render at very narrow width - should not throw
- const lines = editor.render(8);
- // Every rendered line must fit within the width (marker is split)
- for (const line of lines) {
- assert.ok(
- visibleWidth(line) <= 8,
- `line exceeds width 8: visible=${visibleWidth(line)} text=${JSON.stringify(line)}`,
- );
- }
- });
- it("does not crash when text + paste marker exceeds terminal width with cursor on marker", () => {
- // Reproduce: terminal width 54, text "b".repeat(35) + "[paste #1 +27 lines]" + "bbbb"
- // Cursor lands on the paste marker after word-wrap, causing the rendered line
- // to be 55 visible chars (1 over the width).
- const tui = createTestTUI();
- const editor = new Editor(tui, defaultEditorTheme);
- // Type 35 'b' characters
- for (let i = 0; i < 35; i++) editor.handleInput("b");
- // Paste 27 lines
- const bigContent = "line\n".repeat(27).trimEnd();
- editor.handleInput(`\x1b[200~${bigContent}\x1b[201~`);
- // Type a few more characters
- for (let i = 0; i < 4; i++) editor.handleInput("b");
- // Move cursor left to land on the paste marker
- editor.handleInput("\x1b[D"); // past last 'b'
- editor.handleInput("\x1b[D"); // past last 'b'
- editor.handleInput("\x1b[D"); // past last 'b'
- editor.handleInput("\x1b[D"); // past last 'b'
- editor.handleInput("\x1b[D"); // now on the paste marker
- // Render at width 54 - should not throw
- const renderWidth = 54;
- const lines = editor.render(renderWidth);
- for (const line of lines) {
- assert.ok(
- visibleWidth(line) <= renderWidth,
- `line exceeds width ${renderWidth}: visible=${visibleWidth(line)} text=${JSON.stringify(line)}`,
- );
- }
- });
- it("wordWrapLine re-checks overflow after backtracking to wrap opportunity", () => {
- // Reproduce crash #2: " " + "b".repeat(35) + atomic_marker(20 chars) + "bbbb"
- // layoutWidth=53. After wrapping at the space, the remaining 35 b's + marker = 55
- // must trigger a second force-break instead of silently overflowing.
- const tui = createTestTUI();
- const editor = new Editor(tui, defaultEditorTheme);
- // Type a space, then 35 b's
- editor.handleInput(" ");
- for (let i = 0; i < 35; i++) editor.handleInput("b");
- // Paste 27 lines to create marker
- const bigContent = "line\n".repeat(27).trimEnd();
- editor.handleInput(`\x1b[200~${bigContent}\x1b[201~`);
- // Type trailing chars
- for (let i = 0; i < 4; i++) editor.handleInput("b");
- // Render at width 54 (contentWidth=54, layoutWidth=53 with paddingX=0)
- const renderWidth = 54;
- const lines = editor.render(renderWidth);
- for (const line of lines) {
- assert.ok(
- visibleWidth(line) <= renderWidth,
- `line exceeds width ${renderWidth}: visible=${visibleWidth(line)} text=${JSON.stringify(line)}`,
- );
- }
- });
- it("expands large pasted content literally in getExpandedText", () => {
- const editor = new Editor(createTestTUI(), defaultEditorTheme);
- const pastedText = [
- "line 1",
- "line 2",
- "line 3",
- "line 4",
- "line 5",
- "line 6",
- "line 7",
- "line 8",
- "line 9",
- "line 10",
- "tokens $1 $2 $& $$ $` $' end",
- ].join("\n");
- editor.handleInput(`\x1b[200~${pastedText}\x1b[201~`);
- assert.match(editor.getText(), /\[paste #\d+ \+\d+ lines\]/);
- assert.strictEqual(editor.getExpandedText(), pastedText);
- });
- it("snaps to the paste marker start when navigating down into it", () => {
- const editor = new Editor(createTestTUI(), defaultEditorTheme);
- // Line 0: long enough text to establish a sticky column
- editor.setText("12345678901234567890\n\nhello ");
- // Create a large paste to get a marker
- const bigContent = "x".repeat(2000);
- editor.handleInput(`\x1b[200~${bigContent}\x1b[201~`);
- editor.render(80);
- const text = editor.getText();
- const _marker = text.match(/\[paste #\d+ \d+ chars\]/)![0];
- // Line 0: "12345678901234567890"
- // Line 1: "" (empty)
- // Line 2: "hello [paste #1 2000 chars]"
- // marker starts at col 6
- // Navigate to line 0, col 10
- editor.handleInput("\x1b[A"); // Up to line 1
- editor.handleInput("\x1b[A"); // Up to line 0
- editor.handleInput("\x01"); // Ctrl+A (start of line)
- for (let i = 0; i < 10; i++) editor.handleInput("\x1b[C"); // Right 10
- assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 10 });
- // Down to empty line
- editor.handleInput("\x1b[B");
- assert.deepStrictEqual(editor.getCursor(), { line: 1, col: 0 });
- // Down to paste marker line - sticky col 10 falls inside marker (starts at col 6).
- // Cursor should snap to start of marker (col 6), not end (col 6 + marker.length).
- editor.handleInput("\x1b[B");
- assert.deepStrictEqual(editor.getCursor(), { line: 2, col: 6 });
- });
- it("preserves sticky column when navigating through paste marker line", () => {
- const tui = createTestTUI(30, 24);
- const editor = new Editor(tui, defaultEditorTheme);
- // Build:
- // Line 0: "1234567890123456" (16 chars)
- // Line 1: "" (empty)
- // Line 2: "[paste #1 2000 chars]" (22 chars, paste marker)
- // Line 3: "" (empty)
- // Line 4: "abcdefghijklmnop" (16 chars)
- for (const ch of "1234567890123456") editor.handleInput(ch);
- editor.handleInput("\n");
- editor.handleInput("\n");
- editor.handleInput(`\x1b[200~${"x".repeat(2000)}\x1b[201~`);
- editor.handleInput("\n");
- editor.handleInput("\n");
- for (const ch of "abcdefghijklmnop") editor.handleInput(ch);
- editor.render(30);
- // Navigate to line 0, col 10
- for (let i = 0; i < 4; i++) editor.handleInput("\x1b[A"); // Up to line 0
- editor.handleInput("\x01"); // Ctrl+A
- for (let i = 0; i < 10; i++) editor.handleInput("\x1b[C");
- assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 10 });
- // Down to empty line - sticky col 10 established
- editor.handleInput("\x1b[B");
- assert.deepStrictEqual(editor.getCursor(), { line: 1, col: 0 });
- // Down to paste marker - cursor snapped to col 0 (start of marker)
- editor.handleInput("\x1b[B");
- assert.deepStrictEqual(editor.getCursor(), { line: 2, col: 0 });
- // Down to empty line
- editor.handleInput("\x1b[B");
- assert.deepStrictEqual(editor.getCursor(), { line: 3, col: 0 });
- // Down to last line - should restore sticky col 10
- editor.handleInput("\x1b[B");
- assert.deepStrictEqual(editor.getCursor(), { line: 4, col: 10 });
- });
- it("does not get stuck moving down from a multi-visual-line paste marker", () => {
- const tui = createTestTUI(20, 24);
- const editor = new Editor(tui, defaultEditorTheme);
- // Build:
- // Logical line 0: "abcdefgh" + marker(21 chars) + "ijklmnopqr"
- // Logical line 1: "123456789012345678"
- //
- // Marker "[paste #1 +100 lines]" (21 chars) is wider than the
- // terminal (20). Word-wrap splits at the space before "lines",
- // producing:
- // VL1: abcdefgh (startCol 0, len 8)
- // VL2: [paste #1 +100 (startCol 8, len 15) <- marker head
- // VL3: lines]ijklmnopqr (startCol 23, len 16) <- marker tail + content
- // VL4: 123456789012345678 (line 1)
- //
- // On VL3 the marker tail "lines]" occupies visual cols 0-5.
- // Content ("i") starts at visual col 6 = logical col 29.
- for (const ch of "abcdefgh") editor.handleInput(ch);
- const bigContent = "line\n".repeat(100).trimEnd();
- editor.handleInput(`\x1b[200~${bigContent}\x1b[201~`);
- for (const ch of "ijklmnopqr") editor.handleInput(ch);
- editor.handleInput("\n");
- for (const ch of "123456789012345678") editor.handleInput(ch);
- editor.render(20);
- const text = editor.getText();
- const markerMatch = text.match(/\[paste #\d+ \+\d+ lines]/);
- assert.ok(markerMatch, "paste marker should be created");
- const markerLen = markerMatch[0].length; // 21
- assert.ok(markerLen > 20, "marker should be wider than terminal");
- const markerStart = 8;
- const markerEnd = markerStart + markerLen; // 29
- // Navigate to line 0, col 6 (on "g"). Preferred col 6 is past the
- // marker tail on VL3, so the cursor should land on content ("i" at
- // col 29) without snapping back.
- editor.handleInput("\x1b[A"); // Up to line 0
- editor.handleInput("\x01"); // Ctrl+A (start of line)
- for (let i = 0; i < 6; i++) editor.handleInput("\x1b[C"); // Right to col 6
- assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 6 });
- // Down: cursor lands on paste marker start
- editor.handleInput("\x1b[B");
- assert.deepStrictEqual(editor.getCursor(), { line: 0, col: markerStart });
- // Down again: preferred col 6 lands at VL3 col 29 ("i"), which is
- // past the marker. Cursor stays on line 0.
- editor.handleInput("\x1b[B");
- assert.strictEqual(editor.getCursor().line, 0);
- assert.strictEqual(editor.getCursor().col, markerEnd); // col 29 = "i"
- // Up: back to paste marker
- editor.handleInput("\x1b[A");
- assert.deepStrictEqual(editor.getCursor(), { line: 0, col: markerStart });
- // Up again: back to col 6 ("g")
- editor.handleInput("\x1b[A");
- assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 6 });
- });
- it("skips marker continuation VLs when preferred col falls in marker tail", () => {
- const tui = createTestTUI(20, 24);
- const editor = new Editor(tui, defaultEditorTheme);
- // Same layout. Start at col 3 ("d"). Preferred col 3 maps to VL3
- // visual col 3 which is inside the "lines]" marker tail.
- // moveToVisualLine detects the continuation VL and skips to VL4
- // (line 1).
- // VL1: abcdefgh (startCol 0, len 8)
- // VL2: [paste #1 +100 (startCol 8, len 15) <- marker head
- // VL3: lines]ijklmnopqr (startCol 23, len 16) <- marker tail + content
- // VL4: 123456789012345678 (line 1)
- for (const ch of "abcdefgh") editor.handleInput(ch);
- const bigContent = "line\n".repeat(100).trimEnd();
- editor.handleInput(`\x1b[200~${bigContent}\x1b[201~`);
- for (const ch of "ijklmnopqr") editor.handleInput(ch);
- editor.handleInput("\n");
- for (const ch of "123456789012345678") editor.handleInput(ch);
- editor.render(20);
- // Navigate to line 0, col 3 (on "d")
- editor.handleInput("\x1b[A"); // Up to line 0
- editor.handleInput("\x01"); // Ctrl+A
- for (let i = 0; i < 3; i++) editor.handleInput("\x1b[C");
- assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 3 });
- // Down: marker
- editor.handleInput("\x1b[B");
- assert.strictEqual(editor.getCursor().col, 8);
- // Down: skips VL3 (col 3 in marker tail) and lands on line 1
- editor.handleInput("\x1b[B");
- assert.deepStrictEqual(editor.getCursor(), { line: 1, col: 3 });
- // Round-trip back
- editor.handleInput("\x1b[A");
- assert.strictEqual(editor.getCursor().col, 8); // marker
- editor.handleInput("\x1b[A");
- assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 3 });
- });
- it("submits large pasted content literally", () => {
- const editor = new Editor(createTestTUI(), defaultEditorTheme);
- const pastedText = [
- "line 1",
- "line 2",
- "line 3",
- "line 4",
- "line 5",
- "line 6",
- "line 7",
- "line 8",
- "line 9",
- "line 10",
- "tokens $1 $2 $& $$ $` $' end",
- ].join("\n");
- let submitted = "";
- editor.onSubmit = (text) => {
- submitted = text;
- };
- editor.handleInput(`\x1b[200~${pastedText}\x1b[201~`);
- editor.handleInput("\r");
- assert.strictEqual(submitted, pastedText);
- });
- });
- });
|