From 374fd0511ef5349f7621c15f5ce2e22f307f6e68 Mon Sep 17 00:00:00 2001 From: Lukas Barth Date: Wed, 10 May 2023 16:24:57 +0200 Subject: [PATCH] Extract environment variables from launch.json (#739) The launch.json format and the dap-mode format for specifying environment variables is not the same, we need to do some conversion. First, launch.json uses the key 'environment', dap-mode uses 'environment-variables'. Second, launch.json uses a format like {'name': 'foo', 'value': 'bar'} to specify a single variable, while dap-mode uses a ('foo' . 'bar') cons cell. Co-authored-by: Lukas Barth --- dap-launch.el | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/dap-launch.el b/dap-launch.el index 7ae23a14..6d5f352c 100644 --- a/dap-launch.el +++ b/dap-launch.el @@ -83,11 +83,34 @@ Yields nil if it cannot be found or there is no project." Extract the name from the :name property." (push (dap-launch-configuration-get-name conf) conf)) +(defun dap--launch-extract-environment (conf) + "Transform environment config into dap-mode format. +This handles a single configuration plist." + (if (not (plist-get conf :environment)) + ;; No environment specified, just return the old configuration + conf + ;; Standard format for the "environment" key is + ;; {"name": "foo", "value": "bar"}, + ;; which results in a (:name "foo" :value "bar) plist. + ;; We need to transform this into a ("foo" . "bar") cons cell. + (let ((environ-spec (mapcar + (lambda (env-plist) + (cons (plist-get env-plist :name) + (plist-get env-plist :value))) + (plist-get conf :environment)))) + (plist-put conf :environment-variables environ-spec)))) + +(defun dap--launch-extract-environments (conflist) + "Transform environment config into dap-mode format. +This is intended to be run on a list of configurations." + (mapcar #'dap--launch-extract-environment conflist)) + (defun dap-launch-parse-launch-json (json) "Return a list of all launch configurations in JSON. JSON must have been acquired with `dap-launch--get-launch-json'." (mapcar #'dap-launch-configuration-prepend-name - (or (plist-get json :configurations) (list json)))) + (dap--launch-extract-environments + (or (plist-get json :configurations) (list json))))) (defun dap-launch-find-parse-launch-json () "Return a list of all launch configurations for the current project.