insert-each.dhover×60all
#!/usr/bin/env dub
/+ dub.sdl:
name "editor-insert-each"
targetPath "build"
dependency "sparkles:ui" path="../../.."
buildType "checked" {
    buildOptions "optimize" "inline" "debugInfo"
}
+/
/// Position-aware insertion shares one editor transaction across several carets.
module 
(module) insert_each_example

Position-aware insertion shares one editor transaction across several carets.

insert_each_example
;
import
(package) std
std
.
(module) std.stdio
Category Symbols
File handles _popen File isFileHandle openNetwork stderr stdin stdout
Reading chunks lines readf readfln readln
Writing toFile write writef writefln writeln
Misc KeepTerminator LockType StdioException

Standard I/O functions that extend core.stdc.stdio. core.stdc.stdio is publically imported when importing std.stdio.

There are three layers of I/O:

  1. The lowest layer is the operating system layer. The two main schemes are Windows and Posix.

  2. C's stdio.h which unifies the two operating system schemes.

  3. std.stdio, this module, unifies the various stdio.h implementations into a high level package for D programs.

Source

std/stdio.d

@copyrightCopyright The D Language Foundation 2007-.@licenseBoost License 1.0.@authorsWalter Bright, Andrei Alexandrescu, Alex Rønne Petersen
stdio
:
(alias template) insert_each_example.writeln = std.stdio.writeln(T...)(T args)

Equivalent to write(args, '\n'). Calling writeln without arguments is valid and just prints a newline to the standard output.

Example

Reads stdin and writes it to stdout with an argument counter.

import std.stdio;

void main()
{
    string line;

    for (size_t count = 0; (line = readln) !is null; count++)
    {
         writeln("Input ", count, ": ", line);
    }
}
@paramargs the items to write to stdout@throwsIn case of an I/O error, throws an StdioException.
writeln
;
import
(package) sparkles
sparkles
.
(package) sparkles.text_buffer
text_buffer
.
(module) sparkles.text_buffer.text

Text: an immutable UTF-8 text in a persistent summary tree.

Copying a text is O(1) and allocation-free; every "modifying" operation returns a new text that shares all untouched subtrees with its arguments. Positions are byte offsets. Lines end before their line feed, or before the CR of a CR LF pair.

Never in a GC-allocated array. A text counts its references, and druntime fills a GC array, from an array literal or by appending with ~=, by blitting its elements without the copy that counts one; the collector later releases them again. Keep texts, and anything holding one (Edit, ChangeSet), in a sparkles.base.buffer buffer or a static array.

Specification

docs/specs/text-buffer/SPEC.md §2–3.

text
:
(struct) sparkles.text_buffer.text.Text

An immutable byte sequence, normally UTF-8, held in a summary tree.

Text is a Regular value: copies are independent, == compares contents, and copying is one atomic increment. It is the size of one pointer.

Text
;
import
(package) sparkles
sparkles
.
(package) sparkles.ui
ui
.
(package) sparkles.ui.editor
editor
.
(module) sparkles.ui.editor.command

The editor's command vocabulary (EDC1–EDC11).

Commands are named by effect, not by key, so any key table can bind them (KEY11). A command is a small pointer-free value: text arrives as a TextId into the host's input text pool, so a command can sit in a key table, an event queue or a recording and be compared with ==.

Specification

docs/specs/ui/editor.md §3.

command
:
(struct) sparkles.ui.editor.command.EditCommand

One editing command.

EditCommand
,
(enum) sparkles.ui.editor.command.EditKind

What a command does.

EditKind
;
import
(package) sparkles
sparkles
.
(package) sparkles.ui
ui
.
(package) sparkles.ui.editor
editor
.
(module) sparkles.ui.editor.field

EditorField: an editor ready to embed as a text field (EDU4).

It owns the editor state, a small pool for the text that keys and pastes carry, and the target's default key table. A host routes a focused field's keys and pastes to it and reads back whether the text changed, was submitted or was cancelled, and whether the field asked for the clipboard. The kill ring is the host session's when the host provides one (EDY1), else the field's own.

Specification

docs/specs/ui/editor.md §5–8.

field
:
(struct) sparkles.ui.editor.field.EditorFieldOf!(LogicalContext)
EditorField
;
import
(package) sparkles
sparkles
.
(package) sparkles.ui
ui
.
(package) sparkles.ui.editor
editor
.
(module) sparkles.ui.editor.selection

Selection ranges and the normalized selection set (EDT4).

A range has an anchor that stays and a head that moves; the head is where the caret is drawn, and a range whose anchor equals its head is a caret. A selection set holds at least one range, sorted, never overlapping, with exactly one primary.

Specification

docs/specs/ui/editor.md §1–2.

selection
:
(struct) sparkles.ui.editor.selection.EditRange

One selection range.

EditRange
,
(struct) sparkles.ui.editor.selection.SelectionSet

The editor's ranges: at least one, sorted by start, none overlapping, one primary. Copying is O(1) until one copy changes (SharedBuffer).

