Skip to content

Commit 9cae4b5

Browse files
committed
Open editor in specific line from traceback
Took editor arguments snippet from my srcview package. Can potentially extract that specific tidbit to a reusable function.
1 parent efd958a commit 9cae4b5

File tree

5 files changed

+16
-5
lines changed

5 files changed

+16
-5
lines changed

frontend/src/components/editor/output/MarimoTracebackOutput.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ export const replaceTracebackFilenames = (domNode: DOMNode) => {
217217
<div
218218
className="inline-block cursor-pointer text-link hover:underline"
219219
onClick={_ => {
220-
getRequestClient().openFile({path: info.src});
220+
getRequestClient().openFile({path: info.src, lineNumber: info.lineNumber});
221221
}}
222222
>
223223
<span className="nb">"{info.src}"</span>

frontend/src/core/network/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ export interface EditRequests {
142142
request: PreviewDataSourceConnectionRequest,
143143
) => Promise<null>;
144144
validateSQL: (request: ValidateSQLRequest) => Promise<null>;
145-
openFile: (request: { path: string }) => Promise<null>;
145+
openFile: (request: { path: string, lineNumber?: number }) => Promise<null>;
146146
getUsageStats: () => Promise<UsageResponse>;
147147
// Debugger
148148
sendPdb: (request: PdbRequest) => Promise<null>;

marimo/_server/api/endpoints/file_explorer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ async def open_file(
253253
body = await parse_request(request, cls=FileOpenRequest)
254254
try:
255255
file_system.get_details(body.path)
256-
success = file_system.open_in_editor(body.path)
256+
success = file_system.open_in_editor(body.path, body.line_number)
257257
return SuccessResponse(success=success)
258258
except Exception as e:
259259
LOGGER.error(f"Error opening file: {e}")

marimo/_server/files/os_file_system.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ def sort_key(file_info: FileInfo) -> tuple[int, str]:
319319
results.sort(key=sort_key)
320320
return results[:limit]
321321

322-
def open_in_editor(self, path: str) -> bool:
322+
def open_in_editor(self, path: str, line_number: int | None) -> bool:
323323
try:
324324
# First try to get editor from environment variable
325325
editor = os.environ.get("EDITOR")
@@ -328,9 +328,19 @@ def open_in_editor(self, path: str) -> bool:
328328
# otherwise it silently opens the terminal in the same window that is
329329
# running marimo.
330330
if editor and not _is_terminal_editor(editor):
331+
332+
args = [path]
333+
if line_number is not None:
334+
if editor == "code":
335+
args = ["--goto", f"{path}:{line_number}"]
336+
elif editor == "subl":
337+
args = [f"{path}:{line_number}"]
338+
else:
339+
args = [f"+{line_number}", path]
340+
331341
try:
332342
# For GUI editors
333-
subprocess.run([editor, path])
343+
subprocess.run([editor, *args])
334344
return True
335345
except Exception as e:
336346
LOGGER.error(f"Error opening with EDITOR: {e}")

marimo/_server/models/files.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ class FileDetailsRequest(msgspec.Struct, rename="camel"):
3232
class FileOpenRequest(msgspec.Struct, rename="camel"):
3333
# The path of the file to open
3434
path: str
35+
line_number: Optional[int] = None
3536

3637

3738
class FileTreeRequest(msgspec.Struct, rename="camel"):

0 commit comments

Comments
 (0)