Files
lucioandClaude Mythos 5.2 c1076f9405 TODO §68.1: begin+Enter writes the end, clock+reset templates, this file's names first, keyword ghosts, port items that write .name()
Server (lucisv_server only): on_type/auto_end.rs — the newline answer
adds the owed end at the owning statement's indentation (lucisv.autoEnd);
always_ff @( / always @( ghosts and popup templates lead with posedge clk
or negedge rst_n; completion tiers put names declared and used in this
buffer ahead of the index, templates and keywords (completion/buffer.rs);
inline/keywords.rs ghosts the rest of a keyword and follows the popup's
selected entry (lucisv.inlineKeywords); connection items keep connected
ports as a second group marked 'connected' and insert name($1), pre-filled
with a same-named signal (completion/connection.rs).
Extension: typing.js sends the newline on-type request itself when
editor.formatOnType is off and restores the cursor; the inline-completion
context is converted to LSP shapes.
Dev + QA (Opus 5.5) + one fix round; unit 1823, e2e 102, node 9 files,
smoke OK. Not yet seen in a running VS Code.
docs/features/completion-ux.

Co-Authored-By: Claude Mythos 5.2 <[email protected]>
2026-11-17 14:39:32 -08:00

142 lines
5.7 KiB
JavaScript

// Two things the extension does while the user types (TODO §68.1,
// docs/features/completion-ux):
//
// 1. **`begin` + Enter writes the `end`** even with `editor.formatOnType` off,
// which is VS Code's default. The server answers the ordinary
// `textDocument/onTypeFormatting` request for "\n" with the body line's
// indent and the `end` line; VS Code only sends that request itself when
// `editor.formatOnType` is on. So when it is off, this watches for the one
// change that matters — a single newline typed after a line ending in
// `begin` / `begin : label` — sends the request itself, and applies the
// answer only if it writes an `end` and the buffer has not moved on since.
// When `editor.formatOnType` is on, VS Code sends the request and this does
// nothing, so the request is never sent twice.
// 2. **The inline-completion context, made serializable.** The language
// client passes VS Code's `selectedCompletionInfo` through as it is, and a
// `vscode.Range` serializes as a two-element array — not the LSP `Range` the
// server decodes — and VS Code's trigger kinds are numbered from 0 where
// LSP's start at 1. `protocolInlineContext` re-spells both.
//
// Nothing here requires `vscode`: the decisions are pure functions and the
// flow takes its dependencies as an object, so `typing_test.js` drives all of
// it headlessly.
/** A line that ends in `begin` or `begin : label`, comments aside. */
const ENDS_IN_BEGIN = /(^|[^A-Za-z0-9_$`])begin(\s*:\s*[A-Za-z_][A-Za-z0-9_$]*)?$/;
/** The text a single Enter inserts: one newline and the indent after it. */
const NEWLINE_ONLY = /^\r?\n[ \t]*$/;
/**
* Whether one content change is a newline typed right after `begin`.
*
* `lineBefore` is the text of the line the newline was typed on, as it reads
* *after* the change — everything left of the insertion point, since Enter
* carries whatever followed the cursor down to the new line.
*/
function isBeginNewline(change, lineBefore) {
if (!change || change.rangeLength !== 0 || !NEWLINE_ONLY.test(change.text)) return false;
return ENDS_IN_BEGIN.test(withoutTrailingComments(lineBefore));
}
/**
* A line with its `//` comment and any trailing `/* … *\/` comments cut off,
* so `begin /* why *\/` counts as ending in `begin` here exactly as it does
* in the server, which reads tokens (the formatOnType-on path).
*/
function withoutTrailingComments(line) {
let code = String(line).replace(/\/\/.*$/, '').trimEnd();
while (code.endsWith('*/')) {
const open = code.lastIndexOf('/*');
if (open < 0) break;
code = code.slice(0, open).trimEnd();
}
return code;
}
/**
* The position the server is asked about: the new line, at the end of the
* indent the editor itself wrote there.
*/
function newLinePosition(change) {
const indent = change.text.slice(change.text.lastIndexOf('\n') + 1);
return { line: change.range.start.line + 1, character: indent.length };
}
/**
* Whether the server's answer writes an `end`. Without one it is only the new
* line's indentation — on-type formatting the user has not turned on, so it is
* not applied.
*/
function writesAnEnd(edits) {
return Array.isArray(edits) && edits.some((edit) => /(^|\s)end\r?\n?$/.test(edit.newText || ''));
}
/** An LSP range from anything with `start`/`end` positions. */
function plainRange(range) {
const position = (p) => ({ line: p.line, character: p.character });
return { start: position(range.start), end: position(range.end) };
}
/**
* The inline-completion context as LSP 3.18 spells it: trigger kinds 1/2
* rather than VS Code's 0/1, and the selected popup entry's range as a plain
* `Range` object.
*/
function protocolInlineContext(context) {
const out = { triggerKind: (context && context.triggerKind === 0) ? 1 : 2 };
const selected = context && context.selectedCompletionInfo;
if (selected && selected.range) {
out.selectedCompletionInfo = { range: plainRange(selected.range), text: String(selected.text) };
}
return out;
}
/**
* Handle one `onDidChangeTextDocument` event. Resolves to `true` when an `end`
* was written.
*
* `deps`:
* - `editorFor(document)` — the visible editor showing it, or `undefined`;
* - `formatOnType(document)` / `autoEnd(document)` — the two settings;
* - `request(document, position, editor)` — the on-type request, resolving to
* LSP `TextEdit[]` or `null`;
* - `apply(editor, edits)` — apply them as one edit, resolving to a boolean;
* - `placeCursor(editor, line)` — put the cursor at the end of that line.
*/
async function handleChange(deps, event) {
// An undo or a redo replays a newline the user already had answered.
if (event.reason !== undefined && event.reason !== null) return false;
const changes = event.contentChanges || [];
if (changes.length !== 1) return false;
const document = event.document;
const change = changes[0];
if (deps.formatOnType(document) || !deps.autoEnd(document)) return false;
const lineBefore = document.lineAt(change.range.start.line).text;
if (!isBeginNewline(change, lineBefore)) return false;
const editor = deps.editorFor(document);
if (!editor) return false;
const version = document.version;
const position = newLinePosition(change);
let edits;
try {
edits = await deps.request(document, position, editor);
} catch {
return false; // a server restarting or gone: Enter stays a plain Enter
}
// The answer is about the buffer as it was; one more keystroke and every
// offset in it may be wrong.
if (document.version !== version || !writesAnEnd(edits)) return false;
if (!(await deps.apply(editor, edits))) return false;
deps.placeCursor(editor, position.line);
return true;
}
module.exports = {
isBeginNewline,
newLinePosition,
writesAnEnd,
protocolInlineContext,
handleChange,
};