Skip to content

Make phantoms work better with inline(ghost) text #5536

@timfjord

Description

@timfjord

Problem description

Currently, there are 4 fundamental issues that make phantoms API not very suitable for displaying inline(ghost) text

  1. The phantoms API doesn't provide an option to guarantee that a phantom will be inserted from the right of the cursor (if you want to insert it in your current position).
2022-08-17.14.40.54.mov

It is mentioned here, here and also here and I would say, this one is the most critical because it makes using phantom API almost impossible for ghost text-like things.

There is a workaround with inserting a phantom in sublime.Region(current_position + 1, current_position) and then another empty block or below phantom in sublime.Region(current_position, current_position), but unfortunately it works only in around 80% of cases and the cursor still jumps(can be seen on the video above), plus it adds an empty phantom under the current line which doesn't look good

  1. The phantoms API doesn't work very with multi-line text

First of all, there is simply no way to insert a multi-line text with the last line assigned with the rest of the text from the current line

my_method(|)
          ^ insert multiple line phantom here

my_method({
  "key1": "value",
  "key2": "value"
}|)
 ^ the phantom text ends here

Another problem is that since the phantoms API uses minihtml under the hood it makes it hard to work with symbols like \n, \t, or . So you need to replace spaces with &nbsp; and either add <br> or split the text into lines and render with divs(and make sure line_padding_* settings are respected).

  1. When you insert a block phantom into the current line the background colour of the block as well as the gutter is highlighted with the line_highlight colour

Знімок екрана 2022-08-18 о 10 45 44

There is also a workaround with making sure the block phantom overlays the entire screen and applying background-color: var(--background); rule but there is no way to control the background colour of the gutter column.

image

  1. There is always an empty space around an inserted phantom

Знімок екрана 2022-08-18 о 10 50 30

I would say this one is the less critical issue but it would be nice to be able to control that margin somehow

Preferred solution

Since all the minihtml features aren't required so I think there should be a separate API for working with phantom text.

It could be a mix of phantoms API regions API and, say

add_phantom_text(key: str, text_regions: Dict[Region, str], scope: str ='')
get_phantom_text(key: str) -> Dict[Region, str]
erase_phantom_text(key: str)

# or even introduce the `TextRegion` class to maintain the order of text regions 

add_phantom_text(key: str, text_regions: [TextRegion], scope: str ='')
get_phantom_text(key: str) -> [TextRegion]
erase_phantom_text(key: str)

The API should handle all the issues described above

Alternatively, there could be a separate layout option for the existing Phantom class(say, LAYOUT_INLINE_TEXT) that would handle all the cases above(ideally including the multi-line text rendering)

Alternatives

There are a few workarounds for how to make the phantom API works better with inline text, but, unfortunately, they don't really work in all cases(see point 1 and point 3)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions