Hi,

On Thu, 16 Nov 2023 at 17:04, Liliana Marie Prikler <liliana.prik...@gmail.com> 
wrote:

>> It’d be nice to support these as well.  However, how do we know we’re
>> dealing with kate or VSCode?  By checking the basename of $EDITOR?
>> Kinda ugly and brittle, but probably better than nothing.
>
> Maybe we can check for a guix_editor shell function and invoke that
> rather than EDITOR if defined?

Yeah, from my point of view, the two alternatives [1] are:

  1. do we tweak “guix edit” for behaving differently depending on $EDITOR?

or

  2. do we provide some wrappers for the issues that already popped up?

Where #1 is what Ludo suggests and #2 is what Liliana suggests. :-)

Well, see below the code [2] that we concretely need for VSCode.  The
question is:

 1. Do we put this code in guix/scripts/edit.scm?  And trigger on the
 basename of EDITOR?

or

 2. Do we put this code in some etc/vscode-wrapper that user can
 install?  (or that we could automatically installl) Or maybe revamp it
 for calling this code via some shell function?


WDYT?

Cheers,
simon

--8<---------------cut here---------------start------------->8---
#!/usr/bin/env -S guix repl -q --
;; -*- mode: scheme -*-
!#
;;; Copyright © 2023 Simon Tournier <zimon.touto...@gmail.com>
;;;
;;;
;;; VSCode does not respect the convention:
;;;
;;;     $EDITOR +line file
;;;
;;; and instead relies on:
;;;
;;;     code --goto file:line
;;;
;;;
;;; This wrapper is a workaround.  It is Scheme but it could be whatever else
;;; as Bash, Python, etc.  It uses "guix repl" although no Guix library is
;;; required.  Because we assume the invokation,
;;;
;;;     EDITOR=./vscode-wrapper guix edit foo bar
;;;
;;; relying on "guix repl" allows to easily get Guile.
;;;

(use-modules (ice-9 match)
             ((ice-9 string-fun) #:select (string-replace-substring))
             ((ice-9 ftw) #:select (scandir))
             )

(define %vscode--goto
  (let* ((vscode-server (string-append (getenv "HOME") "/.vscode-server"))
         (code (if (file-exists? vscode-server)
                   ;; Maybe Remote-SSH
                   (let* ((vscode-server/bin (string-append vscode-server 
"/bin"))
                          (hash         ;Guess the correct identifier
                           (car
                            (scandir vscode-server/bin
                                     (lambda (file)
                                       (not (member file (list "." ".."))))
                                     (lambda (x y)
                                       (> (stat:mtime
                                           (lstat (string-append 
vscode-server/bin "/" x)))
                                          (stat:mtime
                                           (lstat (string-append 
vscode-server/bin "/" y))))))))
                          (vscode (string-append
                                   vscode-server/bin "/" hash 
"/bin/remote-cli/code"))
                          (run/user/uid (string-append "/run/user/"
                                                       (number->string 
(getuid))))
                          (socket       ;Guess the correct socket, if required
                           (car
                            (scandir run/user/uid
                                     (lambda (file)
                                       (string-prefix? "vscode-ipc" file))
                                     (lambda (x y)
                                       (< (stat:mtime
                                           (lstat (string-append run/user/uid 
"/" x)))
                                          (stat:mtime
                                           (lstat (string-append run/user/uid 
"/" y)))))))))
                     ;; XXXX: Check VSCode API for something more robust
                     (if (getenv "VSCODE_IPC_HOOK_CLI")
                         vscode
                         (string-append
                          "VSCODE_IPC_HOOK_CLI=" socket " " vscode)))
                   ;; No Remote-SSH, try to find the local one in PATH
                   (search-path (string-split (getenv "PATH") #\:)
                                "code"))))
                                        ;(format #t "~a~%" code)
    (string-append code " --goto ")))

(define +line-files
  (match (command-line)
    ((wrapper rest ...)
     (if (eqv? 0 (modulo (length rest) 2))
         rest
         (begin
           (write "Error with 'guix edit'")
           (exit 1))))))

(define files
  (let loop ((files:lines '())
             (lst +line-files))
    (if (null? lst)
        (reverse files:lines)
        (match lst
          ((n file rest ...)
           (loop
            (cons (string-append
                   file ":" (string-replace-substring n "+" ""))
                  files:lines)
            rest))))))

(catch 'system-error
  (lambda ()
    (for-each (lambda (file)
                (system (string-append
                         %vscode--goto file)))
              files))
  (lambda _
    (write "failed to launch!")))
--8<---------------cut here---------------end--------------->8---



1: $EDITOR and “guix edit”
Simon Tournier <zimon.touto...@gmail.com>
Thu, 02 Nov 2023 10:43:57 +0100
id:86wmv0o5gi....@gmail.com
https://lists.gnu.org/archive/html/guix-devel/2023-11
https://yhetil.org/guix/86wmv0o5gi....@gmail.com

2: 
https://gitlab.com/zimoun/advanced-packages-2023/-/blob/670b81e9321ee4af2407babd985222cc37f33e31/vscode-wrapper

Reply via email to