Skip to content

Commit

Permalink
fix: accidentally character remove when input grapheme
Browse files Browse the repository at this point in the history
thanks @thinca
  • Loading branch information
kuuote committed Jan 5, 2025
1 parent 954f2f9 commit 8bb1b87
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
5 changes: 4 additions & 1 deletion denops/skkeleton/preedit.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const segmenter = new Intl.Segmenter("ja");

// 現在の文字列の状態を覚えておいて削除命令を発行することで擬似的にVimでIMEのPreEditを実現する
export class PreEdit {
#current = "";
Expand All @@ -13,7 +15,8 @@ export class PreEdit {
if (!this.#kakutei && next.startsWith(this.#current)) {
ret = next.slice(this.#current.length);
} else {
ret = "\b".repeat([...this.#current].length) + this.#kakutei + next;
ret = "\b".repeat([...segmenter.segment(this.#current)].length) +
this.#kakutei + next;
}
this.#current = next;
this.#kakutei = "";
Expand Down
3 changes: 2 additions & 1 deletion denops/skkeleton/preedit_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@ Deno.test({
});

Deno.test({
name: "preedit with emoji",
name: "preedit with grapheme",
fn() {
const preEdit = new PreEdit();
assertEquals(preEdit.output("💩"), "💩");
assertEquals(preEdit.output("🚽"), "\b🚽");
assertEquals(preEdit.output("☀️"), "\b☀️"); // U+2600 U+FE0Fの合成文字
assertEquals(preEdit.output("🍦"), "\b🍦");
},
});

0 comments on commit 8bb1b87

Please sign in to comment.