Skip to content

Commit

Permalink
Feat/more options (#5)
Browse files Browse the repository at this point in the history
* feat(lsp): add configuration change

* feat(lsp): add more options

- show_symbols: if show Chinese symbols
- match_long_input: if show items for long input
- match_as_same_as_input: if only show items which match same as input

* feat(extension): add more config

* fix(extension): use custom server-path first

* feat(docs): update README

* fix(dict-builder): fix ü to v

* chore(package): v0.2.0
  • Loading branch information
iamcco authored Dec 12, 2022
1 parent 028d204 commit aa86402
Show file tree
Hide file tree
Showing 11 changed files with 287 additions and 126 deletions.
34 changes: 30 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,36 @@

> 如果不启用这个设置,那么在中文字符后面输入拼音会得不到建议选项。
使用扩展 [coc-ds-pinyin-lsp](./packages/coc-ds-pinyin)
##### 1. 使用扩展 [coc-ds-pinyin-lsp](./packages/coc-ds-pinyin)

```
:CocInstall coc-ds-pinyin-lsp
```

或从 [Release](https://github.com/iamcco/ds-pinyin-lsp/releases/tag/v0.1.0) 下载 `ds-pinyin-lsp`
插件设置项:

- `ds-pinyin-lsp.enabled`: 是否启用插件
- `ds-pinyin-lsp.prompt`: 是否运行弹窗询问?比如询问下载 `ds-pinyin-lsp` `dict.db3` 文件
- `ds-pinyin-lsp.server_path`: `ds-pinyin-lsp` 命令或路经
- `ds-pinyin-lsp.trace.server`: 打开日志项
- `ds-pinyin-lsp.show_status_bar`: 是否开启状态栏显示
- `ds-pinyin-lsp.check_on_startup`: 是否检查更新
- `ds-pinyin-lsp.db_path`: `dict.db3` 文件
- `ds-pinyin-lsp.completion_on`: 是否自动启用补全
- `ds-pinyin-lsp.show_symbols`: 是否补全中文标点符号
- `ds-pinyin-lsp.match_as_same_as_input`: 是否只显示完全匹配结果,比如: 输入 `pinyin` 会只显示 `拼音` 选项,不会显示 `拼音输入法` 等选项
- `ds-pinyin-lsp.match_long_input`: 是否显示长句匹配,比如:输入 `nihaonishishei` 在没有补全项的时候会把 `你好` `你是谁` 两个选项拼起来作为补全选项

插件命令:

- `ds-pinyin-lsp.turn-on-completion`: 开启自动补全
- `ds-pinyin-lsp.turn-off-completion`: 关闭自动补全
- `ds-pinyin-lsp.toggle-completion`: 切换自动补全


##### 2. 不使用扩展

[Release](https://github.com/iamcco/ds-pinyin-lsp/releases/tag/v0.1.0) 下载 `ds-pinyin-lsp`
通过 `cargo install ds-pinyin-lsp` 安装 `ds-pinyin-lsp` 然后添加以下配置到 `coc-settings.json`

``` jsonc
Expand All @@ -35,8 +58,11 @@
"command": "path to ds-pinyin-lsp command",
"filetypes": ["*"],
"initializationOptions": {
"db_path": "path to dict.db3",
"completion_on": true
"db_path": "path to dict.db3", // dict.db3 字典文件
"completion_on": true, // 是否开启自动补全
"show_symbols": true, // 是否补全中文标点符号
"match_as_same_as_input": true, // 是否只显示完全匹配结果,比如: 输入 `pinyin` 会只显示 `拼音` 选项,不会显示 `拼音输入法` 选项
"match_long_input": true, // 是否显示长句匹配,比如:输入 `nihaonishishei` 在没有补全项的时候会把 `你好` `你是谁` 两个选项拼起来作为补全选项
}
}
}
Expand Down
13 changes: 8 additions & 5 deletions packages/coc-ds-pinyin/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,17 @@

## Setting

- `ds-pinyin-lsp.db_path`: db path
- `ds-pinyin-lsp.completion_on`: If enable auto completion
- `ds-pinyin-lsp.enabled`: Enable coc-ds-pinyin-lsp extension
- `ds-pinyin-lsp.show_status_bar`: Show Pinyin in status bar
- `ds-pinyin-lsp.server_path`: Path to ds-pinyin-lsp command
- `ds-pinyin-lsp.check_on_startup`: Check ds-pinyin-lsp release on start up
- `ds-pinyin-lsp.prompt`: Allow prompt?
- `ds-pinyin-lsp.server_path`: Path to ds-pinyin-lsp command
- `ds-pinyin-lsp.trace.server`: Trace level of log
- `ds-pinyin-lsp.show_status_bar`: Show Pinyin in status bar
- `ds-pinyin-lsp.check_on_startup`: Check ds-pinyin-lsp release on start up
- `ds-pinyin-lsp.db_path`: db path
- `ds-pinyin-lsp.completion_on`: If enable auto completion
- `ds-pinyin-lsp.show_symbols`: If show Chinese symbols
- `ds-pinyin-lsp.match_as_same_as_input`: If only show the items match the same as input
- `ds-pinyin-lsp.match_long_input`: If show items for the long input

## Commands

Expand Down
17 changes: 16 additions & 1 deletion packages/coc-ds-pinyin/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "coc-ds-pinyin-lsp",
"version": "0.1.2",
"version": "0.2.0",
"description": "pinyin input support for (Neo)vim",
"author": "iamcco <[email protected]>",
"license": "MIT",
Expand Down Expand Up @@ -61,6 +61,21 @@
"default": true,
"description": "If enable auto completion"
},
"ds-pinyin-lsp.show_symbols": {
"type": "boolean",
"default": true,
"description": "If show Chinese symbols"
},
"ds-pinyin-lsp.match_as_same_as_input": {
"type": "boolean",
"default": false,
"description": "If only show the items match the same as input"
},
"ds-pinyin-lsp.match_long_input": {
"type": "boolean",
"default": true,
"description": "If show items for the long input"
},
"ds-pinyin-lsp.server_path": {
"type": "string",
"default": "",
Expand Down
56 changes: 38 additions & 18 deletions packages/coc-ds-pinyin/src/ctx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,22 +56,29 @@ export class Ctx {
},
{
documentSelector: ['*'],
initializationOptions: {
db_path: workspace.getConfiguration(extensionName).get('db_path') || join(this.extCtx.storagePath, dbName),
completion_on: workspace.getConfiguration(extensionName).get('completion_on', true),
},
initializationOptions: this.configuration,
},
);

this.client = client;

this.extCtx.subscriptions.push({
dispose: () => {
if (this.client) {
this.client.stop();
this.extCtx.subscriptions.push(
workspace.onDidChangeConfiguration((evt) => {
if (!evt.affectsConfiguration(extensionName) || !this.client?.started) {
return;
}
this.client.sendNotification('workspace/didChangeConfiguration', {
settings: this.configuration,
});
}),
{
dispose: () => {
if (this.client) {
this.client.stop();
}
},
},
});
);

this.client.start();
}
Expand All @@ -86,20 +93,33 @@ export class Ctx {
return this.extCtx.subscriptions;
}

get configuration() {
const config = workspace.getConfiguration(extensionName);
const db_path = join(this.extCtx.storagePath, dbName);
return {
db_path: config.get<string>('db_path') || (existsSync(db_path) ? db_path : ''),
completion_on: config.get<boolean>('completion_on', true),
show_symbols: config.get<boolean>('show_symbols', true),
match_as_same_as_input: config.get<boolean>('match_as_same_as_input', false),
match_long_input: config.get<boolean>('match_long_input', true),
};
}

resolveBin(): string | undefined {
// 1. from config, custom server path
// 2. bundled
let bin = join(this.extCtx.storagePath, process.platform === 'win32' ? `${extensionName}.exe` : extensionName);
if (!existsSync(bin)) {
bin = workspace.getConfiguration(extensionName).get<string>('server_path', '');

if (bin) {
if (bin?.startsWith('~/')) {
bin = bin.replace('~', homedir());
}
let bin = workspace.getConfiguration(extensionName).get<string>('server_path', '');

bin = which.sync(bin, { nothrow: true }) || bin;
if (bin) {
if (bin?.startsWith('~/')) {
bin = bin.replace('~', homedir());
}

bin = which.sync(bin, { nothrow: true }) || bin;
}

if (!bin || !existsSync(bin)) {
bin = join(this.extCtx.storagePath, process.platform === 'win32' ? `${extensionName}.exe` : extensionName);
}

if (!bin) {
Expand Down
2 changes: 1 addition & 1 deletion packages/dict-builder/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "dict-builder"
version = "0.1.0"
version = "0.2.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
1 change: 1 addition & 0 deletions packages/dict-builder/src/dict.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub fn get_pinyin_from_hanzi(hanzi: &str) -> String {
})
.collect::<Vec<&str>>()
.join("")
.replace("ü", "v")
}

pub fn format_dict(dict_path: &str) -> Result<Vec<(String, String, u32)>, Box<dyn error::Error>> {
Expand Down
2 changes: 1 addition & 1 deletion packages/ds-pinyin-lsp/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ds-pinyin-lsp"
version = "0.1.0"
version = "0.2.0"
edition = "2021"
repository = "https://github.com/iamcco/ds-pinyin-lsp"
description = "Pinyin language server for input Chinese."
Expand Down
Loading

0 comments on commit aa86402

Please sign in to comment.