feat: 新增 formatOrder 配置项;支持根据 formatOrder 对格式进行排序
This commit is contained in:
@@ -684,7 +684,59 @@
|
|||||||
"dotCamelCase.enabled": false
|
"dotCamelCase.enabled": false
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
// "variable-conversion.formatOrder": {},
|
"variable-conversion.formatOrder": {
|
||||||
|
"type": "array",
|
||||||
|
"markdownDescription": "配置变量命名方式的显示顺序\n\nConfigure the display order of variable naming conventions.\n\n该配置中的格式名称必须与enabledFormats中的保持一致。如果某个格式在enabledFormats中被禁用,则即使在formatOrder中配置也不会显示。\n\nFormat names in this configuration must be consistent with those in enabledFormats. If a format is disabled in enabledFormats, it will not be displayed even if configured in formatOrder.\n\n \n\n🌰 e.g. `[\"camel_case\", \"snake_case\", \"pascal_case\"]`\n\n[在 settings.json 中编辑 (Edit in settings.json)](command:workbench.action.openSettingsJson)",
|
||||||
|
"scope": "window",
|
||||||
|
"items": {
|
||||||
|
"type": "string",
|
||||||
|
"enum": [
|
||||||
|
"camel_case",
|
||||||
|
"pascal_case",
|
||||||
|
"snake_case",
|
||||||
|
"snake_camel_case",
|
||||||
|
"snake_pascal_case",
|
||||||
|
"snake_upper_case",
|
||||||
|
"kebab_case",
|
||||||
|
"kebab_camel_case",
|
||||||
|
"kebab_pascal_case",
|
||||||
|
"kebab_upper_case",
|
||||||
|
"space_case",
|
||||||
|
"space_camel_case",
|
||||||
|
"space_pascal_case",
|
||||||
|
"space_upper_case",
|
||||||
|
"dot_case",
|
||||||
|
"dot_camel_case",
|
||||||
|
"dot_pascal_case",
|
||||||
|
"dot_upper_case",
|
||||||
|
"lower_case",
|
||||||
|
"upper_case"
|
||||||
|
],
|
||||||
|
"enumDescriptions": [
|
||||||
|
"驼峰命名 (camel_case)",
|
||||||
|
"大驼峰命名 (pascal_case)",
|
||||||
|
"下划线命名 (snake_case)",
|
||||||
|
"下划线驼峰命名 (snake_camel_case)",
|
||||||
|
"下划线大驼峰命名 (snake_pascal_case)",
|
||||||
|
"下划线大写命名 (snake_upper_case)",
|
||||||
|
"连字符命名 (kebab_case)",
|
||||||
|
"连字符驼峰命名 (kebab_camel_case)",
|
||||||
|
"连字符大驼峰命名 (kebab_pascal_case)",
|
||||||
|
"连字符大写命名 (kebab_upper_case)",
|
||||||
|
"空格命名 (space_case)",
|
||||||
|
"空格驼峰命名 (space_camel_case)",
|
||||||
|
"空格大驼峰命名 (space_pascal_case)",
|
||||||
|
"空格大写命名 (space_upper_case)",
|
||||||
|
"点命名 (dot_case)",
|
||||||
|
"点驼峰命名 (dot_camel_case)",
|
||||||
|
"点大驼峰命名 (dot_pascal_case)",
|
||||||
|
"点大写命名 (dot_upper_case)",
|
||||||
|
"全小写命名 (lower_case)",
|
||||||
|
"全大写命名 (upper_case)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"default": []
|
||||||
|
},
|
||||||
"variable-conversion.disableFormat": {
|
"variable-conversion.disableFormat": {
|
||||||
"order": 1,
|
"order": 1,
|
||||||
// markdownDescription 最多配置 11 行
|
// markdownDescription 最多配置 11 行
|
||||||
|
|||||||
53
package.json
53
package.json
@@ -455,6 +455,59 @@
|
|||||||
"dotCamelCase.enabled": false
|
"dotCamelCase.enabled": false
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"variable-conversion.formatOrder": {
|
||||||
|
"type": "array",
|
||||||
|
"markdownDescription": "配置变量命名方式的显示顺序\n\nConfigure the display order of variable naming conventions.\n\n该配置中的格式名称必须与enabledFormats中的保持一致。如果某个格式在enabledFormats中被禁用,则即使在formatOrder中配置也不会显示。\n\nFormat names in this configuration must be consistent with those in enabledFormats. If a format is disabled in enabledFormats, it will not be displayed even if configured in formatOrder.\n\n \n\n🌰 e.g. `[\"camel_case\", \"snake_case\", \"pascal_case\"]`\n\n[在 settings.json 中编辑 (Edit in settings.json)](command:workbench.action.openSettingsJson)",
|
||||||
|
"scope": "window",
|
||||||
|
"items": {
|
||||||
|
"type": "string",
|
||||||
|
"enum": [
|
||||||
|
"camel_case",
|
||||||
|
"pascal_case",
|
||||||
|
"snake_case",
|
||||||
|
"snake_camel_case",
|
||||||
|
"snake_pascal_case",
|
||||||
|
"snake_upper_case",
|
||||||
|
"kebab_case",
|
||||||
|
"kebab_camel_case",
|
||||||
|
"kebab_pascal_case",
|
||||||
|
"kebab_upper_case",
|
||||||
|
"space_case",
|
||||||
|
"space_camel_case",
|
||||||
|
"space_pascal_case",
|
||||||
|
"space_upper_case",
|
||||||
|
"dot_case",
|
||||||
|
"dot_camel_case",
|
||||||
|
"dot_pascal_case",
|
||||||
|
"dot_upper_case",
|
||||||
|
"lower_case",
|
||||||
|
"upper_case"
|
||||||
|
],
|
||||||
|
"enumDescriptions": [
|
||||||
|
"驼峰命名 (camel_case)",
|
||||||
|
"大驼峰命名 (pascal_case)",
|
||||||
|
"下划线命名 (snake_case)",
|
||||||
|
"下划线驼峰命名 (snake_camel_case)",
|
||||||
|
"下划线大驼峰命名 (snake_pascal_case)",
|
||||||
|
"下划线大写命名 (snake_upper_case)",
|
||||||
|
"连字符命名 (kebab_case)",
|
||||||
|
"连字符驼峰命名 (kebab_camel_case)",
|
||||||
|
"连字符大驼峰命名 (kebab_pascal_case)",
|
||||||
|
"连字符大写命名 (kebab_upper_case)",
|
||||||
|
"空格命名 (space_case)",
|
||||||
|
"空格驼峰命名 (space_camel_case)",
|
||||||
|
"空格大驼峰命名 (space_pascal_case)",
|
||||||
|
"空格大写命名 (space_upper_case)",
|
||||||
|
"点命名 (dot_case)",
|
||||||
|
"点驼峰命名 (dot_camel_case)",
|
||||||
|
"点大驼峰命名 (dot_pascal_case)",
|
||||||
|
"点大写命名 (dot_upper_case)",
|
||||||
|
"全小写命名 (lower_case)",
|
||||||
|
"全大写命名 (upper_case)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"default": []
|
||||||
|
},
|
||||||
"variable-conversion.disableFormat": {
|
"variable-conversion.disableFormat": {
|
||||||
"order": 1,
|
"order": 1,
|
||||||
"markdownDescription": "定义哪些变量命名方式是禁用的\n\nDefine which variable formats are disabled.\n\n若您感觉以下配置比较麻烦,也可以选择在 `settings.json` 中编辑:\n\nIf you find the following configuration troublesome, you can also edit this configuration item in `settings.json`:\n\n`\"variable-conversion.disableFormat\": [ ... ],`\n\n[在 settings.json 中编辑 (Edit in settings.json)](command:workbench.action.openSettingsJson)\n\n配置后,您可能需要*重启扩展宿主*,或*重启当前窗口*才可使该配置完全生效(二选一即可):\n\nYou may need to *restart extension host* or *reload window* after configuration to take full effect (either):\n\n[重启扩展宿主 (Restart Extension Host)](command:workbench.action.restartExtensionHost), [重启当前窗口 (Reload Window)](command:workbench.action.reloadWindow)",
|
"markdownDescription": "定义哪些变量命名方式是禁用的\n\nDefine which variable formats are disabled.\n\n若您感觉以下配置比较麻烦,也可以选择在 `settings.json` 中编辑:\n\nIf you find the following configuration troublesome, you can also edit this configuration item in `settings.json`:\n\n`\"variable-conversion.disableFormat\": [ ... ],`\n\n[在 settings.json 中编辑 (Edit in settings.json)](command:workbench.action.openSettingsJson)\n\n配置后,您可能需要*重启扩展宿主*,或*重启当前窗口*才可使该配置完全生效(二选一即可):\n\nYou may need to *restart extension host* or *reload window* after configuration to take full effect (either):\n\n[重启扩展宿主 (Restart Extension Host)](command:workbench.action.restartExtensionHost), [重启当前窗口 (Reload Window)](command:workbench.action.reloadWindow)",
|
||||||
|
|||||||
@@ -67,11 +67,15 @@ function lazyConvert() {
|
|||||||
|
|
||||||
// 获取用户配置
|
// 获取用户配置
|
||||||
const enabledFormats = getUserConfigurations<Record<string, boolean>>('enabledFormats') || {};
|
const enabledFormats = getUserConfigurations<Record<string, boolean>>('enabledFormats') || {};
|
||||||
|
const formatOrder = getUserConfigurations<string[]>('formatOrder') || [];
|
||||||
|
|
||||||
const textList = userSelection.currentSelectionsText;
|
const textList = userSelection.currentSelectionsText;
|
||||||
// vscode.window.showInformationMessage('lazyConvert' + textList.join('\n'));
|
// vscode.window.showInformationMessage('lazyConvert' + textList.join('\n'));
|
||||||
const eol = userSelection.currentEol;
|
const eol = userSelection.currentEol;
|
||||||
const conversionsTarget: Array<string[]> = [textList];
|
|
||||||
|
// 先收集所有启用的格式及其转换结果
|
||||||
|
// const conversionsTarget: Array<string[]> = [textList];
|
||||||
|
const formatMap = new Map<string, string[]>();
|
||||||
for (const cyclicConvertCase of cyclicConvertCaseOrder) {
|
for (const cyclicConvertCase of cyclicConvertCaseOrder) {
|
||||||
// issue: #1 https://github.com/coder-xiaomo/variable-conversion-vscode-extension/issues/1
|
// issue: #1 https://github.com/coder-xiaomo/variable-conversion-vscode-extension/issues/1
|
||||||
// 跳过禁用的目标格式
|
// 跳过禁用的目标格式
|
||||||
@@ -86,11 +90,34 @@ function lazyConvert() {
|
|||||||
const conversionResult: string = caseConversion(cyclicConvertCase.type, line, eol);
|
const conversionResult: string = caseConversion(cyclicConvertCase.type, line, eol);
|
||||||
conversionsTargetItem.push(conversionResult);
|
conversionsTargetItem.push(conversionResult);
|
||||||
}
|
}
|
||||||
conversionsTarget.push(conversionsTargetItem);
|
// conversionsTarget.push(conversionsTargetItem);
|
||||||
|
formatMap.set(cyclicConvertCase.settingsKey, conversionsTargetItem);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 根据formatOrder对格式进行排序
|
||||||
|
const orderedConversions: Array<string[]> = [textList];
|
||||||
|
const processedSettingsKeys = new Set<string>();
|
||||||
|
// 先添加在formatOrder中配置的格式
|
||||||
|
for (const formatName of formatOrder) {
|
||||||
|
const conversions = formatMap.get(formatName);
|
||||||
|
if (conversions && !processedSettingsKeys.has(formatName)) {
|
||||||
|
orderedConversions.push(conversions);
|
||||||
|
processedSettingsKeys.add(formatName);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// 再添加未在formatOrder中配置但启用的格式,保持原有顺序
|
||||||
|
for (const cyclicConvertCase of cyclicConvertCaseOrder) {
|
||||||
|
const settingsKey = cyclicConvertCase.settingsKey;
|
||||||
|
const conversions = formatMap.get(settingsKey);
|
||||||
|
if (conversions && !processedSettingsKeys.has(settingsKey)) {
|
||||||
|
orderedConversions.push(conversions);
|
||||||
|
processedSettingsKeys.add(settingsKey);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 按数组去重
|
// 按数组去重
|
||||||
const noDuplicate = stringListArrayDuplicateRemoval(conversionsTarget);
|
// const noDuplicate = stringListArrayDuplicateRemoval(conversionsTarget);
|
||||||
|
const noDuplicate = stringListArrayDuplicateRemoval(orderedConversions);
|
||||||
// console.log('noDuplicate', noDuplicate);
|
// console.log('noDuplicate', noDuplicate);
|
||||||
|
|
||||||
userSelection.conversionsTarget = noDuplicate;
|
userSelection.conversionsTarget = noDuplicate;
|
||||||
|
|||||||
@@ -96,10 +96,12 @@ export function handleQuickPick() {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// issue: #1 https://github.com/coder-xiaomo/variable-conversion-vscode-extension/issues/1
|
|
||||||
// 获取用户配置
|
// 获取用户配置
|
||||||
const enabledFormats = getUserConfigurations<Record<string, boolean>>('enabledFormats') || {};
|
const enabledFormats = getUserConfigurations<Record<string, boolean>>('enabledFormats') || {};
|
||||||
|
const formatOrder = getUserConfigurations<string[]>('formatOrder') || [];
|
||||||
|
|
||||||
// 排除禁用的选项
|
// 排除禁用的选项
|
||||||
|
// issue: #1 https://github.com/coder-xiaomo/variable-conversion-vscode-extension/issues/1
|
||||||
const enabledQuickPickSupportCases = [];
|
const enabledQuickPickSupportCases = [];
|
||||||
for (const quickPick of quickPickSupportCases) {
|
for (const quickPick of quickPickSupportCases) {
|
||||||
if (enabledFormats[quickPick.settingsKey] === false) {
|
if (enabledFormats[quickPick.settingsKey] === false) {
|
||||||
@@ -107,11 +109,36 @@ export function handleQuickPick() {
|
|||||||
}
|
}
|
||||||
enabledQuickPickSupportCases.push(quickPick);
|
enabledQuickPickSupportCases.push(quickPick);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (enabledQuickPickSupportCases.length === 0) {
|
if (enabledQuickPickSupportCases.length === 0) {
|
||||||
vscode.window.showInformationMessage('所有格式都已被配置为禁用,请修改配置 `variable-conversion.enabledFormats` 后重试\nAll formats have been configured to disable. Modify the `variable-conversion.enabledFormats` configuration and try again.');
|
vscode.window.showInformationMessage('所有格式都已被配置为禁用,请修改配置 `variable-conversion.enabledFormats` 后重试\nAll formats have been configured to disable. Modify the `variable-conversion.enabledFormats` configuration and try again.');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 按用户配置的顺序排序
|
||||||
|
// issue: #5 https://github.com/coder-xiaomo/variable-conversion-vscode-extension/issues/5
|
||||||
|
// issue: #6 https://github.com/coder-xiaomo/variable-conversion-vscode-extension/issues/6
|
||||||
|
if (formatOrder.length > 0) {
|
||||||
|
enabledQuickPickSupportCases.sort((a, b) => {
|
||||||
|
const indexA = formatOrder.indexOf(a.settingsKey);
|
||||||
|
const indexB = formatOrder.indexOf(b.settingsKey);
|
||||||
|
|
||||||
|
// 如果两个都在配置中,按配置顺序
|
||||||
|
if (indexA !== -1 && indexB !== -1) {
|
||||||
|
return indexA - indexB;
|
||||||
|
}
|
||||||
|
// 如果只有一个在配置中,在配置中的排在前面
|
||||||
|
if (indexA !== -1) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
if (indexB !== -1) {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
// 如果都不在配置中,保持原有顺序
|
||||||
|
return 0;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
// 基于选中的文本生成选项
|
// 基于选中的文本生成选项
|
||||||
const options = generateOptionsBasedOnText(textList, eol, enabledQuickPickSupportCases);
|
const options = generateOptionsBasedOnText(textList, eol, enabledQuickPickSupportCases);
|
||||||
if (options.length === 0) {
|
if (options.length === 0) {
|
||||||
|
|||||||
Reference in New Issue
Block a user