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",
|
"main": "./out/extension.js",
|
||||||
"contributes": {
|
"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": [
|
"commands": [
|
||||||
{
|
{
|
||||||
"command": "extension.rule0",
|
"command": "extension.rule0",
|
||||||
@ -291,22 +307,6 @@
|
|||||||
"typescript": "^3.3.1",
|
"typescript": "^3.3.1",
|
||||||
"vscode-test": "^1.0.2"
|
"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": {
|
"dependencies": {
|
||||||
"transliteration": "^2.1.8"
|
"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 { Rule } from './interface';
|
||||||
import insertLog from './insertLog';
|
import insertLog from './insertLog';
|
||||||
import showResultMessage from './showResultMessage';
|
import showResultMessage from './showResultMessage';
|
||||||
|
|
||||||
export default function (context: ExtensionContext, RULES: Rule[]) {
|
export default function (context: ExtensionContext, RULES: Rule[]) {
|
||||||
RULES.forEach(({ title, rule }, index) => {
|
RULES.forEach(({ title, rule }, index) => {
|
||||||
let disposable = commands.registerCommand(`extension.rule${index}`, () => {
|
const disposable = commands.registerCommand(`extension.rule${index}`, () => {
|
||||||
const editor = window.activeTextEditor;
|
const editor = window.activeTextEditor;
|
||||||
if (editor) {
|
if (editor) {
|
||||||
const { selections } = 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 { Rule } from './interface';
|
||||||
import { COMPLETION_TRIGGER_ID } from './constant';
|
|
||||||
// import { slugify } from 'transliteration';
|
// import { slugify } from 'transliteration';
|
||||||
import insertLog from './insertLog';
|
import insertLog from './insertLog';
|
||||||
import showResultMessage from './showResultMessage';
|
import showResultMessage from './showResultMessage';
|
||||||
|
|
||||||
export default function (context: ExtensionContext, RULES: Rule[]) {
|
export default function (context: ExtensionContext, RULES: Rule[]) {
|
||||||
// commands.registerCommand('functions.insertRegex', insertRule);
|
// commands.registerCommand('functions.insertRegex', insertRule);
|
||||||
|
// 不确定是不是都兼容"*", 保守
|
||||||
const disposable = languages.registerCompletionItemProvider('*', {
|
const { supportedLanguages, triggerStringEnd } = getConfig();
|
||||||
|
const disposable = languages.registerCompletionItemProvider(supportedLanguages.split(','), {
|
||||||
provideCompletionItems(document, position) {
|
provideCompletionItems(document, position) {
|
||||||
|
const { triggerString } = getConfig();
|
||||||
|
// 如果为空表示关闭
|
||||||
|
if (!triggerString) return;
|
||||||
|
|
||||||
const linePrefix = document.lineAt(position).text.substr(0, position.character);
|
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) => {
|
setTimeout(() => {
|
||||||
// const match = title.match(/\((.+)\)/);
|
// showQuickPick
|
||||||
return {
|
window.showQuickPick(RULES.map(({ examples, title, rule }, i) => {
|
||||||
label: title,
|
// const match = title.match(/\((.+)\)/);
|
||||||
// description: null !== match ? match[1] : '',
|
return {
|
||||||
rule: String(rule), // 非标准字段, 仅仅为了传值
|
label: title,
|
||||||
detail: `例如: ${examples.join(' 或 ')}`
|
// description: null !== match ? match[1] : '',
|
||||||
};
|
rule: String(rule), // 非标准字段, 仅仅为了传值
|
||||||
}), {
|
detail: `例如: ${examples.join(' 或 ')}`
|
||||||
placeHolder: '请输入关键词',
|
};
|
||||||
// onDidSelectItem(item){
|
}), {
|
||||||
// console.log(item)
|
placeHolder: '请输入关键词',
|
||||||
// }
|
// onDidSelectItem(item){
|
||||||
}).then(item => {
|
// console.log(item)
|
||||||
if (!item) return
|
// }
|
||||||
insertRule(document, position, item.rule);
|
}).then(item => {
|
||||||
|
if (!item) return
|
||||||
|
insertRule(document, position, item.rule);
|
||||||
|
|
||||||
// 日志
|
// 日志
|
||||||
insertLog({
|
insertLog({
|
||||||
rule: item.rule,
|
rule: item.rule,
|
||||||
title: item.label,
|
title: item.label,
|
||||||
method: 'QuickPick'
|
method: 'QuickPick'
|
||||||
|
});
|
||||||
|
showResultMessage(item.label);
|
||||||
});
|
});
|
||||||
showResultMessage(item.label);
|
}, 0)
|
||||||
});
|
|
||||||
|
|
||||||
return void 0;
|
return void 0;
|
||||||
},
|
},
|
||||||
@ -46,35 +54,41 @@ export default function (context: ExtensionContext, RULES: Rule[]) {
|
|||||||
resolveCompletionItem(item) {
|
resolveCompletionItem(item) {
|
||||||
return item;
|
return item;
|
||||||
}
|
}
|
||||||
}, '.');
|
}, triggerStringEnd);
|
||||||
context.subscriptions.push(disposable);
|
context.subscriptions.push(disposable);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
function insertRule(document: TextDocument, position: Position, ruleString: string) {
|
function insertRule(document: TextDocument, position: Position, ruleString: string) {
|
||||||
|
const { triggerString } = getConfig();
|
||||||
|
|
||||||
const editor = window.activeTextEditor;
|
const editor = window.activeTextEditor;
|
||||||
if (void 0 === editor) return;
|
if (void 0 === editor) return;
|
||||||
editor.edit(editBuilder => {
|
editor.edit(editBuilder => {
|
||||||
const line = document.lineAt(position);
|
const line = document.lineAt(position);
|
||||||
// 起始
|
|
||||||
const startPostion = new Position(line.lineNumber, line.text.indexOf(COMPLETION_TRIGGER_ID));
|
|
||||||
|
|
||||||
// 结束(replace用)
|
// 起始, "zz."前面的位置
|
||||||
const endPostion = new Position(line.lineNumber, startPostion.character + COMPLETION_TRIGGER_ID.length);
|
const startPostion = position.translate(0, -triggerString.length);
|
||||||
|
|
||||||
|
editBuilder.replace(new Range(startPostion, position), ruleString);
|
||||||
// window.showInformationMessage(
|
|
||||||
// '' + startPostion.character
|
|
||||||
// , '' + endPostion.character
|
|
||||||
// );
|
|
||||||
|
|
||||||
editBuilder.replace(new Range(startPostion, endPostion), ruleString);
|
|
||||||
|
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
// 结束(selection用)
|
// 全选正则字符串
|
||||||
const endPostion = new Position(line.lineNumber, startPostion.character + ruleString.length);
|
const endPostion = new Position(line.lineNumber, startPostion.character + ruleString.length);
|
||||||
editor.selection = new Selection(startPostion, endPostion);
|
editor.selection = new Selection(startPostion, endPostion);
|
||||||
}, 0);
|
}, 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