Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
| Package | Use it for |
|---|---|
| [`val_highlight`](val_highlight/) | The engine, in pure Dart: 55 languages plus a generic fallback, flat tokens, incremental updates, language detection, 8 themes, VS Code theme import and HTML output. Runs on the VM, in Flutter and on the web. |
| [`val_highlight_flutter`](val_highlight_flutter/) | Flutter widgets: `CodeView` to show code, `CodeField` to edit it, `HighlightTextController`, and `CodeTheme` for app-wide defaults. |
| [`val_highlight_flutter`](val_highlight_flutter/) | Flutter widgets: `CodeView` to show code, `HighlightTextController` for any text field, and `CodeTheme` for app-wide defaults, in any app; and `CodeField`, an editor built on `material_ui`. |

A `val_highlight_jaspr` package is planned, built on the same engine.

Expand Down Expand Up @@ -99,7 +99,7 @@ pin.

## Built by Val

`val_highlight` is part of [Val](https://useval.io), the live visual layer
`val_highlight` is part of [Val](https://useval.ai), the live visual layer
for AI agents.

## 💬 Community
Expand Down
16 changes: 10 additions & 6 deletions docs/api/_code-field.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,14 @@ the text's own layout, so wrapped lines line up.

Laying out a text field is proportional to its styled runs, so for
documents longer than `styleAllBelow` lines only the visible lines, plus
`margin` lines on each side, are colored. The window follows scrolling
`styleMargin` lines on each side, are colored. The window follows scrolling
(of the field itself or of an enclosing scroll view) and is computed from
the laid-out text, so wrapped lines are handled exactly.

The field is built on `material_ui`'s `TextField`, so it needs a
`material_ui` `MaterialApp` (or `Material`) above it. Code is always
laid out left to right, even in a right-to-left app.

```dart
const CodeField({
Key? key,
Expand All @@ -42,13 +46,13 @@ const CodeField({
ScrollController? scrollController,
TextAlignVertical? textAlignVertical,
int styleAllBelow = 300,
int margin = 150,
int styleMargin = 150,
String indent = ' ',
CodeEditing editing = const CodeEditing(),
Map<ShortcutActivator, Intent>? shortcuts,
List<TextInputFormatter>? inputFormatters,
UndoHistoryController? undoController,
bool lineNumbers = false,
bool? lineNumbers,
int firstLineNumber = 1,
TextStyle? gutterStyle,
Set<int> highlightedLines = const {},
Expand Down Expand Up @@ -76,13 +80,13 @@ Creates a code editor.
| `scrollController` | `ScrollController?` | | Scroll controller for the field's own scrolling. |
| `textAlignVertical` | `TextAlignVertical?` | | Vertical alignment of the text. |
| `styleAllBelow` | `int` | `300` | Documents with at most this many lines are colored in full. |
| `margin` | `int` | `150` | Lines colored beyond each edge of the visible area. |
| `styleMargin` | `int` | `150` | Lines colored beyond each edge of the visible area, once the document is longer than `styleAllBelow` lines. |
| `indent` | `String` | `' '` | Text inserted by Tab and removed by Shift+Tab, such as two spaces or `'\t'`. Enter keeps the current line's indentation, adding one level after an opening `{`, `[`, `(` or `:`. |
| `editing` | `CodeEditing` | `const CodeEditing()` | What typing, Enter and Backspace do beyond inserting text. Use `CodeEditing.none` for a plain text field. |
| `shortcuts` | `Map<ShortcutActivator, Intent>?` | | Keys and the intents they send. Defaults to `defaultShortcuts`; pass `const {}` for none, so Tab moves focus as in other fields. |
| `inputFormatters` | `List<TextInputFormatter>?` | | Formatters applied after the editing behavior, such as a length limit. |
| `inputFormatters` | `List<TextInputFormatter>?` | | Formatters applied after the editing behavior, such as a length limit. Edits made by shortcuts, such as Tab, go through them too. |
| `undoController` | `UndoHistoryController?` | | Controls the field's undo history, as in `TextField.undoController`. Every edit the field makes itself — an auto-closed pair, a toggled comment, an indent — is a single undo step. |
| `lineNumbers` | `bool` | `false` | Whether to show line numbers in a gutter. |
| `lineNumbers` | `bool?` | | Whether to show line numbers in a gutter. Defaults to `CodeThemeData.lineNumbers`, or `false`. |
| `firstLineNumber` | `int` | `1` | Number shown for the first line. |
| `gutterStyle` | `TextStyle?` | | Style of the line numbers. Defaults to the code style in a faded text color. |
| `highlightedLines` | `Set<int>` | `const {}` | Line numbers, as shown in the gutter, to highlight. |
Expand Down
54 changes: 38 additions & 16 deletions docs/api/_code-view.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,15 @@ Shows syntax-highlighted code.
CodeView(source, language: dartLanguage)
```

Everything else is optional. The theme follows the app's brightness
(see `CodeTheme` for app-wide defaults). Results are cached, and inputs
above `CodeHighlighter.isolateThreshold` are highlighted in a background
Everything else is optional. The theme follows the brightness (see
`CodeTheme` for app-wide defaults). Results are cached, and inputs above
`CodeHighlighter.isolateThreshold` are highlighted in a background
isolate while plain text is shown.

Code is always laid out left to right, even in a right-to-left app. To
let people select and copy it, put it — or the page around it — inside
a `SelectionArea`; line numbers are never selected.

Short code renders as one text block. Code with more than
`virtualizeAbove` lines, placed where its height is bounded, renders one
row per line in a lazy list, so only visible lines are laid out.
Expand All @@ -25,14 +29,13 @@ const CodeView(
List<Grammar> detectLanguages = const [],
Grammar fallbackLanguage = genericLanguage,
ValTheme? theme,
TextStyle? textStyle,
TextStyle? style,
EdgeInsetsGeometry? padding,
bool? lineNumbers,
int firstLineNumber = 1,
Set<int> highlightedLines = const {},
Color? highlightColor,
bool wrap = false,
bool selectable = true,
CodeHighlighter? highlighter,
int virtualizeAbove = 1000,
CodeGutterBuilder? gutterBuilder,
Expand All @@ -53,20 +56,19 @@ Creates a code view.
| `fallbackLanguage` | `Grammar` | `genericLanguage` | Language used when `language` is `null` and detection finds nothing. Defaults to `genericLanguage`, which highlights what most languages share. Use `plainTextLanguage` for no highlighting. |
| `detectLanguages` | `List<Grammar>` | `const []` | Candidates for detection when `language` is `null`. |
| `theme` | `ValTheme?` | | Color theme. Defaults to `CodeTheme` or the built-in light/dark theme. |
| `textStyle` | `TextStyle?` | | Text style merged over the default monospace style. |
| `padding` | `EdgeInsetsGeometry?` | | Padding inside the background. Defaults to `CodeTheme.padding`, or 16 on every side. |
| `lineNumbers` | `bool?` | | Whether to show line numbers. Defaults to `CodeTheme.lineNumbers`, or `false`. |
| `style` | `TextStyle?` | | Text style merged over the default code style (`CodeTheme.textStyleOf`). |
| `padding` | `EdgeInsetsGeometry?` | | Padding inside the background. Defaults to `CodeThemeData.padding`, or 16 on every side. |
| `lineNumbers` | `bool?` | | Whether to show line numbers. Defaults to `CodeThemeData.lineNumbers`, or `false`. |
| `firstLineNumber` | `int` | `1` | Number shown for the first line. |
| `highlightedLines` | `Set<int>` | `const {}` | Line numbers (as shown in the gutter) to highlight. |
| `highlightColor` | `Color?` | | Background for `highlightedLines`. |
| `wrap` | `bool` | `false` | Whether long lines wrap. When `false`, code scrolls horizontally. |
| `selectable` | `bool` | `true` | Whether the code can be selected and copied. |
| `highlighter` | `CodeHighlighter?` | | Highlighter to use. Defaults to `CodeHighlighter.shared`. |
| `virtualizeAbove` | `int` | `1000` | Line count above which rows are built lazily. |
| `gutterBuilder` | `CodeGutterBuilder?` | | Custom gutter cells. |
| `lineBuilder` | `CodeLineBuilder?` | | Custom rows. Setting this always renders one row per line. |
| `header` | `Widget?` | | A widget shown above the code, such as a title or copy button. |
| `scrollController` | `ScrollController?` | | Vertical scroll controller for the lazy list. |
| `scrollController` | `ScrollController?` | | Vertical scroll controller for the lazy list. Only attached while the code is laid out lazily (see `virtualizeAbove`). |
| `onTokenTap` | `ValueChanged<CodeToken>?` | | Called when a token is tapped, such as an identifier to jump to. Whitespace is not tappable. |
| `spanBuilder` | `CodeSpanBuilder?` | | Builds each token's span, for hover effects, links, tooltips or custom styling. Takes priority over `onTokenTap`. |

Expand Down Expand Up @@ -99,27 +101,47 @@ Creates a token.
|---|---|---|
| `scope` | `String?` | Innermost scope, or `null` for plain text. |

### `CodeLineDetails`

One line of a `CodeView`, as passed to `CodeView.gutterBuilder` and
`CodeView.lineBuilder`.

```dart
const CodeLineDetails({
required int line,
required int lineNumber,
required bool highlighted,
})
```

Creates the details of a line.

| Field | Type | Default | Description |
|---|---|---|---|
| `line` | `int` | *required* | Zero-based index of the line in the code. |
| `lineNumber` | `int` | *required* | Number shown in the gutter: `line` plus `CodeView.firstLineNumber`. |
| `highlighted` | `bool` | *required* | Whether `lineNumber` is in `CodeView.highlightedLines`. |

### `CodeGutterBuilder`

Builds the gutter cell for `lineNumber`. Return a fixed-width widget so
rows stay aligned.
Builds the gutter cell for `line`. Return a fixed-width widget so rows
stay aligned.

```dart
typedef CodeGutterBuilder = Widget Function(
BuildContext context,
int lineNumber,
bool highlighted,
CodeLineDetails line,
)
```

### `CodeLineBuilder`

Wraps the row for zero-based `line`. `child` is the default row.
Wraps the row for `line`. `child` is the default row.

```dart
typedef CodeLineBuilder = Widget Function(
BuildContext context,
int line,
CodeLineDetails line,
Widget child,
)
```
Expand Down
1 change: 1 addition & 0 deletions docs/api/_highlighting.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ cached per stack id stay valid; later results may only add stacks.
| `tokenAt(int offset)` | `int` | Index of the token covering `offset`, or `tokenCount` if `offset` is at or past the end. |
| `lineCount` | `int` | Number of lines. Empty code has one empty line, and a trailing `\n` starts a final empty line. |
| `lineStart(int line)` | `int` | Start offset of line `line`. |
| `lineAt(int offset)` | `int` | The line holding `offset`. An offset at a line break belongs to the line the break ends; offsets before the start or past the end belong to the first or last line. |
| `lineEnd(int line)` | `int` | End offset of line `line`, excluding its `\n` or `\r\n`. |
| `forEachSegment(…)` | `void` | Calls `segment` for each piece of line `line`, in order, with the piece's offsets and scope stack. Pieces never include the line break. |
| `isStringOrCommentAt(int offset)` | `bool` | Whether the character at `offset` is inside a string, comment or regular expression. Interpolations inside strings count as code. |
Expand Down
27 changes: 24 additions & 3 deletions docs/api/_libraries.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
| `package:val_highlight/languages/all.dart` | Every built-in language, for apps that want them all. |
| `package:val_highlight/themes/all.dart` | Every built-in theme. |
| `package:val_highlight/val_highlight.dart` | Fast, light, fully customizable syntax highlighting in pure Dart. |
| `package:val_highlight_flutter/material.dart` | `CodeField`, a code editor built on `material_ui`'s `TextField`: everything in `val_highlight_flutter.dart`, plus the editor. |
| `package:val_highlight_flutter/val_highlight_flutter.dart` | Flutter widgets for `val_highlight`. |

<details>
Expand Down Expand Up @@ -49,6 +50,22 @@ in `advanced.dart`.

</details>

<details>
<summary><code>package:val_highlight_flutter/material.dart</code>: more</summary>

```dart
import 'package:val_highlight_flutter/material.dart';
import 'package:val_highlight/languages/dart.dart';

CodeField(controller: HighlightTextController(language: dartLanguage))
```

The field needs a `material_ui` `MaterialApp` (or `Material`) above it.
In an app with another design system, give your own text field a
`HighlightTextController` and a `CodeEditingFormatter` instead.

</details>

<details>
<summary><code>package:val_highlight_flutter/val_highlight_flutter.dart</code>: more</summary>

Expand All @@ -59,8 +76,12 @@ import 'package:val_highlight/languages/dart.dart';
CodeView(source, language: dartLanguage)
```

This library re-exports `package:val_highlight/val_highlight.dart`, so
one import covers the core API. Languages and themes are separate
imports under `package:val_highlight/languages/` and `themes/`.
This library needs only Flutter's widgets layer, so it works in any app,
whatever its design system. The code editor, `CodeField`, is built on
`material_ui` and is in `package:val_highlight_flutter/material.dart`.

It re-exports `package:val_highlight/val_highlight.dart`, so one import
covers the core API. Languages and themes are separate imports under
`package:val_highlight/languages/` and `themes/`.

</details>
26 changes: 11 additions & 15 deletions docs/api/_text-field.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ TextField(controller: controller, maxLines: null, style: monospace);
```

Edits are highlighted incrementally: only the lines an edit can affect
are re-scanned, so typing stays fast in large documents.
are re-scanned, so typing stays fast in large documents. A plain
`TextField` still lays out and colors its whole text on every change;
for long text use `CodeField`, which colors only the lines on screen.

```dart
HighlightTextController({
Expand All @@ -23,26 +25,16 @@ HighlightTextController({

Creates a controller for `language`.

`theme` defaults to `CodeTheme` or the built-in theme for the app's
brightness. `registry` resolves languages named inside the text, such
as Markdown code fences.
`theme` defaults to `CodeTheme`'s theme for the current brightness.
`registry` resolves languages named inside the text, such as Markdown
code fences.

| Member | Type | Description |
|---|---|---|
| `window` | `({int first, int last})?` | Zero-based lines to color, or `null` for all of them. *Settable.* |
| `language` | `Grammar` | The language of the text. Setting it re-highlights everything. *Settable.* |
| `theme` | `ValTheme?` | The theme, or `null` to follow the app. *Settable.* |
| `theme` | `ValTheme?` | The theme, or `null` to follow `CodeTheme`. *Settable.* |
| `result` | `HighlightResult` | The highlight result for the current text. |

<details>
<summary><code>window</code>: more</summary>

Laying out a text field with many thousands of styled runs is slow, so
for large documents only the visible lines, plus a margin, need
colors. `CodeField` sets this automatically as you scroll.

</details>

### `CodeEditingFormatter`

Applies `CodeEditing` to text typed into a field driven by a
Expand All @@ -64,6 +56,10 @@ with hardware keyboards, on-screen keyboards and voice input. Text being
composed by an input method is never changed. Each change it makes is a
single edit, so one undo reverts it.

What it needs to remember between edits, such as which closers it
inserted, is kept on the `controller`, so creating a new formatter in
each build is fine.

```dart
CodeEditingFormatter({
required HighlightTextController controller,
Expand Down
Loading
Loading