Skip to content

Commit d130b10

Browse files
committed
chore: review comments
1 parent a3d3726 commit d130b10

File tree

5 files changed

+23
-48
lines changed

5 files changed

+23
-48
lines changed

src/Lean/Server/FileWorker/RequestHandling.lean

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ def getInteractiveTermGoal (p : Lsp.PlainTermGoalParams)
346346
let goal ← ci.runMetaM lctx' do
347347
Widget.goalToInteractive (← Meta.mkFreshExprMVar ty).mvarId!
348348
let range := if let some r := i.range? then r.toLspRange text else ⟨p.position, p.position⟩
349-
return some { goal with range, term := ← WithRpcRef.mkReusable ti }
349+
return some { goal with range, term := ← WithRpcRef.mk ti }
350350

351351
def handlePlainTermGoal (p : PlainTermGoalParams)
352352
: RequestM (RequestTask (Option PlainTermGoal)) := do

src/Lean/Server/Rpc/Basic.lean

Lines changed: 18 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ namespace Lean.Lsp
2323
An object which RPC clients can refer to without marshalling.
2424
2525
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.
2727
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.
2929
-/
3030
structure RpcRef where
3131
/- NOTE(WN): It is important for this to be a single-field structure
@@ -42,48 +42,32 @@ namespace Lean.Server
4242

4343
/--
4444
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`.
4646
4747
See also the docstring for `RpcEncodable`.
4848
-/
4949
structure WithRpcRef (α : Type u) where
5050
private mk' ::
5151
val : α
52-
id? : Option USize
52+
id : USize
5353
deriving Inhabited
5454

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-
6855
builtin_initialize freshWithRpcRefId : IO.Ref USize ← IO.mkRef 0
6956

7057
/--
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
7562
RPC requests.
7663
-/
77-
def WithRpcRef.mkReusable (val : α) : BaseIO (WithRpcRef α) := do
64+
def WithRpcRef.mk (val : α) : BaseIO (WithRpcRef α) := do
7865
let id ← freshWithRpcRefId.modifyGet fun id => (id, id + 1)
79-
return {
80-
val,
81-
id? := some id
82-
}
66+
return { val, id }
8367

8468
structure ReferencedObject where
8569
obj : Dynamic
86-
id? : Option USize
70+
id : USize
8771
rc : Nat
8872

8973
structure RpcObjectStore : Type where
@@ -106,7 +90,7 @@ structure RpcObjectStore : Type where
10690

10791
def rpcStoreRef [TypeName α] (obj : WithRpcRef α) : StateM RpcObjectStore Lsp.RpcRef := do
10892
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
11094
match reusableRef? with
11195
| some ref =>
11296
-- 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.
120104
| none =>
121105
let ref : Lsp.RpcRef := ⟨st.nextRef⟩
122106
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
131110
}
132111
return ref
133112

@@ -138,7 +117,7 @@ def rpcGetRef (α) [TypeName α] (r : Lsp.RpcRef)
138117
let some val := referencedObj.obj.get? α
139118
| throw <| s!"RPC call type mismatch in reference '{r}'\nexpected '{TypeName.typeName α}', " ++
140119
s!"got '{referencedObj.obj.typeName}'"
141-
return { val, id? := referencedObj.id? }
120+
return { val, id := referencedObj.id }
142121

143122
def rpcReleaseRef (r : Lsp.RpcRef) : StateM RpcObjectStore Bool := do
144123
let st ← get
@@ -147,12 +126,8 @@ def rpcReleaseRef (r : Lsp.RpcRef) : StateM RpcObjectStore Bool := do
147126
let referencedObj := { referencedObj with rc := referencedObj.rc - 1 }
148127
if referencedObj.rc == 0 then
149128
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
156131
}
157132
else
158133
set { st with aliveRefs := st.aliveRefs.insert r referencedObj }

src/Lean/Widget/InteractiveCode.lean

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ where
6868
| none => go subTt
6969
| some i =>
7070
let t : SubexprInfo := {
71-
info := ← WithRpcRef.mkReusable { ctx, info := i, children := .empty }
71+
info := ← WithRpcRef.mk { ctx, info := i, children := .empty }
7272
subexprPos := n
7373
}
7474
return TaggedText.tag t (← go subTt)

src/Lean/Widget/InteractiveDiagnostic.lean

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,9 +205,9 @@ partial def msgToInteractive (msgData : MessageData) (hasWidgets : Bool) (indent
205205
let col := indent + col
206206
let children ←
207207
match children with
208-
| .lazy children => pure <| .lazy <| ← WithRpcRef.mkReusable {
208+
| .lazy children => pure <| .lazy <| ← WithRpcRef.mk {
209209
indent := col+2
210-
children := ← children.mapM (WithRpcRef.mkReusable ·)
210+
children := ← children.mapM (WithRpcRef.mk ·)
211211
}
212212
| .strict children => pure <| .strict (← children.mapM (fmtToTT · (col+2)))
213213
return .tag (.trace indent cls (← fmtToTT msg col) collapsed children) default

src/Lean/Widget/InteractiveGoal.lean

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ def goalToInteractive (mvarId : MVarId) : MetaM InteractiveGoal := do
192192
return {
193193
hyps
194194
type := goalFmt
195-
ctx := ← WithRpcRef.mkReusable {← Elab.CommandContextInfo.save with }
195+
ctx := ← WithRpcRef.mk {← Elab.CommandContextInfo.save with }
196196
userName?
197197
goalPrefix := getGoalPrefix mvarDecl
198198
mvarId

0 commit comments

Comments
 (0)