1
0
Code Issues Pull Requests Packages Projects Releases Wiki Activity GitHub Gitee

QuickPick 逻辑移入独立文件

This commit is contained in:
程序员小墨 2024-04-04 22:04:35 +08:00
parent 704b428d5f
commit 4b734d219a
3 changed files with 53 additions and 51 deletions

View File

@ -1,5 +1,4 @@
import * as vscode from 'vscode'; import * as vscode from 'vscode';
import * as TextConversion from '../main-code/variable-conversion';
import { ConvertFunction, EOL } from '../type-definition/convert-function-type'; import { ConvertFunction, EOL } from '../type-definition/convert-function-type';
/** /**

View File

@ -0,0 +1,51 @@
import * as vscode from 'vscode';
import QuickPickItemEx from "../type-definition/extended-quick-pick-item-type";
/**
*
*/
function generateOptionsBasedOnText(text: string): Array<QuickPickItemEx> {
// 根据文本生成选项的逻辑
return [
{ label: '输入对应项下方任一关键词可快速选择', kind: vscode.QuickPickItemKind.Separator },
{ label: text.toUpperCase(), description: '转换为大写', detail: '关键词DaXie 大写 UpperCase', value: text.toUpperCase() },
{ label: 'Group 2', kind: vscode.QuickPickItemKind.Separator },
{ label: text.toLowerCase(), description: '转换为小写', value: text.toLowerCase() },
{ label: text.toLowerCase(), description: '转换为小写', value: text.toLowerCase() },
{ label: text.toLowerCase(), description: '转换为下划线', detail: '关键词_', value: text.toLowerCase() },
{ label: text.toLowerCase(), description: '转换为连字符', detail: '关键词:-', value: text.toLowerCase() },
];
}
export function handleQuickPick() {
// 获取当前编辑器
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, {
matchOnDetail: true,
title: '标题',
placeHolder: '输入对应项下方任一关键词可快速选择!'
}).then(selection => {
if (selection) {
// 处理用户的选择
vscode.window.showInformationMessage(`你选择了: ${selection}`);
}
});
}

View File

@ -4,7 +4,7 @@ import * as vscode from 'vscode';
import * as TextConversion from './main-code/variable-conversion'; import * as TextConversion from './main-code/variable-conversion';
import { ConvertFunction, EOL } from './type-definition/convert-function-type'; import { ConvertFunction, EOL } from './type-definition/convert-function-type';
import handleEditorReplace from './extension-handler/editor-submenu-handler'; import handleEditorReplace from './extension-handler/editor-submenu-handler';
import QuickPickItemEx from './type-definition/extended-quick-pick-item-type'; import { handleQuickPick } from './extension-handler/quick-pick-handler';
// This method is called when your extension is activated // This method is called when your extension is activated
// Your extension is activated the very first time the command is executed // Your extension is activated the very first time the command is executed
@ -62,55 +62,7 @@ export function activate(context: vscode.ExtensionContext) {
context.subscriptions.push(disposable); context.subscriptions.push(disposable);
} }
let convertCaseDisposable = vscode.commands.registerCommand('variable-conversion.convertCase', handleQuickPick);
/**
*
*/
function generateOptionsBasedOnText(text: string): Array<QuickPickItemEx> {
// 根据文本生成选项的逻辑
return [
{ label: '输入对应项下方任一关键词可快速选择', kind: vscode.QuickPickItemKind.Separator },
{ label: text.toUpperCase(), description: '转换为大写', detail: '关键词DaXie 大写 UpperCase', value: text.toUpperCase() },
{ label: 'Group 2', kind: vscode.QuickPickItemKind.Separator },
{ label: text.toLowerCase(), description: '转换为小写', value: text.toLowerCase() },
{ label: text.toLowerCase(), description: '转换为小写', value: text.toLowerCase() },
{ label: text.toLowerCase(), description: '转换为下划线', detail: '关键词_', value: text.toLowerCase() },
{ label: text.toLowerCase(), description: '转换为连字符', detail: '关键词:-', value: text.toLowerCase() },
];
}
let convertCaseDisposable = vscode.commands.registerCommand('variable-conversion.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, {
matchOnDetail: true,
title: '标题',
placeHolder: '输入对应项下方任一关键词可快速选择!'
}).then(selection => {
if (selection) {
// 处理用户的选择
vscode.window.showInformationMessage(`你选择了: ${selection}`);
}
});
});
context.subscriptions.push(convertCaseDisposable); context.subscriptions.push(convertCaseDisposable);
} }