-
Notifications
You must be signed in to change notification settings - Fork 6
/
lsp-origami.el
88 lines (73 loc) · 3.24 KB
/
lsp-origami.el
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
;;; lsp-origami.el --- origami.el support for lsp-mode -*- lexical-binding: t; -*-
;; Copyright (C) 2019-2020 Vibhav Pant <[email protected]>
;; Author: Vibhav Pant
;; Version: 1.0.0
;; Keywords: languages lsp-mode
;; Package-Requires: ((emacs "27.1") (origami "1.0") (lsp-mode "6.1"))
;; URL: https://github.com/emacs-lsp/lsp-origami
;; 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-origami provides support for origami.el using language server
;; protocol's "textDocument/foldingRange" functionality. It can be enabled
;; with
;; (require 'lsp-origami)
;; (add-hook 'lsp-after-open-hook #'lsp-origami-try-enable)
;;; Code:
(require 'origami)
(require 'lsp-mode)
(defun lsp-origami--folding-range-to-fold (range create)
"Using the components of RANGE as arguments, execute the CREATE callback."
(funcall create
(lsp--folding-range-beg range)
(lsp--folding-range-end range)
0
(seq-map
(lambda (range) (lsp-origami--folding-range-to-fold range create))
(seq-remove (lambda (child-range)
(or (eq (lsp--folding-range-beg child-range)
(lsp--folding-range-beg range))
(eq (lsp--folding-range-end child-range)
(lsp--folding-range-end range))))
(lsp--folding-range-children range)))))
(defun lsp-origami--parser (create)
"Get a list of Folding Ranges for the current buffer, with CREATE as the origami callback."
(lambda (_content)
(unless (lsp-feature? "foldingRangeProvider")
(signal 'lsp-capability-not-supported (list "foldingRangeProvider")))
(seq-map (lambda (range)
(lsp-origami--folding-range-to-fold range create))
(lsp--get-nested-folding-ranges))))
;;;###autoload
(defun lsp-origami-try-enable ()
"Turn on `origami-mode' locally and try to enable `lsp-origami-mode'."
(interactive)
(origami-mode 1)
(cond ((lsp-feature? "textDocument/foldingRange")
(lsp-origami-mode 1))
((called-interactively-p 'any)
(signal 'lsp-capability-not-supported (list "foldingRangeProvider")))
(t
(lsp-log "This server does not support foldingRangeProvider"))))
;;;###autoload
(define-minor-mode lsp-origami-mode
"Toggle code folding support for origami."
:group 'lsp-origami
:global nil
(cond
(lsp-origami-mode
(setq-local origami-fold-style 'lsp-mode)
(setq-local origami-parser-alist
(cons '(lsp-mode . lsp-origami--parser) origami-parser-alist)))
(t
(setq-local origami-fold-style nil))))
(provide 'lsp-origami)
;;; lsp-origami.el ends here