feature: Add webview to display the checklist in an editor tab

This commit is contained in:
Keith Solomon
2025-06-22 16:48:18 -05:00
parent 6e0405b192
commit 61ea6fab6b
3 changed files with 101 additions and 37 deletions

View File

@@ -17,44 +17,21 @@ export function activate(context: vscode.ExtensionContext) {
const doc = vscode.workspace.textDocuments.find(d => d.uri.fsPath === checklistFile);
if (!doc) { return; }
const editor = await vscode.window.showTextDocument(doc, { preview: false });
const lines = doc.getText().split('\n');
const labelRegex = new RegExp(`[-*]\\s+\\[(${item.checked ? 'x' : ' '})\\]\\s+${escapeRegex(item.label)}$`);
const matchIndex = lines.findIndex(line => labelRegex.test(line.trim()));
if (matchIndex === -1) {
vscode.window.showWarningMessage(`Could not find task "${item.label}" in the file.`);
return;
}
const line = lines[matchIndex];
const newCheck = item.checked ? '[ ]' : '[x]';
const newLine = line.replace(/\[( |x)\]/, newCheck);
const edit = new vscode.WorkspaceEdit();
const uri = doc.uri;
const lines = doc.getText().split('\n');
const index = lines.findIndex(line =>
line.trim().match(/^[-*]\s+\[[ xX]\]/) &&
line.includes(item.label)
);
if (index === -1) { return; }
edit.replace(uri, doc.lineAt(matchIndex).range, newLine);
// If parent, toggle all children too
if (item.children.length > 0) {
const indentLevel = line.match(/^(\s*)/)?.[1].length || 0;
for (let i = matchIndex + 1; i < lines.length; i++) {
const thisLine = lines[i];
const currentIndent = thisLine.match(/^(\s*)/)?.[1].length || 0;
if (currentIndent <= indentLevel) { break; }
if (/^\s*[-*]\s+\[( |x)\]/.test(thisLine)) {
const toggled = thisLine.replace(/\[( |x)\]/, newCheck);
edit.replace(uri, doc.lineAt(i).range, toggled);
}
}
}
const line = lines[index];
const toggledLine = line.replace(/\[(x| )\]/i, item.checked ? '[ ]' : '[x]');
const range = new vscode.Range(new vscode.Position(index, 0), new vscode.Position(index, line.length));
edit.replace(doc.uri, range, toggledLine);
await vscode.workspace.applyEdit(edit);
await doc.save();
await doc.save(); // ✅ Triggers the existing onDidSaveTextDocument → refresh()
}),
vscode.commands.registerCommand('roadmapChecklist.reveal', async (item: RoadmapItem) => {
treeView.reveal(item, { expand: true });