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
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,14 @@ to HTML (best of 5 runs, on a MacBook):

| Lines | Highlight + HTML |
|---|---|
| 600 | 1.5 ms |
| 2,400 | 4.3 ms |
| 9,600 | 13.9 ms |
| 600 | 1.6 ms |
| 2,400 | 4.4 ms |
| 9,600 | 14.4 ms |

`just bench-languages` measures about 256 KB of realistic source per
language on native code. It averages about 43 MB/s, from 30 MB/s (HTML) to
64 MB/s (Markdown). `just bench-js` runs the same test compiled to
JavaScript, where it averages about 62 MB/s.
62 MB/s (Markdown). `just bench-js` runs the same test compiled to
JavaScript, where it averages about 55 MB/s.

With incremental highlighting, one keystroke in the 9,600-line file takes
about 0.4 ms. Frame timings for scrolling and typing in Flutter are in the
Expand All @@ -61,7 +61,7 @@ about 0.4 ms. Frame timings for scrolling and typing in Flutter are in the

- **One pass, no copying.** The scanner walks the source by offset, never
copies it, and time grows in step with input size.
- **First-character dispatch.** Each rule's pattern is analysed once to find
- **First-character dispatch.** Each rule's pattern is analyzed once to find
the characters it can start with. The scanner skips positions where no
rule can start, and tries only the rules that can. Rules anchored with `^`
are tried only at line starts. States with a rule that can start almost
Expand Down
14 changes: 7 additions & 7 deletions docs/api/_code-field.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ 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 coloured. The window follows scrolling
`margin` 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.

Expand Down Expand Up @@ -75,12 +75,12 @@ Creates a code editor.
| `onChanged` | `ValueChanged<String>?` | | Called when the user edits the text. |
| `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 coloured in full. |
| `margin` | `int` | `150` | Lines coloured beyond each edge of the visible area. |
| `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. |
| `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 behaviour, such as a length limit. |
| `inputFormatters` | `List<TextInputFormatter>?` | | Formatters applied after the editing behavior, such as a length limit. |
| `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. |
| `firstLineNumber` | `int` | `1` | Number shown for the first line. |
Expand All @@ -97,7 +97,7 @@ Creates a code editor.

### `CodeEditing`

Editing behaviour for `CodeField`: what typing, Enter and Backspace do
Editing behavior for `CodeField`: what typing, Enter and Backspace do
beyond inserting text.

