Skip to content

Commit 89322d7

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 e4e1d09 commit 89322d7

File tree

7 files changed

+35
-5
lines changed

7 files changed

+35
-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.filePath });
219+
getRequestClient().openFile({
220+
path: info.filePath,
221+
lineNumber: info.lineNumber,
222+
});
220223
}}
221224
>
222225
<span className="nb">"{info.filePath}"</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: 21 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,17 @@ 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 = (
332+
[path]
333+
if line_number is None
334+
else editor_open_file_in_line_args(
335+
editor, path, line_number
336+
)
337+
)
338+
331339
try:
332340
# For GUI editors
333-
subprocess.run([editor, path])
341+
subprocess.run([editor, *args])
334342
return True
335343
except Exception as e:
336344
LOGGER.error(f"Error opening with EDITOR: {e}")
@@ -350,6 +358,17 @@ def open_in_editor(self, path: str) -> bool:
350358
return False
351359

352360

361+
def editor_open_file_in_line_args(
362+
editor: str, path: str, line_number: int
363+
) -> list[str]:
364+
if editor == "code":
365+
return ["--goto", f"{path}:{line_number}"]
366+
elif editor == "subl":
367+
return [f"{path}:{line_number}"]
368+
else:
369+
return [f"+{line_number}", path]
370+
371+
353372
def natural_sort_file(file: FileInfo) -> list[Union[int, str]]:
354373
return natural_sort(file.name)
355374

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"):

packages/openapi/api.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1400,6 +1400,11 @@ components:
14001400
type: object
14011401
FileOpenRequest:
14021402
properties:
1403+
lineNumber:
1404+
anyOf:
1405+
- type: integer
1406+
- type: 'null'
1407+
default: null
14031408
path:
14041409
type: string
14051410
required:

packages/openapi/src/api.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3391,6 +3391,8 @@ export interface components {
33913391
};
33923392
/** FileOpenRequest */
33933393
FileOpenRequest: {
3394+
/** @default null */
3395+
lineNumber?: number | null;
33943396
path: string;
33953397
};
33963398
/** FileSearchRequest */

0 commit comments

Comments
 (0)