-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Document Color support #13122
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
I think this can potentially be a very useful feature, especially when used with UI frameworks. And I have a couple of brainstorm ideas for how this can work
#[rust_analyzer(color(rgb))]
pub struct Color {
#[rust_analyzer(color(rgb(r)))]
pub r: f32,
#[rust_analyzer(color(rgb(g)))]
pub g: f32,
#[rust_analyzer(color(rgb(b)))]
pub b: f32,
#[rust_analyzer(color(rgb(a)))]
pub a: f32,
} Values of type I annotate the For basic projects I define a few constants: const RED: Color = Color { r: 1.0, g: 0.0, b: 0.0, a: 1.0 };
// \______________________________________/ <- red
const GREEN: Color = Color { r: 0.0, g: 1.0, b: 0.0, a: 1.0 };
// \______________________________________/ <- green
const BLUE: Color = Color { r: 0.0, g: 0.0, b: 1.0, a: 1.0 };
// \________________________________/ <- blue I might also like to have a struct Theme {
primary: Color,
accent: Color,
danger: Color
}
const THEME: Theme = Theme {
primary: RED,
// \_/ <- red
accent: GREEN,
// \___/ <- green
danger: BLUE
// \__/ <- blue
} Each I might store my const CHANNELS: [Color; 3] = [RED, GREEN, BLUE];
// \_/ <- red
// \___/ <- green
// \__/ <- blue
const COLOR: Color = CHANNELS[2];
// \__________/ <- green As you see, this should work with values of type More on the attributes#[rust_analyzer(color(rgb))] This one means that a
Each field may only have 1 attribute. The types of the fields needs to be:
I assume RGB will be most used so it would be good to prioritize that.
And other channels. Some more questions that may pop up
|
Note that it is unlikely we'll try to support more color spaces than simple rgb(a) as that would require us to pull in a color library (though I think hsl might have a simple enough conversion between rgb iirc?) |
The LSP has this interesting feature for document color requests https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textDocument_documentColor which allows a client to ask a server for color-like things in a document, giving the client the ability to render color swatches and the like to edit these color-like things. Now rust as a language doesn't have the concept of first class colors, but there are several libraries that model colors and I believe we can make this feature work for those.
As an example for
palette::rgb::Rgb::new(0.0, 0.0, 0.0)
we should be able to make use of this feature. But this will certainly require information from the library/user code itself, building heuristic for this is non-feasible, but there is something we will hopefully have in the future that will allow libaries/user code to give r-a the needed information, tool attributes.So I would envision this as something like
#[rust_analyzer::color(red, green, blue)]
that you can attach to function definitions, where the components would match up with the functions arguments. Obviously this would need to be fleshed out more, are there more thing this would apply to than just function calls? What about hex strings, we wouldn't want to always interpret them as colors, but there should probably be a way to support these in some fashion as wellThe text was updated successfully, but these errors were encountered: