Skip to content

Commit

Permalink
💥 Wrap binding.ts to provide an OOP API
Browse files Browse the repository at this point in the history
  • Loading branch information
Freed-Wu committed Apr 17, 2024
1 parent 911ce44 commit 0ddfe9d
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 253 deletions.
19 changes: 0 additions & 19 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
import { realpath, mkdir } from 'fs/promises';
import { resolve } from 'path';
import { workspace, WorkspaceConfiguration, ExtensionContext } from 'coc.nvim';
import { exec } from 'child_process';
import { promisify } from 'util';

let execAsync = promisify(exec);

async function get_dir(...dirs: string[]): Promise<string> {
for (const dir of dirs) {
Expand Down Expand Up @@ -46,20 +41,6 @@ export class Config {
get shortcut() {
return this.cfg.get<string>('shortcut');
}
get binaryPath() {
return new Promise<string>(async (res, reject) => {
let binaryPath = await get_dir(resolve(this.context.extensionPath, 'build', 'Release', 'rime_cli'));
if (binaryPath === '') {
await execAsync(`npm rebuild`, { cwd: this.context.extensionPath });
binaryPath = await get_dir(resolve(this.context.extensionPath, 'build', 'Release', 'rime_cli'));
}
try {
res(binaryPath);
} catch (e) {
reject(e);
}
});
}
get traits() {
return new Promise<Traits>(async (res, reject) => {
let shared_data_dir = this.cfg.get<string | string[] | null>('traits.shared_data_dir');
Expand Down
18 changes: 3 additions & 15 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,8 @@ import { Config } from './config';

export async function activate(context: ExtensionContext): Promise<void> {
const userConfig = new Config(context);
let binaryPath = await userConfig.binaryPath;
let traits = await userConfig.traits;
if (binaryPath === '') {
window.showInformationMessage(`'rime.binaryPath' cannot be found. Read README.md to know how to build it.`);
return;
}

const rimeCLI: RimeCLI = new RimeCLI(binaryPath, [traits.shared_data_dir, traits.user_data_dir, traits.log_dir]);
const rimeCLI: RimeCLI = new RimeCLI(await userConfig.traits);
rimeCLI.setCompletionStatus(userConfig.enabled);
rimeCLI.getSchema().then((schemaId) => {
if (schemaId !== userConfig.schemaId && userConfig.schemaId !== '') rimeCLI.setSchema(userConfig.schemaId);
Expand Down Expand Up @@ -126,15 +120,9 @@ export async function activate(context: ExtensionContext): Promise<void> {
rimeCLI
.getContext(inputString)
.then((res) => {
if (
res != null &&
'menu' in res &&
res.menu != null &&
'candidates' in res.menu &&
res.menu.candidates != null
) {
if (res != null && res.candidates != null) {
resolve({
items: res.menu.candidates.map((candidate, order) => {
items: res.candidates.map((candidate, order) => {
return {
label: candidate.text,
sortText: String.fromCharCode(order),
Expand Down
10 changes: 5 additions & 5 deletions src/lists.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,16 @@ export default class SchemaList extends BasicList {
this.rimeCLI = rimeCLI;
this.addAction('open', (item: ListItem) => {
this.rimeCLI
.setSchema(item.data.schemaId)
.setSchema(item.data.schema_id)
.then((_) => {})
.catch((e) => {
console.log(`Error setting the schema: ${e}`);
window.showMessage(`Set schema ${item.data.label} failed.`);
});
this.rimeCLI
.getSchema()
.then((schemaId) => {
window.showMessage(`Changed to schema ${schemaId}.`);
.then((schema_id) => {
window.showMessage(`Changed to schema ${schema_id}.`);
})
.catch((e) => {
console.log(`Error get current schema: ${e}`);
Expand All @@ -38,8 +38,8 @@ export default class SchemaList extends BasicList {
this.rimeCLI.getSchemaList().then((res) => {
let listItems: ListItem[] = res.map((schema) => {
return {
label: schema.name + ': ' + schema.schemaId,
filterText: schema.name + schema.schemaId,
label: schema.name + ': ' + schema.schema_id,
filterText: schema.name + schema.schema_id,
data: schema,
};
});
Expand Down
Loading

0 comments on commit 0ddfe9d

Please sign in to comment.