#Component with lifetime

19 messages · Page 1 of 1 (latest)

old dove
#

I use cosmic-text editor (external crate) that I keep in component per entity here: https://github.com/StaffEngineer/velo/blob/85f75bd0859638d5f33c8ada1eab093618b51ae1/crates/bevy_cosmic_edit/src/lib.rs#L65

Now I want to extend it to keep SyntaxEditor (that is a wrapper of Editor): https://github.com/pop-os/cosmic-text/blob/bfb5eefbfa869915e47824877af68a5307cf301c/src/edit/syntect.rs#L32 and ViEditor (that is a wrapper of SyntaxEditor): https://github.com/pop-os/cosmic-text/blob/bfb5eefbfa869915e47824877af68a5307cf301c/src/edit/vi.rs#L19

The issue is SyntaxEditor and ViEditor has lifetimes, so is there a way to keep them on component... or is there any other better way to solve this issue? Any advice is welcome.

* I access editor in different systems through component
** Editor, SyntaxEditor, ViEditor are all implement Edit trait

#

ping @gritty turtle

brisk plover
#

I'm not 100% sure about the details, but you may want to take a look at https://crates.io/crates/ouroboros. Maybe it'll allow you to put the syntax/vi editor and the things they refer to in the same component?

old dove
#

Looks a bit complicated... considering my nooby rust experience... but thanks for suggestion.

sullen adder
#

@old dove I think there might be some ways, but one important consideration might be to figure out how long these components with lifetimes live, relative to the bevy app

#

do they outlive the bevy app?

#

or does the thing they are referencing live at least as long as the bevy app?

#

what we kind of have to think about is what the references mean if the thing that they are referencing end up dying while the app is running

#

@old dove another thing to think about is whether editor should be a Component, or a Resource

#

for this line: https://github.com/pop-os/cosmic-text/blob/bfb5eefbfa869915e47824877af68a5307cf301c/src/edit/syntect.rs#L34

why can't you just have the SyntaxEditor carry around the SyntaxSystem? why is the SyntaxSystem an external struct, which the SyntaxEditor only carries a reference to? in some sense, the two are tightly coupled, right? why not put them (and other things) together like this:

/// A wrapper of [`Editor`] with syntax highlighting provided by [`SyntaxSystem`]
pub struct SyntaxEditor {
    editor: Editor,
    syntax_system: SyntaxSystem,
    syntax: SyntaxReference,
    theme: Theme,
    highlighter: Highlighter,
    syntax_cache: Vec<(ParseState, HighlightState)>,
}
old dove
#

@sullen adder I don't have power over cosmic-text, this is external crate I am using.

#

I use it inside my bevy app, so the max lifetime they can have the same as bevy app

#

if they die some part of bevy app will be broken in theory

old dove
#

Okay, I tested ViEditor it's too feature incomplete.. and syntax highlighting I can integrate externally using syntect by myself, so I don't need SyntaxEditor & ViEditor from cosmic-text for now, only Editor without lifetime.

sullen adder
#

@old dove okay, but what about the choice between Resource and Componet for editor? what is the thinking there?

old dove
#

Do you see any advantages to put it in resource? Currently every widget (entity) has component with editor, so it's pretty convenient.

sullen adder
old dove
#

Makes sense, I love that it's attached to an entity via component though