9 Racket S-Expression Indentation and Navigation

▼ |  | Syntax Color: Utilities

1 | Parenthesis Matching
2 | Lexer Contract and The Don’t-Stop Structure Type
3 | Racket Lexer
4 | Default Lexer
5 | Module Lexer
6 | Scribble Lexer
7 | Splay Tree for Tokenization
8 | Interface for Clients of Syntax Color Output
9 | Racket S-Expression Indentation and Navigation

► | 9 | Racket S-Expression Indentation and Navigation

9.1 | S-Expression Navigation
9.2 | S-Expression Indentation

On this page:

9.1 S-Expression Navigation
racket-forward-sexp
racket-backward-sexp
racket-up-sexp
racket-down-sexp
racket-stick-to-next-sexp?
9.2 S-Expression Indentation
racket-amount-to-indent
racket-tabify-table->head-sexp-type
racket-tabify-default-table

9.3
Racket
top contents ← prev up next →

## 9 Racket S-Expression Indentation and Navigation🔗ℹ

The "syntax-color" collection provides Racket indentation and navigation functions that take advantage of the token categories and parenthesis information produced by a coloring lexer. They can work with any object that implements color-textoid<%>, which is extended by color:text<%>.

Added in version 1.3.

### 9.1 S-Expression Navigation🔗ℹ

(require syntax-color/racket-navigation)
| package: syntax-color-lib

> procedure (racket-forward-sexp text pos)
> → (or/c #f exact-nonnegative-integer?)
> text : (is-a?/c color-textoid<%>)
> pos : exact-nonnegative-integer?
> procedure (racket-backward-sexp text pos)
> → (or/c #f exact-nonnegative-integer?)
> text : (is-a?/c color-textoid<%>)
> pos : exact-nonnegative-integer?
> procedure (racket-up-sexp text pos) → (or/c #f exact-nonnegative-integer?)
> text : (is-a?/c color-textoid<%>)
> pos : exact-nonnegative-integer?
> procedure (racket-down-sexp text pos)
> → (or/c #f exact-nonnegative-integer?)
> text : (is-a?/c color-textoid<%>)
> pos : exact-nonnegative-integer?

Each of these functions takes a position pos within text and returns a position corresponding to S-expression movement. The result is #f if no movement in the corresponding direction is possible.

> procedure (racket-stick-to-next-sexp? text pos) → boolean?
> text : (is-a?/c color-textoid<%>)
> pos : exact-nonnegative-integer?

Returns whether the content at pos in text corresponds to a token that should “stick” to the following parenthesized sequence for navigation purposes. For example, the result is #t when the token corresponds to ', `, or #'.

### 9.2 S-Expression Indentation🔗ℹ

(require syntax-color/racket-indentation)
| package: syntax-color-lib

> > procedure
> > (racket-amount-to-indent |  | text |
> > |  | pos |
> > | [ | #:head-sexp-type head-sexp-type] |
> > |  | #:graphical-width graphical-width) |
> → (or/c #f exact-nonnegative-integer?)
> text : (is-a?/c color-textoid<%>)
> pos : exact-nonnegative-integer?
> | head-sexp-type |  | : |  |
> (string?
> . -> .
> (or/c #f 'lambda 'define 'begin 'for/fold 'other))
> |  |  | = |  |
> (racket-tabify-table->head-sexp-type
> racket-tabify-default-table)
> | graphical-width |  | : |  |
> (or/c #f (-> (is-a?/c color-textoid<%>)
> exact-nonnegative-integer?
> exact-nonnegative-integer?
> (or/c #f exact-nonnegative-integer?)))

Returns an amount of indentation to use for the line in text that contains the position pos. The result may be more or less than the current amount of indentation on the line.

The head-sexp-type function is used to map identifiers at the start of an S-expression to the indentation rule that the identifier should use. See compute-racket-amount-to-indent in racket:text<%> for more information.

The graphical-width function is used to get the graphical width (distance between the “x” coordinates) of content in text between a start and end position. If graphical-width returns #f, then characters in text are assumed to be all the same width. If graphical-width is #f, it is treated the same as if it had been (λ (t start end) #f).

Changed in version 1.5 of package syntax-color-lib: Allow graphical-width to return #f.

> procedure (racket-tabify-table->head-sexp-type spec)
> → (string? . -> . (or/c #f 'lambda 'define 'begin 'for/fold 'other))
> | spec |  | : |  |
> (list/c (hash/c symbol? (or/c 'lambda 'define 'begin 'for/fold))
> (or/c #f regexp?)
> (or/c #f regexp?)
> (or/c #f regexp?)
> (or/c #f regexp?))

Converts a serializable representation spec of an indentation configuration to a function suitable for use with racket-amount-to-indent.

The first element of spec maps individual symbols to indentation styles. The remaining elements provide patterns to recognize identifiers with the corresponding style, in the order 'lambda, 'define, 'begin, and 'for/fold.

> value racket-tabify-default-table
> : |
> (list/c (hash/c symbol? (or/c 'lambda 'define 'begin 'for/fold))
> (or/c #f regexp?)
> (or/c #f regexp?)
> (or/c #f regexp?)
> (or/c #f regexp?))

A default configuration suitable as an argument to racket-tabify-table->head-sexp-type.
top contents ← prev up next →