forked from cxxxr/cl-lsp
-
Notifications
You must be signed in to change notification settings - Fork 2
/
formatting.lisp
64 lines (59 loc) · 2.35 KB
/
formatting.lisp
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
(defpackage :cl-lsp/formatting
(:use :cl
:cl-lsp/protocol
:cl-lsp/protocol-util
:cl-lsp/logger
:cl-lsp.lem-base)
(:import-from :cl-lsp.lem-lisp-syntax.indent
:calc-indent)
(:export :on-type-formatting
:range-formatting
:buffer-formatting))
(in-package :cl-lsp/formatting)
(defun indent-line (p &optional editp)
(let ((line-number (1- (line-number-at-point p)))
(old-charpos (point-charpos (back-to-indentation p)))
(new-column (calc-indent (copy-point p :temporary))))
(when new-column
(when editp
(line-start p)
(delete-character p old-charpos)
(insert-character p #\space new-column))
(convert-to-hash-table
(make-instance '|TextEdit|
:|range| (make-instance
'|Range|
:|start| (make-instance '|Position|
:|line| line-number
:|character| 0)
:|end| (make-instance '|Position|
:|line| line-number
:|character| old-charpos))
:|newText| (make-string new-column :initial-element #\space))))))
(defun set-formatting-options (options)
(setf (tab-size) (slot-value options '|tabSize|)))
(defun on-type-formatting (point ch options)
(declare (ignore ch))
(set-formatting-options options)
(let ((edit (indent-line point)))
(if edit
(list edit)
(vector))))
(defun range-formatting (start end options)
(set-formatting-options options)
(let ((buffer (point-buffer start))
(edits '()))
(buffer-enable-undo buffer)
(apply-region-lines start end
(lambda (point)
(multiple-value-bind (edit)
(indent-line point t)
(when edit
(push edit edits)))))
(buffer-undo start)
(buffer-disable-undo buffer)
(list-to-object[] (nreverse edits))))
(defun buffer-formatting (buffer options)
(with-point ((start (buffer-start-point buffer))
(end (buffer-end-point buffer)))
(range-formatting start end options)))