p-doom/crowd-code-dataset-0.2 huggingface dataset:
[content] src/extension.ts (typescript)
	switch (parsed.kind) {
		case 'showTextDocument':
			return { kind: 'showTextDocument' };
		case 'setSelections': {
			const selections = Array.isArray(parsed.selections) ? parsed.selections : [];
			const norm = selections.map((s: any) => ({
				start: Array.isArray(s?.start) && s.start.length === 2 ? [Number(s.start[0]) || 0, Number(s.start[1]) || 0] as [number, number] : [0, 0] as [number, number],
				end: Array.isArray(s?.end) && s.end.length === 2 ? [Number(s.end[0]) || 0, Number(s.end[1]) || 0] as [number, number] : [0, 0] as [number, number]
			}));
			return { kind: 'setSelections', selections: norm };
		}
		case 'editInsert': {
			const pos = Array.isArray(parsed.position) && parsed.position.length === 2 ? [Number(parsed.position[0]) || 0, Number(parsed.position[1]) || 0] as [number, number] : [0, 0] as [number, number];
			const textVal = typeof parsed.text === 'string' ? parsed.text : '';
			return { kind: 'editInsert', position: pos, text: textVal };
		}
		case 'editDelete': {
			const start = Array.isArray(parsed.range?.start) && parsed.range.start.length === 2 ? [Number(parsed.range.start[0]) || 0, Number(parsed.range.start[1]) || 0] as [number, number] : [0, 0] as [number, number];
			const end = Array.isArray(parsed.range?.end) && parsed.range.end.length === 2 ? [Number(parsed.range.end[0]) || 0, Number(parsed.range.end[1]) || 0] as [number, number] : [0, 0] as [number, number];
			return { kind: 'editDelete', range: { start, end } };
		}
		case 'editReplace': {
			const start = Array.isArray(parsed.range?.start) && parsed.range.start.length === 2 ? [Number(parsed.range.start[0]) || 0, Number(parsed.range.start[1]) || 0] as [number, number] : [0, 0] as [number, number];
			const end = Array.isArray(parsed.range?.end) && parsed.range.end.length === 2 ? [Number(parsed.range.end[0]) || 0, Number(parsed.range.end[1]) || 0] as [number, number] : [0, 0] as [number, number];
			const textVal = typeof parsed.text === 'string' ? parsed.text : '';
			return { kind: 'editReplace', range: { start, end }, text: textVal };
		}
		case 'terminalShow':
			return { kind: 'terminalShow' };
		case 'terminalSendText': {
			const textVal = typeof parsed.text === 'string' ? parsed.text : '';
			return { kind: 'terminalSendText', text: textVal };
		}
		default:
			return undefined;
[content] src/extension.ts (typescript)
function parsePlannedAction(raw: string): PlannedAction | undefined {
	let text = raw.trim();
	text = text.replace(/^```(?:json)?\s*/i, '').replace(/```\s*$/i, '').trim();
	text = text.replace(/<think>[\s\S]*?<\/think>/gi, '').trim();
	let parsed: any;
	try {
		parsed = JSON.parse(text);
	} catch (err) {
[content] src/extension.ts (typescript)
	const tabbingPrompt = [
		'Your role: Propose the single next edit action (according to the schema) that most helps the developer progress their work.',
		'',
		'Context:',
		`- File: ${doc.fileName}`,
		`- Language: ${doc.languageId}`,
		`- Cursor: (${cursor.line}, ${cursor.character}) [zero-based line, column]`,
		'',
		'Full file content with zero-based line numbers:',
		'```',
		numberedContext,
		'```',
		'',
		'Instructions:',
		'- Use the schema from the system prompt to produce exactly ONE "editReplace" action.',
		'- Choose the range so that it corresponds to the next likely edit the developer intends to make,',
		'  based on the cursor position and surrounding code.',
		'- If appropriate, complete the current token, expression, statement, or block.',
		'- Avoid duplicating code that already exists later in the file unless replacing it.',
		'',
		'Respond with ONLY a single JSON object representing the action, with no extra text.'
[content] src/extension.ts (typescript)
	const schemaDescription = [
		'Role: You are an AI edit predictor for a code editor (VS Code-compatible).',
		'Your job is to suggest the single next edit that most helps the developer progress their current task.',
		'',
		'Output format:',
		'- Output ONLY a single JSON object (no explanations, no prose, no comments, no code fences).',
		'- The JSON must be valid and parseable.',
		'- Use double-quoted JSON strings.',
		'',
		'Coordinates:',
		'- All coordinates are zero-based [line, column].',
		'',
		'Allowed actions (JSON schema-like):',
		'{',
		'	kind: "editInsert",',
		'	position: [number, number],',
		' 	text: string',
	 	'}',
		'{',
		'  "kind": "editDelete",',
		'  "range": {',
		'    "start": [number, number],',
		'    "end": [number, number]',
		'  }',
		'}',
		'{',
		'  "kind": "editReplace",',
		'  "range": {',
		'    "start": [number, number],',
		'    "end": [number, number]',
		'  },',
		'  "text": string',
		'}',
		'',
		'Editing guidelines:',
		'- Suggest exactly ONE action per response.',
		'- Prefer small, high-confidence edits over large or risky rewrites.',
		'- If you insert or complete code, extend until the logical end of the current statement or block,',
		'  but avoid editing more than necessary to be useful.',
		'- When inserting text, do NOT repeat existing code that is already present after the cursor,',
		'  unless you are intentionally replacing it via the range.',
		'- Keep the file syntactically valid in the given language whenever reasonable.',
		'- Do NOT delete large unrelated sections of the file.',
		'- If no useful edit is clear, propose a no-op by setting start == end at the cursor and an empty text ("").'
	].join('
');
[content] src/extension.ts (typescript)
function parsePlannedAction(raw: string, _doc?: vscode.TextDocument): PlannedAction | undefined {
	const command = extractBashCommand(raw);
	if (!command) {
		return undefined;
	}
	const normalized = command.replace(/<think>[\s\S]*?<\/think>/gi, '').trim();
	if (!normalized) {
		return undefined;
	}
	return { kind: 'terminalSendText', text: normalized };
}

function extractBashCommand(raw: string): string | undefined {
	if (!raw) {
		return undefined;
	}
	const trimmed = raw.trim();
	const fenceMatch = trimmed.match(/```(?:bash)?\s*([\s\S]*?)```/i);
	if (fenceMatch && fenceMatch[1]) {
		return fenceMatch[1];
	}
	// Fallback: treat entire response as the command
	return trimmed.length > 0 ? trimmed : undefined;
}
[content] src/extension.ts (typescript)
	const conversationMessages: Array<{ role: 'system' | 'user' | 'assistant'; content: string }> = [
		{ role: 'system', content: systemPrompt },
		{ role: 'assistant', content: fencedBashBlock(`cat -n ${filePath}`) },
		{ role: 'user', content: formatStdoutBlock(formatLineNumberedOutput(fullText)) }
	];

	if (viewport.end >= viewport.start) {
		const viewportOutput = formatLineNumberedOutput(fullText, viewport.start, viewport.end);
		conversationMessages.push(
			{ role: 'assistant', content: fencedBashBlock(`cat -n ${filePath} | sed -n '${viewport.start},${viewport.end}p'`) },
			{ role: 'user', content: formatStdoutBlock(viewportOutput) }
		);
	}
[content] src/extension.ts (typescript)
	const systemPrompt = [
		'You are an AI pair-programmer operating via bash inside VS Code.',
		'Assistant messages are shell commands, and User messages are their stdout.',
		'Emit exactly one bash command wrapped in ```bash``` with no prose or stdout of your own.',
		`Workspace root: ${workspaceRoot}`,
		`Active file: ${filePath}`,
		`Language: ${doc.languageId}`,
		`Cursor (1-based): line ${cursorLine}, column ${cursorColumn}`,
		'Use absolute paths in every command.',
		'When editing files, prefer sed -i or apply_patch, then show the updated viewport with `&& cat -n <file> | sed -n \'start,endp\'`.',
		'Keep edits minimal, targeted near the cursor, and aligned with the displayed context.',
		'Do not include explanations or additional text outside the bash fence.'
[content] src/extension.ts (typescript)
	const filePath = doc.uri.fsPath;
	const workspaceRoot = vscode.workspace.workspaceFolders?.[0]?.uri.fsPath ?? '(unknown)';
	const cursorLine = cursor.line + 1;
	const cursorColumn = cursor.character + 1;
	const totalLines = doc.lineCount;
	const viewport = computeViewport(totalLines, cursorLine, 12);
[content] src/extension.ts (typescript)
// -------------------- Prompt Serialization Helpers --------------------
function formatStdoutBlock(content: string): string {
	const normalized = content ?? '';
	return `<stdout>
${normalized}
</stdout>`;
}

function formatLineNumberedOutput(content: string, startLine?: number, endLine?: number): string {
	const lines = content.split(/
?
/);
	const total = (lines.length === 1 && lines[0] === '') ? 0 : lines.length;
	if (total === 0) {
		return '';
	}
	const s = startLine !== undefined ? Math.max(1, Math.min(startLine, total)) : 1;
	const e = endLine !== undefined ? Math.max(s, Math.min(endLine, total)) : total;
	const buf: string[] = [];
	for (let idx = s; idx <= e; idx++) {
		const lineText = lines[idx - 1] ?? '';
		buf.push(`${idx.toString().padStart(6, ' ')}	${lineText}`);
	}
	return buf.join('
');
}

function computeViewport(totalLines: number, centerLine: number, radius: number): { start: number; end: number } {
	if (totalLines <= 0) {
		return { start: 1, end: 0 };
	}
	const start = Math.max(1, centerLine - radius);
	const end = Math.min(totalLines, centerLine + radius);
	return { start, end };
}

function fencedBashBlock(command: string): string {
	const cleaned = command.replace(/
/g, '').trim();
	return `\`\`\`bash
${cleaned}
\`\`\``;
}
[content] src/extension.ts (typescript)
	return { kind: 'terminalSendText', text: normalized };
}

function extractBashCommand(raw: string): string | undefined {
	if (!raw) {
		return undefined;
	}
	const trimmed = raw.trim();
	const fenceMatch = trimmed.match(/```(?:bash)?\s*([\s\S]*?)```/i);
	if (fenceMatch && fenceMatch[1]) {
		return fenceMatch[1];
	}
	// Fallback: treat entire response as the command
	return trimmed.length > 0 ? trimmed : undefined;
}
[content] src/extension.ts (typescript)
		'You are a helpful assistant that can interact multiple times with a computer shell to solve programming tasks.',
		'Your response must contain exactly ONE bash code block with ONE command (or commands connected with && or ||).',
		'',
		'Format your response as shown in <format_example>.',
		'',
		'<format_example>',
		'```bash',
		'your_command_here',
		'```',
		'</format_example>',
		'',
		'Failure to follow these rules will cause your response to be rejected.'
[content] src/extension.ts (typescript)
	const metadataSummary = [
		`Workspace root: ${workspaceRoot}`,
		`Active file: ${filePath}`,
		`Language: ${doc.languageId}`,
		`Cursor (1-based): line ${cursorLine}, column ${cursorColumn}`
	].join('
');
	const metadataCommand = [
		"cat <<'EOF'",
		metadataSummary,
		'EOF'
	].join('
');
[content] src/extension.ts (typescript)
				textDecoration: `none; display: inline-block; white-space: pre; content: "${label.replace(/"/g, '\\"')}"; border: 1px solid var(--vscode-charts-purple); padding: 4px; border-radius: 4px; box-shadow: 0 4px 8px rgba(0,0,0,0.25); pointer-events: none; position: relative; z-index: 100; vertical-align: top;`
[content] src/extension.ts (typescript)
		const summary = trimText(next.text || '');
		const label = summary
			? `↳ Execute shell command in terminal: ${summary}`
			: '↳ Execute shell command in terminal';
		const margin = getDynamicMargin(editor, cursor.line, label);
[content] src/extension.ts (typescript)
				textDecoration: `none; display: inline-block; white-space: pre; content: "↳ Execute in Terminal:\\A ${cmd}"; border: 1px solid var(--vscode-charts-purple); padding: 4px; border-radius: 4px; box-shadow: 0 4px 8px rgba(0,0,0,0.25); pointer-events: none; position: relative; z-index: 100; vertical-align: top;`
[content] src/extension.ts (typescript)
/**
 * Parse viewport / selection commands of the form:
 *   cat -n <file> | sed -n 'START,ENDp'
 *
 * into a lightweight VS Code selection move (setSelections). This mirrors how
 * selection and viewport events are serialized in serialization_utils.py.
 */
function parseViewportFromCatCommand(command: string, doc: vscode.TextDocument): PlannedAction | undefined {
	const main = command.split(/&&|\|\|/)[0]?.trim() ?? '';
	if (!main) {
		return undefined;
	}

	// Simple file-open: cat -n <file>
	const simpleCatMatch = main.match(/^cat\s+-n\s+([^\s|]+)\s*$/);
	if (simpleCatMatch) {
		const targetFile = simpleCatMatch[1] ?? '';
		if (targetFile !== doc.uri.fsPath) {
			return undefined;
		}
		// Ensure the active document is visible; rely on existing editor to handle this.
		return { kind: 'showTextDocument' };
	}

	// Viewport slice: cat -n <file> | sed -n 'START,ENDp'
	const viewportMatch = main.match(/^cat\s+-n\s+([^\s|]+)\s*\|\s*sed\s+-n\s+'(\d+),(\d+)p'\s*$/);
	if (!viewportMatch) {
		return undefined;
	}

	const targetFile = viewportMatch[1] ?? '';
	const startStr = viewportMatch[2] ?? '';
	const endStr = viewportMatch[3] ?? '';

	if (targetFile !== doc.uri.fsPath) {
		return undefined;
	}

	const startLine1 = Number(startStr);
	const endLine1 = Number(endStr);
	if (!Number.isFinite(startLine1) || !Number.isFinite(endLine1)) {
		return undefined;
	}

	// Place the cursor in the middle of the viewport (1-based to 0-based).
	const center1 = Math.floor((startLine1 + endLine1) / 2);
	const center0 = Math.max(0, center1 - 1);
	const lastLine = Math.max(0, doc.lineCount - 1);
	const line = Math.min(center0, lastLine);
	const endChar = doc.lineAt(line).range.end.character;

	return {
		kind: 'setSelections',
		selections: [
			{
				start: [line, endChar],
				end: [line, endChar],
			},
		],
	};
}
[content] src/extension.ts (typescript)
		// 2) Viewport / selection moves encoded as cat -n ... | sed -n 'vstart,vendp'
		const viewportAction = parseViewportFromCatCommand(normalized, doc);
		if (viewportAction) {
			return viewportAction;
		}