SelectionSet
;
void
void D main() @safe
main
() @safe
{ auto
(local variable) sparkles.ui.editor.field.EditorFieldOf!(LogicalContext) field
field
=
(struct) sparkles.ui.editor.field.EditorFieldOf!(LogicalContext)
EditorField
.
sparkles.ui.editor.field.EditorFieldOf!(LogicalContext) sparkles.ui.editor.field.EditorFieldOf!(sparkles.ui.editor.context.LogicalContext).make(scope const(char)[] text = "", sparkles.ui.editor.state.EditorOptions options = EditorOptions(false, true, IndentOptions(true, cast(ubyte)4u, false), null, Duration(5000000L), null, 0LU), sparkles.ui.editor.keys.EditorTarget target = EditorTarget.terminal) pure nothrow @nogc @safe

A field holding text, caret at its end.

make
("abc");
(struct) sparkles.ui.editor.selection.EditRange

One selection range.

EditRange
[2]
(local variable) sparkles.ui.editor.selection.EditRange[2] carets
carets
= [
(struct) sparkles.ui.editor.selection.EditRange

One selection range.

EditRange
.
sparkles.ui.editor.selection.EditRange sparkles.ui.editor.selection.EditRange.caret(ulong at) pure nothrow @nogc @safe

A caret at at.

caret
(1),
(struct) sparkles.ui.editor.selection.EditRange

One selection range.

EditRange
.
sparkles.ui.editor.selection.EditRange sparkles.ui.editor.selection.EditRange.caret(ulong at) pure nothrow @nogc @safe

A caret at at.

caret
(3)];
(local variable) sparkles.ui.editor.field.EditorFieldOf!(LogicalContext) field
field
.
(field) sparkles.ui.editor.state.EditorState sparkles.ui.editor.field.EditorFieldOf!(sparkles.ui.editor.context.LogicalContext).state
state
.
(field) sparkles.ui.editor.selection.SelectionSet sparkles.ui.editor.state.EditorState.selection
selection
=
sparkles.ui.editor.selection.SelectionSet sparkles.ui.editor.selection.SelectionSet.opAssign(sparkles.ui.editor.selection.SelectionSet p) pure nothrow @nogc return ref @trusted
SelectionSet
.
sparkles.ui.editor.selection.SelectionSet sparkles.ui.editor.selection.SelectionSet.of(scope const(sparkles.ui.editor.selection.EditRange)[] ranges, ulong primary) pure nothrow @nogc @safe

The normalized set of ranges, primary naming the primary one among them. Ranges are sorted by start, stably; two merge into one spanning both when they share any position other than the single common endpoint of two non-empty ranges. A merged range keeps the later range's direction and is primary if either was. An empty ranges gives a caret at 0.

of
(
(local variable) sparkles.ui.editor.selection.EditRange[2] carets
carets
[], 0);
(local variable) sparkles.ui.editor.field.EditorFieldOf!(LogicalContext) field
field
.
sparkles.ui.editor.field.FieldResult sparkles.ui.editor.field.EditorFieldOf!(sparkles.ui.editor.context.LogicalContext).insertEach!(string function(in sparkles.text_buffer.text.Text text, ulong position) pure nothrow @nogc @safe)(string function(in sparkles.text_buffer.text.Text text, ulong position) pure nothrow @nogc @safe insertion, core.time.MonoTimeImpl!(ClockType.normal) now = MonoTimeImpl(0L)) pure @nogc @safe

Compute inserted UTF-8 per selection from the current text and head position. All replacements share the editor's protection, length and undo machinery.

insertEach
((in
(struct) sparkles.text_buffer.text.Text

An immutable byte sequence, normally UTF-8, held in a summary tree.

Text is a Regular value: copies are independent, == compares contents, and copying is one atomic increment. It is the size of one pointer.

Text
(parameter) const(sparkles.text_buffer.text.Text) text
text
,
(alias) object.size_t = ulong
size_t
(parameter) ulong position
position
) @safe pure nothrow @nogc
=>
(parameter) ulong position
position
== 1 ? "A" : "B");
void std.stdio.writeln!string(string __param_0) @safe

Equivalent to write(args, '\n'). Calling writeln without arguments is valid and just prints a newline to the standard output.

Example

Reads stdin and writes it to stdout with an argument counter.

import std.stdio;

void main()
{
    string line;

    for (size_t count = 0; (line = readln) !is null; count++)
    {
         writeln("Input ", count, ": ", line);
    }
}
@paramargs the items to write to stdout@throwsIn case of an I/O error, throws an StdioException.
writeln
(
(local variable) sparkles.ui.editor.field.EditorFieldOf!(LogicalContext) field
field
.
string sparkles.ui.editor.field.EditorFieldOf!(sparkles.ui.editor.context.LogicalContext).text() const pure nothrow @safe

The text, copied.

text
);
(local variable) sparkles.ui.editor.field.EditorFieldOf!(LogicalContext) field
field
.
sparkles.ui.editor.field.FieldResult sparkles.ui.editor.field.EditorFieldOf!(sparkles.ui.editor.context.LogicalContext).run(sparkles.ui.editor.command.EditCommand command, core.time.MonoTimeImpl!(ClockType.normal) now = MonoTimeImpl(0L)) pure nothrow @nogc @safe

Run command.

run
(
(struct) sparkles.ui.editor.command.EditCommand

One editing command.

EditCommand
.
sparkles.ui.editor.command.EditCommand sparkles.ui.editor.command.EditCommand.of(sparkles.ui.editor.command.EditKind kind) pure nothrow @nogc @safe
of
(
(enum) sparkles.ui.editor.command.EditKind

What a command does.

EditKind
.
(enum value) sparkles.ui.editor.command.EditKind.undo = 29
undo
));
void std.stdio.writeln!string(string __param_0) @safe

Equivalent to write(args, '\n'). Calling writeln without arguments is valid and just prints a newline to the standard output.

Example

Reads stdin and writes it to stdout with an argument counter.

import std.stdio;

void main()
{
    string line;

    for (size_t count = 0; (line = readln) !is null; count++)
    {
         writeln("Input ", count, ": ", line);
    }
}
@paramargs the items to write to stdout@throwsIn case of an I/O error, throws an StdioException.
writeln
(
(local variable) sparkles.ui.editor.field.EditorFieldOf!(LogicalContext) field
field
.
string sparkles.ui.editor.field.EditorFieldOf!(sparkles.ui.editor.context.LogicalContext).text() const pure nothrow @safe

The text, copied.

text
);
}