|
| 1 | +;;; dap-julia.el --- Debug Adapter Protocol mode for Julia -*- lexical-binding: t; -*- |
| 2 | + |
| 3 | +;; Copyright (C) 2024 Michael Kovarik |
| 4 | + |
| 5 | +;; Author: Michael Kovarik <[email protected]> |
| 6 | +;; Keywords: languages |
| 7 | + |
| 8 | +;; This program is free software; you can redistribute it and/or modify |
| 9 | +;; it under the terms of the GNU General Public License as published by |
| 10 | +;; the Free Software Foundation, either version 3 of the License, or |
| 11 | +;; (at your option) any later version. |
| 12 | + |
| 13 | +;; This program is distributed in the hope that it will be useful, |
| 14 | +;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | +;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | +;; GNU General Public License for more details. |
| 17 | + |
| 18 | +;; You should have received a copy of the GNU General Public License |
| 19 | +;; along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 20 | + |
| 21 | +;;; Commentary: |
| 22 | +;; Adapter for https://github.com/julia-vscode/DebugAdapter.jl |
| 23 | + |
| 24 | +;;; Code: |
| 25 | + |
| 26 | +(require 'dap-mode) |
| 27 | +(require 'dap-utils) |
| 28 | + |
| 29 | +(defun dap-julia--find-project-root (dir) |
| 30 | + "Search upwards from DIR to find Julia project root." |
| 31 | + (let ((parent (file-name-directory (directory-file-name dir)))) |
| 32 | + (cond |
| 33 | + ((or (file-exists-p (expand-file-name "Project.toml" dir)) |
| 34 | + (file-exists-p (expand-file-name "JuliaProject.toml" dir))) |
| 35 | + dir) |
| 36 | + ((or (null parent) (equal parent dir)) |
| 37 | + (error "No Project.toml or JuliaProject.toml found in any parent directories. Is this a Julia project?")) |
| 38 | + (t (dap-julia--find-project-root parent))))) |
| 39 | + |
| 40 | +(defun dap-julia--populate-start-file-args (conf) |
| 41 | + "Populate CONF with the required arguments." |
| 42 | + (let* ((project-root (dap-julia--find-project-root (file-name-directory (buffer-file-name)))) |
| 43 | + (port (dap--find-available-port)) |
| 44 | + (command (format "julia --project=%s -e \"import DebugAdapter: DebugSession; using Sockets; \ |
| 45 | +port = %d; server = listen(port); \ |
| 46 | +while true; conn = accept(server); @async begin; \ |
| 47 | +session = DebugSession(conn); run(session); close(conn); end; end;\"" project-root port))) |
| 48 | + (-> conf |
| 49 | + (dap--put-if-absent :cwd project-root) |
| 50 | + (dap--put-if-absent :name "Julia Debug") |
| 51 | + (dap--put-if-absent :debugServer port) |
| 52 | + (dap--put-if-absent :host "localhost") |
| 53 | + (dap--put-if-absent :type "Julia") |
| 54 | + (dap--put-if-absent :program (buffer-file-name)) |
| 55 | + (dap--put-if-absent :program-to-start command)))) |
| 56 | + |
| 57 | +(dap-register-debug-provider "Julia" #'dap-julia--populate-start-file-args) |
| 58 | + |
| 59 | + |
| 60 | +(dap-register-debug-template "Julia Run Configuration" |
| 61 | + (list :type "Julia" |
| 62 | + :cwd nil |
| 63 | + :request "launch" |
| 64 | + :name "Julia Debug" |
| 65 | + :sourceMaps t)) |
| 66 | + |
| 67 | +(provide 'dap-julia) |
| 68 | +;;; dap-julia.el ends here |
0 commit comments