diff --git a/CHANGELOG.org b/CHANGELOG.org index 3ebd7678..6744355a 100644 --- a/CHANGELOG.org +++ b/CHANGELOG.org @@ -4,6 +4,7 @@ * Changelog ** Unreleased 0.9 - Added ~dap-gdb~ + - Added ~dap-julia~ ** 0.8 - [Breaking Change] Change debug provider names to match VS Code's naming: ~lldb~ to ~lldb-mi~ and ~codelldb~ to ~lldb~ - Added ~dap-gdscript~ diff --git a/dap-julia.el b/dap-julia.el new file mode 100644 index 00000000..660a3a94 --- /dev/null +++ b/dap-julia.el @@ -0,0 +1,68 @@ +;;; dap-julia.el --- Debug Adapter Protocol mode for Julia -*- lexical-binding: t; -*- + +;; Copyright (C) 2024 Michael Kovarik + +;; Author: Michael Kovarik +;; Keywords: languages + +;; This program is free software; you can redistribute it and/or modify +;; it under the terms of the GNU General Public License as published by +;; the Free Software Foundation, either version 3 of the License, or +;; (at your option) any later version. + +;; This program is distributed in the hope that it will be useful, +;; but WITHOUT ANY WARRANTY; without even the implied warranty of +;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +;; GNU General Public License for more details. + +;; You should have received a copy of the GNU General Public License +;; along with this program. If not, see . + +;;; Commentary: +;; Adapter for https://github.com/julia-vscode/DebugAdapter.jl + +;;; Code: + +(require 'dap-mode) +(require 'dap-utils) + +(defun dap-julia--find-project-root (dir) + "Search upwards from DIR to find Julia project root." + (let ((parent (file-name-directory (directory-file-name dir)))) + (cond + ((or (file-exists-p (expand-file-name "Project.toml" dir)) + (file-exists-p (expand-file-name "JuliaProject.toml" dir))) + dir) + ((or (null parent) (equal parent dir)) + (error "No Project.toml or JuliaProject.toml found in any parent directories. Is this a Julia project?")) + (t (dap-julia--find-project-root parent))))) + +(defun dap-julia--populate-start-file-args (conf) + "Populate CONF with the required arguments." + (let* ((project-root (dap-julia--find-project-root (file-name-directory (buffer-file-name)))) + (port (dap--find-available-port)) + (command (format "julia --project=%s -e \"import DebugAdapter: DebugSession; using Sockets; \ +port = %d; server = listen(port); \ +while true; conn = accept(server); @async begin; \ +session = DebugSession(conn); run(session); close(conn); end; end;\"" project-root port))) + (-> conf + (dap--put-if-absent :cwd project-root) + (dap--put-if-absent :name "Julia Debug") + (dap--put-if-absent :debugServer port) + (dap--put-if-absent :host "localhost") + (dap--put-if-absent :type "Julia") + (dap--put-if-absent :program (buffer-file-name)) + (dap--put-if-absent :program-to-start command)))) + +(dap-register-debug-provider "Julia" #'dap-julia--populate-start-file-args) + + +(dap-register-debug-template "Julia Run Configuration" + (list :type "Julia" + :cwd nil + :request "launch" + :name "Julia Debug" + :sourceMaps t)) + +(provide 'dap-julia) +;;; dap-julia.el ends here diff --git a/docs/page/configuration.md b/docs/page/configuration.md index 88cd918b..7da277b0 100644 --- a/docs/page/configuration.md +++ b/docs/page/configuration.md @@ -696,4 +696,23 @@ Core Attach (Console)" from `dap-debug` menu. Call `dap-debug` and edit the "OCaml Debug Template". Make sure to set the program field as the path of your *byte-code* compiled program. Note that this debugger _only_ works with bytecode compiled OCaml programs. - + + +## Julia + +[DebugAdapter.jl](https://github.com/julia-vscode/DebugAdapter.jl) is DAP-compatible server built on top of [Debugger.jl](https://github.com/JuliaDebug/Debugger.jl). + +To setup, first install `DebugAdapter.jl`: + +``` +$ julia -e 'import Pkg; Pkg.add("DebugAdapter")' +``` + +Then add the following to your configuration: + + +``` elisp +(require 'dap-julia) +``` + +To launch a debug session, call `dap-debug` with the `Julia Run Configuration` template.