Skip to content

Commit 93e0321

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 0c6bd59 commit 93e0321

File tree

5 files changed

+18
-5
lines changed

5 files changed

+18
-5
lines changed

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,10 @@ export const replaceTracebackFilenames = (domNode: DOMNode) => {
216216
<div
217217
className="inline-block cursor-pointer text-destructive hover:underline"
218218
onClick={(_) => {
219-
getRequestClient().openFile({ path: info.src });
219+
getRequestClient().openFile({
220+
path: info.src,
221+
lineNumber: info.lineNumber,
222+
});
220223
}}
221224
>
222225
<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: 11 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,18 @@ 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+
args = [path]
332+
if line_number is not None:
333+
if editor == "code":
334+
args = ["--goto", f"{path}:{line_number}"]
335+
elif editor == "subl":
336+
args = [f"{path}:{line_number}"]
337+
else:
338+
args = [f"+{line_number}", path]
339+
331340
try:
332341
# For GUI editors
333-
subprocess.run([editor, path])
342+
subprocess.run([editor, *args])
334343
return True
335344
except Exception as e:
336345
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)