forked from cxxxr/cl-lsp
-
Notifications
You must be signed in to change notification settings - Fork 2
/
gray-streams.lisp
75 lines (68 loc) · 2.24 KB
/
gray-streams.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
65
66
67
68
69
70
71
72
73
74
75
(defpackage :cl-lsp/gray-streams
(:use :cl :trivial-gray-streams)
(:export :lsp-output-stream))
(in-package :cl-lsp/gray-streams)
(defclass lsp-output-stream (fundamental-character-output-stream)
((output-fn
:initarg :output-fn
:reader output-fn)
(buffer
:initform (make-string 8000)
:reader buffer)
(index
:initform 0
:accessor index)
(column
:initform 0
:accessor column)))
(defmethod stream-write-char ((stream lsp-output-stream) character)
(setf (schar (buffer stream) (index stream))
character)
(incf (index stream))
(if (char= character #\newline)
(setf (column stream) 0)
(incf (column stream)))
(when (= (index stream) (length (buffer stream)))
(stream-finish-output stream))
character)
(defmethod stream-write-string ((stream lsp-output-stream) (string string) &optional start end)
(let* ((start (or start 0))
(end (or end (length string)))
(len (length (buffer stream)))
(count (- end start))
(free (- len (index stream))))
(when (>= count free)
(stream-finish-output stream))
(cond ((< count len)
(replace (buffer stream) string
:start1 (index stream)
:start2 start :end2 end)
(incf (index stream) count))
(t
(funcall (output-fn stream) (subseq string start end))))
(let ((last-newline (position #\newline string
:from-end t
:start start :end end)))
(setf (column stream)
(if last-newline
(- end last-newline 1)
(+ (column stream) count)))))
string)
(defmethod stream-line-column ((stream lsp-output-stream))
(column stream))
(defmethod stream-finish-output ((stream lsp-output-stream))
(when (< 0 (index stream))
(funcall (output-fn stream)
(subseq (buffer stream)
0
(index stream)))
(setf (index stream) 0))
nil)
(defmethod stream-force-output ((stream lsp-output-stream))
(stream-finish-output stream))
(defmethod stream-fresh-line ((stream lsp-output-stream))
(cond ((zerop (column stream))
nil)
(t
(terpri stream)
t)))