config

Personal configuration.
git clone git://code.dwrz.net/config
Log | Files | Refs

lsp-earthly.el (3120B)


      1 ;;; lsp-earthly.el --- earthlyls client         -*- lexical-binding: t; -*-
      2 
      3 ;; Copyright (C) 2024  Samuel Loury
      4 
      5 ;; Author: Samuel Loury <konubinixweb@gmail.com>
      6 ;; Keywords: earthly lsp
      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 
     23 ;; LSP client for Earthfile
     24 
     25 ;;; Code:
     26 
     27 (require 'lsp-mode)
     28 
     29 (defgroup lsp-earthly nil
     30   "LSP support for Earthfile."
     31   :group 'lsp-mode
     32   :link '(url-link "https://github.com/glehmann/earthlyls")
     33   :package-version `(lsp-mode . "9.0.0"))
     34 
     35 (defcustom lsp-earthly-active-modes
     36   '(earthfile-mode)
     37   "List of major mode that work with earthlyls."
     38   :type '(list symbol)
     39   :group 'lsp-earthly)
     40 
     41 (defcustom lsp-earthly-home-url
     42   "https://github.com/glehmann/earthlyls"
     43   "Url we use to install earthlyls."
     44   :type 'string
     45   :group 'lsp-earthly
     46   :package-version '(lsp-mode . "9.0.0"))
     47 
     48 (defcustom lsp-earthly-store-path (f-join lsp-server-install-dir "earthly")
     49   "The path to the file in which `earthlyls' will be stored."
     50   :type 'file
     51   :group 'lsp-earthly
     52   :package-version '(lsp-mode . "9.0.0"))
     53 
     54 (defun lsp-earthly--download-server (_client callback error-callback update?)
     55   "Install/update earthly-ls language server using `cargo install'.
     56 
     57 Will invoke CALLBACK or ERROR-CALLBACK based on result.
     58 Will update if UPDATE? is t."
     59   (when update?
     60     (ignore-errors (delete-directory lsp-earthly-store-path t)))
     61   (lsp-async-start-process
     62    callback
     63    error-callback
     64    "cargo" "install" "--git" lsp-earthly-home-url "--root"
     65    lsp-earthly-store-path "earthlyls"))
     66 
     67 (defun lsp-earthly--executable ()
     68   "Return earthlyls executable."
     69   (let ((local (f-join lsp-earthly-store-path "bin"
     70                        (if (eq system-type 'windows-nt)
     71                            "earthlyls.exe"
     72                          "earthlyls"))))
     73     (or (and (f-exists? local) local)
     74         (executable-find "earthlyls")
     75         (user-error "`earthlyls' is not installed; for installation see %s for more information" lsp-earthly-home-url))))
     76 
     77 (defun lsp-earthly--server-command ()
     78   "Startup command for the earthlyls server."
     79   (list (lsp-earthly--executable)))
     80 
     81 (lsp-register-client
     82  (make-lsp-client
     83   :new-connection (lsp-stdio-connection
     84                    #'lsp-earthly--server-command
     85                    (lambda () (f-exists? lsp-earthly-store-path)))
     86   :major-modes lsp-earthly-active-modes
     87   :priority -1
     88   :server-id 'earthlyls
     89   :download-server-fn #'lsp-earthly--download-server))
     90 
     91 (lsp-consistency-check lsp-earthly)
     92 
     93 (provide 'lsp-earthly)
     94 ;;; lsp-earthly.el ends here