Skip to content
This repository was archived by the owner on Mar 3, 2023. It is now read-only.
This repository was archived by the owner on Mar 3, 2023. It is now read-only.

API Proposal: Decoration layers #9137

Closed

Description

@nathansobo

Note: This document is currently aspirational, and describes the hypothetical design of an API that is yet to be implemented.

/cc @as-cii @maxbrunsfeld

Overview

Decoration layers provide an alternative API to marker-based decorations for situations in which creating markers is too expensive. Two examples are spell check and find and replace. With decoration layers, instead of creating a marker for every single decoration in the buffer, package authors can perform work incrementally to describe only the decorations for the currently-visible portion of the buffer.

Creating a decoration layer

Decoration layers are created by calling the addDecorationLayer method with a single function.

let layer = textEditor.addDecorationLayer(function (visibleBufferRange) {
  return [ /* ... decoration descriptors ... */ ]
})

Returning decorations

When necessary, the editor will call this function with a Range object representing the currently visible portion of the screen, in buffer coordinates. A full range is used rather than row numbers because due to soft wrapping, only a portion of a line in the buffer might be visible. Buffer coordinates are used rather than screen coordinates because they map more directly to most kinds of analyses required for decorations, such as scanning for search matches or highlighting misspellings.

To ensure a smooth visual experience, the decoration layer function is synchronous. We may want to discuss allowing decoration layers to return promises, but this would lead to decorations flickering into view and is unacceptable for our initial use cases.

The return value of a decoration layer's function should be an array of decoration descriptor objects. Each descriptor can contain any of the parameters passed to decorateMarker to create marker-based decorations, such as type, class, etc. In addition, each descriptor object must contain a bufferRange key, describing the range to which the decoration applies. Returned ranges should all overlap with the visibleBufferRange passed to the function.

Decoration layer methods

The DecorationLayer object returned by the call to addDecorationLayer has two methods, update and destroy.

Decoration layer functions will be called automatically when the contents of their editor changes. When decorations change for other reasons, you'll need to manually inform the editor that your layer needs to be re-rendered by calling update. This method is asynchronous, meaning a call to update only schedules a view update for the next animation frame. We can potentially return a promise that resolves when the update is completed if it's needed.

To remove the decoration layer, call destroy on it.

Implementation

Interpolation

Decoration layers need to be fast enough not to impose undue overhead when the visible range changes on the editor. However, performing a full-screen search on every keystroke is untenable. The solution to this problem is interpolation.

When the buffer changes, should debounce calls into user-defined decoration functions so as to only perform them at the end of a sequence of changes. In the meantime, we'll want to keep decorations in the correct logical location during edits by maintaining a local marker index. When changes occur to the buffer, we'll apply them to the layer's marker index synchronously, but we won't re-query the user-defined function for each change.

I'd like to use this as a chance to test drive our new 1-d marker index library. Changes will need to be converted to absolute coordinates in order to use it, but it's the cleanest and fastest implementation and I'd like to migrate to using it in TextBuffer if it works out in this use case. We shouldn't store decorations as markers on the TextBuffer, because there's a ton of overhead associated with creating mutable markers that's unnecessary in this case. Interacting at a low level directly with an index will have much lower overhead.

To avoid the need to row-and-column-based points to absolute character offsets repeatedly, we may want to change the buffer APIs to include offset information. We'll need to calculate this information in the buffer anyway to accomodate use of the new marker index.

Another tool that might be useful to aggregate changes is a patch data structure. This allows a sequence of potentially-overlapping splice operations to be recorded into the patch, and then be played back in a coalesced form asynchronously. Including the offset information and just writing synchronously into the marker index is probably easier, but it's worth keeping in mind that an efficient change-tracking data structure exists.

Updates to the view/model relationship

