1- import type { DocumentUri , Position , TextDocumentPositionParams } from 'vscode-languageserver-protocol'
2- import { ClientRequestOptions } from './infoviewApi'
1+ import type {
2+ DocumentUri ,
3+ Position ,
4+ ServerCapabilities ,
5+ TextDocumentPositionParams ,
6+ } from 'vscode-languageserver-protocol'
7+ import { ClientRequestOptions , LeanServerCapabilities } from './infoviewApi'
38import { RpcCallParams , RpcErrorCode , RpcPtr , RpcReleaseParams } from './lspTypes'
49
510/**
@@ -83,6 +88,7 @@ class RpcSessionForFile {
8388 constructor (
8489 public uri : DocumentUri ,
8590 public sessions : RpcSessions ,
91+ private serverCapabilities : ServerCapabilities < LeanServerCapabilities > ,
8692 ) {
8793 this . sessionId = ( async ( ) => {
8894 try {
@@ -133,23 +139,34 @@ class RpcSessionForFile {
133139 * for future garbage collection.
134140 *
135141 * The function implements a form of "conservative garbage collection"
136- * where it treats any subobject `{'p ': v}` as a potential RPC reference.
137- * Therefore `p ` should not be used as a field name on the Lean side
142+ * where it treats any subobject `{'__rpcref ': v}` as a potential RPC reference.
143+ * Therefore `__rpcref ` should not be used as a field name on the Lean side
138144 * to prevent false positives.
139145 *
140- * It is unclear if the false positives will become a big issue.
141146 * Earlier versions of the extension
142147 * had manually written registration functions for every type,
143148 * but those are a lot of boilerplate.
144149 * If we change back to that approach,
145150 * we should generate them automatically.
151+ * It would also be possible to move to a new RPC wire format
152+ * that specifies which values are references using metadata.
146153 */
147154 registerRefs ( o : any ) {
148155 if ( o instanceof Object ) {
149- if ( Object . keys ( o as { } ) . length === 1 && 'p' in o && typeof o . p !== 'object' ) {
150- this . finalizers . register ( o as { } , RpcPtr . copy ( o as RpcPtr < any > ) )
156+ let isRef = false
157+ if ( this . serverCapabilities . experimental ?. rpcProvider ?. rpcWireFormat === 'v1' ) {
158+ if ( Object . keys ( o as { } ) . length === 1 && '__rpcref' in o && typeof o . __rpcref === 'string' ) {
159+ this . finalizers . register ( o as { } , RpcPtr . copy ( o as RpcPtr < any > ) )
160+ isRef = true
161+ }
151162 } else {
152- for ( const v of Object . values ( o as { } ) ) this . registerRefs ( v )
163+ if ( Object . keys ( o as { } ) . length === 1 && 'p' in o && typeof o . p === 'string' ) {
164+ this . finalizers . register ( o as { } , RpcPtr . copy ( o as RpcPtr < any > ) )
165+ isRef = true
166+ }
167+ }
168+ if ( ! isRef ) {
169+ for ( const v of Object . values ( o ) ) this . registerRefs ( v )
153170 }
154171 } else if ( o instanceof Array ) {
155172 for ( const e of o ) this . registerRefs ( e )
@@ -240,9 +257,12 @@ export class RpcSessions {
240257
241258 constructor ( public iface : RpcServerIface ) { }
242259
243- private connectCore ( uri : DocumentUri ) : RpcSessionForFile {
260+ private connectCore (
261+ uri : DocumentUri ,
262+ serverCapabilities : ServerCapabilities < LeanServerCapabilities > ,
263+ ) : RpcSessionForFile {
244264 if ( this . sessions . has ( uri ) ) return this . sessions . get ( uri ) as RpcSessionForFile
245- const sess = new RpcSessionForFile ( uri , this )
265+ const sess = new RpcSessionForFile ( uri , this , serverCapabilities )
246266 this . sessions . set ( uri , sess )
247267 return sess
248268 }
@@ -254,8 +274,11 @@ export class RpcSessions {
254274 * A new session is only created if a fatal error occurs (the worker crashes)
255275 * or the session is closed manually (the file is closed).
256276 */
257- connect ( pos : TextDocumentPositionParams ) : RpcSessionAtPos {
258- return this . connectCore ( pos . textDocument . uri ) . at ( pos . position )
277+ connect (
278+ pos : TextDocumentPositionParams ,
279+ serverCapabilities : ServerCapabilities < LeanServerCapabilities > ,
280+ ) : RpcSessionAtPos {
281+ return this . connectCore ( pos . textDocument . uri , serverCapabilities ) . at ( pos . position )
259282 }
260283
261284 /** Closes the session for the given URI. */
0 commit comments