config

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

lsp-cobol.el (4811B)


      1 ;;; lsp-cobol.el --- COBOL support  -*- lexical-binding: t; -*-
      2 
      3 ;; Copyright (C) 2024  Shen, Jen-Chieh
      4 
      5 ;; This file is not part of GNU Emacs.
      6 
      7 ;; This program is free software: you can redistribute it and/or modify
      8 ;; it under the terms of the GNU General Public License as published by
      9 ;; the Free Software Foundation, either version 3 of the License, or
     10 ;; (at your option) any later version.
     11 
     12 ;; This program is distributed in the hope that it will be useful,
     13 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
     14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     15 ;; GNU General Public License for more details.
     16 
     17 ;; You should have received a copy of the GNU General Public License
     18 ;; along with this program. If not, see <https://www.gnu.org/licenses/>.
     19 
     20 ;;; Commentary:
     21 ;;
     22 ;; COBOL support.
     23 ;;
     24 
     25 ;;; Code:
     26 
     27 (require 'lsp-mode)
     28 
     29 (defgroup lsp-cobol nil
     30   "LSP support for COBOL."
     31   :group 'lsp-mode
     32   :link '(url-link "https://github.com/eclipse-che4z/che-che4z-lsp-for-cobol")
     33   :package-version `(lsp-mode . "9.0.0"))
     34 
     35 (defcustom lsp-cobol-server-path nil
     36   "Path points for COBOL language service.
     37 
     38 This is only for development use."
     39   :type 'string
     40   :group 'lsp-cobol)
     41 
     42 (defcustom lsp-cobol-port 1044
     43   "Port to connect server to."
     44   :type 'integer
     45   :group 'lsp-cobol)
     46 
     47 ;;
     48 ;;; Installation
     49 
     50 (defcustom lsp-cobol-server-store-path
     51   (expand-file-name "cobol/" lsp-server-install-dir)
     52   "The path to the file in which COBOL language service will be stored."
     53   :type 'file
     54   :group 'lsp-cobol)
     55 
     56 (defcustom lsp-cobol-server-version "2.1.1"
     57   "The COBOL language service version to install."
     58   :type 'file
     59   :group 'lsp-cobol)
     60 
     61 (defconst lsp-cobol-download-url-format
     62   "https://github.com/eclipse-che4z/che-che4z-lsp-for-cobol/releases/download/%s/cobol-language-support-%s-%s-%s%s.vsix"
     63   "Format to the download url link.")
     64 
     65 (defun lsp-cobol--server-url ()
     66   "Return Url points to the cobol language service's zip/tar file."
     67   (let* ((x86 (string-prefix-p "x86_64" system-configuration))
     68          (arch (if x86 "x64" "arm64"))
     69          (version lsp-cobol-server-version))
     70     (cl-case system-type
     71       ((cygwin windows-nt ms-dos)
     72        (format lsp-cobol-download-url-format
     73                version "win32" arch version "-signed"))
     74       (darwin
     75        (format lsp-cobol-download-url-format
     76                version "darwin" arch version ""))
     77       (gnu/linux
     78        (format lsp-cobol-download-url-format
     79                version "linux" arch version "")))))
     80 
     81 (defun lsp-cobol--stored-executable ()
     82   "Return the stored COBOL language service executable."
     83   (f-join lsp-cobol-server-store-path
     84           (concat "extension/server/native/"
     85                   (cl-case system-type
     86                     ((cygwin windows-nt ms-dos) "engine.exe")
     87                     (darwin                     "server-mac")
     88                     (gnu/linux                  "server-linux")))))
     89 
     90 (lsp-dependency
     91  'cobol-ls
     92  '(:system "cobol-ls")
     93  `(:download :url ,(lsp-cobol--server-url)
     94              :decompress :zip
     95              :store-path ,(f-join lsp-cobol-server-store-path "temp")
     96              :set-executable? t)
     97  `(:system ,(lsp-cobol--stored-executable)))
     98 
     99 ;;
    100 ;;; Server
    101 
    102 ;;;###autoload
    103 (add-hook 'cobol-mode-hook #'lsp-cobol-start-ls)
    104 
    105 ;;;###autoload
    106 (defun lsp-cobol-start-ls ()
    107   "Start the COBOL language service."
    108   (interactive)
    109   (when-let ((exe (lsp-cobol--executable))
    110              ((lsp--port-available "localhost" lsp-cobol-port)))
    111     (lsp-async-start-process #'ignore #'ignore exe)))
    112 
    113 ;;
    114 ;;; Core
    115 
    116 (defun lsp-cobol--executable ()
    117   "Return the COBOL language service executable."
    118   (or lsp-cobol-server-path
    119       (lsp-cobol--stored-executable)))
    120 
    121 (defun lsp-cobol-server-start-fn (&rest _)
    122   "Define COOBL language service start function."
    123   `(,(lsp-cobol--executable)))
    124 
    125 (defun lsp-cobol--tcp-connect-to-port ()
    126   "Define a TCP connection to language server."
    127   (list
    128    :connect
    129    (lambda (filter sentinel name _environment-fn _workspace)
    130      (let* ((host "localhost")
    131             (port lsp-cobol-port)
    132             (tcp-proc (lsp--open-network-stream host port (concat name "::tcp"))))
    133 
    134        ;; TODO: Same :noquery issue (see above)
    135        (set-process-query-on-exit-flag tcp-proc nil)
    136        (set-process-filter tcp-proc filter)
    137        (set-process-sentinel tcp-proc sentinel)
    138        (cons tcp-proc tcp-proc)))
    139    :test? (lambda () (file-executable-p (lsp-cobol--executable)))))
    140 
    141 (lsp-register-client
    142  (make-lsp-client
    143   :new-connection (lsp-cobol--tcp-connect-to-port)
    144   :activation-fn (lsp-activate-on "cobol")
    145   :priority -1
    146   :server-id 'cobol-ls
    147   :download-server-fn
    148   (lambda (_client callback error-callback _update?)
    149     (lsp-package-ensure 'cobol-ls callback error-callback))))
    150 
    151 (lsp-consistency-check lsp-cobol)
    152 
    153 (provide 'lsp-cobol)
    154 ;;; lsp-cobol.el ends here