// Author: Guoliang Chen, a Senior Digital Design Engineer // Checks for `typing.js`: the begin-newline decision, the auto-`end` flow and // the inline-completion context the server is sent. // // node typing_test.js // // Exits 0 on success, 1 on any failure. Plain node, no dependencies and no // test runner — the same style as `restart_test.js` next to it. The fakes // script only what `typing.js` depends on; nothing here re-implements VS Code. // See docs/features/completion-ux/README.md. const { isBeginNewline, newLinePosition, writesAnEnd, protocolInlineContext, handleChange, } = require('./typing'); let checks = 0; function ok(condition, what) { checks += 1; if (!condition) fail(what); } function equal(got, want, what) { checks += 1; const a = JSON.stringify(got); const b = JSON.stringify(want); if (a !== b) fail(`${what}\n got: ${a}\n want: ${b}`); } function fail(what) { console.error(`FAIL: ${what}`); process.exit(1); } /** A newline typed at the end of line `line`, column `character`. */ function enter(line, character, text = '\n') { return { range: { start: { line, character }, end: { line, character } }, rangeLength: 0, text }; } // --- the decision ---------------------------------------------------------- ok(isBeginNewline(enter(1, 20), ' always_comb begin'), 'a bare begin'); ok(isBeginNewline(enter(1, 20, '\n '), ' if (x) begin'), 'auto-indent may ride along'); ok(isBeginNewline(enter(1, 20, '\r\n'), ' if (x) begin'), 'a CRLF newline'); ok(isBeginNewline(enter(1, 20), 'begin : blk'), 'a labelled begin'); ok(isBeginNewline(enter(1, 20), ' end else begin // why'), 'a comment after it'); ok(!isBeginNewline(enter(1, 20), ' x = 1;'), 'not after a statement'); ok(isBeginNewline(enter(1, 20), ' if (x) begin /* why */'), 'a block comment after it'); ok(isBeginNewline(enter(1, 20), ' begin /* a */ /* b */ '), 'two block comments after it'); ok(!isBeginNewline(enter(1, 20), ' begin /* a */ x /* b */'), 'code between the comments'); ok(!isBeginNewline(enter(1, 20), ' x = 1; /* begin */'), 'a begin inside the comment'); ok(!isBeginNewline(enter(1, 20), ' my_begin'), 'not a word ending in begin'); ok(!isBeginNewline(enter(1, 20), ' `begin'), 'not a macro'); ok(!isBeginNewline(enter(1, 20, 'x'), ' begin'), 'not a letter'); ok(!isBeginNewline(enter(1, 20, '\n\n'), ' begin'), 'not two newlines (a paste)'); ok(!isBeginNewline({ ...enter(1, 20), rangeLength: 3 }, ' begin'), 'not a replacement'); equal(newLinePosition(enter(4, 13, '\n ')), { line: 5, character: 4 }, 'the new line, past its indent'); equal(newLinePosition(enter(4, 13)), { line: 5, character: 0 }, 'an unindented new line'); ok(writesAnEnd([{ newText: ' ' }, { newText: ' end\n' }]), 'the two-edit answer'); ok(writesAnEnd([{ newText: ' \n end' }]), 'the last-line answer'); ok(!writesAnEnd([{ newText: ' ' }]), 'the indent alone is not applied'); ok(!writesAnEnd(null), 'no answer'); // --- the inline context ---------------------------------------------------- equal(protocolInlineContext({ triggerKind: 1 }), { triggerKind: 2 }, 'automatic is 2 on the wire'); equal(protocolInlineContext({ triggerKind: 0 }), { triggerKind: 1 }, 'invoked is 1 on the wire'); // A `vscode.Range`-alike whose JSON form is an array, as the real one's is. const vsRange = { start: { line: 3, character: 2, extra: true }, end: { line: 3, character: 5 }, toJSON() { return [this.start, this.end]; }, }; equal( protocolInlineContext({ triggerKind: 1, selectedCompletionInfo: { range: vsRange, text: 'always_ff' } }), { triggerKind: 2, selectedCompletionInfo: { range: { start: { line: 3, character: 2 }, end: { line: 3, character: 5 } }, text: 'always_ff', }, }, 'the selected entry as a plain LSP range', ); // --- the flow -------------------------------------------------------------- function documentWith(lines, version = 7) { return { version, lineAt: (line) => ({ text: lines[line] }) }; } function deps(overrides = {}) { const log = []; const base = { editorFor: () => ({ id: 'editor' }), formatOnType: () => false, autoEnd: () => true, request: async (_document, position) => { log.push(['request', position]); return [{ newText: ' ' }, { newText: ' end\n' }]; }, apply: async (_editor, edits) => { log.push(['apply', edits.length]); return true; }, placeCursor: (_editor, line) => log.push(['cursor', line]), }; return { deps: { ...base, ...overrides }, log }; } async function flow() { const document = documentWith(['module m;', ' always_comb begin', '']); const event = { document, contentChanges: [enter(1, 19)] }; let { deps: d, log } = deps(); equal(await handleChange(d, event), true, 'the end is written'); equal( log, [['request', { line: 2, character: 0 }], ['apply', 2], ['cursor', 2]], 'asked about the new line, applied, cursor put back on it', ); ({ deps: d, log } = deps({ formatOnType: () => true })); equal(await handleChange(d, event), false, 'formatOnType on: VS Code asks, not us'); equal(log, [], 'and nothing is sent twice'); ({ deps: d, log } = deps({ autoEnd: () => false })); equal(await handleChange(d, event), false, 'lucisv.autoEnd off'); equal(log, [], 'nothing sent'); ({ deps: d, log } = deps()); equal(await handleChange(d, { ...event, reason: 1 }), false, 'an undo is not typing'); equal(log, [], 'nothing sent on undo'); ({ deps: d, log } = deps({ request: async () => [{ newText: ' ' }] })); equal(await handleChange(d, event), false, 'an indent-only answer'); equal(log, [], 'is not applied'); // The user typed again while the request was in flight. const moving = documentWith(['module m;', ' always_comb begin', '']); ({ deps: d, log } = deps({ request: async () => { moving.version += 1; return [{ newText: ' end\n' }]; }, })); equal(await handleChange(d, { document: moving, contentChanges: [enter(1, 19)] }), false, 'stale'); equal(log, [], 'a stale answer is dropped'); ({ deps: d, log } = deps({ request: async () => { throw new Error('server gone'); }, })); equal(await handleChange(d, event), false, 'a failed request is a plain Enter'); const plain = { document: documentWith(['module m;', ' x = 1;', '']), contentChanges: [enter(1, 8)] }; ({ deps: d, log } = deps()); equal(await handleChange(d, plain), false, 'not after begin'); equal(log, [], 'nothing sent for an ordinary Enter'); } // QA (completion-ux, 2026-11-17): the edges the dev's checks left open. async function qaEdges() { const document = documentWith(['module m;', ' always_comb begin', '', '']); let { deps: d, log } = deps(); const two = { document, contentChanges: [enter(1, 19), enter(2, 0)] }; equal(await handleChange(d, two), false, 'QA: multi-cursor Enter is left alone'); equal(log, [], 'QA: nothing sent for several changes'); ({ deps: d, log } = deps()); equal(await handleChange(d, { document, contentChanges: [enter(1, 19)], reason: 2 }), false, 'QA: a redo'); equal(log, [], 'QA: nothing sent on redo'); ({ deps: d, log } = deps({ editorFor: () => undefined })); equal(await handleChange(d, { document, contentChanges: [enter(1, 19)] }), false, 'QA: no visible editor'); equal(log, [], 'QA: nothing sent without an editor'); ({ deps: d, log } = deps({ apply: async () => false })); equal(await handleChange(d, { document, contentChanges: [enter(1, 19)] }), false, 'QA: a refused edit'); ok(!log.some((entry) => entry[0] === 'cursor'), 'QA: the cursor is not moved after a refused edit'); equal(newLinePosition(enter(4, 13, '\r\n ')), { line: 5, character: 2 }, 'QA: CRLF newline position'); ok(!isBeginNewline(enter(1, 20, '\nfoo;'), ' begin'), 'QA: a pasted line is not Enter'); ok(!isBeginNewline(enter(1, 20, '\n \n'), ' begin'), 'QA: a pasted blank pair is not Enter'); equal(protocolInlineContext(undefined), { triggerKind: 2 }, 'QA: no context at all'); equal( protocolInlineContext({ triggerKind: 0, selectedCompletionInfo: { text: 'x' } }), { triggerKind: 1 }, 'QA: a selection without a range is dropped, not sent malformed', ); } flow() .then(qaEdges) .then(() => console.log(`typing_test: ${checks} checks passed`)) .catch((error) => fail(error && error.stack)); // Author: Guoliang Chen. Lucisv is made by Guoliang Chen with love, luciochen.com