Skip to content

Commit

Permalink
refactor(plugin/closing_pairs): use default_pairs by default
Browse files Browse the repository at this point in the history
  • Loading branch information
magic-akari committed Apr 2, 2024
1 parent 3ec7838 commit d580530
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 33 deletions.
23 changes: 7 additions & 16 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,7 @@
const editor = shikiEditor()
.withPlugins(
hookTab,
hookClosingPairs({
language,
pairs: [
["(", ")"],
["[", "]"],
["{", "}"],
],
}),
hookClosingPairs(),
)
.create(container, h, {
value: `console.log("Hello, shiki-editor 👋!");`,
Expand All @@ -99,7 +92,7 @@
import * as shiki from "shiki";
import { bundledLanguagesInfo, bundledThemesInfo } from "shiki";
import { shikiEditor } from "./lib/index.js";
import { autoload, hookClosingPairs, hookTab, default_pairs } from "./lib/plugins/index.js";
import { autoload, hookClosingPairs, hookTab } from "./lib/plugins/index.js";

bundledLanguagesInfo.forEach((lang) => {
const option = document.createElement("option");
Expand All @@ -123,13 +116,11 @@

const h = await shiki.getHighlighter({ langs: [language], themes: [theme] });

const editor = shikiEditor()
.withPlugins(hookClosingPairs(...default_pairs), hookTab, autoload)
.create(container, h, {
value: example.value,
language,
theme,
});
const editor = shikiEditor().withPlugins(hookClosingPairs(), hookTab, autoload).create(container, h, {
value: example.value,
language,
theme,
});

lang_list.addEventListener("change", (e) => {
editor.updateOptions({
Expand Down
34 changes: 18 additions & 16 deletions src/plugins/closing_pairs.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import type { EditorPlugin } from "./index.js";

export type ClosingPair = [open: string, close: string];
export type ClosingPair = readonly [open: string, close: string];

export type ClosingPairsRules = {
language: string;
pairs: ClosingPair[];
readonly language: string;
readonly pairs: ClosingPair[];
};

interface ResolvedClosingPairsRules {
Expand All @@ -18,19 +18,21 @@ const should_auto_close = " \t\n.,;)]}>=";
/**
* A plugin that automatically inserts closing pairs.
*/
export function hookClosingPairs(...bracketRuleList: ClosingPairsRules[]): EditorPlugin {
const cache = new Map<string, ResolvedClosingPairsRules>();
export function hookClosingPairs(...pairs_rule_list: readonly ClosingPairsRules[]): EditorPlugin {
const rules = new Map<string, ResolvedClosingPairsRules>();

for (const { language, pairs: brackets } of bracketRuleList) {
const auto_closing_pairs_open_by_start = new Map();
const auto_closing_pairs_open_by_end = new Map();
const list = default_pairs.concat(pairs_rule_list);

for (const { language, pairs } of list) {
const auto_closing_pairs_open_by_start = new Map<string, string>();
const auto_closing_pairs_open_by_end = new Map<string, string>();
const auto_closing_paris = new Set<string>();
brackets.forEach(([open, close]) => {
pairs.forEach(([open, close]) => {
auto_closing_pairs_open_by_start.set(open, close);
auto_closing_pairs_open_by_end.set(close, open);
auto_closing_paris.add(open + close);
});
cache.set(language, {
rules.set(language, {
auto_closing_pairs_open_by_start,
auto_closing_pairs_open_by_end,
auto_closing_pairs: auto_closing_paris,
Expand All @@ -39,7 +41,7 @@ export function hookClosingPairs(...bracketRuleList: ClosingPairsRules[]): Edito

return ({ input }, options) => {
const onKeydown = (e: KeyboardEvent) => {
const config = cache.get(options.language);
const config = rules.get(options.language);
if (!config) {
return;
}
Expand Down Expand Up @@ -107,6 +109,10 @@ export function hookClosingPairs(...bracketRuleList: ClosingPairsRules[]): Edito
};
}

function isBackspace(e: KeyboardEvent) {
return e.key === "Backspace" && !e.shiftKey && !e.ctrlKey && !e.altKey && !e.metaKey;
}

export const pairs_parentheses = ["(", ")"] satisfies ClosingPair;
export const pairs_brackets = ["[", "]"] satisfies ClosingPair;
export const pairs_braces = ["{", "}"] satisfies ClosingPair;
Expand All @@ -132,7 +138,7 @@ const c_lang_pairs_with_backticks: ClosingPair[] = [
pairs_backticks,
];

export const default_pairs: ClosingPairsRules[] = [
export const default_pairs: readonly ClosingPairsRules[] = [
{
language: "c",
pairs: c_lang_pairs,
Expand Down Expand Up @@ -202,7 +208,3 @@ export const default_pairs: ClosingPairsRules[] = [
pairs: c_lang_pairs_with_backticks,
},
];

function isBackspace(e: KeyboardEvent) {
return e.key === "Backspace" && !e.shiftKey && !e.ctrlKey && !e.altKey && !e.metaKey;
}
1 change: 0 additions & 1 deletion src/plugins/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,3 @@ export type EditorPlugin = {
export * from "./autoload.js";
export * from "./closing_pairs.js";
export * from "./tab.js";

0 comments on commit d580530

Please sign in to comment.