ZIm/crates/markdown/examples/markdown.rs
Nathan Sobo 6fca1d2b0b
Eliminate GPUI View, ViewContext, and WindowContext types (#22632)
There's still a bit more work to do on this, but this PR is compiling
(with warnings) after eliminating the key types. When the tasks below
are complete, this will be the new narrative for GPUI:

- `Entity<T>` - This replaces `View<T>`/`Model<T>`. It represents a unit
of state, and if `T` implements `Render`, then `Entity<T>` implements
`Element`.
- `&mut App` This replaces `AppContext` and represents the app.
- `&mut Context<T>` This replaces `ModelContext` and derefs to `App`. It
is provided by the framework when updating an entity.
- `&mut Window` Broken out of `&mut WindowContext` which no longer
exists. Every method that once took `&mut WindowContext` now takes `&mut
Window, &mut App` and every method that took `&mut ViewContext<T>` now
takes `&mut Window, &mut Context<T>`

Not pictured here are the two other failed attempts. It's been quite a
month!

Tasks:

- [x] Remove `View`, `ViewContext`, `WindowContext` and thread through
`Window`
- [x] [@cole-miller @mikayla-maki] Redraw window when entities change
- [x] [@cole-miller @mikayla-maki] Get examples and Zed running
- [x] [@cole-miller @mikayla-maki] Fix Zed rendering
- [x] [@mikayla-maki] Fix todo! macros and comments
- [x] Fix a bug where the editor would not be redrawn because of view
caching
- [x] remove publicness window.notify() and replace with
`AppContext::notify`
- [x] remove `observe_new_window_models`, replace with
`observe_new_models` with an optional window
- [x] Fix a bug where the project panel would not be redrawn because of
the wrong refresh() call being used
- [x] Fix the tests
- [x] Fix warnings by eliminating `Window` params or using `_`
- [x] Fix conflicts
- [x] Simplify generic code where possible
- [x] Rename types
- [ ] Update docs

### issues post merge

- [x] Issues switching between normal and insert mode
- [x] Assistant re-rendering failure
- [x] Vim test failures
- [x] Mac build issue



Release Notes:

- N/A

---------

Co-authored-by: Antonio Scandurra <me@as-cii.com>
Co-authored-by: Cole Miller <cole@zed.dev>
Co-authored-by: Mikayla <mikayla@zed.dev>
Co-authored-by: Joseph <joseph@zed.dev>
Co-authored-by: max <max@zed.dev>
Co-authored-by: Michael Sloan <michael@zed.dev>
Co-authored-by: Mikayla Maki <mikaylamaki@Mikaylas-MacBook-Pro.local>
Co-authored-by: Mikayla <mikayla.c.maki@gmail.com>
Co-authored-by: joão <joao@zed.dev>
2025-01-26 03:02:45 +00:00

206 lines
6.3 KiB
Rust

use assets::Assets;
use gpui::{prelude::*, rgb, Application, Entity, KeyBinding, StyleRefinement, WindowOptions};
use language::{language_settings::AllLanguageSettings, LanguageRegistry};
use markdown::{Markdown, MarkdownStyle};
use node_runtime::NodeRuntime;
use settings::SettingsStore;
use std::sync::Arc;
use theme::LoadThemes;
use ui::prelude::*;
use ui::{div, App, Window};
const MARKDOWN_EXAMPLE: &str = r#"
# Markdown Example Document
## Headings
Headings are created by adding one or more `#` symbols before your heading text. The number of `#` you use will determine the size of the heading.
```rust
gpui::window::ViewContext
impl<'a, V> ViewContext<'a, V>
pub fn on_blur(&mut self, handle: &FocusHandle, listener: impl FnMut(&mut V, &mut iewContext<V>) + 'static) -> Subscription
where
// Bounds from impl:
V: 'static,
```
## Emphasis
Emphasis can be added with italics or bold. *This text will be italic*. _This will also be italic_
## Lists
### Unordered Lists
Unordered lists use asterisks `*`, plus `+`, or minus `-` as list markers.
* Item 1
* Item 2
* Item 2a
* Item 2b
### Ordered Lists
Ordered lists use numbers followed by a period.
1. Item 1
2. Item 2
3. Item 3
1. Item 3a
2. Item 3b
## Links
Links are created using the format [http://zed.dev](https://zed.dev).
They can also be detected automatically, for example https://zed.dev/blog.
They may contain dollar signs:
[https://svelte.dev/docs/svelte/$state](https://svelte.dev/docs/svelte/$state)
https://svelte.dev/docs/svelte/$state
## Images
Images are like links, but with an exclamation mark `!` in front.
```markdown
![This is an image](/images/logo.png)
```
## Code
Inline `code` can be wrapped with backticks `` ` ``.
```markdown
Inline `code` has `back-ticks around` it.
```
Code blocks can be created by indenting lines by four spaces or with triple backticks ```.
```javascript
function test() {
console.log("notice the blank line before this function?");
}
```
## Blockquotes
Blockquotes are created with `>`.
> This is a blockquote.
## Horizontal Rules
Horizontal rules are created using three or more asterisks `***`, dashes `---`, or underscores `___`.
## Line breaks
This is a
\
line break!
---
Remember, markdown processors may have slight differences and extensions, so always refer to the specific documentation or guides relevant to your platform or editor for the best practices and additional features.
"#;
pub fn main() {
env_logger::init();
Application::new().with_assets(Assets).run(|cx| {
let store = SettingsStore::test(cx);
cx.set_global(store);
language::init(cx);
SettingsStore::update(cx, |store, cx| {
store.update_user_settings::<AllLanguageSettings>(cx, |_| {});
});
cx.bind_keys([KeyBinding::new("cmd-c", markdown::Copy, None)]);
let node_runtime = NodeRuntime::unavailable();
theme::init(LoadThemes::JustBase, cx);
let language_registry = LanguageRegistry::new(cx.background_executor().clone());
language_registry.set_theme(cx.theme().clone());
let language_registry = Arc::new(language_registry);
languages::init(language_registry.clone(), node_runtime, cx);
Assets.load_fonts(cx).unwrap();
cx.activate(true);
cx.open_window(WindowOptions::default(), |window, cx| {
cx.new(|cx| {
let markdown_style = MarkdownStyle {
base_text_style: gpui::TextStyle {
font_family: "Zed Plex Sans".into(),
color: cx.theme().colors().terminal_ansi_black,
..Default::default()
},
code_block: StyleRefinement::default()
.font_family("Zed Plex Mono")
.m(rems(1.))
.bg(rgb(0xAAAAAAA)),
inline_code: gpui::TextStyleRefinement {
font_family: Some("Zed Mono".into()),
color: Some(cx.theme().colors().editor_foreground),
background_color: Some(cx.theme().colors().editor_background),
..Default::default()
},
rule_color: Color::Muted.color(cx),
block_quote_border_color: Color::Muted.color(cx),
block_quote: gpui::TextStyleRefinement {
color: Some(Color::Muted.color(cx)),
..Default::default()
},
link: gpui::TextStyleRefinement {
color: Some(Color::Accent.color(cx)),
underline: Some(gpui::UnderlineStyle {
thickness: px(1.),
color: Some(Color::Accent.color(cx)),
wavy: false,
}),
..Default::default()
},
syntax: cx.theme().syntax().clone(),
selection_background_color: {
let mut selection = cx.theme().players().local().selection;
selection.fade_out(0.7);
selection
},
..Default::default()
};
MarkdownExample::new(
MARKDOWN_EXAMPLE.to_string(),
markdown_style,
language_registry,
window,
cx,
)
})
})
.unwrap();
});
}
struct MarkdownExample {
markdown: Entity<Markdown>,
}
impl MarkdownExample {
pub fn new(
text: String,
style: MarkdownStyle,
language_registry: Arc<LanguageRegistry>,
window: &mut Window,
cx: &mut App,
) -> Self {
let markdown =
cx.new(|cx| Markdown::new(text, style, Some(language_registry), None, window, cx));
Self { markdown }
}
}
impl Render for MarkdownExample {
fn render(&mut self, _window: &mut Window, _cx: &mut Context<Self>) -> impl IntoElement {
div()
.id("markdown-example")
.debug_selector(|| "foo".into())
.relative()
.bg(gpui::white())
.size_full()
.p_4()
.overflow_y_scroll()
.child(self.markdown.clone())
}
}