Skip to content

fix(better-files): colour files and code blocks outside render - #44

Open
sebastian-quack wants to merge 1 commit into
munza:mainfrom
sebastian-quack:fix/better-files-colour-outside-render
Open

sebastian-quack wants to merge 1 commit into
munza:mainfrom
sebastian-quack:fix/better-files-colour-outside-render

Conversation

@sebastian-quack

Copy link
Copy Markdown

Summary

Opening a 466-line index.html in Better Files showed this instead of the file:

This view could not be rendered

interrupted
    at run (mods/better-files/syntax.js:293:48)
    at tokenizeLine (mods/better-files/syntax.js:222:9)
    at highlightLines (mods/better-files/syntax.js:206:16)
    at lineRuns (mods/better-files/views/viewer.js:316:54)
    at linesView (mods/better-files/views/viewer.js:247:32)
    at viewerView (mods/better-files/views/viewer.js:799:45)
    at render (mods/better-files/main.js:162:45)

lineRuns colours the whole file inside render(). Node does that in 43 ms, but QuickJS is much slower and Herdr interrupts a render that runs too long. Markdown code blocks (codeRuns) had the same problem.

Colouring now runs outside render, from the viewer's existing 300 ms tick:

 render
   linesView
-    lineRuns → highlightLines(whole file)
+    lineRuns → colorLater(lines)              # queue only; null until complete
     rowRuns → tokenizeLine(row)                # visible rows, as for files over 5,000 lines
   previewCode
-    codeRuns → highlightLines(block)
+    colorLater(block lines); draw plain meanwhile

 syncViewer (timer, has cx)
+  colorStep
+    about 4,000 characters (COLOR_CHARS), at least one line, state carried like highlightLines
+    job complete → done(runs), cx.notify()
+    jobs left → cx.timer.after(16, colorStep)
+    job no longer live (file or block changed) → dropped

syntax.js is unchanged, so it stays identical to Better Changes'. The RECIPE describes the new pass and adds a Watch out note.

Evidence

  • Before: the trace above in the app. With the updated tests run against main's viewer.js: # pass 109 / # fail 4
    test: render never colours a whole file
      first draw → lineRuns(lines) === null      # main: runs, from inside render
    
    After: the same index.html opens and is coloured in the app. node --test better-files/tests/*.cjs gives # pass 113 / # fail 0. herdr-app check passes.

New test: 121 lines of TypeScript (about 8,400 characters) open with a block comment. Colouring takes 3 steps (2 of them after a tick), the comment still carries into line 121, and a version replaced mid-way never gets runs. A second new test: a 4,500-character line takes a step to itself, and the next line follows a tick later. Two existing tests now run the colouring steps before they check colours (color(h)). The code-block test also checks that a block is plain before its colours are in.

Merge Danger

Door: two-way

Colouring code only, with no stored state and no new settings. Reverting restores the old behaviour.

Blast Radius: cosmetic

A file or code block shows uncoloured, or coloured row by row, for a few ticks after it opens or changes: about 8 ticks of 16 ms for the 32,000-character file above.

Opening a few hundred lines of HTML failed with "This view could not be
rendered ... interrupted": the viewer coloured the whole file inside
render(), and Herdr's QuickJS interrupts a render that runs too long.

The whole-file pass now runs about 4,000 characters per 16ms timer tick
(at least one line) from the viewer's own timer, with state carried
between steps, and redraws once complete. The budget counts characters,
not lines, since a line's cost is its length. Until then rows are
coloured one at a time as drawn, as files over 5,000 lines already were.
Markdown code blocks go through the same queue and show plain until
coloured. A version that changes mid-way is dropped, so a row never
shows another file's runs.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant