![]() |
Adding a feature or a provider of program code | ![]() |
A feature of program code is a question which a provider answers for the code at the cursor: which blocks fold here, what is the outline, where is the definition of this name. The core asks it (
code-features.scm
), a command of
progs/code
uses the answer, and tree-sitter and the language servers give it from their adapters. Neither the command nor the core knows which provider answers, nor whether a provider is part of the build.
A provider is a procedure of no argument, called with the cursor where the feature is asked for, and a predicate of no argument, which tells whether it can answer there (cheap: it is called for the menus). Its answer, or #f (and '()) when it has none, which lets the next provider answer:
The blocks around the cursor which may fold, as a list of (first last) (rows of the lines of the code).
The entries (level kind name detail path) of the code, in their order: path is where the cursor goes, the names are in Cork.
Goes to the definition of the name at the cursor; #t when it did.
The items (severity start end message) of the code at the cursor (severity: 'error, 'warning or 'info; start, end: paths).
No answer is used: a provider which is available means that a server gives the diagnostics of the code, and the syntax errors are not shown there.
Where the cursor is: 'code, 'string or 'comment.
The names of the code (its definitions), which ⇥ completes where no server follows the code.
(code-provider-set! 'outline 'syntax 20 (lambda () (and (syntax-cursor-language) #t)) syntax-outline-at-cursor) |
gives the feature 'outline a provider named 'syntax of rank 20: the providers are asked by rank, the lowest first (the servers have 10, tree-sitter 20, because a server sees the other files of a project). The preference code:outline chooses one by its name instead (auto, the default, by rank; off, none). code-provider-remove! takes a provider away, and code-available-providers tells which ones can answer at the cursor.
The providers of a feature which may be left out of a build are set in its adapter (
ts-code.scm
,
lsp-code.scm
). A new adapter goes into the list code-adapters of
code-features.scm
, with a glue routine which only a build with the feature has: the adapter is loaded the first time a feature is asked for, if that routine is defined and its file is there. The adapter may use the modules of its feature directly; nothing else may (a module which needs the functions of the syntax tree loads
code-syntax.scm
).
The command, in a module of progs/code, asks the feature with code-ask and says so when no provider answers (set-message); code-available? tells whether it can be offered at the cursor.
The command is defined lazily in
init-texmacs.scm
(lazy-define), and has an entry in focus-extra-menu of
code-menu.scm
and a key in
prog-kbd.scm
, if it is for all code.
The providers which can answer register themselves in their adapters.
The suite code (
code-test.scm
) tests the command with providers of its own (names other than those of the adapters, ranks below theirs), which it removes at the end: it runs in every build. It ignores the preferences of the user (code-ignore-preferences!); a test never writes them.
src/docs/code-features.md
and the pages of this chapter say what the feature is, and the manual what the user sees (editing program code).