-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdeno-bridge-jieba.ts
130 lines (119 loc) · 3.35 KB
/
deno-bridge-jieba.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import { DenoBridge } from "https://deno.land/x/denobridge@0.0.1/mod.ts";
import {
type Token,
tokenize,
} from "./deno-jieba/mod.ts";
const bridge = new DenoBridge(
Deno.args[0],
Deno.args[1],
Deno.args[2],
messageDispatcher
);
interface Sentence {
raw: string;
tokens: Token[];
}
let sentence: Sentence;
function parseSentence(message: string) {
if (sentence == undefined ||
sentence.raw != message) {
sentence = {
raw: message,
tokens: tokenize(message),
};
}
}
function messageDispatcher(message: string) {
const info = JSON.parse(message);
const cmd = info[1][0].trim();
const sentenceStr = info[1][1];
const currentColumn = info[1][2];
parseSentence(sentenceStr);
// console.log(sentence);
if (cmd == "forward-word") {
forwardWord(currentColumn);
} else if (cmd == "backward-word") {
bacwardWord(currentColumn);
} else if (cmd == "mark-word") {
markWord(currentColumn);
} else if (cmd == "kill-word") {
killWord(currentColumn);
} else if (cmd == "backward-kill-word") {
backwardKillWord(currentColumn);
}
}
function killWord(column: number) {
const tokens = sentence.tokens;
for (let i = 0; i < tokens.length; i++) {
const token: Token = tokens[i];
const emacsCmd = `(deno-bridge-jieba-kill-from ${column} ${token.end})`;
if (column >= token.start && column < token.end) {
runAndLog(emacsCmd);
return;
}
}
}
function backwardKillWord(column: number) {
const tokens = sentence.tokens;
for (let i = 0; i < tokens.length; i++) {
const token: Token = tokens[i];
const emacsCmd = `(deno-bridge-jieba-kill-from ${token.start} ${column})`;
if (column > token.start && column <= token.end) {
runAndLog(emacsCmd);
return;
}
}
}
function markWord(column: number) {
const tokens = sentence.tokens;
for (let i = 0; i < tokens.length; i++) {
const start = tokens[i].start;
const end = tokens[i].end;
if (column >= start && column < end) {
// mark work from start to end.
const emacsCmd = `(deno-bridge-jieba-mark-from ${start} ${end})`;
runAndLog(emacsCmd);
return;
}
}
}
function forwardWord(column: number) {
const tokens = sentence.tokens;
for (let i = 0; i < tokens.length; i++) {
const token = tokens[i];
if (column >= token.start && column < token.end) {
// jump to word end
const movePosition = token.end;
const emacsCmd = `(deno-bridge-jieba-goto ${movePosition})`;
runAndLog(emacsCmd);
return;
}
}
}
function bacwardWord(column: number) {
const tokens = sentence.tokens;
for (let i = 0; i < tokens.length; i++) {
const start = tokens[i].start;
const end = tokens[i].end;
let movePosition: number;
if (column > start && column <= end) {
// when current column is in the middle of a word
// jump to word beginning
movePosition = start;
const emacsCmd = `(deno-bridge-jieba-goto ${movePosition})`;
runAndLog(emacsCmd);
return;
} else if (column == start) {
// when current column is in the beginning of a word
// jump to pre word beginning
movePosition = i == 0 ? 0 : tokens[i - 1].start;
const emacsCmd = `(deno-bridge-jieba-goto ${movePosition})`;
runAndLog(emacsCmd);
return;
}
}
}
function runAndLog(cmd: string) {
console.log(cmd);
bridge.evalInEmacs(cmd);
}