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

支持通过VSCode设置页配置哪些格式是禁用的 (#2)

* 添加 VSCode 配置项 (逻辑待实现)

* QuickPick 菜单支持过滤用户配置的禁用格式 (disableFormat)

* 右键菜单支持过滤用户配置的禁用格式 (disableFormat)

* 快捷键循环转换支持过滤用户配置的禁用格式 (disableFormat)

* 为了发版暂时隐藏未开发完成的 variable-conversion.circularConversionFormatOrder 配置项

* 配置项描述调整:中文在前英文在后

* 更新 README.md 自述文档
This commit is contained in:
程序员小墨
2024-07-29 23:39:18 +08:00
committed by GitHub
parent 2f3dc60ca2
commit 9021df5ea8
8 changed files with 500 additions and 62 deletions

View File

@@ -1,11 +1,12 @@
import * as vscode from 'vscode';
import QuickPickItemEx from "../type-definition/QuickPickItemExType";
import { quickPickSupportCases } from '../type-definition/SupportCaseType';
import { QuickPickSupportCaseItem, quickPickSupportCases } from '../type-definition/SupportCaseType';
import { TransformTextResult } from '../type-definition/TransformTextResultType';
import { transformMutliLineText, transformMutliSelectionText } from '../main-code/transform';
import { transformMutliSelectionText } from '../main-code/transform';
import { EOL } from '../type-definition/EOLType';
import { caseConversion } from '../main-code/conversion';
import { isStringArrayEqual } from '../main-code/utils';
import { getUserConfigurations } from '../main-code/user-configuration';
const QuickPickLabelMaxLength = 60;
@@ -18,12 +19,12 @@ interface RecommendItem {
/**
* 弹出的提示
*/
function generateOptionsBasedOnText(textList: string[], eol: EOL): Array<QuickPickItemEx> {
function generateOptionsBasedOnText(textList: string[], eol: EOL, enabledQuickPickSupportCases: Array<QuickPickSupportCaseItem>): Array<QuickPickItemEx> {
// Cut text 切割文本
const resultsList: Array<TransformTextResult[]> = transformMutliSelectionText(textList);
const mergeResultList: Array<RecommendItem> = [];
for (const quickPick of quickPickSupportCases) {
for (const quickPick of enabledQuickPickSupportCases) {
const conversionResults: Array<string> = [];
for (let i = 0; i < textList.length; i++) {
const text = textList[i];
@@ -95,8 +96,24 @@ export function handleQuickPick() {
return;
}
// issue: #1 https://github.com/coder-xiaomo/variable-conversion-vscode-extension/issues/1
// 获取用户配置
const disableFormatList = getUserConfigurations('disableFormat');
// 排除禁用的选项
const enabledQuickPickSupportCases = [];
for (const quickPick of quickPickSupportCases) {
if (disableFormatList.includes(quickPick.settingsKey)) {
continue;
}
enabledQuickPickSupportCases.push(quickPick);
}
if (enabledQuickPickSupportCases.length === 0) {
vscode.window.showInformationMessage('所有格式都已被配置为禁用,请修改配置 `variable-conversion.disableFormat` 后重试\nAll formats have been configured to disable. Modify the `variable-conversion.disableFormat` configuration and try again.');
return;
}
// 基于选中的文本生成选项
const options = generateOptionsBasedOnText(textList, eol);
const options = generateOptionsBasedOnText(textList, eol, enabledQuickPickSupportCases);
if (options.length === 0) {
vscode.window.showInformationMessage('所选内容暂无可选转换,请尝试重新选择\nNo conversion candidates are available for the selected content, please try to select another text.');
return;

View File

@@ -7,6 +7,7 @@ import { commands } from './type-definition/SupportCaseType';
import { createStatusBarItem, updateStatusBarItemVisable } from './extension-handler/status-bar-handler';
import * as CyclicConversion from './main-code/cyclic-conversion';
import { EOL } from './type-definition/EOLType';
import { getUserConfigurations } from './main-code/user-configuration';
// This method is called when your extension is activated
// Your extension is activated the very first time the command is executed
@@ -41,6 +42,14 @@ export function activate(context: vscode.ExtensionContext) {
// 更新 _textSelectionLength (用于判断是否展示右键菜单)
vscode.commands.executeCommand('setContext', '_textSelectionLength', selectTextLength);
// issue: #1 https://github.com/coder-xiaomo/variable-conversion-vscode-extension/issues/1
// 获取用户配置
const disableFormatList = getUserConfigurations('disableFormat');
// 更新右键菜单每一项是否展示
for (const { settingsKey } of commands) {
vscode.commands.executeCommand('setContext', '_isHideSubMenuItem_' + settingsKey, disableFormatList.includes(settingsKey));
}
// 判断是否展示状态栏按钮
updateStatusBarItemVisable(selectTextLength);

View File

@@ -3,6 +3,7 @@ import { EOL } from "../type-definition/EOLType";
import { cyclicConvertCaseOrder } from "../type-definition/SupportCaseType";
import { caseConversion } from "./conversion";
import { isStringArrayEqual, stringListArrayDuplicateRemoval } from './utils';
import { getUserConfigurations } from './user-configuration';
interface UserSelection {
currentEol: EOL
@@ -64,16 +65,25 @@ function lazyConvert() {
return;
}
// 获取用户配置
const disableFormatList = getUserConfigurations('disableFormat');
const textList = userSelection.currentSelectionsText;
// vscode.window.showInformationMessage('lazyConvert' + textList.join('\n'));
const eol = userSelection.currentEol;
const conversionsTarget: Array<string[]> = [textList];
for (const cyclicConvertCase of cyclicConvertCaseOrder) {
// issue: #1 https://github.com/coder-xiaomo/variable-conversion-vscode-extension/issues/1
// 跳过禁用的目标格式
if (disableFormatList.includes(cyclicConvertCase.settingsKey)) {
continue;
}
// 每一个类型
const conversionsTargetItem: string[] = [];
for (const line of textList) {
// 选中区块的每一行
const conversionResult: string = caseConversion(cyclicConvertCase, line, eol);
const conversionResult: string = caseConversion(cyclicConvertCase.type, line, eol);
conversionsTargetItem.push(conversionResult);
}
conversionsTarget.push(conversionsTargetItem);

View File

@@ -0,0 +1,14 @@
const vscode = require('vscode');
function getUserConfigurations(configKey: string) {
const config = vscode.workspace.getConfiguration('variable-conversion');
// 获取 disableFormat 配置项
const configValue = config.get(configKey);
console.log('configValue:', configValue);
return configValue;
}
export {
getUserConfigurations
};

View File

@@ -3,7 +3,7 @@
*
* Code:
* - Add type definition in below `SupportCase` enum and following array
* - Add `commands`, `menus` parts in [package.json] and [package-comment.jsonc]
* - Add `commands`, `menus`, `configuration` parts in [package.json] and [package-comment.jsonc]
* - Add main conversion logic in [src/main-code/conversion.ts]
*
* Test:
@@ -281,188 +281,301 @@ const keyword = {
/**
* 接管的变量转换命令
*/
export const commands: Array<{ command: string; targetCase: SupportCase }> = [
{ command: 'variable-conversion.toCamelCase', targetCase: SupportCase.CAMEL_CASE },
{ command: 'variable-conversion.toPascalCase', targetCase: SupportCase.PASCAL_CASE },
{ command: 'variable-conversion.toSnakeCase', targetCase: SupportCase.SNAKE_CASE },
{ command: 'variable-conversion.toSnakeUpperCase', targetCase: SupportCase.SNAKE_UPPER_CASE },
{ command: 'variable-conversion.toSnakePascalCase', targetCase: SupportCase.SNAKE_PASCAL_CASE },
{ command: 'variable-conversion.toSnakeCamelCase', targetCase: SupportCase.SNAKE_CAMEL_CASE },
{ command: 'variable-conversion.toKebabCase', targetCase: SupportCase.KEBAB_CASE },
{ command: 'variable-conversion.toKebabUpperCase', targetCase: SupportCase.KEBAB_UPPER_CASE },
{ command: 'variable-conversion.toKebabPascalCase', targetCase: SupportCase.KEBAB_PASCAL_CASE },
{ command: 'variable-conversion.toKebabCamelCase', targetCase: SupportCase.KEBAB_CAMEL_CASE },
{ command: 'variable-conversion.toSpaceCase', targetCase: SupportCase.SPACE_CASE },
{ command: 'variable-conversion.toSpaceUpperCase', targetCase: SupportCase.SPACE_UPPER_CASE },
{ command: 'variable-conversion.toSpacePascalCase', targetCase: SupportCase.SPACE_PASCAL_CASE },
{ command: 'variable-conversion.toSpaceCamelCase', targetCase: SupportCase.SPACE_CAMEL_CASE },
{ command: 'variable-conversion.toDotCase', targetCase: SupportCase.DOT_CASE },
{ command: 'variable-conversion.toDotUpperCase', targetCase: SupportCase.DOT_UPPER_CASE },
{ command: 'variable-conversion.toDotPascalCase', targetCase: SupportCase.DOT_PASCAL_CASE },
{ command: 'variable-conversion.toDotCamelCase', targetCase: SupportCase.DOT_CAMEL_CASE },
{ command: 'variable-conversion.toLowerCase', targetCase: SupportCase.LOWER_CASE },
{ command: 'variable-conversion.toUpperCase', targetCase: SupportCase.UPPER_CASE },
export const commands: Array<{ command: string; targetCase: SupportCase; settingsKey: string }> = [
{
command: 'variable-conversion.toCamelCase',
targetCase: SupportCase.CAMEL_CASE,
settingsKey: 'camel_case'
},
{
command: 'variable-conversion.toPascalCase',
targetCase: SupportCase.PASCAL_CASE,
settingsKey: 'pascal_case'
},
// +++++++++++++++++++++++++++++++++++++++++++++++
{
command: 'variable-conversion.toSnakeCase',
targetCase: SupportCase.SNAKE_CASE,
settingsKey: 'snake_case'
},
{
command: 'variable-conversion.toSnakeUpperCase',
targetCase: SupportCase.SNAKE_UPPER_CASE,
settingsKey: 'snake_upper_case'
},
{
command: 'variable-conversion.toSnakePascalCase',
targetCase: SupportCase.SNAKE_PASCAL_CASE,
settingsKey: 'snake_pascal_case'
},
{
command: 'variable-conversion.toSnakeCamelCase',
targetCase: SupportCase.SNAKE_CAMEL_CASE,
settingsKey: 'snake_camel_case'
},
// +++++++++++++++++++++++++++++++++++++++++++++++
{
command: 'variable-conversion.toKebabCase',
targetCase: SupportCase.KEBAB_CASE,
settingsKey: 'kebab_case'
},
{
command: 'variable-conversion.toKebabUpperCase',
targetCase: SupportCase.KEBAB_UPPER_CASE,
settingsKey: 'kebab_upper_case'
},
{
command: 'variable-conversion.toKebabPascalCase',
targetCase: SupportCase.KEBAB_PASCAL_CASE,
settingsKey: 'kebab_pascal_case'
},
{
command: 'variable-conversion.toKebabCamelCase',
targetCase: SupportCase.KEBAB_CAMEL_CASE,
settingsKey: 'kebab_camel_case'
},
// +++++++++++++++++++++++++++++++++++++++++++++++
{
command: 'variable-conversion.toSpaceCase',
targetCase: SupportCase.SPACE_CASE,
settingsKey: 'space_case'
},
{
command: 'variable-conversion.toSpaceUpperCase',
targetCase: SupportCase.SPACE_UPPER_CASE,
settingsKey: 'space_upper_case'
},
{
command: 'variable-conversion.toSpacePascalCase',
targetCase: SupportCase.SPACE_PASCAL_CASE,
settingsKey: 'space_pascal_case'
},
{
command: 'variable-conversion.toSpaceCamelCase',
targetCase: SupportCase.SPACE_CAMEL_CASE,
settingsKey: 'space_camel_case'
},
// +++++++++++++++++++++++++++++++++++++++++++++++
{
command: 'variable-conversion.toDotCase',
targetCase: SupportCase.DOT_CASE,
settingsKey: 'dot_case'
},
{
command: 'variable-conversion.toDotUpperCase',
targetCase: SupportCase.DOT_UPPER_CASE,
settingsKey: 'dot_upper_case'
},
{
command: 'variable-conversion.toDotPascalCase',
targetCase: SupportCase.DOT_PASCAL_CASE,
settingsKey: 'dot_pascal_case'
},
{
command: 'variable-conversion.toDotCamelCase',
targetCase: SupportCase.DOT_CAMEL_CASE,
settingsKey: 'dot_camel_case'
},
// +++++++++++++++++++++++++++++++++++++++++++++++
{
command: 'variable-conversion.toLowerCase',
targetCase: SupportCase.LOWER_CASE,
settingsKey: 'lower_case'
},
{
command: 'variable-conversion.toUpperCase',
targetCase: SupportCase.UPPER_CASE,
settingsKey: 'upper_case'
},
];
export interface QuickPickSupportCaseItem {
type: SupportCase,
name: string,
shortName: string,
keyword: string[],
settingsKey: string,
}
/**
* 所有支持的命名方式
* @since 2024-04-06
*/
export const quickPickSupportCases = [
export const quickPickSupportCases: Array<QuickPickSupportCaseItem> = [
{
type: SupportCase.CAMEL_CASE,
name: '小驼峰(驼峰)命名',
shortName: '小驼峰',
keyword: keyword.camel,
settingsKey: 'camel_case',
},
{
type: SupportCase.PASCAL_CASE,
name: '大驼峰(帕斯卡)命名',
shortName: '帕斯卡',
keyword: keyword.pascal,
settingsKey: 'pascal_case',
},
{
type: SupportCase.SNAKE_CASE,
name: '下划线(蛇形)命名',
shortName: '蛇形',
keyword: [...keyword.snake, ...keyword.lower],
settingsKey: 'snake_case',
},
{
type: SupportCase.SNAKE_CAMEL_CASE,
name: '下划线(蛇形) + 小驼峰(驼峰)命名',
shortName: '蛇形驼峰',
keyword: [...keyword.snake, ...keyword.camel],
settingsKey: 'snake_camel_case',
},
{
type: SupportCase.SNAKE_PASCAL_CASE,
name: '下划线(蛇形) + 大驼峰(帕斯卡)命名',
shortName: '蛇形帕斯卡',
keyword: [...keyword.snake, ...keyword.pascal],
settingsKey: 'snake_pascal_case',
},
{
type: SupportCase.SNAKE_UPPER_CASE,
name: '下划线(蛇形) + 全大写命名',
shortName: '蛇形大写',
keyword: [...keyword.snake, ...keyword.upper],
settingsKey: 'snake_upper_case',
},
{
type: SupportCase.KEBAB_CASE,
name: '中划线(连字符/脊柱式)命名',
shortName: '脊柱',
keyword: [...keyword.kebab, ...keyword.lower],
settingsKey: 'kebab_case',
},
{
type: SupportCase.KEBAB_CAMEL_CASE,
name: '中划线(连字符/脊柱式) + 小驼峰(驼峰)命名',
shortName: '脊柱驼峰',
keyword: [...keyword.kebab, ...keyword.camel],
settingsKey: 'kebab_camel_case',
},
{
type: SupportCase.KEBAB_PASCAL_CASE,
name: '中划线(连字符/脊柱式) + 大驼峰(帕斯卡)命名',
shortName: '脊柱帕斯卡',
keyword: [...keyword.kebab, ...keyword.pascal],
settingsKey: 'kebab_pascal_case',
},
{
type: SupportCase.KEBAB_UPPER_CASE,
name: '中划线(连字符/脊柱式) + 全大写命名',
shortName: '脊柱大写',
keyword: [...keyword.kebab, ...keyword.upper],
settingsKey: 'kebab_upper_case',
},
{
type: SupportCase.SPACE_CASE,
name: '空格分隔命名',
shortName: '脊柱',
keyword: [...keyword.space, ...keyword.lower],
settingsKey: 'space_case',
},
{
type: SupportCase.SPACE_CAMEL_CASE,
name: '空格分隔 + 小驼峰(驼峰)命名',
shortName: '脊柱驼峰',
keyword: [...keyword.space, ...keyword.camel],
settingsKey: 'space_camel_case',
},
{
type: SupportCase.SPACE_PASCAL_CASE,
name: '空格分隔 + 大驼峰(帕斯卡)命名',
shortName: '脊柱帕斯卡',
keyword: [...keyword.space, ...keyword.pascal],
settingsKey: 'space_pascal_case',
},
{
type: SupportCase.SPACE_UPPER_CASE,
name: '空格分隔 + 全大写命名',
shortName: '脊柱大写',
keyword: [...keyword.space, ...keyword.upper],
settingsKey: 'space_upper_case',
},
{
type: SupportCase.DOT_CASE,
name: '点分隔命名',
shortName: '脊柱',
keyword: [...keyword.dot, ...keyword.lower],
settingsKey: 'dot_case',
},
{
type: SupportCase.DOT_CAMEL_CASE,
name: '点分隔 + 小驼峰(驼峰)命名',
shortName: '脊柱驼峰',
keyword: [...keyword.dot, ...keyword.camel],
settingsKey: 'dot_camel_case',
},
{
type: SupportCase.DOT_PASCAL_CASE,
name: '点分隔 + 大驼峰(帕斯卡)命名',
shortName: '脊柱帕斯卡',
keyword: [...keyword.dot, ...keyword.pascal],
settingsKey: 'dot_pascal_case',
},
{
type: SupportCase.DOT_UPPER_CASE,
name: '点分隔 + 全大写命名',
shortName: '脊柱大写',
keyword: [...keyword.dot, ...keyword.upper],
settingsKey: 'dot_upper_case',
},
{
type: SupportCase.LOWER_CASE,
name: '全小写',
shortName: '小写',
keyword: keyword.lower,
settingsKey: 'lower_case',
},
{
type: SupportCase.UPPER_CASE,
name: '全大写',
shortName: '大写',
keyword: keyword.upper,
settingsKey: 'upper_case',
},
];
export interface CyclicConvertCaseOrderItem {
type: SupportCase,
settingsKey: string,
}
/**
* 通过快捷键循环转换的顺序
* @since 2024-04-08
*/
export const cyclicConvertCaseOrder = [
SupportCase.CAMEL_CASE,
SupportCase.SNAKE_CASE,
SupportCase.PASCAL_CASE,
SupportCase.KEBAB_CASE,
SupportCase.SPACE_CASE,
SupportCase.DOT_CASE,
export const cyclicConvertCaseOrder: Array<CyclicConvertCaseOrderItem> = [
{ type: SupportCase.CAMEL_CASE, settingsKey: 'camel_case' },
{ type: SupportCase.SNAKE_CASE, settingsKey: 'snake_case' },
{ type: SupportCase.PASCAL_CASE, settingsKey: 'pascal_case' },
{ type: SupportCase.KEBAB_CASE, settingsKey: 'kebab_case' },
{ type: SupportCase.SPACE_CASE, settingsKey: 'space_case' },
{ type: SupportCase.DOT_CASE, settingsKey: 'dot_case' },
SupportCase.SNAKE_UPPER_CASE,
SupportCase.KEBAB_UPPER_CASE,
SupportCase.SPACE_UPPER_CASE,
SupportCase.DOT_UPPER_CASE,
{ type: SupportCase.SNAKE_UPPER_CASE, settingsKey: 'snake_upper_case' },
{ type: SupportCase.KEBAB_UPPER_CASE, settingsKey: 'kebab_upper_case' },
{ type: SupportCase.SPACE_UPPER_CASE, settingsKey: 'space_upper_case' },
{ type: SupportCase.DOT_UPPER_CASE, settingsKey: 'dot_upper_case' },
SupportCase.SNAKE_PASCAL_CASE,
SupportCase.KEBAB_PASCAL_CASE,
SupportCase.SPACE_PASCAL_CASE,
SupportCase.DOT_PASCAL_CASE,
{ type: SupportCase.SNAKE_PASCAL_CASE, settingsKey: 'snake_pascal_case' },
{ type: SupportCase.KEBAB_PASCAL_CASE, settingsKey: 'kebab_pascal_case' },
{ type: SupportCase.SPACE_PASCAL_CASE, settingsKey: 'space_pascal_case' },
{ type: SupportCase.DOT_PASCAL_CASE, settingsKey: 'dot_pascal_case' },
SupportCase.SNAKE_CAMEL_CASE,
SupportCase.KEBAB_CAMEL_CASE,
SupportCase.SPACE_CAMEL_CASE,
SupportCase.DOT_CAMEL_CASE,
{ type: SupportCase.SNAKE_CAMEL_CASE, settingsKey: 'snake_camel_case' },
{ type: SupportCase.KEBAB_CAMEL_CASE, settingsKey: 'kebab_camel_case' },
{ type: SupportCase.SPACE_CAMEL_CASE, settingsKey: 'space_camel_case' },
{ type: SupportCase.DOT_CAMEL_CASE, settingsKey: 'dot_camel_case' },
SupportCase.LOWER_CASE,
SupportCase.UPPER_CASE,
{ type: SupportCase.LOWER_CASE, settingsKey: 'lower_case' },
{ type: SupportCase.UPPER_CASE, settingsKey: 'upper_case' },
];