Currently, we return visible decorations from the model to the view in an object that's indexed by marker id. I think we should add a new, private method to the model that's intended to be consumed by the view. It should return an array of decoration descriptors, each containing a range, so that the view layer no longer needs to interact with markers. This will make it possible to unify our handling of marker- and layer-based decorations in the view in a single code path. The model will be in charge of querying the marker store and decoration layers and combining everything into a single array of data to be consumed.

Activity

  1. maxbrunsfeld commented on Oct 14, 2015

    @maxbrunsfeld
    Contributor

    ❤️ this so far.

  2. as-cii commented on Oct 14, 2015

    @as-cii
    Contributor

    Looks good to me! Two minor concerns:

    1. Do you think we should rename visibleBufferRange to bufferRange? It seems reasonable not to expose this information to the decoration layer, as sometimes (if I understood correctly) we'll invoke the function with only part of the visible buffer range ('cause we could have the other part already).
    2. What about implementing the decoration layer as a Disposable? This just means to rename destroy with dispose: it seems more consistent with other APIs we have used across the codebase.

    Great write-up, this sounds pretty solid!

  3. nathansobo commented on Oct 14, 2015

    @nathansobo
    ContributorAuthor

    It seems reasonable not to expose this information to the decoration layer, as sometimes (if I understood correctly) we'll invoke the function with only part of the visible buffer range ('cause we could have the other part already).

    Good question. As for the name, I'm not strongly attached to including the word visible. But as for the API, I don't think we should try to piece together decorations from two different calls. The problem is that a call may return a decoration that overlaps with a previous call, and then we have to intersect them to avoid this. Since decorations don't have identifiers, this sounds complicated and expensive. I think each call to the layer's user-defined function should be treated as filling an entire screen. If the user wants to maintain some additional state in their function for optimization purposes on their own, they can do that.

    What about implementing the decoration layer as a Disposable? This just means to rename destroy with dispose: it seems more consistent with other APIs we have used across the codebase.

    So far every object implementing Disposable has been only a disposable. In the case of custom gutters, we went with destroy. Other objects like TextEditors and Panes also implement destroy. @maxbrunsfeld and I debated renaming all existing destroy methods to dispose when Disposables were first introduced, but decided against it. My somewhat hand-wavy reasoning is that disposables are a claim on some resource that is no longer needed, whereas objects that can be destroyed are more first class entities in the domain. It's very possible that we should have bit the bullet and renamed the destroy methods earlier, but I think naming this consistently with custom gutters is the best option for now.

  4. abe33 commented on Oct 14, 2015

    @abe33
    Contributor

    This looks promising, but I may have a concern regarding find-and-replace and its minimap binding.

    If there's no longer any marker created for the matches I won't be able to follow the changes in the results without having some kind of notifications (whether when the search term is modified or when text is inserted while the search is active and the decorations move).

    Currently, I use the onDidCreateMarker to identify the find-and-replace markers and create the decorations on the minimap and then I listen to their range changes to update the canvas accordingly. With the new API it would be necessary to have methods and events so that I can know when I need to update the decorations.

    Or maybe I could also use the function provided as decoration layer with the minimap visible range as argument to get them? But this requires to do twice the work (more than twice as we can fit way more decorations in the minimap).

    Another thing that comes to mind, is that it has to be clear that the decoration layer should not do any processing behind returning the decoration descriptors. For instance if the find-and-replace was performing the search only in the provided range, then there's a high probability that some matches will never be displayed (if a match has a part outside the range, the search won't find it). But I guess this is what you envisioned with this proposal.

  5. nathansobo commented on Oct 19, 2015

    @nathansobo
    ContributorAuthor

    After thinking more, I still think we should try to create markers for everything so minimap continues to work. I have a new proposal in #9208.

  6. lock commented on Jan 19, 2019

    @lock

    This issue has been automatically locked since there has not been any recent activity after it was closed. If you can still reproduce this issue in Safe Mode then please open a new issue and fill out the entire issue template to ensure that we have enough information to address your issue. Thanks!

  7. locked as resolved and limited conversation to collaborators on Jan 19, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions