Skip to content

Commit

Permalink
fix(codelens): Handle $emitCodeLens event (#3003)
Browse files Browse the repository at this point in the history
* Handle emitCodeLens event

* Update CHANGES
  • Loading branch information
bryphe authored Jan 16, 2021
1 parent 33bba93 commit 4cafcb8
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 12 deletions.
1 change: 1 addition & 0 deletions CHANGES_CURRENT.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
7 changes: 6 additions & 1 deletion src/Exthost/Msg.re
Original file line number Diff line number Diff line change
Expand Up @@ -710,7 +710,7 @@ module LanguageFeatures = {
| EmitCodeLensEvent({
eventHandle: int,
event: Yojson.Safe.t,
}) // ??
})
| RegisterCodeLensSupport({
handle: int,
selector: DocumentSelector.t,
Expand Down Expand Up @@ -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]),
Expand Down
28 changes: 25 additions & 3 deletions src/Feature/LanguageSupport/CodeLens.re
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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) =>
Expand Down Expand Up @@ -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)
Expand All @@ -121,6 +142,7 @@ module Sub = {
~buffer,
~startLine=topVisibleBufferLine,
~stopLine=bottomVisibleBufferLine,
~eventTick,
~toMsg,
client,
);
Expand Down
14 changes: 12 additions & 2 deletions src/Feature/LanguageSupport/Feature_LanguageSupport.re
Original file line number Diff line number Diff line change
Expand Up @@ -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})) =>
Expand Down
15 changes: 9 additions & 6 deletions src/Service/Exthost/Service_Exthost.re
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);
};

Expand All @@ -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);
Expand Down Expand Up @@ -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);
};
Expand Down
1 change: 1 addition & 0 deletions src/Service/Exthost/Service_Exthost.rei
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 4cafcb8

Please sign in to comment.