aboutsummaryrefslogtreecommitdiffstats
path: root/elpa/lsp-mode-20220505.630/lsp-rf.el
blob: eee0a16bbb56ed8671d59079b97127f3bb19b40a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
;;; lsp-rf.el --- description -*- lexical-binding: t; -*-

;; Copyright (C) 2020 emacs-lsp maintainers

;; Author: emacs-lsp maintainers
;; Keywords: lsp, rf, robot

;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with this program.  If not, see <https://www.gnu.org/licenses/>.

;;; Commentary:

;; LSP Clients for the Robot Framework.

;;; Code:

(require 'lsp-mode)

(defgroup lsp-rf nil
  "Settings for Robot Framework Language Server."
  :group 'lsp-mode
  :link '(url-link "https://github.com/tomi/vscode-rf-language-server"))

(defcustom lsp-rf-language-server-start-command '("~/.nvm/versions/node/v9.11.2/bin/node" "~/.vscode/extensions/tomiturtiainen.rf-intellisense-2.8.0/server/server.js")
  "Path to the server.js file of the rf-intellisense server.
Accepts a list of strings (path/to/interpreter path/to/server.js)"
  :type 'list
  :group 'lsp-rf)

(defcustom lsp-rf-language-server-include-paths []
  "An array of files that should be included by the parser.
Glob patterns as strings are accepted (eg. *.robot between double quotes)"
  :type 'lsp-string-vector
  :group 'lsp-rf)

(defcustom lsp-rf-language-server-exclude-paths []
  "An array of files that should be ignored by the parser.
Glob patterns as strings are accepted (eg. *bad.robot between double quotes)"
  :type 'lsp-string-vector
  :group 'lsp-rf)

(defcustom lsp-rf-language-server-dir "~/.vscode/extensions/tomiturtiainen.rf-intellisense-2.8.0/server/library-docs/"
  "Libraries directory for libraries in `lsp-rf-language-server-libraries'"
  :type 'string
  :group 'lsp-rf)

(defcustom lsp-rf-language-server-libraries ["BuiltIn-3.1.1" "Collections-3.0.4"]
  "Libraries whose keywords are suggested with `auto-complete'."
  :type '(repeat string)
  ;; :type 'lsp-string-vector
  :group 'lsp-rf)

(defcustom lsp-rf-language-server-log-level "debug"
  "What language server log messages are printed."
  :type 'string
  ;; :type '(choice (:tag "off" "errors" "info" "debug"))
  :group 'lsp-rf)

(defcustom lsp-rf-language-server-trace-server "verbose"
  "Traces the communication between VSCode and the rfLanguageServer service."
  :type 'string
  ;; :type '(choice (:tag "off" "messages" "verbose"))
  :group 'lsp-rf)

(defun parse-rf-language-server-library-dirs (dirs)
  (vconcat (mapcar
   (lambda (x)
     (concat
      (expand-file-name
       lsp-rf-language-server-dir)
      x
      ".json"))
   dirs)))

(defun expand-start-command ()
  (mapcar 'expand-file-name lsp-rf-language-server-start-command))

(defun parse-rf-language-server-globs-to-regex (vector)
  "Convert a VECTOR of globs to a regex."
  (--> (mapcan #'lsp-glob-to-regexps vector)
       (s-join "\\|" it)
       (concat "\\(?:" it "\\)")))

(defun parse-rf-language-server-include-path-regex (vector)
  "Creates regexp to select files from workspace directory."
  (let ((globs (if (eq vector [])
                        ["*.robot" "*.resource"]
                      vector)))
    (parse-rf-language-server-globs-to-regex globs)))

(defun parse-rf-language-server-exclude-paths (seq)
  "Creates regexp to select files from workspace directory."
  (if (eq lsp-rf-language-server-exclude-paths [])
      seq
  (cl-delete-if (lambda (x) (string-match-p
                             (parse-rf-language-server-globs-to-regex
                              lsp-rf-language-server-exclude-paths)
                             x))
                seq)))

(lsp-register-custom-settings
 '(
   ("rfLanguageServer.trace.server" lsp-rf-language-server-trace-server)
   ("rfLanguageServer.logLevel" lsp-rf-language-server-log-level)
   ("rfLanguageServer.libraries" lsp-rf-language-server-libraries)
   ("rfLanguageServer.excludePaths" lsp-rf-language-server-exclude-paths)
   ("rfLanguageServer.includePaths" lsp-rf-language-server-include-paths)))

(lsp-register-client
 (make-lsp-client :new-connection (lsp-stdio-connection
                                   (expand-start-command))
                  :major-modes '(robot-mode)
                  :server-id 'rf-intellisense
                  ;; :library-folders-fn (lambda (_workspace)
                  ;;                        lsp-rf-language-server-libraries)
                  :library-folders-fn (lambda (_workspace)
                                         (parse-rf-language-server-library-dirs
                                         lsp-rf-language-server-libraries))
                  :initialized-fn (lambda (workspace)
                                    (with-lsp-workspace workspace
                                      (lsp--set-configuration
                                       (lsp-configuration-section "rfLanguageServer"))
                                      (lsp-request "buildFromFiles"
                                                   (list :files
                                                         (vconcat
                                                          (parse-rf-language-server-exclude-paths
                                                           (directory-files-recursively
                                                            (lsp--workspace-root workspace)
                                                            (parse-rf-language-server-include-path-regex
                                                             lsp-rf-language-server-include-paths))))))))))



(lsp-consistency-check lsp-rf)

(provide 'lsp-rf)
;;; lsp-rf.el ends here