-
Notifications
You must be signed in to change notification settings - Fork 1.6k
fix(editor): make fold all reliable for large files #2912
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+299
−39
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
bc65902
fix(editor): make fold all reliable for large files
bajrangCoder 3c789b5
Merge branch 'main' into fix/fold-all-large-files
bajrangCoder 6d7b86c
fix
bajrangCoder 83baf4a
Merge branch 'fix/fold-all-large-files' of https://github.com/Acode-F…
bajrangCoder File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,150 @@ | ||
| // @vitest-environment happy-dom | ||
|
|
||
| import { javascript } from "@codemirror/lang-javascript"; | ||
| import { codeFolding, foldedRanges } from "@codemirror/language"; | ||
| import { EditorState } from "@codemirror/state"; | ||
| import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; | ||
|
|
||
| const parsing = vi.hoisted(() => ({ | ||
| ensureSyntaxTree: vi.fn(), | ||
| forceParsing: vi.fn(), | ||
| syntaxTreeAvailable: vi.fn(), | ||
| })); | ||
|
|
||
| vi.mock("@codemirror/language", async (importOriginal) => ({ | ||
| ...(await importOriginal()), | ||
| ensureSyntaxTree: parsing.ensureSyntaxTree, | ||
| forceParsing: parsing.forceParsing, | ||
| syntaxTreeAvailable: parsing.syntaxTreeAvailable, | ||
| })); | ||
|
|
||
| const { foldAllCodeBlocks, unfoldAllCodeBlocks } = await import( | ||
| "cm/foldingCommands" | ||
| ); | ||
|
|
||
| function createView() { | ||
| let state = EditorState.create({ | ||
| doc: [ | ||
| "function outer() {", | ||
| "\tif (ready) {", | ||
| '\t\tconsole.log("ready");', | ||
| "\t}", | ||
| "}", | ||
| ].join("\n"), | ||
| extensions: [javascript(), codeFolding()], | ||
| }); | ||
|
|
||
| return { | ||
| get state() { | ||
| return state; | ||
| }, | ||
| dispatch(spec) { | ||
| state = state.update(spec).state; | ||
| }, | ||
| }; | ||
| } | ||
|
|
||
| function countFoldedRanges(state) { | ||
| let count = 0; | ||
| foldedRanges(state).between(0, state.doc.length, () => { | ||
| count += 1; | ||
| }); | ||
| return count; | ||
| } | ||
|
|
||
| describe("progressive Fold all", () => { | ||
| let idleCallbacks; | ||
| let nextIdleId; | ||
|
|
||
| beforeEach(() => { | ||
| idleCallbacks = new Map(); | ||
| nextIdleId = 1; | ||
| window.requestIdleCallback = vi.fn((callback) => { | ||
| const id = nextIdleId; | ||
| nextIdleId += 1; | ||
| idleCallbacks.set(id, callback); | ||
| return id; | ||
| }); | ||
| window.cancelIdleCallback = vi.fn((id) => { | ||
| idleCallbacks.delete(id); | ||
| }); | ||
|
|
||
| parsing.ensureSyntaxTree.mockReset().mockReturnValue(null); | ||
| parsing.forceParsing.mockReset(); | ||
| parsing.syntaxTreeAvailable.mockReset().mockReturnValue(false); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| vi.restoreAllMocks(); | ||
| }); | ||
|
|
||
| function takeNextIdleCallback() { | ||
| const next = idleCallbacks.entries().next().value; | ||
| expect(next).toBeDefined(); | ||
| const [id, callback] = next; | ||
| idleCallbacks.delete(id); | ||
| return callback; | ||
| } | ||
|
|
||
| function runNextIdleCallback() { | ||
| takeNextIdleCallback()({ | ||
| didTimeout: false, | ||
| timeRemaining: () => 8, | ||
| }); | ||
| } | ||
|
|
||
| it("folds after parsing completes across multiple idle slices", () => { | ||
| const view = createView(); | ||
| parsing.forceParsing | ||
| .mockReturnValueOnce(false) | ||
| .mockReturnValueOnce(false) | ||
| .mockReturnValueOnce(true); | ||
|
|
||
| expect(foldAllCodeBlocks(view)).toBe(true); | ||
| expect(countFoldedRanges(view.state)).toBe(0); | ||
|
|
||
| runNextIdleCallback(); | ||
| runNextIdleCallback(); | ||
| expect(countFoldedRanges(view.state)).toBe(0); | ||
|
|
||
| runNextIdleCallback(); | ||
| expect(parsing.forceParsing).toHaveBeenCalledTimes(3); | ||
| expect(countFoldedRanges(view.state)).toBeGreaterThan(0); | ||
| expect(idleCallbacks.size).toBe(0); | ||
| }); | ||
|
|
||
| it("cancels pending parsing when the document changes", () => { | ||
| const view = createView(); | ||
| parsing.forceParsing.mockReturnValue(false); | ||
|
|
||
| foldAllCodeBlocks(view); | ||
| runNextIdleCallback(); | ||
| expect(parsing.forceParsing).toHaveBeenCalledTimes(1); | ||
|
|
||
| view.dispatch({ changes: { from: 0, insert: "// changed\n" } }); | ||
| runNextIdleCallback(); | ||
|
|
||
| expect(parsing.forceParsing).toHaveBeenCalledTimes(1); | ||
| expect(countFoldedRanges(view.state)).toBe(0); | ||
| expect(idleCallbacks.size).toBe(0); | ||
| }); | ||
|
|
||
| it("cancels pending parsing when Unfold all runs", () => { | ||
| const view = createView(); | ||
| parsing.forceParsing.mockReturnValue(false); | ||
|
|
||
| foldAllCodeBlocks(view); | ||
| const pendingCallback = takeNextIdleCallback(); | ||
| expect(unfoldAllCodeBlocks(view)).toBe(false); | ||
|
|
||
| pendingCallback({ | ||
| didTimeout: false, | ||
| timeRemaining: () => 8, | ||
| }); | ||
|
|
||
| expect(window.cancelIdleCallback).toHaveBeenCalledTimes(1); | ||
| expect(parsing.forceParsing).not.toHaveBeenCalled(); | ||
| expect(countFoldedRanges(view.state)).toBe(0); | ||
| expect(idleCallbacks.size).toBe(0); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.