This commit is contained in:
Simon Cruanes 2025-04-07 14:03:39 -04:00
parent d7dd8ecec0
commit 691eac4863
No known key found for this signature in database
GPG key ID: EBFFF6F283F3A2B4

View file

@ -627,7 +627,8 @@ module Make (IO : IO) = struct
IO.return () IO.return ()
(** Override to handle unprocessed notifications *) (** Override to handle unprocessed notifications *)
method filter_text_document (_doc_uri: Lsp.Types.DocumentUri.t) : bool = true method filter_text_document (_doc_uri : Lsp.Types.DocumentUri.t) : bool =
true
(** Filter the document URI to check if we want to process it or not. (** Filter the document URI to check if we want to process it or not.
By default we accept all documents. *) By default we accept all documents. *)
@ -658,117 +659,117 @@ module Make (IO : IO) = struct
if not (self#filter_text_document doc.uri) then if not (self#filter_text_document doc.uri) then
IO.return () IO.return ()
else ( else (
Log.debug (fun k -> Log.debug (fun k ->
k "notif: did open '%s'" (DocumentUri.to_path doc.uri)); k "notif: did open '%s'" (DocumentUri.to_path doc.uri));
let notify_back = let notify_back =
new notify_back new notify_back
~uri:doc.uri ~workDoneToken:None ~partialResultToken:None ~uri:doc.uri ~workDoneToken:None ~partialResultToken:None
~version:doc.version ~notify_back ~server_request () ~version:doc.version ~notify_back ~server_request ()
in in
let st = let st =
{ {
uri = doc.uri; uri = doc.uri;
version = doc.version; version = doc.version;
content = doc.text; content = doc.text;
languageId = doc.languageId; languageId = doc.languageId;
} }
in in
Hashtbl.replace docs doc.uri st; Hashtbl.replace docs doc.uri st;
async self (fun () -> async self (fun () ->
self#on_notif_doc_did_open self#on_notif_doc_did_open
~notify_back:(notify_back : notify_back) ~notify_back:(notify_back : notify_back)
doc ~content:st.content) doc ~content:st.content)
) )
| Lsp.Client_notification.TextDocumentDidClose { textDocument = doc } -> | Lsp.Client_notification.TextDocumentDidClose { textDocument = doc } ->
if not (self#filter_text_document doc.uri) then if not (self#filter_text_document doc.uri) then
IO.return () IO.return ()
else ( else (
Log.debug (fun k -> Log.debug (fun k ->
k "notif: did close '%s'" (DocumentUri.to_path doc.uri)); k "notif: did close '%s'" (DocumentUri.to_path doc.uri));
let notify_back = let notify_back =
new notify_back new notify_back
~workDoneToken:None ~partialResultToken:None ~uri:doc.uri ~workDoneToken:None ~partialResultToken:None ~uri:doc.uri
~notify_back ~server_request () ~notify_back ~server_request ()
in in
async self (fun () -> async self (fun () ->
self#on_notif_doc_did_close self#on_notif_doc_did_close
~notify_back:(notify_back : notify_back) ~notify_back:(notify_back : notify_back)
doc) doc)
) )
| Lsp.Client_notification.TextDocumentDidChange | Lsp.Client_notification.TextDocumentDidChange
{ textDocument = doc; contentChanges = c } -> { textDocument = doc; contentChanges = c } ->
if not (self#filter_text_document doc.uri) then if not (self#filter_text_document doc.uri) then
IO.return () IO.return ()
else ( else (
Log.debug (fun k -> Log.debug (fun k ->
k "notif: did change '%s'" (DocumentUri.to_path doc.uri)); k "notif: did change '%s'" (DocumentUri.to_path doc.uri));
let notify_back = let notify_back =
new notify_back new notify_back
~workDoneToken:None ~partialResultToken:None ~uri:doc.uri ~workDoneToken:None ~partialResultToken:None ~uri:doc.uri
~notify_back ~server_request () ~notify_back ~server_request ()
in in
let old_doc = let old_doc =
match Hashtbl.find_opt docs doc.uri with match Hashtbl.find_opt docs doc.uri with
| None -> | None ->
(* WTF vscode. Well let's try and deal with it. *) (* WTF vscode. Well let's try and deal with it. *)
Log.err (fun k -> Log.err (fun k ->
k "unknown document: '%s'" (DocumentUri.to_path doc.uri)); k "unknown document: '%s'" (DocumentUri.to_path doc.uri));
let version = doc.version in let version = doc.version in
let languageId = "" in let languageId = "" in
(* FIXME*) (* FIXME*)
Lsp.Text_document.make ~position_encoding:positionEncoding Lsp.Text_document.make ~position_encoding:positionEncoding
(DidOpenTextDocumentParams.create (DidOpenTextDocumentParams.create
~textDocument: ~textDocument:
(TextDocumentItem.create ~languageId ~uri:doc.uri ~version (TextDocumentItem.create ~languageId ~uri:doc.uri
~text:"")) ~version ~text:""))
| Some st -> | Some st ->
Lsp.Text_document.make ~position_encoding:positionEncoding Lsp.Text_document.make ~position_encoding:positionEncoding
(DidOpenTextDocumentParams.create (DidOpenTextDocumentParams.create
~textDocument: ~textDocument:
(TextDocumentItem.create ~languageId:st.languageId (TextDocumentItem.create ~languageId:st.languageId
~uri:doc.uri ~version:st.version ~text:st.content)) ~uri:doc.uri ~version:st.version ~text:st.content))
in in
let new_doc : Lsp.Text_document.t = let new_doc : Lsp.Text_document.t =
Lsp.Text_document.apply_content_changes old_doc c Lsp.Text_document.apply_content_changes old_doc c
in in
let new_st : doc_state = let new_st : doc_state =
{ {
uri = doc.uri; uri = doc.uri;
languageId = Lsp.Text_document.languageId new_doc; languageId = Lsp.Text_document.languageId new_doc;
content = Lsp.Text_document.text new_doc; content = Lsp.Text_document.text new_doc;
version = Lsp.Text_document.version new_doc; version = Lsp.Text_document.version new_doc;
} }
in in
Hashtbl.replace docs doc.uri new_st; Hashtbl.replace docs doc.uri new_st;
async self (fun () -> async self (fun () ->
self#on_notif_doc_did_change self#on_notif_doc_did_change
~notify_back:(notify_back : notify_back) ~notify_back:(notify_back : notify_back)
doc c doc c
~old_content:(Lsp.Text_document.text old_doc) ~old_content:(Lsp.Text_document.text old_doc)
~new_content:new_st.content) ~new_content:new_st.content)
) )
| Lsp.Client_notification.DidSaveTextDocument params -> | Lsp.Client_notification.DidSaveTextDocument params ->
if not (self#filter_text_document params.textDocument.uri) then if not (self#filter_text_document params.textDocument.uri) then
IO.return () IO.return ()
else ( else (
let notify_back = let notify_back =
new notify_back new notify_back
~workDoneToken:None ~partialResultToken:None ~workDoneToken:None ~partialResultToken:None
~uri:params.textDocument.uri ~notify_back ~server_request () ~uri:params.textDocument.uri ~notify_back ~server_request ()
in in
async self (fun () -> async self (fun () ->
self#on_notif_doc_did_save self#on_notif_doc_did_save
~notify_back:(notify_back : notify_back) ~notify_back:(notify_back : notify_back)
params) params)
) )
| Lsp.Client_notification.Exit -> | Lsp.Client_notification.Exit ->
status <- `ReceivedExit; status <- `ReceivedExit;