Skip to content
This repository has been archived by the owner on Jul 14, 2020. It is now read-only.

Commit

Permalink
获得vsc本身的提示并翻译为拼音
Browse files Browse the repository at this point in the history
另外重构了一下代码
  • Loading branch information
hbybyyang committed Jun 26, 2020
1 parent 8d14fd4 commit 04e536e
Showing 1 changed file with 38 additions and 52 deletions.
90 changes: 38 additions & 52 deletions 补全实现.js
Original file line number Diff line number Diff line change
@@ -1,68 +1,54 @@
const vscode = require('vscode');
const bopomofo = require('./bopomofo')
const 五笔 = require('./五笔输入法')
var 提示文本 = ["中文测试", "文本", "数据", "中英结合abdc"];
const wordPattern = /(-?\d*\.\d\w*)|([^\`\~\!\@\^\&\*\(\)\-\#\?\=\+\[\{\]\}\\\|\;\:\'\"\,\.\<\>\/\s·ˉˇ¨]+)/g;
const 提示方式 = vscode.workspace.getConfiguration('中文代码快速补全').get("提示方式")

function provideCompletionItems(document, position, token, context) {

var 总行数 = document.lineCount
var 当前行 = position.line;
var 代码内容 = "";
for (var i = 0; i < 总行数; i++) {
if (i != 当前行)
代码内容 += document.lineAt(i).text + "\n";
function 包含中文(str) {
return /.*[\u4e00-\u9fa5]+.*$/.test(str)
}
function 获得拼音(提示方式, 文本) {
var 拼音 = null
if (["五笔98全码", "五笔98四码"].includes(提示方式)) {
拼音 = 五笔.五笔(文本, 提示方式)
} else {
拼音 = bopomofo.pinyin(文本, 2, false, true, '')
if (提示方式 != "全拼") 拼音 = bopomofo.双拼转换(拼音, 提示方式)
}
var 提示方式 = vscode.workspace.getConfiguration('中文代码快速补全').get("提示方式")

提示文本 = 去重(代码内容.match(wordPattern));
return 提示文本.map(文本 => {
if (["五笔98全码", "五笔98四码"].includes(提示方式)) {
var 拼音 = 五笔.五笔(文本, 提示方式)
} else {
var 拼音 = bopomofo.pinyin(文本, 2, false, true, '')
if (提示方式 != "全拼") 拼音 = bopomofo.双拼转换(拼音, 提示方式)
}
var item = new vscode.CompletionItem(
拼音,
vscode.CompletionItemKind.Text)
item.sortText = 拼音
item.filterText = 拼音
item.label = 文本 + '\t' + 拼音
item.insertText = 文本
return item;
return 拼音
}

var provideCompletionItems_上次调用参数 = null
async function provideCompletionItems(document, position, token, context) {
// 防止无限递归
var 本次调用参数 = JSON.stringify(arguments)
if (本次调用参数 == provideCompletionItems_上次调用参数)
return []
provideCompletionItems_上次调用参数 = 本次调用参数

// 获得提示字
// https://code.visualstudio.com/api/references/commands
// vscode.executeCompletionItemProvider 会调用 provideCompletionItems, 所以要防止无限递归
var 系统解析出的关键字 = await vscode.commands.executeCommand('vscode.executeCompletionItemProvider', document.uri, position)
console.log('系统解析出的关键字', 系统解析出的关键字)

// 过滤掉不包含中文的,就是提示文本了
// 另外它似乎会在引入文本内解析一次,所以包含中文的字段会重复,过滤\t来把重复的字段删掉
var 提示文本 = 系统解析出的关键字.items.filter(a => 包含中文(a.label)).filter(a => a.label.indexOf('\t') == -1)
console.log('提示文本', 提示文本)

// 将标签转成拼音返回
return 提示文本.map(a => {
var r = Object.assign(a)
r.label = a.label + '\t' + 获得拼音(提示方式, a.label)
return r
})

}
function 包含中文(str) {
if (/.*[\u4e00-\u9fa5]+.*$/.test(str)) return true;
return false;
}
/**
* 光标选中当前自动补全item时触发动作,一般情况下无需处理
* @param {*} item
* @param {*} token
*/

function resolveCompletionItem(item, token) {
return null;
}

function 去重(arr) {
var ret = Array.from(new Set(arr))

for (var i = 0; i < ret.length; i++) {
if ((!包含中文(ret[i])) || ret[i].length > 20) {
ret.splice(i, 1);
i--;
}
}

return ret;
}

module.exports = function (context) {
// 注册代码建议提示,只有当按下“.”时才触发
context.subscriptions.push(vscode.languages.registerCompletionItemProvider({ scheme: 'file', language: '*' }, {
provideCompletionItems,
resolveCompletionItem
Expand Down

3 comments on commit 04e536e

@nobodxbodon
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这样是完全基于 IDE 的补全选项、不另外提取当前代码中的中文字段了吗?
一个小问题是,如果 vsc 本身对某个语言的插件没有安装的话(像首页的Kotlin例子)估计就没提示了。我再测测另外的几个例子先。。

@zj1d
Copy link
Collaborator

@zj1d zj1d commented on 04e536e Jun 27, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

妙啊 (>^ω^<)

@lsby
Copy link
Collaborator

@lsby lsby commented on 04e536e Jun 27, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这样是完全基于IDE的补全选项,不另外从内部代码中提取的中文位置了吗?
一个小问题是,如果vsc本身对某一种语言的插件没有安装的话(像家用的Kotlin示例)估计就没提示了。我再测测另外的几个例子先。。

vsc的自动补全中就会搜索当前文档的关键词啦

Please sign in to comment.