Skip to content

Commit 3bbcae2

Browse files
committed
Add insertLineSpeed configuration and implement line-by-line insertion in DemoRunner
1 parent bf11522 commit 3bbcae2

File tree

9 files changed

+104
-16
lines changed

9 files changed

+104
-16
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Change Log
22

3+
## [0.0.29] - 2025-10-29
4+
5+
- Implement a line-by-line insert/replace action
6+
- Configure the line-by-line insert/replace speed with the `demoTime.insertLineSpeed` setting
7+
38
## [0.0.28] - 2025-10-21
49

510
- Changed completed action icon color

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,7 @@ To use the extension, you need to create a `.demo` folder in your workspace. Onc
356356
| `demoTime.highlightZoomEnabled` | Enable zooming when highlighting code | `false` |
357357
| `demoTime.showClock` | Show a clock in the status bar | `true` |
358358
| `demoTime.timer` | Count down timer for how long the session should last. If not set, it will not count down. The value is the number of minutes. | `null` |
359+
| `demoTime.insertLineSpeed` | The speed in milliseconds for inserting lines. If you set it to `0`, it will insert its content immediately. | `25` |
359360

360361
### Tips
361362

package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,11 @@
8585
"type": "boolean",
8686
"default": true,
8787
"description": "Show a clock in the status bar"
88+
},
89+
"demoTime.insertLineSpeed": {
90+
"type": "number",
91+
"default": 25,
92+
"description": "The time out between inserting lines of code (in milliseconds)"
8893
}
8994
}
9095
},

src/constants/Config.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,7 @@ export const Config = {
88
show: "showClock",
99
timer: "timer",
1010
},
11+
insert: {
12+
speed: "insertLineSpeed",
13+
},
1114
};

src/services/DemoRunner.ts

Lines changed: 44 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { COMMAND, StateKeys } from "../constants";
1+
import { COMMAND, Config, StateKeys } from "../constants";
22
import { Demo, DemoFileCache, Demos, Step, Subscription } from "../models";
33
import { Extension } from "./Extension";
44
import {
@@ -17,7 +17,7 @@ import {
1717
} from "vscode";
1818
import { FileProvider } from "./FileProvider";
1919
import { DemoPanel } from "../panels/DemoPanel";
20-
import { getFileContents, sleep } from "../utils";
20+
import { getFileContents, insertContent, insertLineByLine, replaceContent, sleep } from "../utils";
2121
import { ActionTreeItem } from "../providers/ActionTreeviewProvider";
2222
import { DecoratorService } from "./DecoratorService";
2323
import { Notifications } from "./Notifications";
@@ -400,19 +400,29 @@ export class DemoRunner {
400400
// do nothing
401401
}
402402

403-
const edit = new WorkspaceEdit();
403+
const lineSpeed = Extension.getInstance().getSetting<number>(Config.insert.speed) || 0;
404404

405405
let range = new Range(position, position);
406406
if (!lineContent) {
407-
edit.insert(fileUri, position, content);
407+
// Insert the content at the specified position
408+
if (!lineSpeed) {
409+
await insertContent(fileUri, position, content);
410+
} else {
411+
await insertLineByLine(fileUri, editor.lineAt(position), content, lineSpeed);
412+
}
408413
} else {
409-
const line = editor.lineAt(position);
410-
range = line.range;
411-
edit.replace(fileUri, line.range, content);
414+
if (!lineSpeed) {
415+
const line = editor.lineAt(position);
416+
range = line.range;
417+
await replaceContent(fileUri, line.range, content);
418+
} else {
419+
const line = editor.lineAt(position);
420+
range = line.range;
421+
await replaceContent(fileUri, line.range, "");
422+
await insertLineByLine(fileUri, line, content, lineSpeed);
423+
}
412424
}
413425

414-
await workspace.applyEdit(edit);
415-
416426
if (textEditor) {
417427
textEditor.revealRange(range, TextEditorRevealType.InCenter);
418428
textEditor.selection = new Selection(range.start, range.start);
@@ -444,17 +454,35 @@ export class DemoRunner {
444454
return;
445455
}
446456

447-
const edit = new WorkspaceEdit();
457+
const lineSpeed = Extension.getInstance().getSetting<number>(Config.insert.speed) || 0;
448458

449459
if (range) {
450-
edit.replace(fileUri, range, content);
460+
if (!lineSpeed) {
461+
await replaceContent(fileUri, range, content);
462+
} else {
463+
const startLine = editor.lineAt(range.start);
464+
const endLine = editor.lineAt(range.end);
465+
const start = new Position(startLine.lineNumber, 0);
466+
const end = new Position(endLine.lineNumber, endLine.text.length);
467+
468+
await replaceContent(fileUri, new Range(start, end), "");
469+
await insertLineByLine(fileUri, startLine, content, lineSpeed);
470+
}
451471
} else if (position) {
452-
const line = editor.lineAt(position);
453-
range = line.range;
454-
edit.replace(fileUri, line.range, content);
455-
}
472+
if (!lineSpeed) {
473+
const line = editor.lineAt(position);
474+
range = line.range;
456475

457-
await workspace.applyEdit(edit);
476+
await replaceContent(fileUri, line.range, content);
477+
} else {
478+
const line = editor.lineAt(position);
479+
range = line.range;
480+
481+
await replaceContent(fileUri, line.range, "");
482+
483+
await insertLineByLine(fileUri, line, content, lineSpeed);
484+
}
485+
}
458486

459487
if (textEditor && range) {
460488
textEditor.revealRange(range, TextEditorRevealType.InCenter);

src/utils/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
export * from "./getfileContents";
2+
export * from "./insertContent";
3+
export * from "./insertLineByLine";
24
export * from "./parseWinPath";
5+
export * from "./replaceContent";
36
export * from "./sleep";

src/utils/insertContent.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { Position, Uri, workspace, WorkspaceEdit } from "vscode";
2+
3+
export const insertContent = async (fileUri: Uri, position: Position, content: string) => {
4+
const edit = new WorkspaceEdit();
5+
edit.insert(fileUri, position, content);
6+
await workspace.applyEdit(edit);
7+
};

src/utils/insertLineByLine.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { Position, TextLine, Uri } from "vscode";
2+
import { sleep } from "./sleep";
3+
import { insertContent } from ".";
4+
5+
export const insertLineByLine = async (fileUri: Uri, startLine: TextLine, content: string, speed: number) => {
6+
const lines = content.split("\n");
7+
const totalLines = lines.length;
8+
const lastLine = lines[totalLines - 1];
9+
10+
let crntPosition = new Position(startLine.lineNumber, 0);
11+
for (let i = 0; i < totalLines; i++) {
12+
let lineContent = lines[i];
13+
14+
if (totalLines > 1) {
15+
if (i === totalLines - 1 && lastLine.trim() === "") {
16+
lineContent = `\n`;
17+
} else if (i < totalLines - 1) {
18+
lineContent = `${lineContent}\n`;
19+
} else {
20+
lineContent = `${lineContent}`;
21+
}
22+
}
23+
24+
await insertContent(fileUri, crntPosition, lineContent);
25+
26+
crntPosition = new Position(crntPosition.line + 1, 0);
27+
await sleep(speed);
28+
}
29+
};

src/utils/replaceContent.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { Range, Uri, workspace, WorkspaceEdit } from "vscode";
2+
3+
export const replaceContent = async (fileUri: Uri, range: Range, content: string) => {
4+
const edit = new WorkspaceEdit();
5+
edit.replace(fileUri, range, content);
6+
await workspace.applyEdit(edit);
7+
};

0 commit comments

Comments
 (0)