mirror of
https://github.com/any86/any-rule.git
synced 2025-07-14 15:38:58 +08:00
pref: 增加配置功能. 通过setTimeout让zz.触发后置
This commit is contained in:
parent
44da1beefd
commit
b5fbebc67b
32
package.json
32
package.json
@ -23,6 +23,22 @@
|
||||
],
|
||||
"main": "./out/extension.js",
|
||||
"contributes": {
|
||||
"configuration": {
|
||||
"type": "object",
|
||||
"title": "any-rule",
|
||||
"properties": {
|
||||
"AnyRule.triggerString": {
|
||||
"type": "string",
|
||||
"default": "zz.",
|
||||
"description": "触发字符串"
|
||||
},
|
||||
"AnyRule.supportedLanguages": {
|
||||
"type": "string",
|
||||
"default": "*, javascript, typescript, javascriptreact, typescriptreact, markdown, jsx, vue, html, json, plaintext, coffeescript",
|
||||
"description": "如果您的文件格式未被支持, 请在此处添加(⚡添加成功后需要重启vscode)"
|
||||
}
|
||||
}
|
||||
},
|
||||
"commands": [
|
||||
{
|
||||
"command": "extension.rule0",
|
||||
@ -291,22 +307,6 @@
|
||||
"typescript": "^3.3.1",
|
||||
"vscode-test": "^1.0.2"
|
||||
},
|
||||
"configuration": {
|
||||
"type": "object",
|
||||
"title": "Any Rule",
|
||||
"properties": {
|
||||
"anyRule.triggerString": {
|
||||
"type": "string",
|
||||
"default": "zz",
|
||||
"description": "触发字符串"
|
||||
},
|
||||
"anyRule.supportedLanguages": {
|
||||
"type": "string",
|
||||
"default": "javascript,typescript,javascriptreact,typescriptreact,vue,coffeescript",
|
||||
"description": "激活AnyRule的文件类型,多个类型用逗号分隔"
|
||||
}
|
||||
}
|
||||
},
|
||||
"dependencies": {
|
||||
"transliteration": "^2.1.8"
|
||||
}
|
||||
|
@ -1 +0,0 @@
|
||||
export const COMPLETION_TRIGGER_ID = 'zz.';
|
@ -1,11 +1,11 @@
|
||||
import {window,version,commands,Range,ExtensionContext,extensions} from "vscode";
|
||||
import {window,commands,Range,ExtensionContext} from "vscode";
|
||||
import { Rule } from './interface';
|
||||
import insertLog from './insertLog';
|
||||
import showResultMessage from './showResultMessage';
|
||||
|
||||
export default function (context: ExtensionContext, RULES: Rule[]) {
|
||||
RULES.forEach(({ title, rule }, index) => {
|
||||
let disposable = commands.registerCommand(`extension.rule${index}`, () => {
|
||||
const disposable = commands.registerCommand(`extension.rule${index}`, () => {
|
||||
const editor = window.activeTextEditor;
|
||||
if (editor) {
|
||||
const { selections } = editor;
|
||||
|
@ -1,44 +1,52 @@
|
||||
import { ExtensionContext, version, CompletionItemKind, languages, env, Extension, window, commands, TextDocument, Position, Range, Selection, MarkdownString, extensions, Uri } from "vscode";
|
||||
import { ExtensionContext, workspace, languages, window, TextDocument, Position, Range, Selection } from "vscode";
|
||||
import { Rule } from './interface';
|
||||
import { COMPLETION_TRIGGER_ID } from './constant';
|
||||
// import { slugify } from 'transliteration';
|
||||
import insertLog from './insertLog';
|
||||
import showResultMessage from './showResultMessage';
|
||||
|
||||
export default function (context: ExtensionContext, RULES: Rule[]) {
|
||||
// commands.registerCommand('functions.insertRegex', insertRule);
|
||||
|
||||
const disposable = languages.registerCompletionItemProvider('*', {
|
||||
// 不确定是不是都兼容"*", 保守
|
||||
const { supportedLanguages, triggerStringEnd } = getConfig();
|
||||
const disposable = languages.registerCompletionItemProvider(supportedLanguages.split(','), {
|
||||
provideCompletionItems(document, position) {
|
||||
const { triggerString } = getConfig();
|
||||
// 如果为空表示关闭
|
||||
if (!triggerString) return;
|
||||
|
||||
const linePrefix = document.lineAt(position).text.substr(0, position.character);
|
||||
if (!linePrefix.endsWith(COMPLETION_TRIGGER_ID)) return;
|
||||
if (!linePrefix.endsWith(triggerString)) return;
|
||||
|
||||
// showQuickPick
|
||||
window.showQuickPick(RULES.map(({ examples, title, rule }, i) => {
|
||||
// const match = title.match(/\((.+)\)/);
|
||||
return {
|
||||
label: title,
|
||||
// description: null !== match ? match[1] : '',
|
||||
rule: String(rule), // 非标准字段, 仅仅为了传值
|
||||
detail: `例如: ${examples.join(' 或 ')}`
|
||||
};
|
||||
}), {
|
||||
placeHolder: '请输入关键词',
|
||||
// onDidSelectItem(item){
|
||||
// console.log(item)
|
||||
// }
|
||||
}).then(item => {
|
||||
if (!item) return
|
||||
insertRule(document, position, item.rule);
|
||||
// 增加优先级
|
||||
setTimeout(() => {
|
||||
// showQuickPick
|
||||
window.showQuickPick(RULES.map(({ examples, title, rule }, i) => {
|
||||
// const match = title.match(/\((.+)\)/);
|
||||
return {
|
||||
label: title,
|
||||
// description: null !== match ? match[1] : '',
|
||||
rule: String(rule), // 非标准字段, 仅仅为了传值
|
||||
detail: `例如: ${examples.join(' 或 ')}`
|
||||
};
|
||||
}), {
|
||||
placeHolder: '请输入关键词',
|
||||
// onDidSelectItem(item){
|
||||
// console.log(item)
|
||||
// }
|
||||
}).then(item => {
|
||||
if (!item) return
|
||||
insertRule(document, position, item.rule);
|
||||
|
||||
// 日志
|
||||
insertLog({
|
||||
rule: item.rule,
|
||||
title: item.label,
|
||||
method: 'QuickPick'
|
||||
// 日志
|
||||
insertLog({
|
||||
rule: item.rule,
|
||||
title: item.label,
|
||||
method: 'QuickPick'
|
||||
});
|
||||
showResultMessage(item.label);
|
||||
});
|
||||
showResultMessage(item.label);
|
||||
});
|
||||
}, 0)
|
||||
|
||||
|
||||
return void 0;
|
||||
},
|
||||
@ -46,35 +54,41 @@ export default function (context: ExtensionContext, RULES: Rule[]) {
|
||||
resolveCompletionItem(item) {
|
||||
return item;
|
||||
}
|
||||
}, '.');
|
||||
}, triggerStringEnd);
|
||||
context.subscriptions.push(disposable);
|
||||
}
|
||||
|
||||
|
||||
function insertRule(document: TextDocument, position: Position, ruleString: string) {
|
||||
const { triggerString } = getConfig();
|
||||
|
||||
const editor = window.activeTextEditor;
|
||||
if (void 0 === editor) return;
|
||||
editor.edit(editBuilder => {
|
||||
const line = document.lineAt(position);
|
||||
// 起始
|
||||
const startPostion = new Position(line.lineNumber, line.text.indexOf(COMPLETION_TRIGGER_ID));
|
||||
|
||||
// 结束(replace用)
|
||||
const endPostion = new Position(line.lineNumber, startPostion.character + COMPLETION_TRIGGER_ID.length);
|
||||
// 起始, "zz."前面的位置
|
||||
const startPostion = position.translate(0, -triggerString.length);
|
||||
|
||||
|
||||
// window.showInformationMessage(
|
||||
// '' + startPostion.character
|
||||
// , '' + endPostion.character
|
||||
// );
|
||||
|
||||
editBuilder.replace(new Range(startPostion, endPostion), ruleString);
|
||||
editBuilder.replace(new Range(startPostion, position), ruleString);
|
||||
|
||||
setTimeout(() => {
|
||||
// 结束(selection用)
|
||||
// 全选正则字符串
|
||||
const endPostion = new Position(line.lineNumber, startPostion.character + ruleString.length);
|
||||
editor.selection = new Selection(startPostion, endPostion);
|
||||
}, 0);
|
||||
});
|
||||
}
|
||||
|
||||
// 获取配置
|
||||
function getConfig() {
|
||||
const configuration = workspace.getConfiguration();
|
||||
const { triggerString, supportedLanguages = '*' } = configuration.AnyRule;
|
||||
const { length } = triggerString;
|
||||
const triggerStringStart = triggerString.substr(0, length - 1);
|
||||
const triggerStringEnd = triggerString.substr(-1);
|
||||
|
||||
return {
|
||||
triggerStringStart, triggerStringEnd, triggerString, supportedLanguages
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user