move_function
Relocate a function from one file to another using Language Server Protocol refactoring to reorganize code structure.
Instructions
Move a function to a different file using LSP refactoring
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| sourceFile | Yes | Source file containing the function | |
| targetFile | Yes | Target file to move the function to | |
| functionName | Yes | Name of the function to move | |
| line | Yes | Line number where the function is located | |
| character | No | Character position where the function starts | |
| language | No | Programming language | typescript |
Implementation Reference
- src/commands/moveFunction.ts:11-71 (handler)Main handler function that executes the move_function tool: extracts function from source, removes it, adds to target, updates imports using LSP and file ops.export async function moveFunction(args: MoveFunctionArgs, clientManager: LSPClientManager) { const { sourceFile, targetFile, functionName, line, character: _character = 0, language = 'typescript' } = args; const workspaceRoot = findWorkspaceRoot(sourceFile); try { const client = await clientManager.getOrCreateLSPClient(language, workspaceRoot); // Open both files const sourceContent = await fs.readFile(sourceFile, 'utf-8'); const targetContent = await fs.readFile(targetFile, 'utf-8').catch(() => ''); await clientManager.sendLSPNotification(client, 'textDocument/didOpen', { textDocument: { uri: `file://${sourceFile}`, languageId: language, version: 1, text: sourceContent, }, }); await clientManager.sendLSPNotification(client, 'textDocument/didOpen', { textDocument: { uri: `file://${targetFile}`, languageId: language, version: 1, text: targetContent, }, }); // Find the function definition const functionText = extractFunctionFromContent(sourceContent, functionName, line); if (!functionText) { throw new Error(`Function ${functionName} not found at line ${line}`); } // Remove from source file const updatedSourceContent = removeFunctionFromContent(sourceContent, functionName, line); // Add to target file const updatedTargetContent = targetContent + '\n\n' + functionText; // Write the changes await fs.writeFile(sourceFile, updatedSourceContent); await fs.writeFile(targetFile, updatedTargetContent); // Update imports/exports as needed await updateImportsForMovedFunction(sourceFile, targetFile, functionName, language); return { content: [ { type: 'text', text: `Successfully moved function '${functionName}' from ${sourceFile} to ${targetFile}`, }, ], }; } catch (error) { throw new Error(`Failed to move function: ${error instanceof Error ? error.message : String(error)}`); } }
- src/types.ts:53-60 (schema)TypeScript interface defining the input arguments for the move_function tool.export interface MoveFunctionArgs { sourceFile: string; targetFile: string; functionName: string; line: number; character?: number; language?: string; }
- src/server.ts:67-101 (registration)Tool object registration in the listTools response, including name, description, and input schema.name: 'move_function', description: 'Move a function to a different file using LSP refactoring', inputSchema: { type: 'object', properties: { sourceFile: { type: 'string', description: 'Source file containing the function', }, targetFile: { type: 'string', description: 'Target file to move the function to', }, functionName: { type: 'string', description: 'Name of the function to move', }, line: { type: 'number', description: 'Line number where the function is located', }, character: { type: 'number', description: 'Character position where the function starts', default: 0, }, language: { type: 'string', description: 'Programming language', default: 'typescript', }, }, required: ['sourceFile', 'targetFile', 'functionName', 'line'], }, },
- src/server.ts:214-215 (registration)Dispatch to the moveFunction handler in the callTool request switch statement.case 'move_function': return await moveFunction(args as unknown as MoveFunctionArgs, this.clientManager);
- src/lsp/utils.ts:63-91 (helper)Helper utility to remove the function from source content by matching braces and name, used in moveFunction handler.export function removeFunctionFromContent(content: string, functionName: string, line: number): string { const lines = content.split('\n'); const startIndex = line - 1; let braceCount = 0; let inFunction = false; let endIndex = startIndex; for (let i = startIndex; i < lines.length; i++) { const currentLine = lines[i]; if (!inFunction && currentLine.includes(functionName)) { inFunction = true; } if (inFunction) { braceCount += (currentLine.match(/{/g) || []).length; braceCount -= (currentLine.match(/}/g) || []).length; if (braceCount === 0 && inFunction) { endIndex = i; break; } } } lines.splice(startIndex, endIndex - startIndex + 1); return lines.join('\n'); }