Brackets, quotes and comment markers come from the language's
Expand All @@ -110,7 +110,7 @@ CodeField(
)
```

Use `CodeEditing.none` to turn every behaviour off.
Use `CodeEditing.none` to turn every behavior off.

```dart
const CodeEditing({
Expand All @@ -122,7 +122,7 @@ const CodeEditing({
})
```

Creates editing behaviour. Everything is on by default.
Creates editing behavior. Everything is on by default.

| Field | Type | Default | Description |
|---|---|---|---|
Expand Down
61 changes: 18 additions & 43 deletions docs/api/_grammar.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,12 @@ Base constructor for rules.
Matches `pattern` and marks the match with `scope`.

`captures` maps capture group numbers inside `pattern` to scopes. Captured
groups must appear in order and must not overlap.
groups must appear in order and must not overlap. Each captured text is
located by searching the match after the previous capture, so it must
not also occur earlier in that stretch: in `\w+\.(\w+)` on `ab.a`, the
capture `a` would mark the first `a`. Where that can happen, match only
the part to scope and put the context in a lookaround, as in
`(?<=\w\.)\w+`.

```dart
const TokenRule(
Expand Down Expand Up @@ -244,7 +249,8 @@ compiled.

The built-in blocks cover what most languages repeat: comments, strings,
numbers, operators and annotations, so most grammars need no regular
expressions. Extend this class to make your own blocks.
expressions. Extend this class to make your own blocks; subclasses must
be `final`, `base` or `sealed`.

```dart
const BlockRule()
Expand Down Expand Up @@ -409,17 +415,18 @@ Creates a number block.
Runs of operator characters, such as `+`, `==` or `>>=`.

```dart
const Operators([
String chars = '+-*/%=<>!&|^~?:',
const Operators(
String chars, {
String scope = Scopes.operator,
])
})
```

Creates an operator block matching runs of `chars`.
Creates an operator block matching runs of `chars`, such as
`'+-*/%=<>!&|^~?:'`.

| Field | Type | Default | Description |
|---|---|---|---|
| `chars` | `String` | `'+-*/%=<>!&\|^~?:'` | Characters that make up operators. |
| `chars` | `String` | *required* | Characters that make up operators. |
| `scope` | `String` | `Scopes.operator` | Scope of operators. |

### `Annotation`
Expand All @@ -428,10 +435,10 @@ Annotations, attributes and decorators, such as `@override` or
`@app.route`.

```dart
const Annotation([
const Annotation({
String sigil = '@',
String scope = Scopes.meta,
])
})
```

Creates an annotation block starting with `sigil`.
Expand All @@ -447,10 +454,10 @@ Preprocessor lines such as `#include <x>` or `#define X 1`, including
lines continued with a trailing backslash.

```dart
const Preprocessor([
const Preprocessor({
String sigil = '#',
String scope = Scopes.meta,
])
})
```

Creates a preprocessor block starting with `sigil`.
Expand All @@ -472,38 +479,6 @@ Common heuristics for identifiers that are not keywords, for use in
| `static memberCall` | `TokenRule` | After a `.`, a called name is a function (`list.add(`). |
| `static member` | `TokenRule` | After a `.`, any other name is a plain member, never a keyword. |

### `cKeywords`

C keywords (C23 included).

```dart
const List<String> cKeywords
```

### `cTypes`

C built-in and standard types.

```dart
const List<String> cTypes
```

### `cLiterals`

C literals.

```dart
const List<String> cLiterals
```

### `cCommonRules`

Rules shared by C, C++ and Objective-C.

```dart
const List<Rule> cCommonRules
```

### `EditingSyntax`

How code in a language is edited: its comment markers, brackets and
Expand Down
41 changes: 17 additions & 24 deletions docs/api/_highlighting.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -50,21 +50,30 @@ tree of objects. Token `i` covers `code[tokenStart(i), tokenEnd(i))` and
carries a scope stack id from `scopes`. Tokens always cover all of `code`
with no gaps, and neighbouring tokens never share a stack.

A result never changes once made, though its `scopes` table may gain
stacks (see there). It can be sent to another isolate as it is.

| Field | Type | Description |
|---|---|---|
| `code` | `String` | The highlighted source. |
| `language` | `Grammar` | Grammar used to highlight `code`. |
| `scopes` | `ScopeTable` | Scope names and stacks referenced by the tokens. |
| `tokenCount` | `int` | Number of tokens. |

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

Results from one `IncrementalHighlighter` share a table that grows as
the document is edited. A stack id never changes meaning, so styles
cached per stack id stay valid; later results may only add stacks.

</details>

| Member | Type | Description |
|---|---|---|
| `HighlightResult.fromBuffers({…})` | `HighlightResult` | Creates a result from raw token buffers. |
| `tokenStart(int index)` | `int` | Start offset of token `index`. |
| `tokenEnd(int index)` | `int` | End offset (exclusive) of token `index`. |
| `tokenStack(int index)` | `int` | Scope stack id of token `index`. See `scopes`. |
| `rawEnds` | `Int32List` | The raw token end offsets, for engines that copy tokens in bulk. Only the first `tokenCount` entries are valid. |
| `rawStacks` | `Int32List` | The raw token stack ids, alongside `rawEnds`. |
| `tokens` | `Iterable<HighlightToken>` | All tokens, in order. |
| `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. |
Expand All @@ -74,18 +83,9 @@ with no gaps, and neighbouring tokens never share a stack.
| `isStringOrCommentAt(int offset)` | `bool` | Whether the character at `offset` is inside a string, comment or regular expression. Interpolations inside strings count as code. |
| `isCommentAt(int offset)` | `bool` | Whether the character at `offset` is inside a comment. |
| `isInsideStringOrComment(int offset, [EditingSyntax? syntax])` | `bool` | Whether text typed at caret position `offset`, between `code[offset - 1]` and `code[offset]`, would land inside a string or comment. |
| `matchingBracket(…)` | `int` | Offset of the bracket matching the one at `offset`, or `-1` if the character at `offset` is not a bracket in code, or has no match within `limit` characters. |
| `matchingBracket(int offset, {String? brackets, int limit = 1 << 20})` | `int` | Offset of the bracket matching the one at `offset`, or `-1` if the character at `offset` is not a bracket in code, or has no match within `limit` characters. |
| `toHtml([HtmlRenderer renderer = const HtmlRenderer()])` | `String` | Renders the result as HTML. See `HtmlRenderer` for options. |

<details>
<summary><code>HighlightResult.fromBuffers</code>: more</summary>

Engines use this, and so can code that moves results between isolates.
`ends` and `stacks` must have at least `tokenCount` entries. Pass
`lineStarts` if they are already known.

</details>

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

Expand All @@ -101,9 +101,10 @@ the end of a line comment or of an unterminated string is inside.
<summary><code>matchingBracket</code>: more</summary>

`brackets` lists pairs as consecutive opening and closing characters,
as in `EditingSyntax.brackets`. Brackets inside strings and comments
are skipped, so `f(")")` matches correctly. The search walks tokens,
jumping over a whole string or comment at once.
and defaults to the `language`'s `EditingSyntax.brackets`. Brackets
inside strings and comments are skipped, so `f(")")` matches
correctly. The search walks tokens, jumping over a whole string or
comment at once.

</details>

Expand Down Expand Up @@ -133,18 +134,10 @@ the outside in, such as `string > interpolation > keyword`. Stack `0` is
the root and has no scope. A parent always has a smaller id than its
children, so renderers can resolve styles in a single forward pass.

```dart
ScopeTable()
```

Creates an empty table containing only the root stack.

| Member | Type | Description |
|---|---|---|
| `static root` | `int` | Id of the root stack, which has no scope. |
| `stackCount` | `int` | Number of stacks, including the root. |
| `nameId(String name)` | `int` | Returns the id of scope `name`, adding it if needed. |
| `push(int stack, String name)` | `int` | Returns the stack made of `stack` with scope `name` pushed on top. |
| `parentOf(int stack)` | `int` | Parent of `stack`, or `-1` for the root. |
| `scopeOf(int stack)` | `String?` | Innermost scope of `stack`, or `null` for the root. |
| `scopesOf(int stack)` | `List<String>` | Scopes of `stack` from the outermost to the innermost. |
Expand Down
2 changes: 1 addition & 1 deletion docs/api/_incremental.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ languages named inside the code, such as Markdown code fences.
| Field | Type | Description |
|---|---|---|
| `registry` | `LanguageRegistry?` | Languages for embedded regions named in the code. |
| `lastRescannedLines` | `int` | Lines re-scanned by the last `update`, for diagnostics. |

| Member | Type | Description |
|---|---|---|
| `lastRescannedLines` | `int` | Lines re-scanned by the last `update`, for diagnostics. |
| `result` | `HighlightResult` | The current result. |
| `language` | `Grammar` | The current language. Setting it re-highlights everything. *Settable.* |
| `update(String text)` | `HighlightResult` | Updates the document to `text` and returns the new result. |
26 changes: 20 additions & 6 deletions docs/api/_lookup.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
Languages addressable by name, alias, file name or file extension.

```dart
LanguageRegistry([
Iterable<Grammar> languages = const [],
LanguageRegistry(
Iterable<Grammar> languages, {
Grammar? fallback,
])
})
```

Creates a registry containing `languages`.
Expand All @@ -24,7 +24,7 @@ the registry does not have.
| Member | Type | Description |
|---|---|---|
| `languages` | `List<Grammar>` | Registered languages, in registration order. |
| `add(Grammar language, {List<String> aliases = const []})` | `void` | Registers `language` under its name, its aliases and any extra `aliases`. Later registrations replace earlier ones for the same key. |
| `add(Grammar language, {List<String> aliases = const []})` | `void` | Registers `language` under its name, its aliases and any extra `aliases`. Later registrations replace earlier ones for the same key. Adding a language again only adds the new `aliases`. |
| `lookup(String nameOrAlias)` | `Grammar?` | Language registered as `nameOrAlias`, or `null`. |
| `resolve(String nameOrAlias)` | `Grammar?` | Language registered as `nameOrAlias`, else `fallback`. |
| `resolveFileName(String fileName)` | `Grammar?` | Language for `fileName`, matched as `forFileName` does, else `fallback`. |
Expand Down Expand Up @@ -93,7 +93,7 @@ A detected language and how confident the detector is.
const Detection(
Grammar language,
double score, {
String reason = 'content',
DetectionReason reason = DetectionReason.content,
})
```

Expand All @@ -103,7 +103,21 @@ Creates a detection result.
|---|---|---|---|
| `language` | `Grammar` | *required* | The detected language. |
| `score` | `double` | *required* | Detection score. Higher is more confident; only useful for comparing candidates for the same input. |
| `reason` | `String` | `'content'` | Why this language won: `file name`, `shebang` or `content`. |
| `reason` | `DetectionReason` | `DetectionReason.content` | Which check picked this language. |

### `DetectionReason`

Which check picked a `Detection`'s language.

```dart
enum DetectionReason { fileName, shebang, content }
```

| Value | Description |
|---|---|
| `fileName` | The file name or extension. |
| `shebang` | The `#!` first line. |
| `content` | The content score. |

### `allLanguages`

Expand Down
2 changes: 1 addition & 1 deletion docs/api/_rendering.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ List<TextSpan> documentSpans(HighlightResult result, CodeStyles styles)
Spans covering every character of `result` exactly, as a text field
needs. When `composing` is a valid range, that range is underlined.

Only code within `styled` (all of it by default) is coloured; the rest
Only code within `styled` (all of it by default) is colored; the rest
is plain text, which keeps huge documents cheap to lay out. Adjacent
pieces with the same style share one span.

Expand Down
8 changes: 4 additions & 4 deletions docs/api/_text-field.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ as Markdown code fences.

| Member | Type | Description |
|---|---|---|
| `window` | `({int first, int last})?` | Zero-based lines to colour, or `null` for all of them. *Settable.* |
| `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.* |
| `result` | `HighlightResult` | The highlight result for the current text. |
Expand All @@ -39,7 +39,7 @@ as Markdown code fences.

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
colours. `CodeField` sets this automatically as you scroll.
colors. `CodeField` sets this automatically as you scroll.

</details>

Expand All @@ -49,7 +49,7 @@ Applies `CodeEditing` to text typed into a field driven by a
`HighlightTextController`.

`CodeField` installs one automatically. Add one to a plain `TextField`'s
`inputFormatters` to get the same behaviour there:
`inputFormatters` to get the same behavior there:

```dart
TextField(
Expand Down Expand Up @@ -77,5 +77,5 @@ Creates a formatter for `controller`.
| Field | Type | Default | Description |
|---|---|---|---|
| `controller` | `HighlightTextController` | *required* | The controller whose highlighting tells strings and comments apart. |
| `editing` | `CodeEditing` | `const CodeEditing()` | The behaviour to apply. |
| `editing` | `CodeEditing` | `const CodeEditing()` | The behavior to apply. |
| `indent` | `String` | `' '` | One level of indentation, for Enter. |
4 changes: 2 additions & 2 deletions docs/api/_theme-list.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@
| `dimTheme` | `themes/dim.dart` | `dim` | yes | `#c4c8ce` on `#22262c` | A soft, low-glare dark theme. |
| `midnightTheme` | `themes/midnight.dart` | `midnight` | yes | `#d8e0f0` on `#0e1628` | A deep blue dark theme. |
| `forestTheme` | `themes/forest.dart` | `forest` | yes | `#d6e2d0` on `#15201a` | A green-tinted dark theme. |
| `highContrastLightTheme` | `themes/high_contrast_light.dart` | `high-contrast-light` | no | `#000000` on `#ffffff` | A high-contrast light theme: every colour has at least 7:1 contrast. |
| `highContrastDarkTheme` | `themes/high_contrast_dark.dart` | `high-contrast-dark` | yes | `#ffffff` on `#000000` | A high-contrast dark theme: every colour has at least 7:1 contrast. |
| `highContrastLightTheme` | `themes/high_contrast_light.dart` | `high-contrast-light` | no | `#000000` on `#ffffff` | A high-contrast light theme: every color has at least 7:1 contrast. |
| `highContrastDarkTheme` | `themes/high_contrast_dark.dart` | `high-contrast-dark` | yes | `#ffffff` on `#000000` | A high-contrast dark theme: every color has at least 7:1 contrast. |
Loading
Loading