From 4cafcb88f6b5e6f2cf296c512078851ded4b6641 Mon Sep 17 00:00:00 2001 From: Bryan Phelps Date: Sat, 16 Jan 2021 11:55:34 -0800 Subject: [PATCH] fix(codelens): Handle $emitCodeLens event (#3003) * Handle emitCodeLens event * Update CHANGES --- CHANGES_CURRENT.md | 1 + src/Exthost/Msg.re | 7 ++++- src/Feature/LanguageSupport/CodeLens.re | 28 +++++++++++++++++-- .../Feature_LanguageSupport.re | 14 ++++++++-- src/Service/Exthost/Service_Exthost.re | 15 ++++++---- src/Service/Exthost/Service_Exthost.rei | 1 + 6 files changed, 54 insertions(+), 12 deletions(-) diff --git a/CHANGES_CURRENT.md b/CHANGES_CURRENT.md index e5f1d62e7c..97e693bab1 100644 --- a/CHANGES_CURRENT.md +++ b/CHANGES_CURRENT.md @@ -61,6 +61,7 @@ - #2995 - Extensions: Fix bug with 3-param http/https request (fixes #2981) - #2999 - Extensions: Elm - fix bug with diagnostics not displaying (fixes #2640) - #3000 - Extensions: Search - Fix out-of-order search results (fixes #2979 - thanks @jakubbaron!) +- #3003 - Extensions: CodeLens - Handle the `$emitCodeLens` event ### Performance diff --git a/src/Exthost/Msg.re b/src/Exthost/Msg.re index 0c122d2580..83d6e4cf6c 100644 --- a/src/Exthost/Msg.re +++ b/src/Exthost/Msg.re @@ -710,7 +710,7 @@ module LanguageFeatures = { | EmitCodeLensEvent({ eventHandle: int, event: Yojson.Safe.t, - }) // ?? + }) | RegisterCodeLensSupport({ handle: int, selector: DocumentSelector.t, @@ -842,8 +842,13 @@ module LanguageFeatures = { Ok(RegisterDocumentHighlightProvider({handle, selector})) | Error(error) => Error(Json.Decode.string_of_error(error)) } + + | ("$emitCodeLensEvent", `List([`Int(eventHandle)])) => + Ok(EmitCodeLensEvent({eventHandle, event: `Null})) + | ("$emitCodeLensEvent", `List([`Int(eventHandle), json])) => Ok(EmitCodeLensEvent({eventHandle, event: json})) + | ( "$registerCodeLensSupport", `List([handleJson, selectorJson, eventHandleJson]), diff --git a/src/Feature/LanguageSupport/CodeLens.re b/src/Feature/LanguageSupport/CodeLens.re index cbe29f9dd8..e461825b64 100644 --- a/src/Feature/LanguageSupport/CodeLens.re +++ b/src/Feature/LanguageSupport/CodeLens.re @@ -25,7 +25,10 @@ let text = (lens: Exthost.CodeLens.t) => textFromExthost(lens); type provider = { handle: int, + maybeEventHandle: option(int), selector: Exthost.DocumentSelector.t, + // [eventTick] is incremented for each [emitCodeLens] event + eventTick: int, }; type handleToLenses = IntMap.t(list(codeLens)); @@ -55,14 +58,32 @@ type msg = lenses: list(Exthost.CodeLens.t), }); -let register = (~handle: int, ~selector, model) => { - providers: [{handle, selector}, ...model.providers], +let register = (~handle: int, ~selector, ~maybeEventHandle, model) => { + providers: [ + {handle, selector, maybeEventHandle, eventTick: 0}, + ...model.providers, + ], }; let unregister = (~handle: int, model) => { providers: model.providers |> List.filter(prov => prov.handle != handle), }; +let emit = (~eventHandle: int, model) => { + { + // Increment event tick for any matching providers + providers: + model.providers + |> List.map(provider => + if (provider.maybeEventHandle == Some(eventHandle)) { + {...provider, eventTick: provider.eventTick + 1}; + } else { + provider; + } + ), + }; +}; + // UPDATE let update = (msg, model) => @@ -103,7 +124,7 @@ module Sub = { |> List.filter(({selector, _}) => Exthost.DocumentSelector.matchesBuffer(~buffer, selector) ) - |> List.map(({handle, _}) => { + |> List.map(({handle, eventTick, _}) => { let toMsg = fun | Error(msg) => CodeLensesError(msg) @@ -121,6 +142,7 @@ module Sub = { ~buffer, ~startLine=topVisibleBufferLine, ~stopLine=bottomVisibleBufferLine, + ~eventTick, ~toMsg, client, ); diff --git a/src/Feature/LanguageSupport/Feature_LanguageSupport.re b/src/Feature/LanguageSupport/Feature_LanguageSupport.re index 977235667d..52425f4e70 100644 --- a/src/Feature/LanguageSupport/Feature_LanguageSupport.re +++ b/src/Feature/LanguageSupport/Feature_LanguageSupport.re @@ -135,8 +135,18 @@ let update = ) | Pasted(_) => (model, Nothing) - | Exthost(RegisterCodeLensSupport({handle, selector, _})) => - let codeLens' = CodeLens.register(~handle, ~selector, model.codeLens); + | Exthost(EmitCodeLensEvent({eventHandle, _})) => + let codeLens' = CodeLens.emit(~eventHandle, model.codeLens); + ({...model, codeLens: codeLens'}, Nothing); + + | Exthost(RegisterCodeLensSupport({handle, selector, eventHandle})) => + let codeLens' = + CodeLens.register( + ~handle, + ~selector, + ~maybeEventHandle=eventHandle, + model.codeLens, + ); ({...model, codeLens: codeLens'}, Nothing); | Exthost(RegisterDefinitionSupport({handle, selector})) => diff --git a/src/Service/Exthost/Service_Exthost.re b/src/Service/Exthost/Service_Exthost.re index b9f0a26e29..b87a6ee55c 100644 --- a/src/Service/Exthost/Service_Exthost.re +++ b/src/Service/Exthost/Service_Exthost.re @@ -681,19 +681,21 @@ module Sub = { type codeLensesParams = { handle: int, + eventTick: int, client: Exthost.Client.t, buffer: Oni_Core.Buffer.t, startLine: int, // One-based start line stopLine: int // One-based stop line }; - let idFromBufferRange = (~handle, ~buffer, ~startLine) => { + let idForCodeLens = (~handle, ~buffer, ~startLine, ~eventTick) => { Printf.sprintf( - "%d-%d-%d:%d", + "%d-%d-%d:%d-%d", handle, Oni_Core.Buffer.getId(buffer), Oni_Core.Buffer.getVersion(buffer), startLine, + eventTick, ); }; @@ -708,8 +710,8 @@ module Sub = { }; let name = "Service_Exthost.CodeLensesSubscription"; - let id = ({handle, buffer, startLine, _}: params) => - idFromBufferRange(~handle, ~buffer, ~startLine); + let id = ({handle, buffer, startLine, eventTick, _}: params) => + idForCodeLens(~handle, ~buffer, ~startLine, ~eventTick); let init = (~params, ~dispatch) => { let active = ref(true); @@ -799,11 +801,12 @@ module Sub = { }; }); - let codeLenses = (~handle, ~buffer, ~startLine, ~stopLine, ~toMsg, client) => { + let codeLenses = + (~handle, ~eventTick, ~buffer, ~startLine, ~stopLine, ~toMsg, client) => { let startLine = EditorCoreTypes.LineNumber.toOneBased(startLine); let stopLine = EditorCoreTypes.LineNumber.toOneBased(stopLine); CodeLensesSubscription.create( - {handle, buffer, client, startLine, stopLine}: codeLensesParams, + {handle, buffer, client, startLine, stopLine, eventTick}: codeLensesParams, ) |> Isolinear.Sub.map(toMsg); }; diff --git a/src/Service/Exthost/Service_Exthost.rei b/src/Service/Exthost/Service_Exthost.rei index 9d7e0526e1..95e640fed7 100644 --- a/src/Service/Exthost/Service_Exthost.rei +++ b/src/Service/Exthost/Service_Exthost.rei @@ -131,6 +131,7 @@ module Sub: { let codeLenses: ( ~handle: int, + ~eventTick: int, ~buffer: Oni_Core.Buffer.t, ~startLine: EditorCoreTypes.LineNumber.t, ~stopLine: EditorCoreTypes.LineNumber.t,