Skip to content

Latest commit

 

History

History
395 lines (306 loc) · 17.2 KB

TECHNICAL.md

File metadata and controls

395 lines (306 loc) · 17.2 KB

© A.Voß – codebasedlearning.dev – Study Maths and Computer Science (AMI) with us @ FH Aachen

Codebook Technical

This document explains what the syntax means; for how a snippet is best built up, see AUTHORING.md.

Writing blocks

A CBL block is a block comment whose first non-blank interior line is a dash-framed title. The frame is what you would draw on a whiteboard anyway, and it reads as a section header even in an editor that knows nothing about Codebook. Depth is the width of the frame — four levels:

/* ---- Topic ----     level 1     bold in the outline
   /* --- Child ---    level 2     italic
   /* -- Detail --     level 3
   /* - Aside -        level 4

The framed text is the title; the remaining interior lines are the body, rendered as Markdown. Text after the frame on the same line becomes the first body line, so a one-liner is a complete block:

/* --- The member function --- it reads celsius_, so it needs an object.

   `const` promises not to modify it - the `this` pointer is
   `const Temperature*` inside. */
[[nodiscard]] double as_fahrenheit() const { return celsius_ * 9.0 / 5.0 + 32.0; }

Python uses the triple-quoted form, """ … """ or ''' … ''':

@staticmethod
def is_plausible(celsius: float) -> bool:
    """ --- The static method --- no self, no cls: a plain function in the
    class's [#namespace].

    It validates a value without needing any object at all.
    """
    return -273.15 <= celsius <= 5000.0

One block comment is one block; nothing merges, and there are no end markers — a block extends until the next one or the end of the file. The header need not sit on the opening line, so a Javadoc-style opening works too:

/**
 * ---- Static member functions ----
 *
   This snippet discusses `static` member functions … */

