1
This commit is contained in:
1 parent
8531403c33
commit
b03ece33a2
37 files changed
+6388
-908
No files matched your search
@@ -0,0 +1,568 @@
|
||||
import 'package:code_editor/l10n/app_localizations.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
/// Toast 类型
|
||||
enum ToastType {
|
||||
info,
|
||||
success,
|
||||
warning,
|
||||
error,
|
||||
}
|
||||
|
||||
/// 保存未保存更改提示结果
|
||||
enum SavePromptResult {
|
||||
save,
|
||||
discard,
|
||||
cancel,
|
||||
}
|
||||
|
||||
/// 外部文件冲突提示结果
|
||||
enum FileConflictResult {
|
||||
reloadFromDisk,
|
||||
keepLocal,
|
||||
}
|
||||
|
||||
/// 集中封装 Dialog 与 Toast 的工具类
|
||||
class DialogUtils {
|
||||
DialogUtils._();
|
||||
|
||||
// ==========================================
|
||||
// 1. Toast 轻提示
|
||||
// ==========================================
|
||||
|
||||
/// 显示浮动 Toast
|
||||
static void showToast(
|
||||
BuildContext context,
|
||||
String message, {
|
||||
ToastType type = ToastType.info,
|
||||
Duration duration = const Duration(seconds: 2),
|
||||
}) {
|
||||
final overlay = Overlay.maybeOf(context, rootOverlay: true);
|
||||
if (overlay == null) return;
|
||||
|
||||
final theme = Theme.of(context);
|
||||
final colorScheme = theme.colorScheme;
|
||||
|
||||
// 根据类型配置图标与色彩
|
||||
final (iconData, bgColor, textColor, borderColor) = switch (type) {
|
||||
ToastType.success => (
|
||||
Icons.check_circle_outline,
|
||||
Colors.green.shade800,
|
||||
Colors.white,
|
||||
Colors.green.shade600,
|
||||
),
|
||||
ToastType.error => (
|
||||
Icons.error_outline,
|
||||
colorScheme.errorContainer,
|
||||
colorScheme.onErrorContainer,
|
||||
colorScheme.error,
|
||||
),
|
||||
ToastType.warning => (
|
||||
Icons.warning_amber_rounded,
|
||||
Colors.amber.shade900,
|
||||
Colors.white,
|
||||
Colors.amber.shade700,
|
||||
),
|
||||
ToastType.info => (
|
||||
Icons.info_outline,
|
||||
colorScheme.inverseSurface,
|
||||
colorScheme.onInverseSurface,
|
||||
colorScheme.outlineVariant,
|
||||
),
|
||||
};
|
||||
|
||||
late OverlayEntry entry;
|
||||
entry = OverlayEntry(
|
||||
builder: (ctx) => Positioned(
|
||||
bottom: 48,
|
||||
left: 0,
|
||||
right: 0,
|
||||
child: Center(
|
||||
child: Material(
|
||||
color: Colors.transparent,
|
||||
child: Container(
|
||||
margin: const EdgeInsets.symmetric(horizontal: 24),
|
||||
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 10),
|
||||
decoration: BoxDecoration(
|
||||
color: bgColor,
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
border: Border.all(color: borderColor, width: 1),
|
||||
boxShadow: const [
|
||||
BoxShadow(
|
||||
color: Colors.black26,
|
||||
blurRadius: 10,
|
||||
offset: Offset(0, 4),
|
||||
),
|
||||
],
|
||||
),
|
||||
child: Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Icon(iconData, size: 18, color: textColor),
|
||||
const SizedBox(width: 8),
|
||||
Flexible(
|
||||
child: Text(
|
||||
message,
|
||||
style: theme.textTheme.bodyMedium?.copyWith(
|
||||
color: textColor,
|
||||
fontWeight: FontWeight.w500,
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
overlay.insert(entry);
|
||||
Future.delayed(duration, () {
|
||||
if (entry.mounted) {
|
||||
entry.remove();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
static void showSuccessToast(BuildContext context, String message) {
|
||||
showToast(context, message, type: ToastType.success);
|
||||
}
|
||||
|
||||
static void showErrorToast(BuildContext context, String message) {
|
||||
showToast(context, message, type: ToastType.error);
|
||||
}
|
||||
|
||||
static void showWarningToast(BuildContext context, String message) {
|
||||
showToast(context, message, type: ToastType.warning);
|
||||
}
|
||||
|
||||
static void showInfoToast(BuildContext context, String message) {
|
||||
showToast(context, message, type: ToastType.info);
|
||||
}
|
||||
|
||||
// ==========================================
|
||||
// 2. 消息提示 Dialog (Info, Warning, Error)
|
||||
// ==========================================
|
||||
|
||||
/// 信息提示弹窗 (Info)
|
||||
static Future<void> showInfoDialog(
|
||||
BuildContext context, {
|
||||
required String title,
|
||||
required String message,
|
||||
String? confirmText,
|
||||
}) {
|
||||
final theme = Theme.of(context);
|
||||
final l10n = AppLocalizations.of(context);
|
||||
return _showMessageDialog(
|
||||
context,
|
||||
title: title,
|
||||
message: message,
|
||||
confirmText: confirmText ?? l10n?.confirm ?? '确定',
|
||||
icon: Icon(Icons.info_outline, color: theme.colorScheme.primary, size: 28),
|
||||
);
|
||||
}
|
||||
|
||||
/// 警告提示弹窗 (Warning)
|
||||
static Future<void> showWarningDialog(
|
||||
BuildContext context, {
|
||||
required String title,
|
||||
required String message,
|
||||
String? confirmText,
|
||||
}) {
|
||||
final l10n = AppLocalizations.of(context);
|
||||
return _showMessageDialog(
|
||||
context,
|
||||
title: title,
|
||||
message: message,
|
||||
confirmText: confirmText ?? l10n?.gotIt ?? '知道了',
|
||||
icon: const Icon(Icons.warning_amber_rounded, color: Colors.amber, size: 28),
|
||||
);
|
||||
}
|
||||
|
||||
/// 错误提示弹窗 (Error)
|
||||
static Future<void> showErrorDialog(
|
||||
BuildContext context, {
|
||||
required String title,
|
||||
required String message,
|
||||
String? confirmText,
|
||||
}) {
|
||||
final theme = Theme.of(context);
|
||||
final l10n = AppLocalizations.of(context);
|
||||
return _showMessageDialog(
|
||||
context,
|
||||
title: title,
|
||||
message: message,
|
||||
confirmText: confirmText ?? l10n?.close ?? '关闭',
|
||||
icon: Icon(Icons.error_outline, color: theme.colorScheme.error, size: 28),
|
||||
);
|
||||
}
|
||||
|
||||
static Future<void> _showMessageDialog(
|
||||
BuildContext context, {
|
||||
required String title,
|
||||
required String message,
|
||||
required String confirmText,
|
||||
required Widget icon,
|
||||
}) {
|
||||
final theme = Theme.of(context);
|
||||
return showDialog<void>(
|
||||
context: context,
|
||||
builder: (dialogContext) {
|
||||
return AlertDialog(
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.circular(16.0),
|
||||
side: BorderSide(
|
||||
color: theme.colorScheme.outlineVariant,
|
||||
width: 1.0,
|
||||
),
|
||||
),
|
||||
icon: icon,
|
||||
title: Text(title),
|
||||
content: Text(message),
|
||||
actions: [
|
||||
FilledButton(
|
||||
onPressed: () => Navigator.of(dialogContext).pop(),
|
||||
child: Text(confirmText),
|
||||
),
|
||||
],
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
// ==========================================
|
||||
// 3. 确认弹窗 (普通确认 vs 特殊/高危确认)
|
||||
// ==========================================
|
||||
|
||||
/// 普通确认弹窗 (如:保存提示、重新载入等常规操作)
|
||||
static Future<bool> showConfirmDialog(
|
||||
BuildContext context, {
|
||||
required String title,
|
||||
required String message,
|
||||
String? confirmText,
|
||||
String? cancelText,
|
||||
Widget? icon,
|
||||
}) async {
|
||||
final theme = Theme.of(context);
|
||||
final l10n = AppLocalizations.of(context);
|
||||
final effConfirmText = confirmText ?? l10n?.confirm ?? '确定';
|
||||
final effCancelText = cancelText ?? l10n?.cancel ?? '取消';
|
||||
final result = await showDialog<bool>(
|
||||
context: context,
|
||||
builder: (dialogContext) {
|
||||
return AlertDialog(
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.circular(16.0),
|
||||
side: BorderSide(
|
||||
color: theme.colorScheme.outlineVariant,
|
||||
width: 1.0,
|
||||
),
|
||||
),
|
||||
icon: icon,
|
||||
title: Text(title),
|
||||
content: Text(message),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () => Navigator.of(dialogContext).pop(false),
|
||||
child: Text(effCancelText),
|
||||
),
|
||||
FilledButton(
|
||||
onPressed: () => Navigator.of(dialogContext).pop(true),
|
||||
child: Text(effConfirmText),
|
||||
),
|
||||
],
|
||||
);
|
||||
},
|
||||
);
|
||||
return result ?? false;
|
||||
}
|
||||
|
||||
/// 特殊/高危确认弹窗 (如:删除文件、清空历史等不可逆操作)
|
||||
static Future<bool> showDestructiveConfirmDialog(
|
||||
BuildContext context, {
|
||||
required String title,
|
||||
required String message,
|
||||
String? confirmText,
|
||||
String? cancelText,
|
||||
IconData icon = Icons.delete_outline,
|
||||
}) async {
|
||||
final theme = Theme.of(context);
|
||||
final l10n = AppLocalizations.of(context);
|
||||
final effConfirmText = confirmText ?? l10n?.delete ?? '删除';
|
||||
final effCancelText = cancelText ?? l10n?.cancel ?? '取消';
|
||||
final result = await showDialog<bool>(
|
||||
context: context,
|
||||
builder: (dialogContext) {
|
||||
return AlertDialog(
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.circular(16.0),
|
||||
side: BorderSide(
|
||||
color: theme.colorScheme.error.withValues(alpha: 0.5),
|
||||
width: 1.0,
|
||||
),
|
||||
),
|
||||
icon: Icon(icon, color: theme.colorScheme.error, size: 28),
|
||||
title: Text(
|
||||
title,
|
||||
style: TextStyle(color: theme.colorScheme.error),
|
||||
),
|
||||
content: Text(message),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () => Navigator.of(dialogContext).pop(false),
|
||||
child: Text(effCancelText),
|
||||
),
|
||||
FilledButton(
|
||||
style: FilledButton.styleFrom(
|
||||
backgroundColor: theme.colorScheme.error,
|
||||
foregroundColor: theme.colorScheme.onError,
|
||||
),
|
||||
onPressed: () => Navigator.of(dialogContext).pop(true),
|
||||
child: Text(effConfirmText),
|
||||
),
|
||||
],
|
||||
);
|
||||
},
|
||||
);
|
||||
return result ?? false;
|
||||
}
|
||||
|
||||
/// 未保存更改提示弹窗 (保存、不保存、取消)
|
||||
static Future<SavePromptResult> showSavePromptDialog(
|
||||
BuildContext context, {
|
||||
required String fileName,
|
||||
}) async {
|
||||
final theme = Theme.of(context);
|
||||
final l10n = AppLocalizations.of(context);
|
||||
final title = l10n?.saveChangesTitle ?? '保存更改';
|
||||
final message = l10n?.saveChangesMessage(fileName) ?? '文件 "$fileName" 已被修改,是否保存更改?';
|
||||
final saveText = l10n?.save ?? '保存';
|
||||
final dontSaveText = l10n?.dontSave ?? '不保存';
|
||||
final cancelText = l10n?.cancel ?? '取消';
|
||||
|
||||
final result = await showDialog<SavePromptResult>(
|
||||
context: context,
|
||||
barrierDismissible: false,
|
||||
builder: (dialogContext) {
|
||||
return AlertDialog(
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.circular(16.0),
|
||||
side: BorderSide(
|
||||
color: theme.colorScheme.outlineVariant,
|
||||
width: 1.0,
|
||||
),
|
||||
),
|
||||
icon: Icon(
|
||||
Icons.help_outline,
|
||||
color: theme.colorScheme.primary,
|
||||
size: 28,
|
||||
),
|
||||
title: Text(title),
|
||||
content: Text(message),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () => Navigator.of(dialogContext).pop(SavePromptResult.cancel),
|
||||
child: Text(cancelText),
|
||||
),
|
||||
TextButton(
|
||||
style: TextButton.styleFrom(
|
||||
foregroundColor: theme.colorScheme.error,
|
||||
),
|
||||
onPressed: () => Navigator.of(dialogContext).pop(SavePromptResult.discard),
|
||||
child: Text(dontSaveText),
|
||||
),
|
||||
FilledButton(
|
||||
onPressed: () => Navigator.of(dialogContext).pop(SavePromptResult.save),
|
||||
child: Text(saveText),
|
||||
),
|
||||
],
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
return result ?? SavePromptResult.cancel;
|
||||
}
|
||||
|
||||
/// 未保存所有更改提示弹窗 (保存所有、不保存、取消)
|
||||
static Future<SavePromptResult> showSaveAllPromptDialog(
|
||||
BuildContext context,
|
||||
) async {
|
||||
final theme = Theme.of(context);
|
||||
final l10n = AppLocalizations.of(context);
|
||||
final title = l10n?.saveAllPromptTitle ?? '保存所有更改?';
|
||||
final message = l10n?.saveAllPromptMessage ?? '当前项目中有未保存的修改,是否保存所有文件?';
|
||||
final saveText = l10n?.saveAll ?? '保存所有';
|
||||
final dontSaveText = l10n?.dontSave ?? '不保存';
|
||||
final cancelText = l10n?.cancel ?? '取消';
|
||||
|
||||
final result = await showDialog<SavePromptResult>(
|
||||
context: context,
|
||||
barrierDismissible: false,
|
||||
builder: (dialogContext) {
|
||||
return AlertDialog(
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.circular(16.0),
|
||||
side: BorderSide(
|
||||
color: theme.colorScheme.outlineVariant,
|
||||
width: 1.0,
|
||||
),
|
||||
),
|
||||
icon: Icon(
|
||||
Icons.help_outline,
|
||||
color: theme.colorScheme.primary,
|
||||
size: 28,
|
||||
),
|
||||
title: Text(title),
|
||||
content: Text(message),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () => Navigator.of(dialogContext).pop(SavePromptResult.cancel),
|
||||
child: Text(cancelText),
|
||||
),
|
||||
TextButton(
|
||||
style: TextButton.styleFrom(
|
||||
foregroundColor: theme.colorScheme.error,
|
||||
),
|
||||
onPressed: () => Navigator.of(dialogContext).pop(SavePromptResult.discard),
|
||||
child: Text(dontSaveText),
|
||||
),
|
||||
FilledButton(
|
||||
onPressed: () => Navigator.of(dialogContext).pop(SavePromptResult.save),
|
||||
child: Text(saveText),
|
||||
),
|
||||
],
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
return result ?? SavePromptResult.cancel;
|
||||
}
|
||||
|
||||
/// 外部文件冲突提示弹窗 (用磁盘内容覆盖、保留本地修改)
|
||||
static Future<FileConflictResult> showFileConflictDialog(
|
||||
BuildContext context, {
|
||||
required String fileName,
|
||||
}) async {
|
||||
final theme = Theme.of(context);
|
||||
final l10n = AppLocalizations.of(context);
|
||||
final title = l10n?.fileConflictTitle ?? '外部文件已被更改';
|
||||
final message = l10n?.fileConflictMessage(fileName) ?? '文件 "$fileName" 已在外部被更改。您希望如何处理?';
|
||||
final reloadText = l10n?.reloadFromDisk ?? '用磁盘内容覆盖';
|
||||
final keepText = l10n?.keepLocal ?? '保留本地修改';
|
||||
|
||||
final result = await showDialog<FileConflictResult>(
|
||||
context: context,
|
||||
barrierDismissible: false,
|
||||
builder: (dialogContext) {
|
||||
return AlertDialog(
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.circular(16.0),
|
||||
side: BorderSide(
|
||||
color: theme.colorScheme.outlineVariant,
|
||||
width: 1.0,
|
||||
),
|
||||
),
|
||||
icon: Icon(
|
||||
Icons.warning_amber_rounded,
|
||||
color: Colors.amber.shade800,
|
||||
size: 28,
|
||||
),
|
||||
title: Text(title),
|
||||
content: Text(message),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () => Navigator.of(dialogContext).pop(FileConflictResult.keepLocal),
|
||||
child: Text(keepText),
|
||||
),
|
||||
FilledButton(
|
||||
style: FilledButton.styleFrom(
|
||||
backgroundColor: theme.colorScheme.error,
|
||||
foregroundColor: theme.colorScheme.onError,
|
||||
),
|
||||
onPressed: () => Navigator.of(dialogContext).pop(FileConflictResult.reloadFromDisk),
|
||||
child: Text(reloadText),
|
||||
),
|
||||
],
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
return result ?? FileConflictResult.keepLocal;
|
||||
}
|
||||
|
||||
// ==========================================
|
||||
// 4. 输入弹窗 (Input Dialog)
|
||||
// ==========================================
|
||||
|
||||
/// 通用文本输入弹窗,返回用户输入的内容;若取消或关闭则返回 null
|
||||
static Future<String?> showInputDialog(
|
||||
BuildContext context, {
|
||||
required String title,
|
||||
String? hintText,
|
||||
String? initialValue,
|
||||
String? confirmText,
|
||||
String? cancelText,
|
||||
String? Function(String?)? validator,
|
||||
}) {
|
||||
final controller = TextEditingController(text: initialValue);
|
||||
final formKey = GlobalKey<FormState>();
|
||||
final theme = Theme.of(context);
|
||||
final l10n = AppLocalizations.of(context);
|
||||
final effConfirmText = confirmText ?? l10n?.confirm ?? '确定';
|
||||
final effCancelText = cancelText ?? l10n?.cancel ?? '取消';
|
||||
|
||||
// 默认校验:不可为空且不能包含常见系统非法字符
|
||||
String? defaultValidator(String? val) {
|
||||
if (val == null || val.trim().isEmpty) {
|
||||
return l10n?.nameCannotBeEmpty ?? '名称不能为空';
|
||||
}
|
||||
if (val.contains(RegExp(r'[\\/:*?"<>|]'))) {
|
||||
return l10n?.nameInvalidChars ?? '名称不能包含非法字符 (\\/:*?"<>|)';
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
return showDialog<String>(
|
||||
context: context,
|
||||
builder: (dialogContext) {
|
||||
return AlertDialog(
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.circular(16.0),
|
||||
side: BorderSide(
|
||||
color: theme.colorScheme.outlineVariant,
|
||||
width: 1.0,
|
||||
),
|
||||
),
|
||||
title: Text(title),
|
||||
content: Form(
|
||||
key: formKey,
|
||||
child: TextFormField(
|
||||
controller: controller,
|
||||
autofocus: true,
|
||||
decoration: InputDecoration(
|
||||
hintText: hintText,
|
||||
border: const OutlineInputBorder(),
|
||||
),
|
||||
validator: validator ?? defaultValidator,
|
||||
),
|
||||
),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () => Navigator.of(dialogContext).pop(null),
|
||||
child: Text(effCancelText),
|
||||
),
|
||||
FilledButton(
|
||||
onPressed: () {
|
||||
if (formKey.currentState?.validate() ?? false) {
|
||||
Navigator.of(dialogContext).pop(controller.text);
|
||||
}
|
||||
},
|
||||
child: Text(effConfirmText),
|
||||
),
|
||||
],
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,178 @@
|
||||
import 'package:path/path.dart' as p;
|
||||
import 'package:re_editor/re_editor.dart';
|
||||
import 'package:re_highlight/languages/all.dart';
|
||||
|
||||
/// 语法高亮辅助类:
|
||||
/// 1. 基于文件后缀与特殊文件名的 O(1) 语法匹配,彻底避免全量遍历
|
||||
/// 2. 语法模式单例缓存,避免每帧重复创建 CodeHighlightThemeMode
|
||||
class SyntaxHighlightHelper {
|
||||
SyntaxHighlightHelper._();
|
||||
|
||||
static final Map<String, CodeHighlightThemeMode> _modeCache = {};
|
||||
|
||||
/// 特殊文件名映射(忽略大小写比较)
|
||||
static const Map<String, String> _fileNameMap = {
|
||||
'dockerfile': 'dockerfile',
|
||||
'makefile': 'makefile',
|
||||
'gnumakefile': 'makefile',
|
||||
'cmakelists.txt': 'cmake',
|
||||
'gemfile': 'ruby',
|
||||
'rakefile': 'ruby',
|
||||
'podfile': 'ruby',
|
||||
'vagrantfile': 'ruby',
|
||||
'package.json': 'json',
|
||||
'composer.json': 'json',
|
||||
'pubspec.yaml': 'yaml',
|
||||
'pubspec.lock': 'yaml',
|
||||
'.bashrc': 'bash',
|
||||
'.bash_profile': 'bash',
|
||||
'.zshrc': 'bash',
|
||||
'.profile': 'bash',
|
||||
'.gitignore': 'ini',
|
||||
'.gitattributes': 'ini',
|
||||
'.gitmodules': 'ini',
|
||||
'.editorconfig': 'ini',
|
||||
'.env': 'ini',
|
||||
};
|
||||
|
||||
/// 常见扩展名到 re_highlight 语言标识符的 O(1) 映射
|
||||
static const Map<String, String> _extensionMap = {
|
||||
// Dart & Flutter
|
||||
'.dart': 'dart',
|
||||
|
||||
// Web & JS / TS
|
||||
'.js': 'javascript',
|
||||
'.mjs': 'javascript',
|
||||
'.cjs': 'javascript',
|
||||
'.jsx': 'javascript',
|
||||
'.ts': 'typescript',
|
||||
'.mts': 'typescript',
|
||||
'.cts': 'typescript',
|
||||
'.tsx': 'typescript',
|
||||
'.json': 'json',
|
||||
'.jsonc': 'json',
|
||||
'.arb': 'json',
|
||||
'.html': 'xml',
|
||||
'.htm': 'xml',
|
||||
'.xhtml': 'xml',
|
||||
'.vue': 'xml',
|
||||
'.css': 'css',
|
||||
'.scss': 'scss',
|
||||
'.less': 'less',
|
||||
|
||||
// 配置与数据标记
|
||||
'.yaml': 'yaml',
|
||||
'.yml': 'yaml',
|
||||
'.xml': 'xml',
|
||||
'.iml': 'xml',
|
||||
'.svg': 'xml',
|
||||
'.plist': 'xml',
|
||||
'.xaml': 'xml',
|
||||
'.properties': 'properties',
|
||||
'.ini': 'ini',
|
||||
'.cfg': 'ini',
|
||||
'.conf': 'ini',
|
||||
'.config': 'ini',
|
||||
'.toml': 'ini',
|
||||
'.proto': 'protobuf',
|
||||
|
||||
// 文档
|
||||
'.md': 'markdown',
|
||||
'.markdown': 'markdown',
|
||||
'.tex': 'latex',
|
||||
|
||||
// 常用后端与系统语言
|
||||
'.py': 'python',
|
||||
'.pyw': 'python',
|
||||
'.java': 'java',
|
||||
'.kt': 'kotlin',
|
||||
'.kts': 'kotlin',
|
||||
'.c': 'c',
|
||||
'.h': 'c',
|
||||
'.cpp': 'cpp',
|
||||
'.cxx': 'cpp',
|
||||
'.cc': 'cpp',
|
||||
'.hpp': 'cpp',
|
||||
'.hxx': 'cpp',
|
||||
'.hh': 'cpp',
|
||||
'.cs': 'csharp',
|
||||
'.go': 'go',
|
||||
'.rs': 'rust',
|
||||
'.swift': 'swift',
|
||||
'.php': 'php',
|
||||
'.rb': 'ruby',
|
||||
'.lua': 'lua',
|
||||
'.r': 'r',
|
||||
'.sql': 'sql',
|
||||
|
||||
// 脚本与自动化
|
||||
'.sh': 'bash',
|
||||
'.bash': 'bash',
|
||||
'.zsh': 'bash',
|
||||
'.ps1': 'powershell',
|
||||
'.psm1': 'powershell',
|
||||
'.bat': 'dos',
|
||||
'.cmd': 'dos',
|
||||
|
||||
// 构建与版本控制
|
||||
'.cmake': 'cmake',
|
||||
'.gradle': 'gradle',
|
||||
'.groovy': 'groovy',
|
||||
'.diff': 'diff',
|
||||
'.patch': 'diff',
|
||||
'.dockerfile': 'dockerfile',
|
||||
'.graphql': 'graphql',
|
||||
'.gql': 'graphql',
|
||||
};
|
||||
|
||||
/// 根据文件路径获取对应的 re_highlight 语言 ID,未匹配则返回 null
|
||||
static String? getLanguageId(String? filePath) {
|
||||
if (filePath == null || filePath.trim().isEmpty) {
|
||||
return null;
|
||||
}
|
||||
final fileName = p.basename(filePath).toLowerCase();
|
||||
|
||||
// 1. 优先根据完整文件名精准匹配 (例如 Dockerfile, Makefile, pubspec.yaml)
|
||||
final exactMatch = _fileNameMap[fileName];
|
||||
if (exactMatch != null) {
|
||||
return exactMatch;
|
||||
}
|
||||
|
||||
// 2. 根据文件扩展名 O(1) 匹配
|
||||
final ext = p.extension(filePath).toLowerCase();
|
||||
if (ext.isNotEmpty) {
|
||||
return _extensionMap[ext];
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/// 获取指定语言的 CodeHighlightThemeMode 实例(带缓存单例)
|
||||
static CodeHighlightThemeMode? getMode(String langId) {
|
||||
final cached = _modeCache[langId];
|
||||
if (cached != null) {
|
||||
return cached;
|
||||
}
|
||||
final definition = builtinAllLanguages[langId];
|
||||
if (definition != null) {
|
||||
final mode = CodeHighlightThemeMode(mode: definition);
|
||||
_modeCache[langId] = mode;
|
||||
return mode;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/// 为指定文件获取仅包含所需语法的精简映射表。
|
||||
/// 若为纯文本或未知格式则返回空 Map,不会注入无用的 194 种模式。
|
||||
static Map<String, CodeHighlightThemeMode> getLanguagesForFile(String? filePath) {
|
||||
final langId = getLanguageId(filePath);
|
||||
if (langId == null) {
|
||||
return const {};
|
||||
}
|
||||
final mode = getMode(langId);
|
||||
if (mode == null) {
|
||||
return const {};
|
||||
}
|
||||
return {langId: mode};
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user