Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 39 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,22 @@
{
"command": "claude-code-extension.focusInput",
"title": "Focus on Claude Code Input"
},
{
"command": "claude-code-extension.explainFile",
"title": "Explain with Claude Code"
},
{
"command": "claude-code-extension.explainFolder",
"title": "Explain Folder with Claude Code"
},
{
"command": "claude-code-extension.explainSelection",
"title": "Explain Selection with Claude Code"
},
{
"command": "claude-code-extension.explainCurrentFile",
"title": "Explain File with Claude Code"
}
],
"viewsContainers": {
Expand Down Expand Up @@ -99,7 +115,29 @@
{
"command": "claude-code-extension.addSelectionToInput",
"when": "editorHasSelection",
"group": "navigation"
"group": "claude@1"
},
{
"command": "claude-code-extension.explainSelection",
"when": "editorHasSelection",
"group": "claude@2"
},
{
"command": "claude-code-extension.explainCurrentFile",
"when": "!editorHasSelection",
"group": "claude@1"
}
],
"explorer/context": [
{
"command": "claude-code-extension.explainFile",
"when": "!explorerResourceIsFolder",
"group": "claude@1"
},
{
"command": "claude-code-extension.explainFolder",
"when": "explorerResourceIsFolder",
"group": "claude@1"
}
]
},
Expand Down
235 changes: 235 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,241 @@ export async function activate(context: vscode.ExtensionContext) {

context.subscriptions.push(focusInputCommand);

// Register command to explain file with Claude Code
const explainFileCommand = vscode.commands.registerCommand('claude-code-extension.explainFile', async (uri: vscode.Uri) => {
if (!claudeTerminalInputProvider) {
vscode.window.showErrorMessage('Claude terminal input provider not initialized');
return;
}

try {
// Check if file exists
const fileExists = await vscode.workspace.fs.stat(uri).then(() => true, () => false);
if (!fileExists) {
vscode.window.showWarningMessage(`File does not exist: ${uri.fsPath}`);
return;
}

// Get workspace-relative path
const workspaceFolders = vscode.workspace.workspaceFolders;
let relativePath = uri.fsPath;

if (workspaceFolders && workspaceFolders.length > 0) {
const workspaceRoot = workspaceFolders[0].uri.fsPath;
if (relativePath.startsWith(workspaceRoot)) {
relativePath = relativePath.substring(workspaceRoot.length);
if (relativePath.startsWith('/') || relativePath.startsWith('\\')) {
relativePath = relativePath.substring(1);
}
}
} else {
const pathParts = relativePath.split(/[/\\]/);
relativePath = pathParts[pathParts.length - 1];
}

// Format message to explain the file
const message = `Explain this file: @${relativePath}`;

// Ensure terminal is available and send message
const terminalResult = await ensureClaudeTerminal(context);
claudeTerminalInputProvider.updateTerminal(terminalResult.terminal, terminalResult.isExisting);
terminalResult.terminal.show(false);

if (!terminalResult.isRunningClaude && !isClaudeRunning) {
await claudeTerminalInputProvider.sendToTerminal('claude');
isClaudeRunning = true;
await new Promise(resolve => setTimeout(resolve, 1000));
}

await claudeTerminalInputProvider.sendToTerminal(message);
vscode.commands.executeCommand('claudeCodeInputView.focus');
} catch (error) {
console.error('Error in explainFile command:', error);
vscode.window.showErrorMessage(`Failed to explain file: ${error}`);
}
});

context.subscriptions.push(explainFileCommand);

// Register command to explain folder with Claude Code
const explainFolderCommand = vscode.commands.registerCommand('claude-code-extension.explainFolder', async (uri: vscode.Uri) => {
if (!claudeTerminalInputProvider) {
vscode.window.showErrorMessage('Claude terminal input provider not initialized');
return;
}

try {
// Check if folder exists
const stat = await vscode.workspace.fs.stat(uri);
if (!(stat.type & vscode.FileType.Directory)) {
vscode.window.showWarningMessage(`Path is not a directory: ${uri.fsPath}`);
return;
}

// Get workspace-relative path
const workspaceFolders = vscode.workspace.workspaceFolders;
let relativePath = uri.fsPath;

if (workspaceFolders && workspaceFolders.length > 0) {
const workspaceRoot = workspaceFolders[0].uri.fsPath;
if (relativePath.startsWith(workspaceRoot)) {
relativePath = relativePath.substring(workspaceRoot.length);
if (relativePath.startsWith('/') || relativePath.startsWith('\\')) {
relativePath = relativePath.substring(1);
}
}
} else {
const pathParts = relativePath.split(/[/\\]/);
relativePath = pathParts[pathParts.length - 1];
}

// Format message to explain the folder
const message = `Explain the structure and purpose of this folder: @${relativePath}`;

// Ensure terminal is available and send message
const terminalResult = await ensureClaudeTerminal(context);
claudeTerminalInputProvider.updateTerminal(terminalResult.terminal, terminalResult.isExisting);
terminalResult.terminal.show(false);

if (!terminalResult.isRunningClaude && !isClaudeRunning) {
await claudeTerminalInputProvider.sendToTerminal('claude');
isClaudeRunning = true;
await new Promise(resolve => setTimeout(resolve, 1000));
}

await claudeTerminalInputProvider.sendToTerminal(message);
vscode.commands.executeCommand('claudeCodeInputView.focus');
} catch (error) {
console.error('Error in explainFolder command:', error);
vscode.window.showErrorMessage(`Failed to explain folder: ${error}`);
}
});

context.subscriptions.push(explainFolderCommand);

// Register command to explain selection with Claude Code
const explainSelectionCommand = vscode.commands.registerCommand('claude-code-extension.explainSelection', async () => {
if (!claudeTerminalInputProvider) {
vscode.window.showErrorMessage('Claude terminal input provider not initialized');
return;
}

try {
const editor = vscode.window.activeTextEditor;
if (!editor) {
vscode.window.showWarningMessage('No active editor found');
return;
}

const selection = editor.selection;
const selectedText = editor.document.getText(selection);

if (!selectedText) {
vscode.window.showWarningMessage('No text selected');
return;
}

// Get workspace-relative path
const workspaceFolders = vscode.workspace.workspaceFolders;
let relativePath = editor.document.fileName;

if (workspaceFolders && workspaceFolders.length > 0) {
const workspaceRoot = workspaceFolders[0].uri.fsPath;
if (relativePath.startsWith(workspaceRoot)) {
relativePath = relativePath.substring(workspaceRoot.length);
if (relativePath.startsWith('/') || relativePath.startsWith('\\')) {
relativePath = relativePath.substring(1);
}
}
} else {
const pathParts = relativePath.split(/[/\\]/);
relativePath = pathParts[pathParts.length - 1];
}

// Get line numbers
const startLine = selection.start.line + 1;
const endLine = selection.end.line + 1;
const lineRange = startLine === endLine ? `${startLine}` : `${startLine}-${endLine}`;

// Format message to explain the selection
const message = `Explain this code: @${relativePath}#L${lineRange}`;

// Ensure terminal is available and send message
const terminalResult = await ensureClaudeTerminal(context);
claudeTerminalInputProvider.updateTerminal(terminalResult.terminal, terminalResult.isExisting);
terminalResult.terminal.show(false);

if (!terminalResult.isRunningClaude && !isClaudeRunning) {
await claudeTerminalInputProvider.sendToTerminal('claude');
isClaudeRunning = true;
await new Promise(resolve => setTimeout(resolve, 1000));
}

await claudeTerminalInputProvider.sendToTerminal(message);
vscode.commands.executeCommand('claudeCodeInputView.focus');
} catch (error) {
console.error('Error in explainSelection command:', error);
vscode.window.showErrorMessage(`Failed to explain selection: ${error}`);
}
});

context.subscriptions.push(explainSelectionCommand);

// Register command to explain current file with Claude Code
const explainCurrentFileCommand = vscode.commands.registerCommand('claude-code-extension.explainCurrentFile', async () => {
if (!claudeTerminalInputProvider) {
vscode.window.showErrorMessage('Claude terminal input provider not initialized');
return;
}

try {
const editor = vscode.window.activeTextEditor;
if (!editor) {
vscode.window.showWarningMessage('No active editor found');
return;
}

// Get workspace-relative path
const workspaceFolders = vscode.workspace.workspaceFolders;
let relativePath = editor.document.fileName;

if (workspaceFolders && workspaceFolders.length > 0) {
const workspaceRoot = workspaceFolders[0].uri.fsPath;
if (relativePath.startsWith(workspaceRoot)) {
relativePath = relativePath.substring(workspaceRoot.length);
if (relativePath.startsWith('/') || relativePath.startsWith('\\')) {
relativePath = relativePath.substring(1);
}
}
} else {
const pathParts = relativePath.split(/[/\\]/);
relativePath = pathParts[pathParts.length - 1];
}

// Format message to explain the current file
const message = `Explain this file: @${relativePath}`;

// Ensure terminal is available and send message
const terminalResult = await ensureClaudeTerminal(context);
claudeTerminalInputProvider.updateTerminal(terminalResult.terminal, terminalResult.isExisting);
terminalResult.terminal.show(false);

if (!terminalResult.isRunningClaude && !isClaudeRunning) {
await claudeTerminalInputProvider.sendToTerminal('claude');
isClaudeRunning = true;
await new Promise(resolve => setTimeout(resolve, 1000));
}

await claudeTerminalInputProvider.sendToTerminal(message);
vscode.commands.executeCommand('claudeCodeInputView.focus');
} catch (error) {
console.error('Error in explainCurrentFile command:', error);
vscode.window.showErrorMessage(`Failed to explain current file: ${error}`);
}
});

context.subscriptions.push(explainCurrentFileCommand);

// Register Claude Code Action Provider for Quick Fix menu
const claudeCodeActionProvider = new ClaudeCodeActionProvider(claudeTerminalInputProvider);
context.subscriptions.push(
Expand Down
4 changes: 2 additions & 2 deletions src/ui/claudeTerminalInputProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -299,15 +299,15 @@ export class ClaudeTerminalInputProvider implements vscode.WebviewViewProvider {
this._terminal?.sendText('\x1b[200~', false);

// Send the actual text
this._terminal?.sendText(text, false);
this._terminal?.sendText(text + " ", false);

// Send bracketed paste end sequence
this._terminal?.sendText('\x1b[201~', false);

// Keep bracketed paste mode enabled (Claude Code keeps it on)
} else {
// Send text to terminal normally
this._terminal?.sendText(text, false);
this._terminal?.sendText(text + " ", false);
}

// Use the same delay logic for both paste and normal mode
Expand Down