1
This commit is contained in:
1 parent
96358eae07
commit
7a3db86a10
13 files changed
+621
-33
No files matched your search
+7
-1
@@ -1327,5 +1327,11 @@
|
||||
}
|
||||
},
|
||||
"gitLoadingMoreCommits": "Loading more commits...",
|
||||
"searchExpandAllMatchesInFile": "Expand all matches"
|
||||
"searchExpandAllMatchesInFile": "Expand all matches",
|
||||
"preserveCase": "Preserve Case",
|
||||
"replaceMoreOptions": "More Replace Options",
|
||||
"searchRefresh": "Refresh",
|
||||
"searchPreviousMatch": "Previous Match",
|
||||
"searchNextMatch": "Next Match",
|
||||
"replaceCurrentMatch": "Replace"
|
||||
}
|
||||
@@ -3873,6 +3873,42 @@ abstract class AppLocalizations {
|
||||
/// In zh, this message translates to:
|
||||
/// **'展开全部匹配项'**
|
||||
String get searchExpandAllMatchesInFile;
|
||||
|
||||
/// No description provided for @preserveCase.
|
||||
///
|
||||
/// In zh, this message translates to:
|
||||
/// **'保留大小写'**
|
||||
String get preserveCase;
|
||||
|
||||
/// No description provided for @replaceMoreOptions.
|
||||
///
|
||||
/// In zh, this message translates to:
|
||||
/// **'更多替换选项'**
|
||||
String get replaceMoreOptions;
|
||||
|
||||
/// No description provided for @searchRefresh.
|
||||
///
|
||||
/// In zh, this message translates to:
|
||||
/// **'刷新'**
|
||||
String get searchRefresh;
|
||||
|
||||
/// No description provided for @searchPreviousMatch.
|
||||
///
|
||||
/// In zh, this message translates to:
|
||||
/// **'上一个匹配项'**
|
||||
String get searchPreviousMatch;
|
||||
|
||||
/// No description provided for @searchNextMatch.
|
||||
///
|
||||
/// In zh, this message translates to:
|
||||
/// **'下一个匹配项'**
|
||||
String get searchNextMatch;
|
||||
|
||||
/// No description provided for @replaceCurrentMatch.
|
||||
///
|
||||
/// In zh, this message translates to:
|
||||
/// **'替换'**
|
||||
String get replaceCurrentMatch;
|
||||
}
|
||||
|
||||
class _AppLocalizationsDelegate
|
||||
|
||||
@@ -2311,4 +2311,22 @@ class AppLocalizationsEn extends AppLocalizations {
|
||||
|
||||
@override
|
||||
String get searchExpandAllMatchesInFile => 'Expand all matches';
|
||||
|
||||
@override
|
||||
String get preserveCase => 'Preserve Case';
|
||||
|
||||
@override
|
||||
String get replaceMoreOptions => 'More Replace Options';
|
||||
|
||||
@override
|
||||
String get searchRefresh => 'Refresh';
|
||||
|
||||
@override
|
||||
String get searchPreviousMatch => 'Previous Match';
|
||||
|
||||
@override
|
||||
String get searchNextMatch => 'Next Match';
|
||||
|
||||
@override
|
||||
String get replaceCurrentMatch => 'Replace';
|
||||
}
|
||||
@@ -2246,4 +2246,22 @@ class AppLocalizationsZh extends AppLocalizations {
|
||||
|
||||
@override
|
||||
String get searchExpandAllMatchesInFile => '展开全部匹配项';
|
||||
|
||||
@override
|
||||
String get preserveCase => '保留大小写';
|
||||
|
||||
@override
|
||||
String get replaceMoreOptions => '更多替换选项';
|
||||
|
||||
@override
|
||||
String get searchRefresh => '刷新';
|
||||
|
||||
@override
|
||||
String get searchPreviousMatch => '上一个匹配项';
|
||||
|
||||
@override
|
||||
String get searchNextMatch => '下一个匹配项';
|
||||
|
||||
@override
|
||||
String get replaceCurrentMatch => '替换';
|
||||
}
|
||||
+7
-1
@@ -1327,5 +1327,11 @@
|
||||
}
|
||||
},
|
||||
"gitLoadingMoreCommits": "正在加载更多提交...",
|
||||
"searchExpandAllMatchesInFile": "展开全部匹配项"
|
||||
"searchExpandAllMatchesInFile": "展开全部匹配项",
|
||||
"preserveCase": "保留大小写",
|
||||
"replaceMoreOptions": "更多替换选项",
|
||||
"searchRefresh": "刷新",
|
||||
"searchPreviousMatch": "上一个匹配项",
|
||||
"searchNextMatch": "下一个匹配项",
|
||||
"replaceCurrentMatch": "替换"
|
||||
}
|
||||
@@ -14,6 +14,7 @@ class SearchOptions {
|
||||
final bool isRegex;
|
||||
final SearchMode mode;
|
||||
final bool isReplaceExpanded;
|
||||
final bool preserveCase;
|
||||
|
||||
const SearchOptions({
|
||||
this.query = '',
|
||||
@@ -23,6 +24,7 @@ class SearchOptions {
|
||||
this.isRegex = false,
|
||||
this.mode = SearchMode.text,
|
||||
this.isReplaceExpanded = false,
|
||||
this.preserveCase = false,
|
||||
});
|
||||
|
||||
SearchOptions copyWith({
|
||||
@@ -33,6 +35,7 @@ class SearchOptions {
|
||||
bool? isRegex,
|
||||
SearchMode? mode,
|
||||
bool? isReplaceExpanded,
|
||||
bool? preserveCase,
|
||||
}) {
|
||||
return SearchOptions(
|
||||
query: query ?? this.query,
|
||||
@@ -42,6 +45,7 @@ class SearchOptions {
|
||||
isRegex: isRegex ?? this.isRegex,
|
||||
mode: mode ?? this.mode,
|
||||
isReplaceExpanded: isReplaceExpanded ?? this.isReplaceExpanded,
|
||||
preserveCase: preserveCase ?? this.preserveCase,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -69,7 +73,8 @@ class SearchOptions {
|
||||
other.wholeWord == wholeWord &&
|
||||
other.isRegex == isRegex &&
|
||||
other.mode == mode &&
|
||||
other.isReplaceExpanded == isReplaceExpanded;
|
||||
other.isReplaceExpanded == isReplaceExpanded &&
|
||||
other.preserveCase == preserveCase;
|
||||
}
|
||||
|
||||
@override
|
||||
@@ -81,6 +86,7 @@ class SearchOptions {
|
||||
isRegex,
|
||||
mode,
|
||||
isReplaceExpanded,
|
||||
preserveCase,
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@ import 'package:code_editor/models/search_model.dart';
|
||||
import 'package:code_editor/providers/tab_provider.dart';
|
||||
import 'package:code_editor/services/file_service.dart';
|
||||
import 'package:code_editor/services/project_search_service.dart';
|
||||
import 'package:code_editor/utils/case_utils.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:path/path.dart' as p;
|
||||
|
||||
@@ -25,6 +26,7 @@ class SearchProvider extends ChangeNotifier {
|
||||
bool get caseSensitive => _options.caseSensitive;
|
||||
bool get wholeWord => _options.wholeWord;
|
||||
bool get isRegex => _options.isRegex;
|
||||
bool get preserveCase => _options.preserveCase;
|
||||
|
||||
double scrollOffset = 0.0;
|
||||
int _displayedFilesLimit = 20;
|
||||
@@ -151,6 +153,17 @@ class SearchProvider extends ChangeNotifier {
|
||||
triggerSearch();
|
||||
}
|
||||
|
||||
void togglePreserveCase() {
|
||||
_options = _options.copyWith(preserveCase: !_options.preserveCase);
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
void setPreserveCase(bool value) {
|
||||
if (_options.preserveCase == value) return;
|
||||
_options = _options.copyWith(preserveCase: value);
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
void toggleFileExpanded(FileSearchResult file) {
|
||||
file.isExpanded = !file.isExpanded;
|
||||
_fileExpandedStates[file.filePath] = file.isExpanded;
|
||||
@@ -249,10 +262,18 @@ class SearchProvider extends ChangeNotifier {
|
||||
originalContent = await FileService.instance.readFileContent(cleanPath);
|
||||
}
|
||||
|
||||
String replacement = _options.replaceText;
|
||||
if (_options.preserveCase) {
|
||||
replacement = CaseUtils.applyPreserveCase(
|
||||
original: match.matchedText,
|
||||
replacement: replacement,
|
||||
);
|
||||
}
|
||||
|
||||
final updatedContent = ProjectSearchService.instance.replaceSingleMatchInContent(
|
||||
fileContent: originalContent,
|
||||
match: match,
|
||||
replacement: _options.replaceText,
|
||||
replacement: replacement,
|
||||
);
|
||||
|
||||
if (openTab != null && openTab.isLoaded) {
|
||||
@@ -300,11 +321,22 @@ class SearchProvider extends ChangeNotifier {
|
||||
}
|
||||
|
||||
final replacedCount = file.matches.length;
|
||||
final updatedContent = ProjectSearchService.instance.replaceAllInContent(
|
||||
final String updatedContent;
|
||||
if (_options.preserveCase) {
|
||||
updatedContent = originalContent.replaceAllMapped(regExp, (m) {
|
||||
final matched = m[0] ?? '';
|
||||
return CaseUtils.applyPreserveCase(
|
||||
original: matched,
|
||||
replacement: _options.replaceText,
|
||||
);
|
||||
});
|
||||
} else {
|
||||
updatedContent = ProjectSearchService.instance.replaceAllInContent(
|
||||
fileContent: originalContent,
|
||||
regExp: regExp,
|
||||
replacement: _options.replaceText,
|
||||
);
|
||||
}
|
||||
|
||||
if (openTab != null && openTab.isLoaded) {
|
||||
openTab.content = updatedContent.replaceAll('\r\n', '\n');
|
||||
|
||||
@@ -30,6 +30,9 @@ class TabProvider extends ChangeNotifier {
|
||||
String? _activeFilePath;
|
||||
bool _isModified = false;
|
||||
Future<bool> Function()? _saveHandler;
|
||||
void Function()? _findNextHandler;
|
||||
void Function()? _findPreviousHandler;
|
||||
void Function()? _replaceHandler;
|
||||
EditorNavigationTarget? _navigationTarget;
|
||||
|
||||
ProjectProvider? _projectProvider;
|
||||
@@ -336,6 +339,20 @@ class TabProvider extends ChangeNotifier {
|
||||
_saveHandler = handler;
|
||||
}
|
||||
|
||||
void registerSearchHandlers({
|
||||
void Function()? findNext,
|
||||
void Function()? findPrevious,
|
||||
void Function()? replace,
|
||||
}) {
|
||||
_findNextHandler = findNext;
|
||||
_findPreviousHandler = findPrevious;
|
||||
_replaceHandler = replace;
|
||||
}
|
||||
|
||||
void findNext() => _findNextHandler?.call();
|
||||
void findPrevious() => _findPreviousHandler?.call();
|
||||
void replaceCurrent() => _replaceHandler?.call();
|
||||
|
||||
Future<void> selectFile(FileItem item) async {
|
||||
if (item.isDirectory) return;
|
||||
await openFile(item.path);
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
/// 大小写保留与变换工具类(用于替换时的 Preserve Case 逻辑)
|
||||
class CaseUtils {
|
||||
/// 根据原匹配字符串的大小写模式,将替换字符串转换为对应的大小写风格
|
||||
static String applyPreserveCase({
|
||||
required String original,
|
||||
required String replacement,
|
||||
}) {
|
||||
if (original.isEmpty || replacement.isEmpty) {
|
||||
return replacement;
|
||||
}
|
||||
|
||||
// 1. 全大写 (ALL UPPERCASE): 例如 "FOO" -> "BAR"
|
||||
if (original == original.toUpperCase() && original != original.toLowerCase()) {
|
||||
return replacement.toUpperCase();
|
||||
}
|
||||
|
||||
// 2. 全小写 (all lowercase): 例如 "foo" -> "bar"
|
||||
if (original == original.toLowerCase() && original != original.toUpperCase()) {
|
||||
return replacement.toLowerCase();
|
||||
}
|
||||
|
||||
// 3. 首字母大写 (Title Case / Capitalized): 例如 "Foo" -> "Bar"
|
||||
final isFirstUpper = original[0] == original[0].toUpperCase() &&
|
||||
original[0] != original[0].toLowerCase();
|
||||
final isRestLower = original.length > 1 &&
|
||||
original.substring(1) == original.substring(1).toLowerCase();
|
||||
|
||||
if (isFirstUpper && (original.length == 1 || isRestLower)) {
|
||||
if (replacement.length == 1) {
|
||||
return replacement.toUpperCase();
|
||||
}
|
||||
return replacement[0].toUpperCase() + replacement.substring(1).toLowerCase();
|
||||
}
|
||||
|
||||
// 其他情况保留原 replacement
|
||||
return replacement;
|
||||
}
|
||||
}
|
||||
@@ -1,10 +1,13 @@
|
||||
import 'package:code_editor/l10n/app_localizations.dart';
|
||||
import 'package:code_editor/providers/search_provider.dart';
|
||||
import 'package:code_editor/providers/tab_provider.dart';
|
||||
import 'package:code_editor/services/permission_service.dart';
|
||||
import 'package:code_editor/views/terminal_view.dart';
|
||||
import 'package:code_editor/widgets/terminal_entry_guard.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:path/path.dart' as p;
|
||||
import 'package:provider/provider.dart';
|
||||
import 'package:re_editor/re_editor.dart';
|
||||
|
||||
class CodeEditorAppBar extends StatelessWidget implements PreferredSizeWidget {
|
||||
@@ -76,6 +79,11 @@ class CodeEditorAppBar extends StatelessWidget implements PreferredSizeWidget {
|
||||
subtitleText = null;
|
||||
}
|
||||
|
||||
final searchProvider = context.watch<SearchProvider?>();
|
||||
final tabProvider = context.watch<TabProvider?>();
|
||||
final hasSearchQuery = searchProvider != null && searchProvider.query.trim().isNotEmpty;
|
||||
final isReplaceExpanded = searchProvider != null && searchProvider.isReplaceExpanded;
|
||||
|
||||
final isDark = theme.brightness == Brightness.dark;
|
||||
|
||||
return CodeEditorTapRegion(
|
||||
@@ -111,6 +119,39 @@ class CodeEditorAppBar extends StatelessWidget implements PreferredSizeWidget {
|
||||
],
|
||||
),
|
||||
actions: [
|
||||
if (hasSearchQuery) ...[
|
||||
Focus(
|
||||
canRequestFocus: false,
|
||||
skipTraversal: true,
|
||||
child: IconButton(
|
||||
visualDensity: VisualDensity.compact,
|
||||
icon: const Icon(Icons.arrow_upward),
|
||||
tooltip: l10n.searchPreviousMatch,
|
||||
onPressed: () => tabProvider?.findPrevious(),
|
||||
),
|
||||
),
|
||||
Focus(
|
||||
canRequestFocus: false,
|
||||
skipTraversal: true,
|
||||
child: IconButton(
|
||||
visualDensity: VisualDensity.compact,
|
||||
icon: const Icon(Icons.arrow_downward),
|
||||
tooltip: l10n.searchNextMatch,
|
||||
onPressed: () => tabProvider?.findNext(),
|
||||
),
|
||||
),
|
||||
if (isReplaceExpanded)
|
||||
Focus(
|
||||
canRequestFocus: false,
|
||||
skipTraversal: true,
|
||||
child: IconButton(
|
||||
visualDensity: VisualDensity.compact,
|
||||
icon: const Icon(Icons.find_replace),
|
||||
tooltip: l10n.replaceCurrentMatch,
|
||||
onPressed: () => tabProvider?.replaceCurrent(),
|
||||
),
|
||||
),
|
||||
],
|
||||
Focus(
|
||||
canRequestFocus: false,
|
||||
skipTraversal: true,
|
||||
|
||||
@@ -14,6 +14,7 @@ import 'package:code_editor/services/code_completion/smart_prompts_builder.dart'
|
||||
import 'package:code_editor/services/lsp/lsp_diagnostics_store.dart';
|
||||
import 'package:code_editor/services/lsp/lsp_manager.dart';
|
||||
import 'package:code_editor/services/lsp/lsp_protocol.dart';
|
||||
import 'package:code_editor/utils/case_utils.dart';
|
||||
import 'package:code_editor/utils/dialog_utils.dart';
|
||||
import 'package:code_editor/utils/syntax_highlight_helper.dart';
|
||||
import 'package:code_editor/widgets/code_autocomplete_view.dart';
|
||||
@@ -488,6 +489,20 @@ class _CodeEditorWidgetState extends State<CodeEditorWidget> {
|
||||
}
|
||||
}
|
||||
|
||||
CodeFindValue _buildCurrentFindValue(SearchProvider? searchProvider) {
|
||||
final isTextSearch = searchProvider?.options.mode == SearchMode.text;
|
||||
final query = (isTextSearch ? searchProvider?.query : null) ?? '';
|
||||
return CodeFindValue(
|
||||
option: CodeFindOption(
|
||||
pattern: query,
|
||||
caseSensitive: searchProvider?.caseSensitive ?? false,
|
||||
regex: searchProvider?.isRegex ?? false,
|
||||
),
|
||||
replaceMode: false,
|
||||
searching: query.isNotEmpty,
|
||||
);
|
||||
}
|
||||
|
||||
void _syncSearchHighlight(SearchProvider? searchProvider) {
|
||||
if (searchProvider == null || _findController == null) return;
|
||||
// 仅在文本内容搜索模式(SearchMode.text)下才同步编辑区高亮;
|
||||
@@ -501,13 +516,9 @@ class _CodeEditorWidgetState extends State<CodeEditorWidget> {
|
||||
curOpt.pattern != query ||
|
||||
curOpt.caseSensitive != searchProvider.caseSensitive ||
|
||||
curOpt.regex != searchProvider.isRegex) {
|
||||
// 严禁设置 _focusNode.canRequestFocus = false!
|
||||
// 在 Flutter 框架中,当编辑区拥有焦点时(用户正在编辑代码输入),
|
||||
// 将 canRequestFocus 置为 false 会强制触发 unfocus(),紧接着在输入时又重获焦点,
|
||||
// 造成软键盘反复弹起收起(一跳一跳的致命抖动)。
|
||||
// 同时此处保持 _findController.value 非 null(pattern 为 query),
|
||||
// re_editor 在 pattern 为空时会自动将 matches 清空,既彻底清除了高亮,
|
||||
// 又避免了在 value == null 时触发 re_editor 内部偷夺焦点的 requestFocus()。
|
||||
if (_findController!.findInputController.text != query) {
|
||||
_findController!.findInputController.text = query;
|
||||
}
|
||||
_findController!.value = CodeFindValue(
|
||||
option: CodeFindOption(
|
||||
pattern: query,
|
||||
@@ -517,10 +528,130 @@ class _CodeEditorWidgetState extends State<CodeEditorWidget> {
|
||||
replaceMode: false,
|
||||
searching: query.isNotEmpty,
|
||||
);
|
||||
if (_findController!.findInputController.text != query) {
|
||||
_findController!.findInputController.text = query;
|
||||
_controller?.forceRepaint();
|
||||
}
|
||||
}
|
||||
|
||||
List<CodeLineSelection> _findAllMatches(RegExp regExp) {
|
||||
if (_controller == null) return const [];
|
||||
final matches = <CodeLineSelection>[];
|
||||
final lines = _controller!.codeLines;
|
||||
for (int i = 0; i < lines.length; i++) {
|
||||
final lineText = lines[i].text;
|
||||
for (final m in regExp.allMatches(lineText)) {
|
||||
matches.add(CodeLineSelection(
|
||||
baseIndex: i,
|
||||
baseOffset: m.start,
|
||||
extentIndex: i,
|
||||
extentOffset: m.end,
|
||||
));
|
||||
}
|
||||
}
|
||||
return matches;
|
||||
}
|
||||
|
||||
void _handleFindNext() {
|
||||
if (!mounted || _controller == null) return;
|
||||
SearchProvider? searchProvider;
|
||||
try {
|
||||
searchProvider = context.read<SearchProvider?>();
|
||||
} catch (_) {}
|
||||
if (searchProvider == null || searchProvider.query.trim().isEmpty) return;
|
||||
|
||||
final regExp = searchProvider.options.buildRegExp();
|
||||
if (regExp == null) return;
|
||||
|
||||
final matches = _findAllMatches(regExp);
|
||||
if (matches.isEmpty) return;
|
||||
|
||||
final curSelection = _controller!.selection;
|
||||
final curEndLine = curSelection.endIndex;
|
||||
final curEndOffset = curSelection.endOffset;
|
||||
|
||||
CodeLineSelection? target;
|
||||
for (final match in matches) {
|
||||
if (match.startIndex > curEndLine ||
|
||||
(match.startIndex == curEndLine && match.startOffset >= curEndOffset)) {
|
||||
target = match;
|
||||
break;
|
||||
}
|
||||
}
|
||||
target ??= matches.first;
|
||||
|
||||
_controller!.selection = target;
|
||||
_controller!.makePositionCenterIfInvisible(target.start);
|
||||
_syncSearchHighlight(searchProvider);
|
||||
}
|
||||
|
||||
void _handleFindPrevious() {
|
||||
if (!mounted || _controller == null) return;
|
||||
SearchProvider? searchProvider;
|
||||
try {
|
||||
searchProvider = context.read<SearchProvider?>();
|
||||
} catch (_) {}
|
||||
if (searchProvider == null || searchProvider.query.trim().isEmpty) return;
|
||||
|
||||
final regExp = searchProvider.options.buildRegExp();
|
||||
if (regExp == null) return;
|
||||
|
||||
final matches = _findAllMatches(regExp);
|
||||
if (matches.isEmpty) return;
|
||||
|
||||
final curSelection = _controller!.selection;
|
||||
final curStartLine = curSelection.startIndex;
|
||||
final curStartOffset = curSelection.startOffset;
|
||||
|
||||
CodeLineSelection? target;
|
||||
for (int i = matches.length - 1; i >= 0; i--) {
|
||||
final match = matches[i];
|
||||
if (match.endIndex < curStartLine ||
|
||||
(match.endIndex == curStartLine && match.endOffset <= curStartOffset)) {
|
||||
target = match;
|
||||
break;
|
||||
}
|
||||
}
|
||||
target ??= matches.last;
|
||||
|
||||
_controller!.selection = target;
|
||||
_controller!.makePositionCenterIfInvisible(target.start);
|
||||
_syncSearchHighlight(searchProvider);
|
||||
}
|
||||
|
||||
void _handleReplaceCurrent() {
|
||||
if (!mounted || _controller == null) return;
|
||||
SearchProvider? searchProvider;
|
||||
try {
|
||||
searchProvider = context.read<SearchProvider?>();
|
||||
} catch (_) {}
|
||||
if (searchProvider == null || searchProvider.query.trim().isEmpty) return;
|
||||
|
||||
final selection = _controller!.selection;
|
||||
if (selection.isCollapsed) return;
|
||||
|
||||
final selectedText = _controller!.selectedText;
|
||||
final regExp = searchProvider.options.buildRegExp();
|
||||
if (regExp == null) return;
|
||||
|
||||
// 先判断框选文本和搜索内容是否一致(全匹配校验)
|
||||
final match = regExp.firstMatch(selectedText);
|
||||
if (match == null || match.start != 0 || match.end != selectedText.length) {
|
||||
// 不一致,静默失败
|
||||
return;
|
||||
}
|
||||
|
||||
// 计算替换文本(支持保留大小写)
|
||||
String replacement = searchProvider.replaceText;
|
||||
if (searchProvider.preserveCase) {
|
||||
replacement = CaseUtils.applyPreserveCase(
|
||||
original: selectedText,
|
||||
replacement: replacement,
|
||||
);
|
||||
}
|
||||
|
||||
_controller!.replaceSelection(replacement);
|
||||
|
||||
// 替换后自动定位并框选下一个匹配
|
||||
_handleFindNext();
|
||||
}
|
||||
|
||||
void _onDiagnosticsChanged() {
|
||||
@@ -571,7 +702,9 @@ class _CodeEditorWidgetState extends State<CodeEditorWidget> {
|
||||
@override
|
||||
void dispose() {
|
||||
try {
|
||||
_getTabProvider(context).registerSaveHandler(null);
|
||||
final provider = _getTabProvider(context);
|
||||
provider.registerSaveHandler(null);
|
||||
provider.registerSearchHandlers();
|
||||
} catch (_) {}
|
||||
LspDiagnosticsStore.instance.removeListener(_onDiagnosticsChanged);
|
||||
if (_currentLoadedPath != null) {
|
||||
@@ -1110,19 +1243,19 @@ class _CodeEditorWidgetState extends State<CodeEditorWidget> {
|
||||
);
|
||||
|
||||
_controller = newController;
|
||||
SearchProvider? searchProvider;
|
||||
try {
|
||||
searchProvider = context.read<SearchProvider?>();
|
||||
} catch (_) {}
|
||||
final initialFindValue = _buildCurrentFindValue(searchProvider);
|
||||
_findController?.dispose();
|
||||
_findController = CodeFindController(
|
||||
newController,
|
||||
const CodeFindValue(
|
||||
option: CodeFindOption(
|
||||
pattern: '',
|
||||
caseSensitive: false,
|
||||
regex: false,
|
||||
),
|
||||
replaceMode: false,
|
||||
searching: false,
|
||||
),
|
||||
initialFindValue,
|
||||
);
|
||||
if (initialFindValue.option.pattern.isNotEmpty) {
|
||||
_findController!.findInputController.text = initialFindValue.option.pattern;
|
||||
}
|
||||
_currentLoadedPath = fullFilePath;
|
||||
_currentIndentSize = activeIndentSize;
|
||||
_errorMessage = null;
|
||||
@@ -1145,6 +1278,11 @@ class _CodeEditorWidgetState extends State<CodeEditorWidget> {
|
||||
try {
|
||||
provider.setModified(tab.isModified);
|
||||
provider.registerSaveHandler(() => _saveFile());
|
||||
provider.registerSearchHandlers(
|
||||
findNext: _handleFindNext,
|
||||
findPrevious: _handleFindPrevious,
|
||||
replace: _handleReplaceCurrent,
|
||||
);
|
||||
} catch (_) {}
|
||||
|
||||
if (mounted) {
|
||||
@@ -1152,6 +1290,7 @@ class _CodeEditorWidgetState extends State<CodeEditorWidget> {
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||
if (mounted) {
|
||||
_handleNavigationTarget(provider);
|
||||
_syncSearchHighlight(searchProvider);
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -1187,19 +1326,19 @@ class _CodeEditorWidgetState extends State<CodeEditorWidget> {
|
||||
);
|
||||
|
||||
_controller = newController;
|
||||
SearchProvider? searchProvider;
|
||||
try {
|
||||
searchProvider = context.read<SearchProvider?>();
|
||||
} catch (_) {}
|
||||
final initialFindValue = _buildCurrentFindValue(searchProvider);
|
||||
_findController?.dispose();
|
||||
_findController = CodeFindController(
|
||||
newController,
|
||||
const CodeFindValue(
|
||||
option: CodeFindOption(
|
||||
pattern: '',
|
||||
caseSensitive: false,
|
||||
regex: false,
|
||||
),
|
||||
replaceMode: false,
|
||||
searching: false,
|
||||
),
|
||||
initialFindValue,
|
||||
);
|
||||
if (initialFindValue.option.pattern.isNotEmpty) {
|
||||
_findController!.findInputController.text = initialFindValue.option.pattern;
|
||||
}
|
||||
_currentLoadedPath = fullFilePath;
|
||||
_currentIndentSize = activeIndentSize;
|
||||
_errorMessage = null;
|
||||
@@ -1229,10 +1368,16 @@ class _CodeEditorWidgetState extends State<CodeEditorWidget> {
|
||||
try {
|
||||
provider.setModified(tab?.isModified ?? false);
|
||||
provider.registerSaveHandler(() => _saveFile());
|
||||
provider.registerSearchHandlers(
|
||||
findNext: _handleFindNext,
|
||||
findPrevious: _handleFindPrevious,
|
||||
replace: _handleReplaceCurrent,
|
||||
);
|
||||
} catch (_) {}
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||
if (mounted) {
|
||||
_handleNavigationTarget(provider);
|
||||
_syncSearchHighlight(searchProvider);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -72,6 +72,114 @@ class _SearchPanelWidgetState extends State<SearchPanelWidget> {
|
||||
}
|
||||
|
||||
final MenuController _menuController = MenuController();
|
||||
final MenuController _replaceMenuController = MenuController();
|
||||
|
||||
/// 替换框右侧更多选项弹出菜单(保留大小写)
|
||||
Widget _buildReplaceOptionsMenu(
|
||||
SearchProvider searchProvider,
|
||||
SearchOptions options,
|
||||
ThemeData theme,
|
||||
AppLocalizations l10n,
|
||||
) {
|
||||
final hasActiveFilter = options.preserveCase;
|
||||
final colorScheme = theme.colorScheme;
|
||||
|
||||
return MenuAnchor(
|
||||
controller: _replaceMenuController,
|
||||
alignmentOffset: const Offset(0, 4),
|
||||
style: MenuStyle(
|
||||
elevation: const WidgetStatePropertyAll(4),
|
||||
shape: WidgetStatePropertyAll(
|
||||
RoundedRectangleBorder(borderRadius: BorderRadius.circular(10)),
|
||||
),
|
||||
padding: const WidgetStatePropertyAll(
|
||||
EdgeInsets.symmetric(vertical: 4),
|
||||
),
|
||||
),
|
||||
menuChildren: [
|
||||
StatefulBuilder(
|
||||
builder: (context, setMenuState) {
|
||||
final curOptions = searchProvider.options;
|
||||
return SizedBox(
|
||||
width: 220,
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
// 保留大小写
|
||||
InkWell(
|
||||
onTap: () {
|
||||
searchProvider.togglePreserveCase();
|
||||
setMenuState(() {});
|
||||
},
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 14, vertical: 4),
|
||||
child: Row(
|
||||
children: [
|
||||
SizedBox(
|
||||
width: 24,
|
||||
child: Text(
|
||||
'AB',
|
||||
style: TextStyle(
|
||||
fontWeight: FontWeight.bold,
|
||||
fontSize: 13,
|
||||
color: curOptions.preserveCase
|
||||
? colorScheme.primary
|
||||
: colorScheme.onSurface,
|
||||
),
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 8),
|
||||
Expanded(
|
||||
child: Text(
|
||||
l10n.preserveCase,
|
||||
style: const TextStyle(fontSize: 13),
|
||||
),
|
||||
),
|
||||
Switch(
|
||||
value: curOptions.preserveCase,
|
||||
onChanged: (val) {
|
||||
searchProvider.togglePreserveCase();
|
||||
setMenuState(() {});
|
||||
},
|
||||
materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
],
|
||||
builder: (context, controller, child) {
|
||||
return Tooltip(
|
||||
message: l10n.replaceMoreOptions,
|
||||
child: InkWell(
|
||||
borderRadius: BorderRadius.circular(4),
|
||||
onTap: () {
|
||||
if (controller.isOpen) {
|
||||
controller.close();
|
||||
} else {
|
||||
controller.open();
|
||||
}
|
||||
},
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(2.0),
|
||||
child: Icon(
|
||||
Icons.more_vert,
|
||||
size: 18,
|
||||
color: hasActiveFilter
|
||||
? colorScheme.primary
|
||||
: colorScheme.onSurfaceVariant,
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
/// 搜索框右侧更多选项弹出菜单(区分大小写、全字匹配、正则表达式)
|
||||
Widget _buildSearchOptionsMenu(
|
||||
@@ -459,6 +567,13 @@ class _SearchPanelWidgetState extends State<SearchPanelWidget> {
|
||||
maxLines: 1,
|
||||
),
|
||||
),
|
||||
IconButton(
|
||||
visualDensity: VisualDensity.compact,
|
||||
iconSize: 18,
|
||||
tooltip: l10n.searchRefresh,
|
||||
icon: const Icon(Icons.refresh),
|
||||
onPressed: () => searchProvider.triggerSearch(),
|
||||
),
|
||||
if (options.mode == SearchMode.fileName && result.fileResults.isNotEmpty) ...[
|
||||
IconButton(
|
||||
visualDensity: VisualDensity.compact,
|
||||
@@ -721,6 +836,9 @@ class _SearchPanelWidgetState extends State<SearchPanelWidget> {
|
||||
),
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 2),
|
||||
// 更多替换选项菜单按钮(保留大小写)
|
||||
_buildReplaceOptionsMenu(searchProvider, options, theme, l10n),
|
||||
],
|
||||
),
|
||||
],
|
||||
|
||||
@@ -0,0 +1,107 @@
|
||||
import 'package:code_editor/providers/search_provider.dart';
|
||||
import 'package:code_editor/providers/tab_provider.dart';
|
||||
import 'package:code_editor/utils/case_utils.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
|
||||
void main() {
|
||||
group('CaseUtils.applyPreserveCase Tests', () {
|
||||
test('ALL UPPERCASE preserves to uppercase', () {
|
||||
expect(
|
||||
CaseUtils.applyPreserveCase(original: 'HELLO', replacement: 'world'),
|
||||
'WORLD',
|
||||
);
|
||||
expect(
|
||||
CaseUtils.applyPreserveCase(original: 'A', replacement: 'b'),
|
||||
'B',
|
||||
);
|
||||
});
|
||||
|
||||
test('all lowercase preserves to lowercase', () {
|
||||
expect(
|
||||
CaseUtils.applyPreserveCase(original: 'hello', replacement: 'WORLD'),
|
||||
'world',
|
||||
);
|
||||
expect(
|
||||
CaseUtils.applyPreserveCase(original: 'a', replacement: 'B'),
|
||||
'b',
|
||||
);
|
||||
});
|
||||
|
||||
test('Title Case / Capitalized preserves to Capitalized', () {
|
||||
expect(
|
||||
CaseUtils.applyPreserveCase(original: 'Hello', replacement: 'world'),
|
||||
'World',
|
||||
);
|
||||
expect(
|
||||
CaseUtils.applyPreserveCase(original: 'Hello', replacement: 'WORLD'),
|
||||
'World',
|
||||
);
|
||||
expect(
|
||||
CaseUtils.applyPreserveCase(original: 'H', replacement: 'w'),
|
||||
'W',
|
||||
);
|
||||
});
|
||||
|
||||
test('Mixed or empty cases fallback appropriately', () {
|
||||
expect(
|
||||
CaseUtils.applyPreserveCase(original: 'hElLo', replacement: 'world'),
|
||||
'world',
|
||||
);
|
||||
expect(
|
||||
CaseUtils.applyPreserveCase(original: '', replacement: 'world'),
|
||||
'world',
|
||||
);
|
||||
expect(
|
||||
CaseUtils.applyPreserveCase(original: 'hello', replacement: ''),
|
||||
'',
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
group('SearchProvider preserveCase Tests', () {
|
||||
test('togglePreserveCase updates options properly', () {
|
||||
final provider = SearchProvider();
|
||||
expect(provider.preserveCase, isFalse);
|
||||
|
||||
provider.togglePreserveCase();
|
||||
expect(provider.preserveCase, isTrue);
|
||||
|
||||
provider.setPreserveCase(false);
|
||||
expect(provider.preserveCase, isFalse);
|
||||
});
|
||||
});
|
||||
|
||||
group('TabProvider Search Handlers Registration Tests', () {
|
||||
test('registerSearchHandlers and callbacks execution', () {
|
||||
final tabProvider = TabProvider();
|
||||
int nextCount = 0;
|
||||
int prevCount = 0;
|
||||
int replaceCount = 0;
|
||||
|
||||
tabProvider.registerSearchHandlers(
|
||||
findNext: () => nextCount++,
|
||||
findPrevious: () => prevCount++,
|
||||
replace: () => replaceCount++,
|
||||
);
|
||||
|
||||
tabProvider.findNext();
|
||||
expect(nextCount, 1);
|
||||
|
||||
tabProvider.findPrevious();
|
||||
expect(prevCount, 1);
|
||||
|
||||
tabProvider.replaceCurrent();
|
||||
expect(replaceCount, 1);
|
||||
|
||||
// Unregister
|
||||
tabProvider.registerSearchHandlers();
|
||||
tabProvider.findNext();
|
||||
tabProvider.findPrevious();
|
||||
tabProvider.replaceCurrent();
|
||||
|
||||
expect(nextCount, 1);
|
||||
expect(prevCount, 1);
|
||||
expect(replaceCount, 1);
|
||||
});
|
||||
});
|
||||
}
|
||||
Reference in new issue
Block a user