Skip to content

Commit

Permalink
Mmm. Chunky goodness.
Browse files Browse the repository at this point in the history
  • Loading branch information
dylanbeattie committed Mar 23, 2024
1 parent 8d91b32 commit 63f1490
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 16 deletions.
10 changes: 10 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

root = true

[**.{ts,json,js}]
end_of_line = crlf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
indent_style = space
indent_size = 2
45 changes: 45 additions & 0 deletions src/instruction-handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
CreateFile,
Wait,
TypeTextFromFile,
TypeChunksFromFile
} from './instructions';
import {
mkdirIfNotExists,
Expand Down Expand Up @@ -45,6 +46,50 @@ export const typeTextFromFile = async (
await typeTextIntoActiveTextEditor(data, instruction.delay);
};

export const typeChunksFromFile = async (
instruction: TypeChunksFromFile
): Promise<void> => {
if (!workspace.workspaceFolders) {
return;
}

const workspaceFolder = workspace.workspaceFolders[0].uri.fsPath;
const path = join(workspaceFolder, '.presentation-buddy', instruction.path);
const text = await readFileAsync(path);
const waitInsteadOf = instruction.waitInsteadOf || ["/*WAIT*/"];
const waitAfter = instruction.waitAfter || ["\n" ]
const skipChunksContaining = instruction.skipChunksContaining || ["/*SKIP*/"];
var chunks = [ text ];
for(var token of waitAfter) {
chunks = chunks
.map(chunk => chunk.split(token).map(chunk => chunk ? chunk : token))
.flat();
}
for(var token of waitInsteadOf) {
chunks = chunks
.map(chunk => chunk.split(token))
.flat();
}

function keep(chunk: string): boolean { return !skipChunksContaining.some(t => chunk.includes(t)) };

chunks = chunks.filter(chunk => keep(chunk));

for (const chunk of chunks) {
await typeTextIntoActiveTextEditor(Array.from(chunk), instruction.delay);
if (chunk.endsWith('\n')) await command({
type: "command",
command: "cursorHome",
args: [],
repeat: 1
});
await wait({
delay: "manual",
type: 'wait'
});
}
}

export const typeText = async (instruction: TypeText): Promise<void> => {
const data = Array.from(instruction.text.join('\n'));

Expand Down
10 changes: 10 additions & 0 deletions src/instructions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@ export type TypeTextFromFile = {
delay?: number;
};

export type TypeChunksFromFile = {
type: "typeChunksFromFile";
path: string;
delay?: number;
waitInsteadOf: string[];
waitAfter: string[];
skipChunksContaining: string[];
};

export type OpenFile = { type: "openFile"; path: string };

export type CreateFile = { type: "createFile"; path: string };
Expand All @@ -31,6 +40,7 @@ export type Instruction = (
| OpenFile
| TypeText
| TypeTextFromFile
| TypeChunksFromFile
| Wait
) &
Skipable;
Expand Down
25 changes: 9 additions & 16 deletions src/test/extension.test.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,15 @@
//
// Note: This example test is leveraging the Mocha test framework.
// Please refer to their documentation on https://mochajs.org/ for help.
//

// The module 'assert' provides assertion methods from node
import * as assert from 'assert';

// You can import and use all API from the 'vscode' module
// as well as import your extension to test it
// import * as vscode from 'vscode';
// import * as myExtension from '../extension';
import * as vscode from 'vscode';
// import * as myExtension from '../../extension';

// Defines a Mocha test suite to group tests of similar kind together
suite("Extension Tests", function () {
suite('Extension Test Suite', () => {
vscode.window.showInformationMessage('Start all tests.');

// Defines a Mocha unit test
test("Something 1", function() {
assert.equal(-1, [1, 2, 3].indexOf(5));
assert.equal(-1, [1, 2, 3].indexOf(0));
});
});
test('Sample test', () => {
assert.strictEqual(-1, [1, 2, 3].indexOf(5));
assert.strictEqual(-1, [1, 2, 3].indexOf(0));
});
});
12 changes: 12 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,18 @@ const mkdirAsync = async (path: string): Promise<void> => {
}
};

export function multiSplit(text: string, tokens: string[]): string[] {
if (tokens.length) {
tokens.sort((a, b) => b.length - a.length);
let token = tokens[0];
for (var i = 1; i < tokens.length; i++) {
text = text.split(tokens[i]).join(token);
}
return text.split(token);
}
return [text];
}

export async function mkdirIfNotExists(dir: string) {
const parts = dir.split(/[\/\\]/);

Expand Down

0 comments on commit 63f1490

Please sign in to comment.