config

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

lsp-ruby-lsp.el (3272B)


      1 ;;; lsp-ruby-lsp.el --- lsp-mode for the Ruby ruby-lsp gem -*- lexical-binding: t; -*-
      2 
      3 ;; Copyright (C) 2023 Šimon Lukašík
      4 
      5 ;; Author: Šimon Lukašík
      6 ;; Keywords: languages
      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 the Ruby ruby-lsp - an optionated language server for Ruby.
     24 ;; Not to be confused with lsp-ruby that has been deprecated for a while.
     25 
     26 ;;; Code:
     27 
     28 (require 'lsp-mode)
     29 
     30 (defgroup lsp-ruby-lsp nil
     31   "LSP support for the ruby-lsp language server."
     32   :group 'lsp-mode
     33   :link '(url-link "https://github.com/shopify/ruby-lsp"))
     34 
     35 (defcustom lsp-ruby-lsp-use-bundler nil
     36   "Run ruby-lsp using bundler."
     37   :type 'boolean
     38   :safe #'booleanp
     39   :group 'lsp-ruby-lsp)
     40 
     41 (defcustom lsp-ruby-lsp-library-directories
     42   '("~/.rbenv/" "/usr/lib/ruby/" "~/.rvm/" "~/.gem/" "~/.asdf")
     43   "List of directories which will be considered to be libraries."
     44   :type '(repeat string)
     45   :group 'lsp-ruby-lsp
     46   :package-version '(lsp-mode . "9.0.1"))
     47 
     48 (defun lsp-ruby-lsp--build-command ()
     49   (append
     50    (if lsp-ruby-lsp-use-bundler '("bundle" "exec"))
     51    '("ruby-lsp")))
     52 
     53 (defun lsp-ruby-lsp--open-file (arg_hash)
     54   "Open a file. This function is for code-lens provided by ruby-lsp-rails."
     55   (let* ((arguments (gethash "arguments" arg_hash))
     56          (uri (aref (aref arguments 0) 0))
     57          (path-with-line-number (split-string (lsp--uri-to-path uri) "#L"))
     58          (path (car path-with-line-number))
     59          (line-number (cadr path-with-line-number)))
     60     (find-file path)
     61     (when line-number (forward-line (1- (string-to-number line-number))))))
     62 
     63 (defun lsp-ruby-lsp--run-test (arg_hash)
     64   "Run a test file. This function is for code-lens provided by ruby-lsp-rails."
     65   (let* ((arguments (gethash "arguments" arg_hash))
     66          (command (aref arguments 2))
     67          (default-directory (lsp-workspace-root))
     68          (buffer-name "*run test results*")
     69          (buffer (progn
     70                    (when (get-buffer buffer-name) (kill-buffer buffer-name))
     71                    (generate-new-buffer buffer-name))))
     72     (async-shell-command command buffer)))
     73 
     74 (lsp-register-client
     75  (make-lsp-client
     76   :new-connection (lsp-stdio-connection #'lsp-ruby-lsp--build-command)
     77   :activation-fn (lsp-activate-on "ruby")
     78   :library-folders-fn (lambda (_workspace) lsp-ruby-lsp-library-directories)
     79   :priority -2
     80   :action-handlers (ht ("rubyLsp.openFile" #'lsp-ruby-lsp--open-file)
     81                        ("rubyLsp.runTest" #'lsp-ruby-lsp--run-test)
     82                        ("rubyLsp.runTestInTerminal" #'lsp-ruby-lsp--run-test))
     83   :server-id 'ruby-lsp-ls))
     84 
     85 (lsp-consistency-check lsp-ruby-lsp)
     86 
     87 (provide 'lsp-ruby-lsp)
     88 ;;; lsp-ruby-lsp.el ends here