Skip to content

Commit 8ec3245

Browse files
feat: split view for edit cmd
1 parent a53fa22 commit 8ec3245

File tree

1 file changed

+45
-23
lines changed

1 file changed

+45
-23
lines changed

src/commands/edit.py

Lines changed: 45 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -31,26 +31,48 @@ def edit(
3131
problem if problem.isdigit() else question_data.get("questionId")
3232
)
3333

34-
# Save problem description using _save_problem_to_file function
3534
try:
3635
_save_problem_to_file(question_data)
3736
except Exception as e:
3837
typer.echo(f"Warning: Could not save problem description: {str(e)}")
3938

4039
problem_title_slug = question_data.get("titleSlug", problem)
4140
markdown_file = f"{problem_title_slug}.md"
41+
code_file_path = f"{filename_prefix}.{lang}"
4242

4343
def create_file_with_template(lang: str):
44-
filename = f"{filename_prefix}.{lang}"
45-
with open(filename, "w") as f:
46-
for snippet in question_data.get("codeSnippets", []):
47-
if snippet.get("langSlug").lower() == LANGUAGE_MAP.get(lang):
48-
f.write(snippet.get("code"))
49-
return
50-
typer.echo(f"No template found for language {lang}")
51-
return filename
44+
available_languages = {}
45+
for snippet in question_data.get("codeSnippets", []):
46+
available_languages[snippet.get("langSlug").lower()] = snippet.get("code")
5247

53-
code_file_path = f"{filename_prefix}.{lang}"
48+
with open(code_file_path, "w") as f:
49+
if lang.lower() in available_languages:
50+
f.write(available_languages[lang.lower()])
51+
return code_file_path
52+
53+
mapped_lang = LANGUAGE_MAP.get(lang.lower())
54+
if mapped_lang and mapped_lang in available_languages:
55+
f.write(available_languages[mapped_lang])
56+
return code_file_path
57+
58+
reverse_map = {v: k for k, v in LANGUAGE_MAP.items()}
59+
if (
60+
lang.lower() in reverse_map
61+
and reverse_map[lang.lower()] in available_languages
62+
):
63+
f.write(available_languages[reverse_map[lang.lower()]])
64+
return code_file_path
65+
66+
typer.echo(f"No template found for language {lang}.")
67+
typer.echo(
68+
"Available language templates: " + ", ".join(available_languages.keys())
69+
)
70+
typer.echo("Creating empty file.")
71+
72+
return code_file_path
73+
74+
if not os.path.exists(code_file_path):
75+
code_file_path = create_file_with_template(lang)
5476

5577
if not os.path.exists(markdown_file):
5678
typer.echo(f"Warning: Problem description file not found: {markdown_file}")
@@ -62,23 +84,23 @@ def create_file_with_template(lang: str):
6284
editor_name = os.path.basename(editor.split()[0]).lower()
6385

6486
if editor_name in vim_like_editors:
65-
subprocess.run([editor, "-O", code_file_path, markdown_file])
87+
subprocess.run(
88+
[editor, "-O", code_file_path, markdown_file, "-c", "set noswapfile"]
89+
)
6690
typer.echo(
6791
f"Tip: In {editor_name}, you might need a plugin to preview markdown. "
6892
"Try ':set ft=markdown' to at least get syntax highlighting."
6993
)
70-
elif editor == "code":
71-
subprocess.run(
72-
[
73-
editor,
74-
"--goto",
75-
code_file_path,
76-
"-r",
77-
"--goto",
78-
f"{markdown_file}:1",
79-
]
80-
)
81-
subprocess.run([editor, "--command", "markdown.showPreview"])
94+
elif editor == "code" or editor == "code-insiders":
95+
subprocess.run([editor, "--goto", code_file_path])
96+
subprocess.run([editor, markdown_file])
97+
try:
98+
subprocess.run(
99+
[editor, "--command", "markdown.showPreview"],
100+
stderr=subprocess.DEVNULL,
101+
)
102+
except Exception:
103+
pass
82104
elif editor == "nano":
83105
typer.echo(
84106
"Note: Nano doesn't support split view or markdown preview. Opening code file only."

0 commit comments

Comments
 (0)