A title starting with a dot marks the block as unlisted: it appears neither in the outline nor in the breadcrumb — the two places that tell a reader where they are — while everything else about it stays the same: notes, folding, and refs (the dot is a marker, not part of the title, so .Setup helpers is still addressed as [#setup-helpers]). Its notes still stack under the caret, and since no crumb names it, they are headed by its own title instead. Use it for scaffolding a file needs but a reader should not have to walk past. Children of an unlisted block are listed as usual.

/* ---- .Setup helpers ---- includes and boilerplate, not a topic of its own */

What is not a block

Deliberately narrow, so the DSL collides with nothing you already write:

Form Why it is an ordinary comment
/* ----- Banner ----- */ five or more dashes — wider runs stay banners
/* ---- Title --- */ asymmetric frame: both sides must match exactly
/* -------- */ no title, so it is a separator
// ---- Title ---- line comments are never blocks, by design
int n = 0; /* ---- x ---- */ trailing comment — the block comment must be the first thing on its line

Markers are ordinary comments throughout: they never break compilation, and in an editor without Codebook they degrade to exactly what they look like.

Where to put the comment

The convention differs per language, on purpose:

  • Python — use the docstring. A bare string statement above a def is not idiomatic (Pylint flags W0105 pointless-string-statement), and having the didactic text show up in quick-doc is a feature in teaching material. Asides inside a function body are plain string statements — there is no docstring position for them.
  • C++, Kotlin — the comment sits above the definition. There is no docstring position.

Both behave identically in the panel: a block whose comment opens a construct (the line above ends with : or {) owns that opening line, so putting the caret on a signature selects the block that documents it — unless a CBL comment stands directly above that line, in which case the line belongs to it:

/* --- namespace --- */
namespace {
    /* --- `define_and_init` --- */
    void define_and_init() {

The caret on namespace { selects namespace, the caret on the function signature selects define_and_init. Without the exception the inner block would swallow the very line the outer comment was written for.

Notes are Markdown

GitHub-flavoured: bold, italics, inline code, lists, fenced code — and tables, which is why GFM rather than plain CommonMark (comparison tables are a teaching staple, and CommonMark has none). Keep tables narrow; the panel is not wide.

Images work via ![alt](img/sketch.png), resolved relative to the folder of the source file. Lists use -: a leading * is stripped as boxed-comment decoration, while */** emphasis survives.

Indentation is relative. The body's common indent is removed — a comment nested ten columns deep in a method reads like a document that starts at the left margin — and anything indented further keeps the difference, so sub-lists nest as they do anywhere else:

/* ---- Two core design principles ----
   - 'zero overhead': you only pay for what you use
     - virtual only where you ask for it
   - 'const correctness': `const` unless observable state changes */

Two Markdown rules are easy to trip over here, both inherited, neither ours: changing the bullet character (- → +) starts a new list, and a blank line inside a list makes it loose (paragraph gaps around every item).

References and embeds

Blocks are addressed by their title slug — lowercase, non-word runs to -, exactly like a GitHub heading anchor — or by an explicit label.

A : separates a qualifying ancestor chain — [code style](#final-remarks:code-style) — whose segments must match a suffix of the target's real chain. Unqualified references resolve to the first title match in file order. Cross-file refs carry a path: [TCO](doc/glossary.md#tail-call-optimization), relative to the current file.

Three forms, three intentions — the first is Markdown's own, the other two are this plugin's:

[#main-guard]      a LINK: navigates, exactly as a link does anywhere
![#main-guard]     an EMBED: the target's text, always there
!![#main-guard]    a FOLD: headline plus ▸, the text when the reader asks
[../notes.md#tco]  an explicit path works in all three

A link opens what it names: a fragment into this file selects that block (and jumps the caret when follow-caret is on), a path#ref opens that file at that block, a path without a fragment opens the file. Nothing is inlined — which is what a reader who knows Markdown expects of a link.

An embed is the image form one step further: transclusion instead of navigation. Its headline stays in the line that referred to it, link-coloured but inert on a plain click — text that is simply there should not move the editor under you — and the target's text sits in an indented block below.

A fold is the question form: the headline plus ▸, and the target's text only when clicked. It opens in the same indented block an embed uses — no header, no close button, since the arrow that opened it closes it again. A fold inside an open block replaces that block instead of nesting another one under it, so a chain of definitions stays flat — and from the first replacement on the block carries the headline of what it now shows, since the arrow above it names something else by then.

⌘-click (Ctrl elsewhere) on a headline or a fold's arrow opens the source in the editor — one gesture for "the file itself", wherever you are. A plain click keeps you in the panel: the arrow toggles, the headline does nothing.

Inside borrowed text the rule flips: in an embedded or unfolded body, a plain link is a lookup too — it replaces the block it stands in, or opens one of its own under an embed. A glossary entry is written as a document, by an author who cannot know where it will be cited; the reader, though, is in the middle of a snippet, and following that link to the file would cost them the context that made the entry worth reading. So: links in your own text take you somewhere, links in borrowed text keep you where you are — and ⌘-click is the way out when the file is what you actually want. Everything in borrowed text is resolved against the file it came from: fragments, relative paths and images alike.

A path-less fragment — #tco, in any of the three forms, short or long — is looked up in the current file first and then along glossary.path (see below). So an explicit text needs no path either:

!![#tco]                  the target's headline as the text
!![printed](#tco)         your own text, same lookup

All three forms are valid CommonMark, so a foreign renderer shows a link, a broken image, and a literal ! before a link — never junk.

Code files are read through the parser; Markdown files have no comments, so ATX headings are the blocks there — ## Code Style, depth = heading level. Slugs equal GitHub's anchors, so the same links work when reading the repository online.

Labels

A block can declare its own address, as a trailing anchor in the header:

## Background Const                     <a id="acdf"></a>

An ordinary HTML anchor, nothing invented: every Markdown renderer passes it through and shows nothing, so headings stay clean in exports and on GitHub — where it is a real anchor as well (#user-content-acdf). It can be tabbed far to the right, out of the way of the prose. name= is accepted alongside id=, and the closing tag may be omitted (<a id="acdf">) — though only the closed form is valid HTML5, so write it closed for anything that leaves the IDE.

[#acdf] now addresses that block, and the title slug (background-const) no longer does — the label replaces it, so rewording the heading cannot silently break or re-point a ref. The label is stripped from the title, so everything that shows a title shows the front part alone: the outline, the breadcrumb, the embed's headline, and the link text of [#acdf], which renders as [Background Const] ▸. Headers without an anchor keep addressing by title slug, exactly as before.

Question blocks

No extra syntax needed: put the answers in a second file on the path and write a one-liner whose body is a fold.

/* ---- Why does this not compile? ---- !![#const-ref-answer] */

What prints `cout << v1`? !![#answer-init-value]

The fold shows the target's headline and a ▸; the answer appears only when the reader clicks, and — unlike a marker inside the snippet — is genuinely not in the student's file. Which puts the burden on the headline: it is what a reader sees before deciding to unfold, so it must not answer the question by itself. Either name answers neutrally (## Answer 0x02-3) or let the headline repeat the question.

The tool window

Docked right, one tab, three parts:

  • Header — file name, Run and Debug (derived from the current file's context), a follow-caret toggle, and unfold-all / fold-all. The last three are ordinary IDE actions as well: Tools → Codebook, Find Action, and Settings → Keymap → Plugins → Codebook for a shortcut.
  • Outline — every block in file order, showing its level three ways at once: bullet (•/◦/·), indent, and emphasis (bold, italic, plain below). With follow-caret on, a single click jumps to the block; with it off, a single click only selects and shows the notes, a double click jumps.
  • Notes — a breadcrumb line (Topic ▸ child ▸ detail) over the bodies of all layers in the chain, outermost first, separated by thin dividers. So the topic set at the demo function stays visible while you read the detail under discussion.

Follow-caret (on by default) makes selection and notes track the block above the caret; switch it off to keep the panel put while working in the code. The setting is remembered per project.

Editor folding

The note body folds away — from the end of the frame line to the end of the comment — while the frame line stays visible as ordinary source. A folded file therefore reads as a table of contents interleaved with code:

/* ---- A class with all three kinds ---- ▸ …
class Temperature {

Fold-all and unfold-all act on CBL comments only (author mode vs. presentation mode), which is the one thing the IDE's native Expand/Collapse All cannot express. Both are registered actions (Fold All CBL Comments, Unfold All CBL Comments), so they can be bound to a shortcut in the keymap.

Nothing folds as a consequence of typing. Fold state belongs to the block, not to its title: it survives re-parsing, rewording the title, and a second block with the same title. A block that comes into existence while you write — an ordinary comment that gets its frame — starts open. Only opening a file starts folded, which is the presentation default.

Single-line comments get no region; there is nothing to hide.

Output linking

After a run, the Run console is parsed for section headers — by default a name line underlined with =:

using_print
===========

If a section name also occurs in the source file — as a whole word, so init does not match inside define_and_init — its first occurrence outside a comment gets a gutter icon; clicking it opens the Run window, scrolls to that section and highlights the header line.

There is no output verification. Output is illustration, never judge.

Course configuration (cbl.properties)

Course conventions live in cbl.properties files, searched upwards from the current source file and cascaded like .editorconfig: every config between the file and the project root contributes, nearer files override per key. A chapter only sets what it changes; the rest is inherited from the course root, and built-in defaults fill the remainder. The search stops at the project root, so a stray file in a home directory cannot leak in, and the configs travel with the repository — every student clone behaves the same.

Format: key = value, # for comments, values taken verbatim (no backslash escaping, so regexes are written as-is). Changes apply on save.

Key Meaning
block.level1.regex … block.level4.regex the header pattern per level. Must capture (?<title>…); the optional (?<rest>…) picks up text after the frame on the same line. Levels are independent — redefining one inherits the other three.
output.section.regex how console output is split into sections. Must bind (?<name>…); the match position marks the section start.
glossary.path comma-separated search path for path-less short refs, first hit wins. Entries resolve relative to the declaring cbl.properties, so one course-root entry serves every chapter depth.
# use '=' frames for the two top levels, keep dashes below
block.level1.regex = ^={4}(?!=)\s*(?<title>\S.*?)\s*(?<!=)={4}(?!=)\s*(?<rest>.*)$
block.level2.regex = ^={3}(?!=)\s*(?<title>\S.*?)\s*(?<!=)={3}(?!=)\s*(?<rest>.*)$

# a glossary plus an answer file
glossary.path = ./docs/glossary.md, ./docs/answers.md

An invalid pattern — uncompilable, or missing its capture group — produces a warning notification and leaves that key at its built-in default.

What is configurable is deliberate: the shape of a header is a course convention and has a key; its meaning (block comment, four levels, framed text is the title, the rest is a Markdown body) is the language and has none. sample-cpp/cbl.properties is the fully documented reference, with every key listed at its default and commented out.

Install

  • From disk: Settings → Plugins → ⚙ → Install Plugin from Disk… with the zip from plugin/build/distributions/, built via ./gradlew :plugin:buildPlugin.
  • Marketplace: listing planned.

Samples

Three self-contained sample projects — sample-cpp/, sample-python/ and sample-kotlin/ — cover the same topic (static methods) on purpose, so the panel can be compared across languages. All three use the same layout: snippets/ for the code with snippets/utils/ for shared helpers, docs/ for glossary.md and answers.md, and a cbl.properties at the root. Glossaries are per language, so nothing has to stay in sync.

cd sample-python && uv run snippets/staticmethod.py
cd sample-kotlin && gradle run
cd sample-cpp    && cmake -B cmake-build-debug && cmake --build cmake-build-debug \
                 && ./cmake-build-debug/staticmethod

Open a sample in the IDE that speaks its language: C++ needs CLion, Python needs PyCharm, Kotlin is native to IntelliJ IDEA. An IDE without support for a language produces no comment tokens, and the panel stays empty.

Development

See DEVELOPMENT.md for the full cycle — build, test, verify, sign, publish — and CHANGELOG.md for what changed when. Short version:

./gradlew :plugin:runIde        # sandbox IDEA (Java/Kotlin)
./gradlew :plugin:runClion      # C++ sandbox
./gradlew :plugin:runPycharm    # Python sandbox
./gradlew :plugin:test          # parser/model tests
./gradlew :plugin:buildPlugin   # distributable zip

License

MIT