From d4f401b9896b810d13b3d7d304ce56f40c8d7ede Mon Sep 17 00:00:00 2001 From: Steven Hugg Date: Tue, 3 Nov 2026 20:53:06 +0100 Subject: [PATCH] urust: show gutter for header windows Header windows (the read-only view of toolchain and shared include files, and urust modules) show the code-offset gutter for lines the build compiled, the same as source files, and follow the *Show line numbers* setting. Headers that emit no code show an empty gutter. --- CHANGELOG.md | 2 ++ src/docs/editor.md | 3 +- src/ide/views/editors.ts | 53 ++++++++++++++++++++++++++++++++--- src/ide/views/gutter.ts | 5 ++-- test/unit/testlistingfiles.ts | 15 ++++++++++ 5 files changed, 70 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9df84bd4..b3f3e69a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -48,6 +48,8 @@ See [extension/CHANGELOG.md](extension/CHANGELOG.md). ### Editor and UI +- Header windows (the read-only view of toolchain and shared include files, and urust modules) show the code-offset gutter for lines the build compiled, the same as source files, and follow the *Show line numbers* setting. Headers that emit no code show an empty gutter. + - On large screens a small **filter button** next to the Project Selector opens a menu that narrows its examples to one category, or to **Local Files** or **Repositories** when the platform has them (**All** shows everything). While a filter is active the button turns amber and the top of the project list says **Filtering by ...**. The picker holds a fixed width there, so changing the filter or its options doesn't resize it. The choice is remembered per platform; on smaller screens the menu is hidden and the full list is shown. - Clicking the link on an urust `use rs8::nes::util::…` line now opens the child module (`nes/util.rs`) instead of the parent `nes.rs`. A `use` of a child module resolves to its own file, while an item of the parent (`rs8::nes::PPUCTRL`, `rs8::math::sin`) still opens the module; the bundled sources now include the `rs8//` subdirectories, and a test keeps the child list in step with what the package ships. diff --git a/src/docs/editor.md b/src/docs/editor.md index ec24ac43..0d6e13da 100644 --- a/src/docs/editor.md +++ b/src/docs/editor.md @@ -19,7 +19,8 @@ The sidebar lists one entry per file in the project: Address**. - **Toolchain headers** — clicking the badge next to an `#include` line opens the referenced header from the toolchain's bundled include - filesystem in a read-only viewer. + filesystem in a read-only viewer. Lines that compile to code show + their hex offsets in the gutter, as in source files. - **Binary files** — binary files open in a read-only hex dump. ## Gutter diff --git a/src/ide/views/editors.ts b/src/ide/views/editors.ts index 15cdb101..be03cd51 100644 --- a/src/ide/views/editors.ts +++ b/src/ide/views/editors.ts @@ -105,6 +105,15 @@ function getEditorSelectionText(view: EditorView): string { return view.state.sliceDoc(range.from, range.to); } +/** Executable line offsets for the gutter, keyed by source line. */ +function sourceLineOffsetMap(sourcefile: SourceFile): Map { + const offsets = new Map(); + for (var info of (sourcefile && sourcefile.lines) || []) { + if (info.offset >= 0) offsets.set(info.line, hex(info.offset & 0xffff, 4)); + } + return offsets; +} + export class SourceEditor implements ProjectView { // Whether to highlight recently-executed lines from live trace data. // Off by default (probing has a runtime cost); toggled via toolbar button. @@ -574,15 +583,12 @@ export class SourceEditor implements ProjectView { this.clearErrors(); var lstlines = this.sourcefile.lines || []; - const newOffsets = new Map(); + const newOffsets = sourceLineOffsetMap(this.sourcefile); const newBytes = new Map(); const newClocks = new Map(); for (var info of lstlines) { //if (info.path && info.path != this.path) continue; - if (info.offset >= 0) { - newOffsets.set(info.line, hex(info.offset & 0xffff, 4)); - } if (info.insns) { var insnstr = info.insns.length > 9 ? ("...") : info.insns; newBytes.set(info.line, insnstr); @@ -1190,6 +1196,8 @@ export class HeaderView implements ProjectView { } currentPath: string; languageCompartment = new Compartment(); + sourcefile: SourceFile; + dirtylisting = true; constructor(public fn?: string) { } @@ -1208,15 +1216,32 @@ export class HeaderView implements ProjectView { EditorState.allowMultipleSelections.of(true), drawSelection(), highlightActiveLine(), + highlightActiveLineGutter(), highlightSelectionMatches(), + // line numbers follow the user setting, like SourceEditor; the + // offset gutter below is the default view of code in headers + ...settingsExtensions(loadSettings()), + foldGutter(), search({ top: true }), keymap.of(ideSearchKeymap), this.languageCompartment.of(lang), mbo, editorTheme, + // offsets for the lines that the build actually compiled + offset.field, + offset.gutter, EditorState.readOnly.of(true), + EditorView.updateListener.of(update => { + for (let effect of update.transactions.flatMap(tr => tr.effects)) { + if (effect.is(currentPcMarker.runToLine)) { + const pc = this.sourcefile && this.sourcefile.line2offset.get(effect.value); + if (pc >= 0) runToPC([pc]); + } + } + }), ], }); + registerEditor(this.view); this.refresh(false); return div; } @@ -1232,6 +1257,25 @@ export class HeaderView implements ProjectView { effects: this.languageCompartment.reconfigure(lang), changes: { from: 0, to: this.view.state.doc.length, insert: text } }); + // replacing the whole document drops the gutter markers, so they have to + // be applied again even if the listing itself is unchanged + this.dirtylisting = true; + this.refreshListing(); + } + + // show the listing's code offsets for this header, when the build produced + // any (a declarations-only header has none) + refreshListing() { + if (!this.view || !this.currentPath) return; + var lst = current_project.getListingForFile(this.currentPath); + var sourcefile = (lst && lst.sourcefile) || null; + if (sourcefile !== this.sourcefile) { + this.sourcefile = sourcefile; + this.dirtylisting = true; + } + if (!this.dirtylisting) return; + this.view.dispatch({ effects: offset.set.of(sourceLineOffsetMap(sourcefile)) }); + this.dirtylisting = false; } refresh(moveCursor: boolean) { @@ -1243,6 +1287,7 @@ export class HeaderView implements ProjectView { fn = this.fn = decodeURIComponent(hash.substring('#headerview/'.length)); } this.loadIncludeFile(fn); + this.refreshListing(); } async loadIncludeFile(fn: string) { diff --git a/src/ide/views/gutter.ts b/src/ide/views/gutter.ts index f479d1b8..e8de3a81 100644 --- a/src/ide/views/gutter.ts +++ b/src/ide/views/gutter.ts @@ -1,9 +1,8 @@ import { RangeSet, StateEffect, StateField } from "@codemirror/state"; import { gutter, GutterMarker } from "@codemirror/view"; -import { hex } from "../../common/util"; import { canUseBreakpoints } from "../breakpoints"; -const setOffset = StateEffect.define>(); +const setOffset = StateEffect.define>(); const setBytes = StateEffect.define>(); const setClock = StateEffect.define>(); // clicking the gutter only *requests* a toggle; the breakpoint store @@ -34,7 +33,7 @@ const offsetField = StateField.define>({ for (let [line, val] of map.entries()) { if (line >= 1 && line <= tr.state.doc.lines) { const pos = tr.state.doc.line(line).from; - ranges.push(new OffsetMarker(hex(val, 4)).range(pos)); + ranges.push(new OffsetMarker(val).range(pos)); } } value = RangeSet.of(ranges, true); diff --git a/test/unit/testlistingfiles.ts b/test/unit/testlistingfiles.ts index d30d575c..ae991993 100644 --- a/test/unit/testlistingfiles.ts +++ b/test/unit/testlistingfiles.ts @@ -52,4 +52,19 @@ describe('CodeProject.getListingForFile', function () { assert.strictEqual(lst.sourcefile.line2offset.get(5), 0x200); }); + // the read-only header view asks for the listing of a library module + // (urust keys its listings by the module's path) + it('finds the listing for a library module opened in the header view', function () { + const proj = makeProject('main.rs'); + proj.processBuildListings({ listings: { + 'main.rs': { lines: [{ line: 3, path: 'main.rs', offset: 0xc000, iscode: true }] }, + 'rs8/nes/util.rs': { lines: [{ line: 70, path: 'rs8/nes/util.rs', offset: 0xc100, iscode: true }] }, + } } as any); + const lst = proj.getListingForFile('rs8/nes/util.rs'); + assert.strictEqual(lst.sourcefile.line2offset.get(70), 0xc100); + assert.strictEqual(proj.getListingForFile('rs8/nes/nothing.rs'), undefined); + // the header view names it relative to the shared filesystem's /rs8 + assert.strictEqual(proj.getListingForFile('nes/util.rs')?.sourcefile.line2offset.get(70), 0xc100); + }); + });