支持 Shift + Alt + T 弹出 QuickPick 选择框
This commit is contained in:
parent
73f7ae9113
commit
e7b62379c9
@ -1,4 +1,4 @@
|
||||
# README
|
||||
# Variable Conversion 命名方式转换插件
|
||||
|
||||
After writing up a brief description, we recommend including the following sections.
|
||||
|
||||
|
@ -27,13 +27,15 @@
|
||||
}
|
||||
],
|
||||
"main": "./out/extension.js",
|
||||
"activationEvents": [],
|
||||
"activationEvents": [
|
||||
"*"
|
||||
],
|
||||
"contributes": {
|
||||
"commands": [
|
||||
// {
|
||||
// "command": "extension.convertCase",
|
||||
// "title": "字符串转换"
|
||||
// },
|
||||
{
|
||||
"command": "extension.convertCase",
|
||||
"title": "字符串转换"
|
||||
},
|
||||
{
|
||||
"command": "extension.toCamelCase",
|
||||
"title": "小驼峰 / 驼峰命名 (Camel Case) [ fooBar ]"
|
||||
@ -110,11 +112,12 @@
|
||||
"menus": {
|
||||
// 编辑器右键菜单
|
||||
"editor/context": [
|
||||
// {
|
||||
// "when": "editorTextFocus",
|
||||
// "command": "extension.convertCase",
|
||||
// "group": "1_modification@9"
|
||||
// },
|
||||
{
|
||||
"when": "editorTextFocus",
|
||||
"command": "extension.convertCase",
|
||||
// "group": "1_modification@9"
|
||||
"group": "navigation@9"
|
||||
},
|
||||
{
|
||||
"when": "editorTextFocus && _textSelectionLength >= 1",
|
||||
"submenu": "extension.stringConversionMenu",
|
||||
|
13
package.json
13
package.json
@ -24,9 +24,15 @@
|
||||
}
|
||||
],
|
||||
"main": "./out/extension.js",
|
||||
"activationEvents": [],
|
||||
"activationEvents": [
|
||||
"*"
|
||||
],
|
||||
"contributes": {
|
||||
"commands": [
|
||||
{
|
||||
"command": "extension.convertCase",
|
||||
"title": "字符串转换"
|
||||
},
|
||||
{
|
||||
"command": "extension.toCamelCase",
|
||||
"title": "小驼峰 / 驼峰命名 (Camel Case) [ fooBar ]"
|
||||
@ -69,6 +75,11 @@
|
||||
],
|
||||
"menus": {
|
||||
"editor/context": [
|
||||
{
|
||||
"when": "editorTextFocus",
|
||||
"command": "extension.convertCase",
|
||||
"group": "navigation@9"
|
||||
},
|
||||
{
|
||||
"when": "editorTextFocus && _textSelectionLength >= 1",
|
||||
"submenu": "extension.stringConversionMenu",
|
||||
|
@ -24,9 +24,16 @@ export function activate(context: vscode.ExtensionContext) {
|
||||
// 用于判断是否展示右键菜单
|
||||
vscode.window.onDidChangeTextEditorSelection(event => {
|
||||
const text = event.textEditor.document.getText(event.selections[0]);
|
||||
// console.log('text.length', text.length);
|
||||
vscode.commands.executeCommand('setContext', '_textSelectionLength', text.length);
|
||||
});
|
||||
|
||||
/**
|
||||
* 编辑器右键菜单
|
||||
*
|
||||
* @param convertFunction
|
||||
* @returns
|
||||
*/
|
||||
const handleEditorReplace = (convertFunction: ConvertFunction) => {
|
||||
// 获取当前编辑器
|
||||
let editor = vscode.window.activeTextEditor;
|
||||
@ -80,6 +87,51 @@ export function activate(context: vscode.ExtensionContext) {
|
||||
context.subscriptions.push(disposable);
|
||||
}
|
||||
|
||||
interface ExtendedQuickPickItem extends vscode.QuickPickItem {
|
||||
value: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* 弹出的提示
|
||||
*/
|
||||
function generateOptionsBasedOnText(text: string): Array<ExtendedQuickPickItem | vscode.QuickPickItem> {
|
||||
// 根据文本生成选项的逻辑
|
||||
return [
|
||||
{ label: text.toUpperCase(), description: '转换为大写', value: text.toUpperCase() },
|
||||
{ label: 'Group 1', kind: vscode.QuickPickItemKind.Separator },
|
||||
{ label: text.toLowerCase(), description: '转换为小写', value: text.toLowerCase() },
|
||||
];
|
||||
}
|
||||
|
||||
let convertCaseDisposable = vscode.commands.registerCommand('extension.convertCase', () => {
|
||||
// 获取当前编辑器
|
||||
let editor = vscode.window.activeTextEditor;
|
||||
if (!editor) {
|
||||
return;
|
||||
}
|
||||
|
||||
let document = editor.document;
|
||||
let selection = editor.selection;
|
||||
|
||||
// 获取选中的文本
|
||||
let text = document.getText(selection);
|
||||
|
||||
if (text.length === 0) {
|
||||
vscode.window.showInformationMessage('Please select the variable you want to convert and try again.\n请选择需要转换的变量后重试');
|
||||
return;
|
||||
}
|
||||
|
||||
// 基于选中的文本生成选项
|
||||
const options = generateOptionsBasedOnText(text);
|
||||
// 显示推荐项列表
|
||||
vscode.window.showQuickPick(options).then(selection => {
|
||||
if (selection) {
|
||||
// 处理用户的选择
|
||||
vscode.window.showInformationMessage(`你选择了: ${selection}`);
|
||||
}
|
||||
});
|
||||
});
|
||||
context.subscriptions.push(convertCaseDisposable);
|
||||
}
|
||||
|
||||
// This method is called when your extension is deactivated
|
||||
|
Loading…
Reference in New Issue
Block a user