Skip to main content

Crate colorfulcode_ffi

Crate colorfulcode_ffi 

Source
Expand description

C ABI for colorfulcode, for native apps written in Swift, Kotlin, C++, C# and friends. The matching header is include/colorfulcode.h.

Highlighting returns a flat array of cc_tokens that covers the input, and cc_theme_style maps each token kind to a color, so drawing comes down to one attribute per token.

Conventions:

  • Languages and themes are referred to by index. Indices are only stable within one build of the library, so persist ids or names instead.
  • Input strings must be UTF-8. Functions taking source code return NULL for invalid UTF-8 rather than guessing.
  • Null pointers are accepted everywhere and treated as missing input.
  • Panics never cross the boundary: they are caught, or abort in release builds where panic = "abort".

Structs§

CcStyle
How a token kind is drawn.
CcToken
A highlighted span of the input, start..end.
CcTokens
The result of cc_highlight. Opaque to C.

Constants§

CC_ANSI_256
cc_render_ansi flag: use the 256 color palette instead of 24 bit colors.
CC_HTML_LINES
cc_render_html flag: wrap every line in <span class="cc-line">.
CC_HTML_WRAP
cc_render_html flag: wrap the output in <pre class="cc"><code>.
CC_UTF16_OFFSETS
cc_highlight flag: report offsets in UTF-16 code units instead of bytes.

Functions§

cc_highlight⚠
Highlights len bytes of UTF-8 code. An unknown language index means plain text. Returns NULL for invalid UTF-8. Free with cc_tokens_free.
cc_kind_name
Lowercase name of a token kind, for example "keyword". NULL if out of range.
cc_language_count
Number of languages.
cc_language_detect⚠
Guesses the language from the content alone, scoring the first few KB against every language. Opt-in: highlighting never calls it. Returns -1 when unsure or for invalid UTF-8.
cc_language_find⚠
Finds a language by id, alias, name or extension. Returns -1 if unknown.
cc_language_from_content⚠
Guesses the language from a shebang or similar first line. Returns -1 if unknown.
cc_language_from_path⚠
Guesses the language from a file path. Returns -1 if unknown.
cc_language_id
Canonical id of a language, for example "rust". NULL if out of range.
cc_language_name
Display name of a language, for example "Rust". NULL if out of range.
cc_render_ansi⚠
Renders text with ANSI color escapes. Out of range themes mean the default theme. Takes CC_ANSI_256. Returns NULL for invalid UTF-8 or NUL bytes in the input. Free with cc_string_free.
cc_render_html⚠
Renders HTML. A valid theme index gives inline styles, -1 gives class names. Takes CC_HTML_* flags. Returns NULL for invalid UTF-8 or NUL bytes in the input. Free with cc_string_free.
cc_string_free⚠
Frees a string returned by this library. NULL is ignored.
cc_theme_background
Background color as 0xRRGGBB. Out of range means the default theme.
cc_theme_count
Number of themes.
cc_theme_css⚠
CSS for the class based HTML, scoped to selector (NULL means ".cc"). Free with cc_string_free.
cc_theme_find⚠
Finds a theme by name (case insensitive). Returns -1 if unknown.
cc_theme_foreground
Default text color as 0xRRGGBB. Out of range means the default theme.
cc_theme_is_dark
1 for a dark theme, 0 for a light one. Out of range means the default theme.
cc_theme_name
Name of a theme, for example "dracula". NULL if out of range.
cc_theme_style
Style of a token kind. Out of range themes mean the default theme and unknown kinds mean plain text.
cc_tokens_data⚠
The tokens, valid until cc_tokens_free. NULL for NULL.
cc_tokens_free⚠
Frees tokens. NULL is ignored.
cc_tokens_len⚠
Number of tokens. 0 for NULL.
cc_version
Library version, for example "0.1.0".