MCPcopy Create free account
hub / github.com/OpenRaiser/NanoResearch / code_edit

Function code_edit

nanoresearch/cli_code_edit.py:16–175  ·  view source on GitHub ↗

Interactive code editor with LLM assistance and auto-backup. Enter instructions in natural language. The LLM reads your code, makes changes, and auto-backs up before each edit. Commands during interactive mode: preview <...> --- dry-run: show proposed edits without applying

(
    dir: Path = typer.Option(
        ..., "--dir", "-d",
        help="Path to the code directory to edit",
    ),
    config_path: Path = typer.Option(None, "--config", "-c", help="Path to config file"),
    instruction: str = typer.Option(
        None, "--instruction", "-i",
        help="One-shot instruction (non-interactive mode)",
    ),
)

Source from the content-addressed store, hash-verified

14
15@app.command("code")
16def code_edit(
17 dir: Path = typer.Option(
18 ..., "--dir", "-d",
19 help="Path to the code directory to edit",
20 ),
21 config_path: Path = typer.Option(None, "--config", "-c", help="Path to config file"),
22 instruction: str = typer.Option(
23 None, "--instruction", "-i",
24 help="One-shot instruction (non-interactive mode)",
25 ),
26) -> None:
27 """Interactive code editor with LLM assistance and auto-backup.
28
29 Enter instructions in natural language. The LLM reads your code,
30 makes changes, and auto-backs up before each edit.
31
32 Commands during interactive mode:
33 preview <...> --- dry-run: show proposed edits without applying
34 undo --- rollback to the state before the last edit
35 rollback --- show all snapshots and pick one to restore
36 history --- show edit history
37 files --- list current code files
38 exit / quit --- exit the editor
39 """
40 from rich.panel import Panel
41
42 from nanoresearch.agents.code_editor import (
43 CodeSnapshotManager,
44 InteractiveCodeEditor,
45 read_code_context,
46 )
47
48 code_dir = Path(dir).resolve()
49 if not code_dir.is_dir():
50 console.print(f"[red]Directory not found:[/red] {code_dir}")
51 raise typer.Exit(1)
52
53 config = _load_config_safe(config_path)
54 editor = InteractiveCodeEditor(
55 code_dir, config, log_fn=lambda msg: console.print(f" [dim]{msg}[/dim]"),
56 )
57
58 # -- One-shot mode --
59 if instruction:
60 result = asyncio.run(_code_apply(editor, instruction))
61 _print_edit_result(result)
62 return
63
64 # -- Interactive mode --
65 console.print(Panel(
66 f"[bold]Code directory:[/bold] {code_dir}\n"
67 f"[bold]Files:[/bold] {len(read_code_context(code_dir))}\n\n"
68 "Type your instructions in natural language.\n"
69 "Commands: [cyan]undo[/cyan] | [cyan]rollback[/cyan] | "
70 "[cyan]history[/cyan] | [cyan]files[/cyan] | [cyan]exit[/cyan]",
71 title="NanoResearch Code Editor",
72 border_style="blue",
73 ))

Callers

nothing calls this directly

Calls 11

undoMethod · 0.95
rollback_toMethod · 0.95
_load_config_safeFunction · 0.90
read_code_contextFunction · 0.90
_code_applyFunction · 0.85
_print_edit_resultFunction · 0.85
_code_previewFunction · 0.85
getMethod · 0.80
runMethod · 0.45
list_snapshotsMethod · 0.45

Tested by

no test coverage detected