Skip to content

Commit

Permalink
✨ Realize a true IME
Browse files Browse the repository at this point in the history
  • Loading branch information
Freed-Wu committed Jun 24, 2024
1 parent c534bb1 commit ca183fc
Show file tree
Hide file tree
Showing 4 changed files with 2,498 additions and 3,794 deletions.
18 changes: 15 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -167,16 +167,28 @@
},
"commands": [
{
"command": "rime.enable",
"command": "rime.source.enable",
"title": "Enable the Rime source"
},
{
"command": "rime.disable",
"command": "rime.source.disable",
"title": "Disable the Rime source"
},
{
"command": "rime.toggle",
"command": "rime.source.toggle",
"title": "Toggle the Rime source"
},
{
"command": "rime.enable",
"title": "Enable the Rime IME"
},
{
"command": "rime.disable",
"title": "Disable the Rime IME"
},
{
"command": "rime.toggle",
"title": "Toggle the Rime IME"
}
]
},
Expand Down
32 changes: 32 additions & 0 deletions src/ime.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { window, workspace } from 'coc.nvim';

let win = null;
export function register() {
workspace.registerAutocmd({
event: 'InsertCharPre',
pattern: '<buffer>',
arglist: ['v:char'],
callback: async (character) => {
window.showInformationMessage(character);

let buffer = await workspace.nvim.createNewBuffer(false, true);
buffer.setLines(['hello', 'world']);
if (win === null) {
win = await workspace.nvim.openFloatWindow(buffer, false, {
relative: 'cursor',
height: 2,
width: 10,
row: 1,
col: 0,
});
} else {
win.close(false);
win = null;
}
},
});
}

export function unregister() {
// https://github.com/neoclide/coc.nvim/discussions/5054
}
27 changes: 24 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import { TextDocument } from 'vscode-languageserver-textdocument';
import SchemaList from './lists';
import { Rime } from './rime';
import { Config } from './config';
import { register, unregister } from './ime';

let rime: Rime;
let isRegisterd: Boolean = false;

export async function activate(context: ExtensionContext): Promise<void> {
const userConfig = new Config(context);
Expand Down Expand Up @@ -33,17 +35,17 @@ export async function activate(context: ExtensionContext): Promise<void> {

context.subscriptions.push(
// Commands
commands.registerCommand('rime.enable', async () => {
commands.registerCommand('rime.source.enable', async () => {
rime.setCompletionStatus(true);
statusBarItem.show();
}),

commands.registerCommand('rime.disable', async () => {
commands.registerCommand('rime.source.disable', async () => {
rime.setCompletionStatus(false);
statusBarItem.hide();
}),

commands.registerCommand('rime.toggle', async () => {
commands.registerCommand('rime.source.toggle', async () => {
rime.toggleCompletionStatus();
if (rime.getCompletionStatus()) {
statusBarItem.show();
Expand All @@ -52,6 +54,25 @@ export async function activate(context: ExtensionContext): Promise<void> {
}
}),

commands.registerCommand('rime.enable', async () => {
register();
isRegisterd = true;
}),

commands.registerCommand('rime.disable', async () => {
unregister();
isRegisterd = false;
}),

commands.registerCommand('rime.toggle', async () => {
if (isRegisterd) {
unregister();
} else {
register();
}
isRegisterd = !isRegisterd;
}),

// Completion Source
languages.registerCompletionItemProvider(
'rime',
Expand Down
Loading

0 comments on commit ca183fc

Please sign in to comment.