# Liminalia Studio

## Search and editing

**Search and commands** (⌘K; "⌘K" in the top bar, or the search button in the bar of the phone app) is a panel where you type a name to open something (`palette.ts`, `panes/overlays.ts`).
It searches files, scenes (for the open file, its current labels before saving), bookmarks, screens, characters, commands, assets and variables in a single list, and shows the kind on the right.
When you choose a result, files and scenes open at that line, screens open that screen, characters open that character on the Characters screen, assets open that file on the Assets screen, and variables open that variable on the Variables screen (each is added to the back history).
The commands are the ones in the top bar, the "…" menu and the script heading menu (test play, play from the cursor line, export, search and replace across files, check, the all-routes test, block view and code view, collapsing commands, comments, importing text, change history, showing panels, text size, color scheme, language, Refresh, the getting started tour, plugin commands and so on), with their keys on the right if they have any.
It ignores the difference between hiragana and katakana (the two Japanese kana scripts), between full-width and half-width characters, and between upper and lower case. A result matches if it contains the typed characters in order, and results that match by name come before those that match only by description or kind (up to 80 results).
Scenes (and the scene at the top of a file) can also be found by their flowchart title (`extra.flowchart.titles` in liminalia.json), their first line of text (from `GET /map`, the same as the flowchart; for the open file, the current document before saving),
and their tags and status (see "Edit tags and status" below; `#tag` and the status name). The title or the first line is shown faintly after the name. These three only match when the typed characters appear together
(after matches on the name and before matches on the description; the titles are read again each time ⌘K opens).
Variables are reloaded when the panel opens and added to the list once they have loaded.
When nothing matches what you typed (and at the end of the list), "Search all files for "…"" is shown; choosing it searches all files for those words (`QuickOpen.fallback`).
The actions also include "Find in this file" and "Replace in this file" (in the script heading's menu too), so the block view's find and replace opens on the mobile studio, which has no ⌘F.

**Ruby** (small reading aids printed above Japanese characters): select characters in the text (dialogue and narration) and press ⌘⌥R ("Add ruby" in ⌘K, or in the script heading menu) to turn them into `{r base|reading}` (`model/ruby.ts`).
If the glossary (`extra.glossary` in `liminalia.json`) has a `reading` for the word, the reading is filled in too; if not, the cursor is placed where you type the reading (between `|` and `}`).
In the code view, ruby goes on the selected characters; in the block view, on the characters you select while editing the text in place (if you aren't editing, a notice tells you to start editing the cursor line, select the characters and press ⌘⌥R).
Ruby isn't added to the speaker's name, inside markup (`{…}`), or to a selection that spans lines. "Add ruby from glossary readings" in the script heading menu (and in ⌘K) goes through the open file
scene by scene (from one label to the next) and adds ruby to the first occurrence of each word that has a reading (longer words first; it skips a word that already has ruby in that scene, and doesn't look inside `@define`; ⌘Z undoes it).

**File tabs**: when you open two or more script files, tabs for the open files appear above the script heading (`file-tabs.ts`; the name is the file name without the folder, and hovering shows the name with its folder).
A file you open is added to the right of the current tab, and when there are more than 10, the oldest ones other than the current file are removed. Click a tab to go to that file, and close it with × or the middle button (if it is the current file, the file of the tab on its right opens, or the one on its left if there is none).
Right-click a tab for "Close tab," "Close other tabs" and "Open to the side" ("Split view" below). The tab order is remembered per project in that device's localStorage (files that are gone are removed). They aren't shown in the phone app.

**Split view**: "Open to the side" in the right-click menu (and "…") of a file tab or a file in the scene list, or "Open a file to the side" in ⌘K, opens another file in the code view to the right of the script (`split-pane.ts`).
You can edit the file on the side in place too; automatic saving, bringing in outside changes and choosing between conflicting changes work as in the script (it has its own saving).
Breakpoints (click to the left of a line number), bookmarks (⌘⌥K) and comment marks (click one to show that line's thread in the right panel; add one with ⌘⌥M) can also be added and removed in the side file as in the script,
and while it is open, that editor moves the marks. While you are typing in the side file, the preview (the stage and "State at this line") and the comments in the right panel follow that file's cursor line
(the state at that point is taken from the lines around it), and when input returns to the script, they go back to the script's cursor line. ⌘⌥P also starts a test play from the cursor line of the side file.
Right-click in the line number column for "Play from Here," "Add comment," "Breakpoint" and "Bookmark" for that line.
The panel heading has the file name and save status, "Swap with the main editor" (swaps them, keeping each cursor line) and a close button (also in ⌘K).
The same file isn't opened in both (a file open in the script can't be put on the side, and if you open the side file in the script, it is saved and the split view closes).
Drag the border with the script to change the width (remembered in `liminalia.studio.width--split-w` in that device's localStorage; double-click to go back to half; neither panel gets narrower than 240px).
While the split view is open, the right panel uses its narrower width (340px), and each panel's width decides its margins and its summary cards. It isn't shown in the phone app.

Choose "Open containing folder" from the menu of a file in the scene list or of an open file's tab to open the folder that contains that script; in the Mac Finder and Windows Explorer, the script is selected. If you've changed where scenarios are saved, or the script is in a subfolder, it follows that location. On Linux, it opens the parent folder. It isn't shown in the version that runs only in the browser.

**Scene markers**: Click "Scene markers" in the scene list to list the `// @scene` comments in the project's scripts in a popover, with their files and lines. It closes with the close button, Esc or a click outside, and on a phone also with the back gesture. The order follows the scene list's name order or story order, and within a file, the line order. The field filters by date, event and file name, and clicking a marker goes to that line. "Add at current line" inserts a comment before the cursor line, and the list reflects changes being edited too. For example, write `// @scene August 27, Kanade visits` or `// @scene date=8/27 event=visit`. When a value contains a space, put it in quotation marks, as in `event="Kanade visits"`. The date can also be text such as "next day." A marker doesn't become a game command or a jump target.

Hover over or click the problems and the character count at the top of the script to see a breakdown. The problems are the errors, warnings and info in the open file; click one to go to that line. For the count in this scene, it shows the line range from the label to just before the next label (from the start of the file if there's no label) and how the characters are counted. Scene markers don't divide this range. When the screen is too narrow to show the character count, the same explanation opens from the script's "…" menu.

**Bookmarks**: marks put on lines (`editor/bookmarks.ts`). ⌘⌥K (in the block view, also "Bookmark" in a line's right-click menu, or "Toggle bookmark on the cursor line" in ⌘K) adds or removes one on the cursor line.
The code view shows a colored line at the left edge of the line, and the block view shows a mark to the left of the line (click it to remove). The mark is placed at the start of the line, so it stays on the same line even when lines are added above it.
A "Bookmarks" list appears at the top of the scene list (sorted by file name and line order; it shows the content of the line, the file and the line. Click one to go to that line, or right-click it to remove it), and bookmarks are listed in ⌘K too.
F2 and ⇧F2 go to the next and previous bookmark in the open file. Bookmarks are remembered in `bookmarks` in the project's `.liminalia/studio.json` (file → line, the content of that line (up to 80 characters, shown in the list) and a hash of the whole content), and aren't included in the game.
For a file open in the split view, that editor moves the marks while it is open too. When a file that isn't open is rewritten elsewhere (another editor, the split view, replace, a plugin, Git and so on,
detected when the file's hash differs from when the marks were last placed), the marks are put back, as with breakpoints, on the line with the same content (the one nearest the original line if there are several), or else on a similar line
(if none is found, they keep the original line number; `model/bookmarks.ts`). Remembered marks are also put back onto the current content when a file is opened. The dev server moves the marks of a renamed file (renaming, below),
and when a scene is moved to another file or a file is split, the marks of the moved lines move to the lines where they ended up. Marks of files that are gone aren't listed.
When saving to Studio's data file, only the entries for files changed on this screen are written over a freshly reloaded copy of it (so marks the dev server moved aren't lost).

**Copy scene text**: from the right-click menu (and "…") of a scene in the scene list or a scene heading in the block view, the script heading menu (the cursor's scene) or ⌘K,
copies only the text of that scene to the clipboard (for proofreading or handing to an AI; `model/scene-text.ts`). Commands, comments, labels, empty lines and the inside of macros are left out.
Dialogue becomes `Speaker「line」` or `Speaker "line"` (on a line whose name box name was changed with `{name …}`, that name), narration stays as it is, and choices become `Choice: text`, one per line.
Inside the text, ruby becomes `base《reading》` (the Japanese convention, the same as in the recording script), `{br}` becomes a line break and embedded values become `［expression］`, and click waits and inline commands are removed.
Scenes in files that aren't open are copied from the content on disk.

**Play from Here** starts a test play from a line or a scene (`play-launch.ts`; the save is built in `model/play-from.ts`). It is in the right-click menu (and "…") of lines, pages and scene headings in the block view,
the right-click menu (and "…") of scenes in the scene list (including the first scene of a file), the right-click menu of flowchart cards, and "Play from the Cursor Line" in the script heading menu and ⌘⌥P (the cursor line, in both the code view and the block view).
It opens in the same window as "Test play" (in the desktop app, the app's test play window; in a browser, another tab; in the phone app, over the screen). It works the same for scripts in subfolders (`part1/chapter2`).
The player's `?start=file#label&skip=N` only starts from the beginning of that scene in an empty state (variables at their initial values, no background, sprites or BGM) and fast-forwards to page N, so it isn't used.
Instead, from the current position data (`GET /sweep`), Studio builds a save with the background, sprites, BGM and variables just before that line runs, and starts from it (built the same way as "Play" on the timeline):

- For text and choice lines, the save of that line's state (the same as the preview). For a label line, the state right after passing that label (the state on arriving there by a jump)
- For other lines (commands and so on), the save of the file's page is the skeleton (the `@call` return points and the `@let` values), the position is that line's command, and the look of the game screen and the variables come from
  the nearest earlier line that has a state (or, if there is none, from the point of entering the file; `Sweep.base.stage`)
- Choice lines and lines inside `@script` and `@html` start from their `@choice` or `@script` line. Empty lines and comments start from the next command. It can't start from inside a macro (`@define`), so it starts from the beginning of that scene with variables at their initial values, and says so
- For a line the current route doesn't pass, it first looks for a route that does (`GET /sweep/reach`, searching the same way as the preview, for up to 15 seconds). If none is found, it starts from the state of the line before and says so

The save is put in localStorage (`PLAY_FROM_KEY`), and the player opens with `/?play=1&from=<id>&start=<scene>`. The player loads the save with the matching id using `loadSave` (only when served by the dev server; `packages/web/src/studio-play-from.ts`),
and if it can't, starts from the beginning of the `?start=` scene. To allow sound to play, it first shows "クリックで開始" (Click to start). The desktop app asks with `STUDIO_PLAY` with the same query, and the app passes it straight on to the test play window.
`temp.` variables, which aren't part of a save (`SaveData`), aren't carried over.
The part that looks up the current position and builds the save (`findPlayStart`) is also used by "Play from Here" in the VS Code extension (`@liminalia/studio/play-from`). The extension can't write to the browser's localStorage,
so it leaves the save with the dev server through `POST /play-from`, and the page `GET /play-from?id=&start=` puts it in localStorage and then moves to the same `/?play=1&from=<id>&start=<scene>` (`server/play-from.ts`; what it is given is only kept in memory and not written to files).

**Test play tools** (only when served by the dev server; `packages/web/src/play-dev.ts` in the player; not included in exported games):

- When you fix the script, a test play in progress shows "The script changed" and "Reload from here" (the dev server reports script changes with
  `liminalia:scenario-changed`; a test play keeps running the script it loaded, so fixing it alone changes nothing). Reloading keeps the current state
  (including the history, the record of choices and the play time), reloads the script and continues from the current page (`SystemController.reloadHere`; even if lines have shifted, it finds the same page the same way as loading).
  ⇧R reloads too. It doesn't show this on the title screen (the next play started reads the new script). When plugin code or project data changes, the whole player reloads as before
- ⇧E and "Open this page in the script" in the menu at the top right open the line of the current page (for choices, those choices) in Studio. In a browser, it's passed through a same-origin BroadcastChannel
  (`TEST_PLAY_CHANNEL`), and if no Studio receives it, the test play shows "The studio isn't open". In the desktop app, it's passed from the test play window's preload
  (`preload-test-play.ts`) through the app, and the app brings the Studio window to the front. "Project → Reload from Here" and
  "Open This Page in the Script" in the app's menu (only while a test play is open) do the same
- Runtime problems (a missing screen, an error in an expression and so on; they also go to the console) are shown small at the top right as "N warnings" and "N errors", and clicking lists them in a menu (click one to open that line in Studio).
  They're also sent to Studio's check, in the "Found in test play" category. Notices that a save position shifted because of script changes aren't sent
- ⇧R and ⇧E aren't in the player's default key assignments. Even if the project assigns these keys, the test play gets them first

**Go to Jump Target**: opens the jump target of `@jump`, `@call`, a choice, or a parameter of the jump target type (including `@dialog target=` and `{@…}` in the text) (`goto-target.ts`).
In the code view, hold ⌘ (Ctrl except on the Mac) and point at a jump target to underline it, and ⌘-click to go there (`editor/goto-link.ts`; ⌘-clicking a collapsed command card also goes to that line's jump target).
F12 goes to the jump target of the cursor line (in the code view, the one at the cursor position, or else the first one on the line). In the block view, click "→ jump target" on a choice, ⌘-click the line, or choose "Go to Jump Target" in the line's right-click menu.
If it is in another file (including `part1/chapter2` in a subfolder), that file opens, and where you were is added to the back history (go back with ⌘[). It finds and resolves the target the same way as "Go to Definition" in the VS Code extension
(`goto.ts` in `@liminalia/lang`; for the open file, it resolves with the current labels before saving). For a jump target decided by an expression or one that can't be found, it doesn't move and says so.

**Go to Macro Definition**: on a macro call (`@name` on a line, `@with name`, or `{@name}` in the text), F12 and ⌘-click (the name is underlined in the code view) also go to the macro's `@define` line
(a jump target on the line comes first; the open file is searched in its current document before saving, and other files with `defines` of each file from `/project`; `goto-target.ts`).
F12 on an `@define` line, ⇧F12 on the cursor line, and "Lines using this macro" in the block view's right-click menu list the lines that use the macro in a menu, in the same form as "Lines Leading Here"
(the line's text, the file and the line; click one to go there; `GET /macro/uses`, `server/macros.ts`; it includes lines inside `@if` and `@define`, and `{@…}` in text and choices; changes waiting to be saved are saved first).
⌘K also shows "Go to macro definition" and "Lines using this macro" while the cursor line is a macro. Completion and hover read the macros of the open file with `@liminalia/lang` (`defineSpec`)
and also offer the macros of files brought in with `@use` (with candidates for the `asset(folder)`, `cast` and `enum(…)` types and the summary from the comment right above `@define`; `macroSpecs` in `editor/complete.ts`).

**Lines Leading Here**: the right-click menu of a scene in the scene list, a scene heading in the block view or a flowchart card, and the script heading menu, list the lines that lead to that scene in a menu
(how it gets there, the file and the line; click one to go to that line). It is built by following the arrows of `GET /map`, the same data as the flowchart, in reverse (`model/incoming.ts`), so it includes `@jump`, `@call`, choices, commands with a jump target
(including what plugin commands report they might jump to, and the candidates for jump targets given as expressions) and scenes continuing from the previous one, and it never disagrees with the lines in the flowchart. Changes waiting to be saved are saved before it looks.
