Skip to content
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

Move Plugs to Space Config and rework "Plug: ..." commands #1122

Merged
merged 1 commit into from
Dec 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions plugs/editor/editor.plug.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ config:
defaultLinkStyle:
type: string
nullable: true
plugs:
type: array
items:
type: string
functions:
setEditorMode:
path: "./editor.ts:setEditorMode"
Expand Down
110 changes: 110 additions & 0 deletions plugs/plug-manager/plugmanager.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
import { assertEquals } from "@std/assert";
import { insertIntoPlugPage } from "./plugmanager.ts";

/** Convenience function simulating repeatedly calling `editor.replaceRange` */
function replaceRanges(
pageText: string,
ranges: Array<{ from: number; to: number; text: string }>,
): string {
let result = pageText;
for (const { from, to, text } of ranges) {
result = result.substring(0, from) + text + result.substring(to);
}
return result;
}

const exampleURI = "test:my.plug.js";
const exampleBlock = `\`\`\`space-config
plugs:
- ${exampleURI}
\`\`\``;

Deno.test("Updating PLUGS page", () => {
// Empty page
let before = "";
let after = replaceRanges(before, insertIntoPlugPage(exampleURI, before));
assertEquals(after, exampleBlock);

// Page with some content and newline
before = "Lorem ipsum dolor sit amet.\n";
let expected = `${before}${exampleBlock}`;
after = replaceRanges(before, insertIntoPlugPage(exampleURI, before));
assertEquals(after, expected);

// Page without a newline at the end
before = "Lorem ipsum dolor sit amet.";
expected = `${before}\n${exampleBlock}`;
after = replaceRanges(before, insertIntoPlugPage(exampleURI, before));
assertEquals(after, expected);

// Old PLUGS page
before = `Old prelude

\`\`\`yaml
- test:old.plug.js
- test:another.plug.js
\`\`\`
Some stuff below`;
expected = `Old prelude

\`\`\`space-config
plugs:
- test:old.plug.js
- test:another.plug.js
- test:my.plug.js
\`\`\`
Some stuff below`;
after = replaceRanges(before, insertIntoPlugPage("test:my.plug.js", before));
assertEquals(after, expected);

// Page with an existing space config block
before = `Page content
\`\`\`space-config
foo:
bar: baz
\`\`\`
Some stuff below`;
expected = `Page content
\`\`\`space-config
foo:
bar: baz
plugs:
- test:my.plug.js
\`\`\`
Some stuff below`;
after = replaceRanges(before, insertIntoPlugPage("test:my.plug.js", before));
assertEquals(after, expected);

// Append at the end of an existing plugs list
before = `Page content
\`\`\`space-config
plugs:
- test:old.plug.js
# some comment
\`\`\`
`;
expected = `Page content
\`\`\`space-config
plugs:
- test:old.plug.js
# some comment
- test:my.plug.js
\`\`\`
`;
after = replaceRanges(before, insertIntoPlugPage("test:my.plug.js", before));
assertEquals(after, expected);

// Why would you do this to yourself?
before = `I love square brackets
\`\`\`space-config
plugs: [ "test:old.plug.js" ]
\`\`\`
`;
expected = `I love square brackets
\`\`\`space-config
plugs: [ "test:old.plug.js" , "test:my.plug.js" ]
\`\`\`
`;
after = replaceRanges(before, insertIntoPlugPage("test:my.plug.js", before));
assertEquals(after, expected);
});
Loading