-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d9d9c1f
commit 8bafc86
Showing
9 changed files
with
166 additions
and
125 deletions.
There are no files selected for viewing
This file contains 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 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 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
108 changes: 54 additions & 54 deletions
108
...ext_builder/code_block/code_block.test.ts → lib/code_block/code_block.test.ts
This file contains 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 |
---|---|---|
@@ -1,54 +1,54 @@ | ||
import { assertEquals } from "../../../deps/std/testing.ts"; | ||
import { CodeBlock } from "./code_block.ts"; | ||
|
||
Deno.test("new code block is empty", () => { | ||
assertEquals(new CodeBlock().export(), ""); | ||
}); | ||
|
||
Deno.test("add 3 lines of code to the block", () => { | ||
const block = new CodeBlock(); | ||
block.append("a"); | ||
block.append("b"); | ||
block.append("c"); | ||
const expectation = "a\nb\nc"; | ||
const reality = block.export(); | ||
assertEquals(expectation, reality); | ||
}); | ||
|
||
Deno.test("add 3 lines of code to the block (indented)", () => { | ||
const block = new CodeBlock(); | ||
block.append("a", 0); | ||
block.append("b", 1); | ||
block.append("c", 2); | ||
const expectation = "a\n b\n c"; | ||
const reality = block.export(); | ||
assertEquals(expectation, reality); | ||
}); | ||
|
||
Deno.test("join 3 code blocks", () => { | ||
const block1 = new CodeBlock(); | ||
block1.append("a", 0); | ||
block1.append("b", 1); | ||
block1.append("c", 2); | ||
const block2 = new CodeBlock(); | ||
block2.append("d", 1); | ||
block2.append("e", 0); | ||
block2.append("f", 1); | ||
const block3 = new CodeBlock(); | ||
block3.append("g", 2); | ||
block3.append("h", 1); | ||
block3.append("i", 0); | ||
const expectation = `a | ||
b | ||
c | ||
d | ||
e | ||
f | ||
g | ||
h | ||
i`; | ||
const reality = CodeBlock.join(block1, block2, block3); | ||
assertEquals(expectation, reality); | ||
}); | ||
import { assertEquals } from "../../deps/std/testing.ts"; | ||
import { CodeBlock } from "./code_block.ts"; | ||
|
||
Deno.test("new code block is empty", () => { | ||
assertEquals(new CodeBlock().export(), ""); | ||
}); | ||
|
||
Deno.test("add 3 lines of code to the block", () => { | ||
const block = new CodeBlock(); | ||
block.append("a"); | ||
block.append("b"); | ||
block.append("c"); | ||
const expectation = "a\nb\nc"; | ||
const reality = block.export(); | ||
assertEquals(expectation, reality); | ||
}); | ||
|
||
Deno.test("add 3 lines of code to the block (indented)", () => { | ||
const block = new CodeBlock(); | ||
block.append("a", 0); | ||
block.append("b", 1); | ||
block.append("c", 2); | ||
const expectation = "a\n b\n c"; | ||
const reality = block.export(); | ||
assertEquals(expectation, reality); | ||
}); | ||
|
||
Deno.test("join 3 code blocks", () => { | ||
const block1 = new CodeBlock(); | ||
block1.append("a", 0); | ||
block1.append("b", 1); | ||
block1.append("c", 2); | ||
const block2 = new CodeBlock(); | ||
block2.append("d", 1); | ||
block2.append("e", 0); | ||
block2.append("f", 1); | ||
const block3 = new CodeBlock(); | ||
block3.append("g", 2); | ||
block3.append("h", 1); | ||
block3.append("i", 0); | ||
const expectation = `a | ||
b | ||
c | ||
d | ||
e | ||
f | ||
g | ||
h | ||
i`; | ||
const reality = CodeBlock.join(block1, block2, block3); | ||
assertEquals(expectation, reality); | ||
}); |
112 changes: 58 additions & 54 deletions
112
lib/text_builder/code_block/code_block.ts → lib/code_block/code_block.ts
This file contains 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 |
---|---|---|
@@ -1,54 +1,58 @@ | ||
import { getIndent, Indent, IndentOption } from "../../indent/mod.ts"; | ||
|
||
export interface LineOfCode { | ||
content: string; | ||
indentLevel: number; | ||
} | ||
|
||
/** | ||
* Represents a block of code. | ||
*/ | ||
export class CodeBlock { | ||
public code: LineOfCode[] = []; | ||
|
||
append(content: string, indentLevel = 0): void { | ||
this.code.push( | ||
...content.split("\n").map((line) => ({ content: line, indentLevel })), | ||
); | ||
} | ||
|
||
export(indent: IndentOption = Indent.Space2): string { | ||
return this.code | ||
.map(({ content, indentLevel }) => | ||
getIndent(indent, indentLevel) + content | ||
) | ||
.join("\n"); | ||
} | ||
|
||
/** | ||
* `toString` is an alias for `CodeBlock.export`. | ||
*/ | ||
toString = this.export.bind(this); | ||
|
||
static join( | ||
indentOrFirstBlock: IndentOption | CodeBlock, | ||
...blocks: CodeBlock[] | ||
): string { | ||
const blockPadding = 2; // lines between each code block | ||
const blockSeparator = "\n".repeat(blockPadding); | ||
const indentSpecified = !(indentOrFirstBlock instanceof CodeBlock); | ||
if (!indentSpecified) blocks = [indentOrFirstBlock, ...blocks]; | ||
return blocks | ||
.filter((block) => block !== null) | ||
.reduce( | ||
(file, block, i) => { | ||
const exportedCode = indentSpecified | ||
? block.export(indentOrFirstBlock) | ||
: block.export(); | ||
return file + exportedCode + | ||
(blocks.length - 1 > i ? blockSeparator : ""); | ||
}, | ||
"", | ||
); | ||
} | ||
} | ||
import { getIndent, Indent, IndentOption } from "../indent/mod.ts"; | ||
|
||
export interface LineOfCode { | ||
content: string; | ||
indentLevel: number; | ||
} | ||
|
||
/** | ||
* Represents a block of code. | ||
*/ | ||
export class CodeBlock { | ||
public code: LineOfCode[] = []; | ||
|
||
/** | ||
* @param content string that is split up by line break | ||
* @param indentLevel depth of nesting; defaults to 0 | ||
*/ | ||
append(content: string, indentLevel = 0): void { | ||
this.code.push( | ||
...content.split("\n").map((line) => ({ content: line, indentLevel })), | ||
); | ||
} | ||
|
||
export(indent: IndentOption = Indent.Space2): string { | ||
return this.code | ||
.map(({ content, indentLevel }) => | ||
getIndent(indent, indentLevel) + content | ||
) | ||
.join("\n"); | ||
} | ||
|
||
/** | ||
* `toString` is an alias for `CodeBlock.export`. | ||
*/ | ||
toString = this.export.bind(this); | ||
|
||
static join( | ||
indentOrFirstBlock: IndentOption | CodeBlock, | ||
...blocks: CodeBlock[] | ||
): string { | ||
const blockPadding = 2; // lines between each code block | ||
const blockSeparator = "\n".repeat(blockPadding); | ||
const indentSpecified = !(indentOrFirstBlock instanceof CodeBlock); | ||
if (!indentSpecified) blocks = [indentOrFirstBlock, ...blocks]; | ||
return blocks | ||
.filter((block) => block !== null) | ||
.reduce( | ||
(file, block, i) => { | ||
const exportedCode = indentSpecified | ||
? block.export(indentOrFirstBlock) | ||
: block.export(); | ||
const isLast = blocks.length - 1 <= i; | ||
return file + exportedCode + (isLast ? "" : blockSeparator); | ||
}, | ||
"", | ||
); | ||
} | ||
} |
File renamed without changes.
This file contains 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 |
---|---|---|
@@ -1,2 +1 @@ | ||
export { TextBuilder } from "./text_builder.ts"; | ||
export { CodeBlock } from "./code_block/mod.ts"; |
This file contains 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 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,8 @@ | ||
import type { CodeBlock } from "../code_block/mod.ts"; | ||
import type { Token } from "../tokenize/mod.ts"; | ||
import { CartridgeEvent } from "../cartridge/mod.ts"; | ||
|
||
export const makeFileStartEventContext = ( | ||
code: CodeBlock, | ||
tokens: Token[], | ||
) => ({ type: CartridgeEvent.FileStart, code, tokens, data: null }); |
8bafc86
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Failed to deploy: