@@ -23,9 +23,9 @@ namespace Lean.Lsp
23
23
An object which RPC clients can refer to without marshalling.
24
24
25
25
The language server may serve the same `RpcRef` multiple times and maintains a reference count
26
- to track how often it has served the reference.
26
+ to track how many times it has served the reference.
27
27
If clients want to release the object associated with an `RpcRef`,
28
- they must release the reference as often as they have received it from the server.
28
+ they must release the reference as many times as they have received it from the server.
29
29
-/
30
30
structure RpcRef where
31
31
/- NOTE(WN): It is important for this to be a single-field structure
@@ -42,48 +42,32 @@ namespace Lean.Server
42
42
43
43
/--
44
44
Marks values to be encoded as opaque references in RPC packets.
45
- Two identical `WithRpcRef`s with `id? = some id` and the same `id` will yield the same `RpcRef`.
45
+ Two `WithRpcRef`s with the same `id` will yield the same `RpcRef`.
46
46
47
47
See also the docstring for `RpcEncodable`.
48
48
-/
49
49
structure WithRpcRef (α : Type u) where
50
50
private mk' ::
51
51
val : α
52
- id? : Option USize
52
+ id : USize
53
53
deriving Inhabited
54
54
55
- /--
56
- Creates a non-reusable `WithRpcRef`.
57
- Every time `val` is served to the client, it generates a new `RpcRef`.
58
- -/
59
- def WithRpcRef.mkNonReusable (val : α) : WithRpcRef α where
60
- val
61
- id? := none
62
-
63
- /--
64
- Alias for `WithRpcRef.mkNonReusable`.
65
- -/
66
- def WithRpcRef.mk (val : α) : WithRpcRef α := .mkNonReusable val
67
-
68
55
builtin_initialize freshWithRpcRefId : IO.Ref USize ← IO.mkRef 0
69
56
70
57
/--
71
- Creates an `WithRpcRef` instance with a unique `id? `.
72
- Two identical `WithRpcRef`s with `id? = some id` and the same `id` will yield the same `RpcRef`.
73
- Hence, storing a `WithRpcRef` produced by `mkReusable ` and serving it to the client twice will also
74
- yield the same `RpcRef` twice, allowing clients to reuse their associated UI state across
58
+ Creates an `WithRpcRef` instance with a unique `id`.
59
+ Two `WithRpcRef`s with the same `id` will yield the same `RpcRef`.
60
+ Hence, storing a `WithRpcRef` produced by `WithRpcRef.mk ` and serving it to the client twice will
61
+ also yield the same `RpcRef` twice, allowing clients to reuse their associated UI state across
75
62
RPC requests.
76
63
-/
77
- def WithRpcRef.mkReusable (val : α) : BaseIO (WithRpcRef α) := do
64
+ def WithRpcRef.mk (val : α) : BaseIO (WithRpcRef α) := do
78
65
let id ← freshWithRpcRefId.modifyGet fun id => (id, id + 1 )
79
- return {
80
- val,
81
- id? := some id
82
- }
66
+ return { val, id }
83
67
84
68
structure ReferencedObject where
85
69
obj : Dynamic
86
- id? : Option USize
70
+ id : USize
87
71
rc : Nat
88
72
89
73
structure RpcObjectStore : Type where
@@ -106,7 +90,7 @@ structure RpcObjectStore : Type where
106
90
107
91
def rpcStoreRef [TypeName α] (obj : WithRpcRef α) : StateM RpcObjectStore Lsp.RpcRef := do
108
92
let st ← get
109
- let reusableRef? : Option Lsp.RpcRef := do st.refsById.find? (← obj.id?)
93
+ let reusableRef? : Option Lsp.RpcRef := st.refsById.find? obj.id
110
94
match reusableRef? with
111
95
| some ref =>
112
96
-- Reuse `RpcRef` for this `obj` so that clients can reuse their UI state for it.
@@ -120,14 +104,9 @@ def rpcStoreRef [TypeName α] (obj : WithRpcRef α) : StateM RpcObjectStore Lsp.
120
104
| none =>
121
105
let ref : Lsp.RpcRef := ⟨st.nextRef⟩
122
106
set { st with
123
- aliveRefs :=
124
- st.aliveRefs.insert ref ⟨.mk obj.val, obj.id?, 1 ⟩
125
- refsById :=
126
- match obj.id? with
127
- | none => st.refsById
128
- | some id => st.refsById.insert id ref
129
- nextRef :=
130
- st.nextRef + 1
107
+ aliveRefs := st.aliveRefs.insert ref ⟨.mk obj.val, obj.id, 1 ⟩
108
+ refsById := st.refsById.insert obj.id ref
109
+ nextRef := st.nextRef + 1
131
110
}
132
111
return ref
133
112
@@ -138,7 +117,7 @@ def rpcGetRef (α) [TypeName α] (r : Lsp.RpcRef)
138
117
let some val := referencedObj.obj.get? α
139
118
| throw <| s! "RPC call type mismatch in reference '{ r} '\n expected '{ TypeName.typeName α} ', " ++
140
119
s! "got '{ referencedObj.obj.typeName} '"
141
- return { val, id? := referencedObj.id? }
120
+ return { val, id := referencedObj.id }
142
121
143
122
def rpcReleaseRef (r : Lsp.RpcRef) : StateM RpcObjectStore Bool := do
144
123
let st ← get
@@ -147,12 +126,8 @@ def rpcReleaseRef (r : Lsp.RpcRef) : StateM RpcObjectStore Bool := do
147
126
let referencedObj := { referencedObj with rc := referencedObj.rc - 1 }
148
127
if referencedObj.rc == 0 then
149
128
set { st with
150
- aliveRefs :=
151
- st.aliveRefs.erase r
152
- refsById :=
153
- match referencedObj.id? with
154
- | none => st.refsById
155
- | some id => st.refsById.erase id
129
+ aliveRefs := st.aliveRefs.erase r
130
+ refsById := st.refsById.erase referencedObj.id
156
131
}
157
132
else
158
133
set { st with aliveRefs := st.aliveRefs.insert r referencedObj }
0 commit comments