Code block
Store multiline code with an optional language.
code_block() registers a text block for code. It keeps its language in the language attribute and stores the source as plain text.
Try it
Section titled “Try it”Install
Section titled “Install”cargo add gpdoc-plugin-code-blockExample
Section titled “Example”Register code blocks alongside paragraphs so a block can switch between code and body text. Use this configuration in the Quickstart editor.
use gpdoc::{EditorConfig, PluginViewExt, ViewPlugin};use gpdoc_plugin_paragraph::paragraph;use gpdoc_plugin_code_block::{code_block, code_block_body};
let config = EditorConfig::builder() .plugin(paragraph()) .plugin(code_block().with_view(code_view)) .build() .expect("configure editor");
fn code_view() -> ViewPlugin { code_block_body(|block, _, _| block.content)}At the start of a paragraph, type ~~~rust and press Space. The editor turns the paragraph into a Rust code block.
Compose a code block
Section titled “Compose a code block”The renderer receives the code block and its editable source. Keep labels outside the source so they stay out of the document.
Anatomy
Section titled “Anatomy”code_block_body(...)└── div() Your wrapper ├── label Language read from the code block └── block.content Editable source textShow the language above the source
Section titled “Show the language above the source”Replace code_view in the example above.
use gpui::{div, prelude::*};
fn code_view() -> ViewPlugin { code_block_body(|block, _, _| { let language = block.node.attrs.get("language") .and_then(|value| value.as_str()) .unwrap_or("Plain text") .to_owned(); div().child(language).child(block.content) })}Place a language label above the editable source. A code block with no language displays “Plain text”.
Convert a paragraph to Rust code
Section titled “Convert a paragraph to Rust code”use gpdoc::{Editor, Node, Value};use gpdoc_plugin_code_block::Language;
let mut editor = Editor::from_document(&config, vec![Node::textblock("paragraph", "fn main() {}")])?;editor.run(gpdoc_plugin_code_block::Set(Language::Set("rust".into())))?;
let block = &editor.document().blocks()[0];assert_eq!(block.kind, "codeBlock");assert_eq!(block.attrs["language"], Value::String("rust".into()));Settings
Section titled “Settings”language
Section titled “language”Pass Language::Set(name) to select a language, Language::Keep to preserve the existing language, or Language::Clear to remove it. A new block defaults to plain text.
editor.run(gpdoc_plugin_code_block::Set(Language::Clear))?;Syntax highlighting
Section titled “Syntax highlighting”code_block() colors recognized languages using the bundled syntect definitions. If the language is missing or unknown, the highlighter checks the first line for a hint such as a shebang. Colors apply only when rendering. Both Tiptap CodeBlock and CodeBlockLowlight documents use the same codeBlock node.
GPUI follows the window’s light or dark appearance. For a code block with a fixed background, select the matching palette on its view. This example uses a light palette on an off-white background.
use gpdoc::SyntaxPalette;use gpui::{div, prelude::*};
fn code_view() -> ViewPlugin { code_block_body(|block, _, _| { div().bg(gpui::rgb(0xf9f8f6)).child(block.content) }) .node_format("codeBlock", |_, style| { style.syntax_palette = Some(SyntaxPalette::Light); })}The default Cargo features enable both gpui and syntect. The gpui feature provides code_block_body() for composing the editable block in a GPUI view. The syntect feature colors the source. To use the GPUI view helper with plain code text, set default-features = false and features = ["gpui"] on the crate dependency.
Commands
Section titled “Commands”setCodeBlock
Section titled “setCodeBlock”Convert selected text blocks where the parent permits code. The conversion removes text formatting, turns inline line breaks into newlines, and removes other inline nodes such as mentions.
editor.run(gpdoc_plugin_code_block::Set(Language::Set("rust".into())))?;toggleCodeBlock
Section titled “toggleCodeBlock”Switch to paragraphs when every selected block is code and matches the supplied attributes. Language::Keep matches any code-block language. Otherwise, convert eligible text blocks to code.
editor.run(gpdoc_plugin_code_block::Toggle(Language::Keep))?;Keyboard shortcuts
Section titled “Keyboard shortcuts”| Shortcut | Action |
|---|---|
| Mod + Alt + C | Toggle code formatting for the selected blocks. |
Start a code block while typing
Section titled “Start a code block while typing”Start a paragraph with three backticks or three tildes and press Space. To choose a language, type a lowercase name after the fence, as in ~~~rust, then press Space. The input rule consumes the fence.