2
This commit is contained in:
1 parent
5ac164b407
commit
f93f6095af
94 files changed
+7877
-4018
No files matched your search
@@ -0,0 +1,252 @@
|
||||
import 'dart:math' as math;
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:re_editor/re_editor.dart';
|
||||
import '../services/code_completion/smart_prompts_builder.dart';
|
||||
|
||||
/// 移动端友好的现代化代码智能补全悬浮面板
|
||||
class CodeAutocompleteView extends StatefulWidget implements PreferredSizeWidget {
|
||||
const CodeAutocompleteView({
|
||||
super.key,
|
||||
required this.notifier,
|
||||
required this.onSelected,
|
||||
this.width = 270.0,
|
||||
this.height = 196.0,
|
||||
});
|
||||
|
||||
final ValueNotifier<CodeAutocompleteEditingValue> notifier;
|
||||
final ValueChanged<CodeAutocompleteResult> onSelected;
|
||||
final double width;
|
||||
final double height;
|
||||
|
||||
static const double itemHeight = 36.0;
|
||||
static const double verticalPadding = 8.0;
|
||||
static const double borderWidth = 2.0;
|
||||
|
||||
@override
|
||||
Size get preferredSize {
|
||||
final prompts = notifier.value.prompts;
|
||||
if (prompts.isEmpty) return Size(width, 0.0);
|
||||
final contentHeight = prompts.length * itemHeight + verticalPadding + borderWidth;
|
||||
final effectiveHeight = contentHeight.clamp(itemHeight + verticalPadding + borderWidth, height);
|
||||
return Size(width, effectiveHeight);
|
||||
}
|
||||
|
||||
@override
|
||||
State<CodeAutocompleteView> createState() => _CodeAutocompleteViewState();
|
||||
}
|
||||
|
||||
class _CodeAutocompleteViewState extends State<CodeAutocompleteView> {
|
||||
final ScrollController _scrollController = ScrollController();
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
widget.notifier.addListener(_handleValueChanged);
|
||||
}
|
||||
|
||||
@override
|
||||
void didUpdateWidget(covariant CodeAutocompleteView oldWidget) {
|
||||
super.didUpdateWidget(oldWidget);
|
||||
if (oldWidget.notifier != widget.notifier) {
|
||||
oldWidget.notifier.removeListener(_handleValueChanged);
|
||||
widget.notifier.addListener(_handleValueChanged);
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
widget.notifier.removeListener(_handleValueChanged);
|
||||
_scrollController.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
void _handleValueChanged() {
|
||||
if (!mounted) return;
|
||||
final value = widget.notifier.value;
|
||||
final targetOffset = value.index * CodeAutocompleteView.itemHeight;
|
||||
if (_scrollController.hasClients && _scrollController.position.hasViewportDimension) {
|
||||
final currentOffset = _scrollController.offset;
|
||||
final viewportDimension = _scrollController.position.viewportDimension;
|
||||
final maxView = currentOffset + viewportDimension - CodeAutocompleteView.itemHeight - CodeAutocompleteView.verticalPadding;
|
||||
if (targetOffset < currentOffset) {
|
||||
_scrollController.jumpTo(targetOffset);
|
||||
} else if (targetOffset > maxView) {
|
||||
_scrollController.jumpTo(math.max(0.0, targetOffset - viewportDimension + CodeAutocompleteView.itemHeight + CodeAutocompleteView.verticalPadding));
|
||||
}
|
||||
}
|
||||
setState(() {});
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final theme = Theme.of(context);
|
||||
final isDark = theme.brightness == Brightness.dark;
|
||||
final value = widget.notifier.value;
|
||||
final prompts = value.prompts;
|
||||
|
||||
final bgColor = isDark ? const Color(0xFF1E1E2E) : Colors.white;
|
||||
final borderColor = isDark ? Colors.white12 : Colors.black12;
|
||||
|
||||
return Container(
|
||||
width: widget.width,
|
||||
constraints: BoxConstraints(maxHeight: widget.height),
|
||||
decoration: BoxDecoration(
|
||||
color: bgColor,
|
||||
borderRadius: BorderRadius.circular(10.0),
|
||||
border: Border.all(color: borderColor, width: 1.0),
|
||||
boxShadow: [
|
||||
BoxShadow(
|
||||
color: Colors.black.withValues(alpha: isDark ? 0.45 : 0.15),
|
||||
blurRadius: 14.0,
|
||||
offset: const Offset(0, 4),
|
||||
),
|
||||
],
|
||||
),
|
||||
clipBehavior: Clip.antiAlias,
|
||||
child: ListView.builder(
|
||||
controller: _scrollController,
|
||||
physics: const ClampingScrollPhysics(),
|
||||
padding: const EdgeInsets.symmetric(vertical: 4.0),
|
||||
itemCount: prompts.length,
|
||||
itemExtent: CodeAutocompleteView.itemHeight,
|
||||
itemBuilder: (context, index) {
|
||||
final prompt = prompts[index];
|
||||
final isSelected = index == value.index;
|
||||
return _buildPromptItem(context, prompt, isSelected, index, theme, isDark);
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildPromptItem(
|
||||
BuildContext context,
|
||||
CodePrompt prompt,
|
||||
bool isSelected,
|
||||
int index,
|
||||
ThemeData theme,
|
||||
bool isDark,
|
||||
) {
|
||||
final smart = prompt is SmartPrompt ? prompt : null;
|
||||
final kind = smart?.kind ?? SmartPromptKind.word;
|
||||
final typeText = smart?.type ?? '';
|
||||
final matchedIndices = smart?.matchedIndices ?? const [];
|
||||
|
||||
final highlightBg = theme.colorScheme.primary.withValues(alpha: isDark ? 0.25 : 0.15);
|
||||
final defaultBg = Colors.transparent;
|
||||
|
||||
return InkWell(
|
||||
onTap: () {
|
||||
widget.notifier.value = widget.notifier.value.copyWith(index: index);
|
||||
widget.onSelected(widget.notifier.value.autocomplete);
|
||||
},
|
||||
child: Container(
|
||||
height: CodeAutocompleteView.itemHeight,
|
||||
color: isSelected ? highlightBg : defaultBg,
|
||||
padding: const EdgeInsets.symmetric(horizontal: 10.0),
|
||||
child: Row(
|
||||
children: [
|
||||
_buildKindIcon(kind, theme),
|
||||
const SizedBox(width: 8.0),
|
||||
Expanded(
|
||||
child: _buildHighlightedText(prompt.word, matchedIndices, theme, isSelected),
|
||||
),
|
||||
if (typeText.isNotEmpty)
|
||||
Text(
|
||||
typeText,
|
||||
style: TextStyle(
|
||||
fontSize: 11.0,
|
||||
color: isDark ? Colors.white38 : Colors.black38,
|
||||
fontFamily: 'monospace',
|
||||
),
|
||||
maxLines: 1,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildKindIcon(SmartPromptKind kind, ThemeData theme) {
|
||||
IconData icon;
|
||||
Color color;
|
||||
|
||||
switch (kind) {
|
||||
case SmartPromptKind.keyword:
|
||||
icon = Icons.vpn_key_rounded;
|
||||
color = const Color(0xFFC586C0); // 类似 VS Code 关键字粉紫色
|
||||
break;
|
||||
case SmartPromptKind.function:
|
||||
icon = Icons.functions_rounded;
|
||||
color = const Color(0xFFDCDCAA); // 函数黄色
|
||||
break;
|
||||
case SmartPromptKind.field:
|
||||
icon = Icons.data_object_rounded;
|
||||
color = const Color(0xFF9CDCFE); // 变量浅蓝色
|
||||
break;
|
||||
case SmartPromptKind.type:
|
||||
icon = Icons.category_rounded;
|
||||
color = const Color(0xFF4EC9B0); // 类型青绿色
|
||||
break;
|
||||
case SmartPromptKind.constant:
|
||||
icon = Icons.tag_rounded;
|
||||
color = const Color(0xFF4FC1FF); // 常量亮天蓝色
|
||||
break;
|
||||
case SmartPromptKind.word:
|
||||
icon = Icons.text_snippet_outlined;
|
||||
color = Colors.grey;
|
||||
break;
|
||||
}
|
||||
|
||||
return Icon(icon, size: 15.0, color: color);
|
||||
}
|
||||
|
||||
Widget _buildHighlightedText(
|
||||
String word,
|
||||
List<int> matchedIndices,
|
||||
ThemeData theme,
|
||||
bool isSelected,
|
||||
) {
|
||||
if (matchedIndices.isEmpty) {
|
||||
return Text(
|
||||
word,
|
||||
style: TextStyle(
|
||||
fontSize: 13.0,
|
||||
fontWeight: isSelected ? FontWeight.w600 : FontWeight.normal,
|
||||
fontFamily: 'monospace',
|
||||
),
|
||||
maxLines: 1,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
);
|
||||
}
|
||||
|
||||
final matchedSet = matchedIndices.toSet();
|
||||
final List<TextSpan> spans = [];
|
||||
|
||||
for (int i = 0; i < word.length; i++) {
|
||||
final isHit = matchedSet.contains(i);
|
||||
spans.add(
|
||||
TextSpan(
|
||||
text: word[i],
|
||||
style: TextStyle(
|
||||
color: isHit ? theme.colorScheme.primary : null,
|
||||
fontWeight: isHit ? FontWeight.bold : (isSelected ? FontWeight.w600 : FontWeight.normal),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
return RichText(
|
||||
text: TextSpan(
|
||||
style: TextStyle(
|
||||
fontSize: 13.0,
|
||||
color: theme.textTheme.bodyMedium?.color,
|
||||
fontFamily: 'monospace',
|
||||
),
|
||||
children: spans,
|
||||
),
|
||||
maxLines: 1,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -118,7 +118,7 @@ class CodeEditorAppBar extends StatelessWidget implements PreferredSizeWidget {
|
||||
visualDensity: VisualDensity.compact,
|
||||
icon: const Icon(Icons.play_arrow),
|
||||
tooltip: l10n.run,
|
||||
onPressed: onRun ?? () {},
|
||||
onPressed: (isDetecting || onRun == null) ? null : onRun,
|
||||
),
|
||||
),
|
||||
Focus(
|
||||
@@ -319,7 +319,9 @@ class _MoreMenuButtonState extends State<_MoreMenuButton> with SingleTickerProvi
|
||||
icon: Icons.playlist_play,
|
||||
title: l10n.runTasks,
|
||||
iconColor: onSurfaceColor,
|
||||
enabled: !widget.isDetecting,
|
||||
onTap: () {
|
||||
if (widget.isDetecting) return;
|
||||
_closeMenu(onClosed: widget.onRunTasks);
|
||||
},
|
||||
),
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
import 'dart:math';
|
||||
|
||||
import 'package:code_editor/l10n/app_localizations.dart';
|
||||
import 'package:code_editor/services/lsp/lsp_diagnostics_store.dart';
|
||||
import 'package:code_editor/services/lsp/lsp_protocol.dart';
|
||||
import 'package:code_editor/widgets/lsp_quick_fix_dialog.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:re_editor/re_editor.dart';
|
||||
@@ -150,6 +153,7 @@ class _MobileSelectionToolbarWidget extends StatefulWidget {
|
||||
final VoidCallback onDismiss;
|
||||
final FocusNode? focusNode;
|
||||
final Rect? renderRect;
|
||||
final String? Function()? filePathGetter;
|
||||
|
||||
const _MobileSelectionToolbarWidget({
|
||||
required this.anchors,
|
||||
@@ -157,6 +161,7 @@ class _MobileSelectionToolbarWidget extends StatefulWidget {
|
||||
required this.onDismiss,
|
||||
this.focusNode,
|
||||
this.renderRect,
|
||||
this.filePathGetter,
|
||||
});
|
||||
|
||||
@override
|
||||
@@ -197,6 +202,12 @@ class _MobileSelectionToolbarWidgetState extends State<_MobileSelectionToolbarWi
|
||||
|
||||
final theme = Theme.of(context);
|
||||
|
||||
final filePath = widget.filePathGetter?.call();
|
||||
final line = widget.controller.selection.baseIndex;
|
||||
final diags = (filePath != null && line >= 0)
|
||||
? LspDiagnosticsStore.instance.getDiagnosticsForLine(filePath, line)
|
||||
: const <LspDiagnostic>[];
|
||||
|
||||
final toolbarContent = Material(
|
||||
elevation: 6.0,
|
||||
shadowColor: Colors.black45,
|
||||
@@ -213,6 +224,23 @@ class _MobileSelectionToolbarWidgetState extends State<_MobileSelectionToolbarWi
|
||||
child: Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
if (diags.isNotEmpty && filePath != null) ...[
|
||||
_buildItem(
|
||||
context,
|
||||
label: '修复 💡',
|
||||
onTap: () {
|
||||
widget.onDismiss();
|
||||
LspQuickFixDialog.show(
|
||||
context,
|
||||
filePath: filePath,
|
||||
lineIndex: line,
|
||||
diagnostics: diags,
|
||||
controller: widget.controller,
|
||||
);
|
||||
},
|
||||
),
|
||||
_buildDivider(context),
|
||||
],
|
||||
if (hasSelection) ...[
|
||||
_buildItem(
|
||||
context,
|
||||
@@ -311,6 +339,7 @@ class _MobileSelectionToolbarWidgetState extends State<_MobileSelectionToolbarWi
|
||||
/// 自定义代码编辑器选区与右键菜单控制器
|
||||
class CodeEditorToolbarController implements SelectionToolbarController {
|
||||
final FocusNode? focusNode;
|
||||
final String? Function()? filePathGetter;
|
||||
late final SelectionToolbarController _mobileController;
|
||||
|
||||
CodeLineEditingController? _lastController;
|
||||
@@ -319,7 +348,7 @@ class CodeEditorToolbarController implements SelectionToolbarController {
|
||||
LayerLink? _lastLayerLink;
|
||||
ValueNotifier<bool>? _lastVisibility;
|
||||
|
||||
CodeEditorToolbarController({this.focusNode}) {
|
||||
CodeEditorToolbarController({this.focusNode, this.filePathGetter}) {
|
||||
_mobileController = MobileSelectionToolbarController(
|
||||
builder: ({
|
||||
required BuildContext context,
|
||||
@@ -335,6 +364,7 @@ class CodeEditorToolbarController implements SelectionToolbarController {
|
||||
onDismiss: onDismiss,
|
||||
focusNode: focusNode,
|
||||
renderRect: renderRect,
|
||||
filePathGetter: filePathGetter,
|
||||
);
|
||||
},
|
||||
);
|
||||
@@ -410,6 +440,12 @@ class CodeEditorToolbarController implements SelectionToolbarController {
|
||||
final clipboardData = await Clipboard.getData(Clipboard.kTextPlain);
|
||||
final hasClipboard = clipboardData != null && (clipboardData.text?.isNotEmpty ?? false);
|
||||
|
||||
final filePath = filePathGetter?.call();
|
||||
final line = controller.selection.baseIndex;
|
||||
final diags = (filePath != null && line >= 0)
|
||||
? LspDiagnosticsStore.instance.getDiagnosticsForLine(filePath, line)
|
||||
: const <LspDiagnostic>[];
|
||||
|
||||
if (!context.mounted) return;
|
||||
|
||||
final l10n = AppLocalizations.of(context);
|
||||
@@ -433,6 +469,23 @@ class CodeEditorToolbarController implements SelectionToolbarController {
|
||||
Offset.zero & mediaQuery.size,
|
||||
),
|
||||
items: [
|
||||
if (diags.isNotEmpty && filePath != null) ...[
|
||||
_EditorContextMenuItem(
|
||||
text: '快速修复 (Quick Fix)',
|
||||
icon: Icons.lightbulb,
|
||||
enabled: true,
|
||||
onTap: () {
|
||||
LspQuickFixDialog.show(
|
||||
context,
|
||||
filePath: filePath,
|
||||
lineIndex: line,
|
||||
diagnostics: diags,
|
||||
controller: controller,
|
||||
);
|
||||
},
|
||||
),
|
||||
const PopupMenuDivider(height: 1),
|
||||
],
|
||||
_EditorContextMenuItem(
|
||||
text: l10n?.cut ?? 'Cut',
|
||||
icon: Icons.content_cut,
|
||||
|
||||
@@ -8,9 +8,17 @@ import 'package:code_editor/providers/settings_provider.dart';
|
||||
import 'package:code_editor/providers/tab_provider.dart';
|
||||
import 'package:code_editor/services/file_service.dart';
|
||||
import 'package:code_editor/services/file_watcher_service.dart';
|
||||
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/dialog_utils.dart';
|
||||
import 'package:code_editor/utils/syntax_highlight_helper.dart';
|
||||
import 'package:code_editor/widgets/code_autocomplete_view.dart';
|
||||
import 'package:code_editor/widgets/code_editor_menu.dart';
|
||||
import 'package:code_editor/widgets/editor/editor_diagnostic_controller.dart';
|
||||
import 'package:code_editor/widgets/editor/editor_lsp_coordinator.dart';
|
||||
import 'package:code_editor/widgets/editor/editor_overlay_scope.dart';
|
||||
import 'package:code_editor/widgets/virtual_keyboard_widget.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/scheduler.dart';
|
||||
@@ -18,6 +26,37 @@ import 'package:path/path.dart' as p;
|
||||
import 'package:provider/provider.dart';
|
||||
import 'package:re_editor/re_editor.dart';
|
||||
|
||||
/// 行号指示器红/黄圆点装饰器
|
||||
class DiagnosticGutterDotDecoration extends Decoration {
|
||||
final Color color;
|
||||
final double radius;
|
||||
|
||||
const DiagnosticGutterDotDecoration({
|
||||
required this.color,
|
||||
this.radius = 3.0,
|
||||
});
|
||||
|
||||
@override
|
||||
BoxPainter createBoxPainter([VoidCallback? onChanged]) {
|
||||
return _DiagnosticGutterDotPainter(this);
|
||||
}
|
||||
}
|
||||
|
||||
class _DiagnosticGutterDotPainter extends BoxPainter {
|
||||
final DiagnosticGutterDotDecoration decoration;
|
||||
_DiagnosticGutterDotPainter(this.decoration);
|
||||
|
||||
@override
|
||||
void paint(Canvas canvas, Offset offset, ImageConfiguration configuration) {
|
||||
final size = configuration.size ?? Size.zero;
|
||||
final paint = Paint()
|
||||
..color = decoration.color
|
||||
..style = PaintingStyle.fill;
|
||||
final center = Offset(offset.dx + 4.0, offset.dy + size.height / 2);
|
||||
canvas.drawCircle(center, decoration.radius, paint);
|
||||
}
|
||||
}
|
||||
|
||||
class CodeEditorWidget extends StatefulWidget {
|
||||
final String? rootPath;
|
||||
final String? filePath;
|
||||
@@ -45,12 +84,15 @@ class _CodeEditorWidgetState extends State<CodeEditorWidget> {
|
||||
CodeScrollController? _scrollController;
|
||||
final FocusNode _focusNode = FocusNode();
|
||||
late final CodeEditorToolbarController _toolbarController;
|
||||
final EditorDiagnosticController _diagnosticController = const EditorDiagnosticController();
|
||||
final EditorLspCoordinator _lspCoordinator = const EditorLspCoordinator();
|
||||
bool _isLoading = false;
|
||||
int _currentLoadVersion = 0;
|
||||
String? _errorMessage;
|
||||
String? _currentLoadedPath;
|
||||
int? _currentIndentSize;
|
||||
bool _isShowingConflictDialog = false;
|
||||
int _currentCursorLine = -1;
|
||||
|
||||
// 双指捏合实时缩放字号及中心锚定状态
|
||||
final GlobalKey _editorContainerKey = GlobalKey();
|
||||
@@ -420,10 +462,26 @@ class _CodeEditorWidgetState extends State<CodeEditorWidget> {
|
||||
});
|
||||
}
|
||||
|
||||
void _onDiagnosticsChanged() {
|
||||
if (!mounted || _controller == null) return;
|
||||
_controller?.forceRepaint();
|
||||
if (WidgetsBinding.instance.schedulerPhase == SchedulerPhase.persistentCallbacks) {
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||
if (mounted) setState(() {});
|
||||
});
|
||||
} else {
|
||||
setState(() {});
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_toolbarController = CodeEditorToolbarController(focusNode: _focusNode);
|
||||
_toolbarController = CodeEditorToolbarController(
|
||||
focusNode: _focusNode,
|
||||
filePathGetter: () => _currentLoadedPath ?? widget.filePath,
|
||||
);
|
||||
LspDiagnosticsStore.instance.addListener(_onDiagnosticsChanged);
|
||||
_loadFileContent();
|
||||
}
|
||||
|
||||
@@ -454,6 +512,10 @@ class _CodeEditorWidgetState extends State<CodeEditorWidget> {
|
||||
try {
|
||||
_getTabProvider(context).registerSaveHandler(null);
|
||||
} catch (_) {}
|
||||
LspDiagnosticsStore.instance.removeListener(_onDiagnosticsChanged);
|
||||
if (_currentLoadedPath != null) {
|
||||
LspManager.instance.onFileClosed(_currentLoadedPath!);
|
||||
}
|
||||
_toolbarController.hide(context);
|
||||
final currentTab = _getCurrentTab();
|
||||
_saveCurrentTabScrollState(currentTab);
|
||||
@@ -468,6 +530,19 @@ class _CodeEditorWidgetState extends State<CodeEditorWidget> {
|
||||
|
||||
void _onTextChanged() {
|
||||
if (!mounted || _controller == null) return;
|
||||
|
||||
final newCursorLine = _controller?.selection.baseIndex ?? -1;
|
||||
if (_currentCursorLine != newCursorLine) {
|
||||
_currentCursorLine = newCursorLine;
|
||||
if (WidgetsBinding.instance.schedulerPhase == SchedulerPhase.persistentCallbacks) {
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||
if (mounted) setState(() {});
|
||||
});
|
||||
} else {
|
||||
setState(() {});
|
||||
}
|
||||
}
|
||||
|
||||
final currentTab = _getCurrentTab();
|
||||
if (currentTab == null || !currentTab.isLoaded) return;
|
||||
// 确保当前活跃 tab 与当前加载的控制器路径严格一致,杜绝切换过程中误写其他 tab
|
||||
@@ -485,6 +560,15 @@ class _CodeEditorWidgetState extends State<CodeEditorWidget> {
|
||||
currentTab.content = current;
|
||||
currentTab.isModified = isModified;
|
||||
|
||||
// LSP 文档变更增量/全量同步及本地语法诊断兜底
|
||||
if (_currentLoadedPath != null && _controller != null) {
|
||||
_lspCoordinator.onFileChanged(
|
||||
_currentLoadedPath!,
|
||||
current,
|
||||
_controller!.codeLines,
|
||||
);
|
||||
}
|
||||
|
||||
void notifyProvider() {
|
||||
if (!mounted) return;
|
||||
try {
|
||||
@@ -500,6 +584,34 @@ class _CodeEditorWidgetState extends State<CodeEditorWidget> {
|
||||
}
|
||||
}
|
||||
|
||||
/// 依据 LSP 实时诊断结果,为当前行的代码文本施加波浪下划线(保留已有代码高亮色彩)
|
||||
TextSpan _buildDiagnosticSpans({
|
||||
required BuildContext context,
|
||||
required int index,
|
||||
required CodeLine codeLine,
|
||||
required TextSpan textSpan,
|
||||
required TextStyle style,
|
||||
}) {
|
||||
return _diagnosticController.buildDiagnosticSpans(
|
||||
context: context,
|
||||
filePath: _currentLoadedPath ?? widget.filePath,
|
||||
index: index,
|
||||
codeLine: codeLine,
|
||||
textSpan: textSpan,
|
||||
style: style,
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildDiagnosticBanner(BuildContext context, List<LspDiagnostic> diags, int lineIndex) {
|
||||
return _diagnosticController.buildDiagnosticBanner(
|
||||
context: context,
|
||||
filePath: _currentLoadedPath ?? widget.filePath,
|
||||
controller: _controller,
|
||||
diags: diags,
|
||||
lineIndex: lineIndex,
|
||||
);
|
||||
}
|
||||
|
||||
Future<bool> _saveFile({bool showToast = false}) async {
|
||||
final currentPath = widget.filePath;
|
||||
if (currentPath == null || currentPath.isEmpty || _controller == null) {
|
||||
@@ -649,6 +761,7 @@ class _CodeEditorWidgetState extends State<CodeEditorWidget> {
|
||||
AppFontItem activeEditorFont = AppFonts.editorJetBrainsMono;
|
||||
bool showLineNumbers = true;
|
||||
bool pinLineNumbers = true;
|
||||
bool enableLspCompletion = true;
|
||||
try {
|
||||
final settings = _getSettingsProvider(context, listen: true);
|
||||
activeTheme = settings.editorTheme;
|
||||
@@ -659,6 +772,7 @@ class _CodeEditorWidgetState extends State<CodeEditorWidget> {
|
||||
enableVirtualKeyboard = settings.enableVirtualKeyboard;
|
||||
keyboardConfig = settings.virtualKeyboardConfig;
|
||||
activeEditorFont = settings.editorFont;
|
||||
enableLspCompletion = settings.enableLspCompletion;
|
||||
if (_currentIndentSize != null && _currentIndentSize != settings.indentSize) {
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||
if (mounted) _loadFileContent();
|
||||
@@ -683,77 +797,112 @@ class _CodeEditorWidgetState extends State<CodeEditorWidget> {
|
||||
onPointerUp: _handlePointerUp,
|
||||
onPointerCancel: _handlePointerCancel,
|
||||
child: Stack(
|
||||
clipBehavior: Clip.none,
|
||||
children: [
|
||||
IgnorePointer(
|
||||
ignoring: _isPinching,
|
||||
child: CodeEditor(
|
||||
key: ValueKey(_currentLoadedPath),
|
||||
controller: controller,
|
||||
focusNode: _focusNode,
|
||||
scrollController: _scrollController,
|
||||
wordWrap: activeWordWrap,
|
||||
pinLineNumbers: pinLineNumbers,
|
||||
toolbarController: _toolbarController,
|
||||
margin: EdgeInsets.zero,
|
||||
extraHorizontalScroll: 160.0,
|
||||
padding: const EdgeInsets.fromLTRB(6.0, 0.0, 0.0, 0.0),
|
||||
leadingDivider: showLineNumbers
|
||||
? Container(
|
||||
width: 1.0,
|
||||
color: activeTheme.gutterTextColor.withValues(alpha: 0.25),
|
||||
)
|
||||
: null,
|
||||
style: CodeEditorStyle(
|
||||
fontSize: displayFontSize,
|
||||
textColor: activeTheme.textColor,
|
||||
backgroundColor: activeTheme.backgroundColor,
|
||||
cursorColor: activeTheme.cursorColor,
|
||||
cursorLineColor: activeTheme.cursorLineColor,
|
||||
selectionColor: activeTheme.selectionColor,
|
||||
fontFamily: activeEditorFont.fontFamily,
|
||||
fontFamilyFallback: activeEditorFont.fallback,
|
||||
codeTheme: CodeHighlightTheme(
|
||||
languages: SyntaxHighlightHelper.getLanguagesForFile(_currentLoadedPath ?? widget.filePath),
|
||||
theme: activeTheme.highlightTheme,
|
||||
),
|
||||
),
|
||||
indicatorBuilder: showLineNumbers
|
||||
? (context, editingController, chunkController, notifier) {
|
||||
return Row(
|
||||
children: [
|
||||
DefaultCodeLineNumber(
|
||||
controller: editingController,
|
||||
notifier: notifier,
|
||||
textStyle: TextStyle(
|
||||
color: activeTheme.gutterTextColor,
|
||||
fontSize: (displayFontSize - 1).clamp(9.0, 30.0),
|
||||
fontFamily: activeEditorFont.fontFamily,
|
||||
fontFamilyFallback: activeEditorFont.fallback,
|
||||
height: 1.4,
|
||||
),
|
||||
focusedTextStyle: TextStyle(
|
||||
color: activeTheme.focusedGutterTextColor,
|
||||
fontSize: (displayFontSize - 1).clamp(9.0, 30.0),
|
||||
fontWeight: FontWeight.bold,
|
||||
fontFamily: activeEditorFont.fontFamily,
|
||||
fontFamilyFallback: activeEditorFont.fallback,
|
||||
height: 1.4,
|
||||
),
|
||||
),
|
||||
DefaultCodeChunkIndicator(
|
||||
width: 20,
|
||||
controller: chunkController,
|
||||
notifier: notifier,
|
||||
painter: DefaultCodeChunkIndicatorPainter(
|
||||
color: activeTheme.gutterTextColor,
|
||||
),
|
||||
),
|
||||
],
|
||||
child: Builder(
|
||||
builder: (context) {
|
||||
final promptsBuilder = SmartCodeAutocompletePromptsBuilder(
|
||||
controller: controller,
|
||||
filePath: _currentLoadedPath ?? widget.filePath,
|
||||
enableLspCompletion: enableLspCompletion,
|
||||
);
|
||||
return EditorOverlayScope(
|
||||
key: ValueKey(_currentLoadedPath),
|
||||
child: CodeAutocomplete(
|
||||
viewBuilder: (context, notifier, onSelected) {
|
||||
promptsBuilder.activeNotifier = notifier;
|
||||
return CodeAutocompleteView(
|
||||
notifier: notifier,
|
||||
onSelected: onSelected,
|
||||
);
|
||||
}
|
||||
: null,
|
||||
),
|
||||
),
|
||||
},
|
||||
promptsBuilder: promptsBuilder,
|
||||
child: CodeEditor(
|
||||
key: ValueKey(_currentLoadedPath),
|
||||
controller: controller,
|
||||
focusNode: _focusNode,
|
||||
scrollController: _scrollController,
|
||||
wordWrap: activeWordWrap,
|
||||
pinLineNumbers: pinLineNumbers,
|
||||
toolbarController: _toolbarController,
|
||||
margin: EdgeInsets.zero,
|
||||
extraHorizontalScroll: 160.0,
|
||||
padding: const EdgeInsets.fromLTRB(6.0, 0.0, 0.0, 0.0),
|
||||
leadingDivider: showLineNumbers
|
||||
? Container(
|
||||
width: 1.0,
|
||||
color: activeTheme.gutterTextColor.withValues(alpha: 0.25),
|
||||
)
|
||||
: null,
|
||||
style: CodeEditorStyle(
|
||||
fontSize: displayFontSize,
|
||||
textColor: activeTheme.textColor,
|
||||
backgroundColor: activeTheme.backgroundColor,
|
||||
cursorColor: activeTheme.cursorColor,
|
||||
cursorLineColor: activeTheme.cursorLineColor,
|
||||
selectionColor: activeTheme.selectionColor,
|
||||
fontFamily: activeEditorFont.fontFamily,
|
||||
fontFamilyFallback: activeEditorFont.fallback,
|
||||
codeTheme: CodeHighlightTheme(
|
||||
languages: SyntaxHighlightHelper.getLanguagesForFile(_currentLoadedPath ?? widget.filePath),
|
||||
theme: activeTheme.highlightTheme,
|
||||
),
|
||||
),
|
||||
indicatorBuilder: showLineNumbers
|
||||
? (context, editingController, chunkController, notifier) {
|
||||
return Row(
|
||||
children: [
|
||||
DefaultCodeLineNumber(
|
||||
controller: editingController,
|
||||
notifier: notifier,
|
||||
lineDecorationBuilder: (lineIndex) {
|
||||
final filePath = _currentLoadedPath ?? widget.filePath;
|
||||
if (filePath == null) return null;
|
||||
final diags = LspDiagnosticsStore.instance.getDiagnosticsForLine(filePath, lineIndex);
|
||||
if (diags.isEmpty) return null;
|
||||
final hasError = diags.any((d) => d.severity == LspDiagnosticSeverity.error);
|
||||
final hasWarning = diags.any((d) => d.severity == LspDiagnosticSeverity.warning);
|
||||
final dotColor = hasError
|
||||
? Colors.redAccent
|
||||
: (hasWarning ? Colors.amberAccent : Colors.lightBlueAccent);
|
||||
return DiagnosticGutterDotDecoration(color: dotColor);
|
||||
},
|
||||
textStyle: TextStyle(
|
||||
color: activeTheme.gutterTextColor,
|
||||
fontSize: (displayFontSize - 1).clamp(9.0, 30.0),
|
||||
fontFamily: activeEditorFont.fontFamily,
|
||||
fontFamilyFallback: activeEditorFont.fallback,
|
||||
height: 1.4,
|
||||
),
|
||||
focusedTextStyle: TextStyle(
|
||||
color: activeTheme.focusedGutterTextColor,
|
||||
fontSize: (displayFontSize - 1).clamp(9.0, 30.0),
|
||||
fontWeight: FontWeight.bold,
|
||||
fontFamily: activeEditorFont.fontFamily,
|
||||
fontFamilyFallback: activeEditorFont.fallback,
|
||||
height: 1.4,
|
||||
),
|
||||
),
|
||||
DefaultCodeChunkIndicator(
|
||||
width: 20,
|
||||
controller: chunkController,
|
||||
notifier: notifier,
|
||||
painter: DefaultCodeChunkIndicatorPainter(
|
||||
color: activeTheme.gutterTextColor,
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
: null,
|
||||
),
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
// 双指缩放实时字号悬浮胶囊提示
|
||||
if (_isPinching && _activeZoomFontSize != null)
|
||||
Positioned(
|
||||
@@ -794,6 +943,18 @@ class _CodeEditorWidgetState extends State<CodeEditorWidget> {
|
||||
),
|
||||
),
|
||||
),
|
||||
if (_currentCursorLine >= 0 && (_currentLoadedPath ?? widget.filePath) != null) ...[
|
||||
Builder(
|
||||
builder: (ctx) {
|
||||
final diags = LspDiagnosticsStore.instance.getDiagnosticsForLine(
|
||||
_currentLoadedPath ?? widget.filePath,
|
||||
_currentCursorLine,
|
||||
);
|
||||
if (diags.isEmpty) return const SizedBox.shrink();
|
||||
return _buildDiagnosticBanner(ctx, diags, _currentCursorLine);
|
||||
},
|
||||
),
|
||||
],
|
||||
if (enableVirtualKeyboard && keyboardConfig != null && keyboardConfig.hasKeys)
|
||||
VirtualKeyboardWidget(
|
||||
controller: controller,
|
||||
@@ -866,6 +1027,7 @@ class _CodeEditorWidgetState extends State<CodeEditorWidget> {
|
||||
final newController = CodeLineEditingController.fromText(
|
||||
normalizedContent,
|
||||
CodeLineOptions(indentSize: activeIndentSize),
|
||||
_buildDiagnosticSpans,
|
||||
);
|
||||
|
||||
_controller = newController;
|
||||
@@ -874,6 +1036,13 @@ class _CodeEditorWidgetState extends State<CodeEditorWidget> {
|
||||
_errorMessage = null;
|
||||
_isLoading = false;
|
||||
|
||||
_lspCoordinator.onFileOpened(
|
||||
fullFilePath,
|
||||
normalizedContent,
|
||||
newController.codeLines,
|
||||
workspaceRoot: widget.rootPath,
|
||||
);
|
||||
|
||||
// 必须在 _controller 和 tab 状态准备就绪后再挂载文本监听,避免初始化阶段被误判为脏
|
||||
newController.addListener(_onTextChanged);
|
||||
|
||||
@@ -917,6 +1086,7 @@ class _CodeEditorWidgetState extends State<CodeEditorWidget> {
|
||||
final newController = CodeLineEditingController.fromText(
|
||||
diskContent,
|
||||
CodeLineOptions(indentSize: activeIndentSize),
|
||||
_buildDiagnosticSpans,
|
||||
);
|
||||
|
||||
_controller = newController;
|
||||
@@ -924,6 +1094,13 @@ class _CodeEditorWidgetState extends State<CodeEditorWidget> {
|
||||
_currentIndentSize = activeIndentSize;
|
||||
_errorMessage = null;
|
||||
|
||||
_lspCoordinator.onFileOpened(
|
||||
fullFilePath,
|
||||
diskContent,
|
||||
newController.codeLines,
|
||||
workspaceRoot: widget.rootPath,
|
||||
);
|
||||
|
||||
if (tab != null) {
|
||||
tab.content = diskContent;
|
||||
tab.originalContent = diskContent;
|
||||
|
||||
@@ -1,380 +0,0 @@
|
||||
import 'dart:io';
|
||||
import 'package:code_editor/l10n/app_localizations.dart';
|
||||
import 'package:code_editor/providers/distro_provider.dart';
|
||||
import 'package:code_editor/providers/terminal_provider.dart';
|
||||
import 'package:code_editor/models/distro_manifest.dart';
|
||||
import 'package:code_editor/services/distro_download_manager.dart';
|
||||
import 'package:code_editor/utils/dialog_utils.dart';
|
||||
import 'package:code_editor/views/distro_management_view.dart';
|
||||
import 'package:code_editor/widgets/distro_extract_dialog.dart';
|
||||
import 'package:file_picker/file_picker.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:path/path.dart' as p;
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
/// 独立的 Linux 系统选择与管理对话框
|
||||
class DistroSelectorDialog extends StatelessWidget {
|
||||
const DistroSelectorDialog({super.key});
|
||||
|
||||
/// 打开系统选择与管理对话框
|
||||
static Future<void> show(BuildContext context) {
|
||||
return showDialog<void>(
|
||||
context: context,
|
||||
builder: (ctx) => const DistroSelectorDialog(),
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final theme = Theme.of(context);
|
||||
final l10n = AppLocalizations.of(context)!;
|
||||
final distroProvider = context.watch<DistroProvider>();
|
||||
final systems = distroProvider.installedSystems;
|
||||
final selectedSystem = distroProvider.selectedSystem;
|
||||
|
||||
return AlertDialog(
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.circular(16.0),
|
||||
),
|
||||
titlePadding: const EdgeInsets.fromLTRB(20.0, 16.0, 12.0, 8.0),
|
||||
contentPadding: const EdgeInsets.symmetric(horizontal: 16.0, vertical: 8.0),
|
||||
title: Row(
|
||||
children: [
|
||||
const Icon(Icons.dns_outlined, size: 22, color: Colors.blueAccent),
|
||||
const SizedBox(width: 8),
|
||||
Expanded(
|
||||
child: Text(
|
||||
l10n.systemManagement,
|
||||
style: const TextStyle(fontSize: 17.0, fontWeight: FontWeight.bold),
|
||||
),
|
||||
),
|
||||
// 导入按钮与下拉选项
|
||||
PopupMenuButton<String>(
|
||||
icon: const Icon(Icons.add_circle_outline, color: Colors.blueAccent),
|
||||
tooltip: l10n.importNewSystem,
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.circular(12.0),
|
||||
),
|
||||
onSelected: (action) => _handleImportAction(context, action),
|
||||
itemBuilder: (context) => [
|
||||
PopupMenuItem(
|
||||
value: 'management_view',
|
||||
child: Row(
|
||||
children: [
|
||||
const Icon(Icons.apps_rounded, size: 18),
|
||||
const SizedBox(width: 8),
|
||||
Flexible(
|
||||
child: Text(
|
||||
l10n.importFromApp,
|
||||
maxLines: 1,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
PopupMenuItem(
|
||||
value: 'external',
|
||||
child: Row(
|
||||
children: [
|
||||
const Icon(Icons.folder_open_outlined, size: 18),
|
||||
const SizedBox(width: 8),
|
||||
Flexible(
|
||||
child: Text(
|
||||
l10n.importExternalTarGz,
|
||||
maxLines: 1,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
content: SizedBox(
|
||||
width: double.maxFinite,
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(bottom: 8.0),
|
||||
child: Text(
|
||||
l10n.selectSystemDefaultPrompt,
|
||||
style: TextStyle(
|
||||
fontSize: 12.0,
|
||||
color: theme.colorScheme.onSurfaceVariant,
|
||||
),
|
||||
),
|
||||
),
|
||||
const Divider(height: 1),
|
||||
Flexible(
|
||||
child: systems.isEmpty
|
||||
? Container(
|
||||
padding: const EdgeInsets.symmetric(vertical: 32.0),
|
||||
alignment: Alignment.center,
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Icon(
|
||||
Icons.layers_clear_outlined,
|
||||
size: 40,
|
||||
color: theme.colorScheme.onSurfaceVariant.withValues(alpha: 0.5),
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
Text(
|
||||
l10n.noSystemsPrompt,
|
||||
style: TextStyle(
|
||||
fontSize: 13.0,
|
||||
color: theme.colorScheme.onSurfaceVariant,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
)
|
||||
: ListView.builder(
|
||||
shrinkWrap: true,
|
||||
padding: const EdgeInsets.symmetric(vertical: 4.0),
|
||||
itemCount: systems.length,
|
||||
itemBuilder: (context, index) {
|
||||
final system = systems[index];
|
||||
final isSelected = system == selectedSystem;
|
||||
|
||||
return Container(
|
||||
margin: const EdgeInsets.symmetric(vertical: 3.0),
|
||||
decoration: BoxDecoration(
|
||||
borderRadius: BorderRadius.circular(8.0),
|
||||
border: Border.all(
|
||||
color: isSelected
|
||||
? theme.colorScheme.primary.withValues(alpha: 0.5)
|
||||
: theme.colorScheme.outlineVariant.withValues(alpha: 0.4),
|
||||
width: 1.0,
|
||||
),
|
||||
),
|
||||
child: Material(
|
||||
color: isSelected
|
||||
? theme.colorScheme.primaryContainer.withValues(alpha: 0.4)
|
||||
: Colors.transparent,
|
||||
borderRadius: BorderRadius.circular(8.0),
|
||||
child: ListTile(
|
||||
dense: true,
|
||||
contentPadding: const EdgeInsets.symmetric(horizontal: 16.0, vertical: 2.0),
|
||||
title: Text(
|
||||
system,
|
||||
style: TextStyle(
|
||||
fontWeight: isSelected ? FontWeight.bold : FontWeight.normal,
|
||||
color: isSelected ? theme.colorScheme.primary : null,
|
||||
fontSize: 14.0,
|
||||
),
|
||||
),
|
||||
trailing: IconButton(
|
||||
icon: Icon(
|
||||
Icons.delete_outline,
|
||||
color: theme.colorScheme.error,
|
||||
size: 20,
|
||||
),
|
||||
tooltip: l10n.deleteSystemTooltip,
|
||||
onPressed: () => _handleDeleteSystem(context, system),
|
||||
),
|
||||
onTap: () {
|
||||
distroProvider.selectSystem(system);
|
||||
},
|
||||
),
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () => Navigator.of(context).pop(),
|
||||
child: Text(l10n.done),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
/// 处理导入菜单操作
|
||||
bool _isValidDistroArchive(String path) {
|
||||
final lower = path.toLowerCase();
|
||||
return lower.endsWith('.tar.gz') ||
|
||||
lower.endsWith('.tgz') ||
|
||||
lower.endsWith('.tar.xz') ||
|
||||
lower.endsWith('.txz') ||
|
||||
lower.endsWith('.tar');
|
||||
}
|
||||
|
||||
Future<void> _handleImportAction(BuildContext context, String action) async {
|
||||
final distroProvider = context.read<DistroProvider>();
|
||||
final l10n = AppLocalizations.of(context)!;
|
||||
|
||||
if (action == 'management_view') {
|
||||
// 1. 跳转到应用内系统管理页面
|
||||
final selectedItem = await Navigator.of(context).push<DistroManifestItem>(
|
||||
MaterialPageRoute(builder: (_) => const DistroManagementView()),
|
||||
);
|
||||
|
||||
if (selectedItem != null && context.mounted) {
|
||||
// 用户直接点击已经安装的 item 本身,代表用户选择了那个 zip,返回后解压创建系统
|
||||
await _handleCreateSystemFromManifest(context, selectedItem);
|
||||
}
|
||||
} else if (action == 'external') {
|
||||
// 2. 从外部选择 .tar.gz / .tar.xz 压缩包导入
|
||||
final result = await FilePicker.platform.pickFiles(
|
||||
type: FileType.custom,
|
||||
allowedExtensions: const ['tar', 'gz', 'tgz', 'xz', 'txz'],
|
||||
);
|
||||
|
||||
if (result == null || result.files.single.path == null || !context.mounted) return;
|
||||
final filePath = result.files.single.path!;
|
||||
|
||||
if (!_isValidDistroArchive(filePath)) {
|
||||
if (context.mounted) {
|
||||
DialogUtils.showErrorToast(context, l10n.unsupportedDistroArchiveFormat);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
final file = File(filePath);
|
||||
|
||||
// 提取默认名称(去掉 .tar.gz, .tgz 等后缀)
|
||||
String baseName = p.basename(filePath);
|
||||
if (baseName.endsWith('.tar.gz')) {
|
||||
baseName = baseName.substring(0, baseName.length - 7);
|
||||
} else if (baseName.endsWith('.tgz')) {
|
||||
baseName = baseName.substring(0, baseName.length - 4);
|
||||
} else {
|
||||
baseName = p.basenameWithoutExtension(filePath);
|
||||
}
|
||||
baseName = baseName.replaceAll(RegExp(r'[^a-zA-Z0-9_\-]'), '_');
|
||||
if (baseName.isEmpty) baseName = 'custom_linux';
|
||||
|
||||
final defaultName = _generateUniqueSystemName(baseName, distroProvider.installedSystems);
|
||||
|
||||
final systemName = await DialogUtils.showInputDialog(
|
||||
context,
|
||||
title: l10n.importExternalSystemTitle,
|
||||
hintText: l10n.systemNameHint,
|
||||
initialValue: defaultName,
|
||||
);
|
||||
|
||||
if (systemName == null || systemName.trim().isEmpty || !context.mounted) return;
|
||||
final cleanName = systemName.trim();
|
||||
|
||||
// 模态同步解压
|
||||
final success = await DistroExtractDialog.show(
|
||||
context: context,
|
||||
systemName: cleanName,
|
||||
task: (onProgress, isCancelled) {
|
||||
return distroProvider.importFromCustomTarGz(
|
||||
systemName: cleanName,
|
||||
tarGzFile: file,
|
||||
onProgress: onProgress,
|
||||
isCancelled: isCancelled,
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
if (success && context.mounted) {
|
||||
DialogUtils.showSuccessToast(context, l10n.externalSystemImportSuccess(cleanName));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// 从选中的发行版安装包解压创建新的系统实例
|
||||
Future<void> _handleCreateSystemFromManifest(BuildContext context, DistroManifestItem item) async {
|
||||
final distroProvider = context.read<DistroProvider>();
|
||||
final l10n = AppLocalizations.of(context)!;
|
||||
|
||||
final defaultName = _generateUniqueSystemName(item.id, distroProvider.installedSystems);
|
||||
final systemName = await DialogUtils.showInputDialog(
|
||||
context,
|
||||
title: l10n.importSystemInstanceTitle(item.name),
|
||||
hintText: l10n.systemNameHintWithDefault(defaultName),
|
||||
initialValue: defaultName,
|
||||
);
|
||||
|
||||
if (systemName == null || systemName.trim().isEmpty || !context.mounted) return;
|
||||
final cleanName = systemName.trim();
|
||||
|
||||
// 模态同步解压
|
||||
final success = await DistroExtractDialog.show(
|
||||
context: context,
|
||||
systemName: cleanName,
|
||||
task: (onProgress, isCancelled) async {
|
||||
if (item.id == 'ubuntu') {
|
||||
return distroProvider.importBuiltinUbuntu(
|
||||
systemName: cleanName,
|
||||
onProgress: onProgress,
|
||||
isCancelled: isCancelled,
|
||||
);
|
||||
} else if (item.id == 'alpine') {
|
||||
return distroProvider.importBuiltinAlpine(
|
||||
systemName: cleanName,
|
||||
onProgress: onProgress,
|
||||
isCancelled: isCancelled,
|
||||
);
|
||||
} else {
|
||||
final file = await DistroDownloadManager().getPackageFile(item.id, item.currentPackageSource!);
|
||||
return distroProvider.importFromCustomTarGz(
|
||||
systemName: cleanName,
|
||||
tarGzFile: file,
|
||||
onProgress: onProgress,
|
||||
isCancelled: isCancelled,
|
||||
);
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
if (success && context.mounted) {
|
||||
DialogUtils.showSuccessToast(context, l10n.systemImportSuccess(cleanName));
|
||||
await distroProvider.refreshSystems();
|
||||
await distroProvider.selectSystem(cleanName);
|
||||
}
|
||||
}
|
||||
|
||||
/// 高危操作:二次确认删除系统
|
||||
Future<void> _handleDeleteSystem(BuildContext context, String systemName) async {
|
||||
final l10n = AppLocalizations.of(context)!;
|
||||
final confirmed = await DialogUtils.showDestructiveConfirmDialog(
|
||||
context,
|
||||
title: l10n.deleteSystemConfirmTitle,
|
||||
message: l10n.deleteSystemConfirmMessage(systemName),
|
||||
confirmText: l10n.permanentDelete,
|
||||
);
|
||||
|
||||
if (confirmed && context.mounted) {
|
||||
final distroProvider = context.read<DistroProvider>();
|
||||
final terminalProvider = context.read<TerminalProvider?>();
|
||||
try {
|
||||
await distroProvider.deleteSystem(systemName);
|
||||
terminalProvider?.removeSessionsForDistro(systemName);
|
||||
if (!distroProvider.hasAnySystem) {
|
||||
terminalProvider?.clearAllSessions();
|
||||
}
|
||||
if (context.mounted) {
|
||||
DialogUtils.showToast(context, l10n.deleteSystemSuccess(systemName));
|
||||
}
|
||||
} catch (e) {
|
||||
if (context.mounted) {
|
||||
DialogUtils.showErrorToast(context, l10n.deleteSystemFailed(e.toString()));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// 自动生成不重复的系统实例名
|
||||
String _generateUniqueSystemName(String prefix, List<String> existingSystems) {
|
||||
if (!existingSystems.contains(prefix)) return prefix;
|
||||
int index = 1;
|
||||
while (existingSystems.contains('${prefix}_$index')) {
|
||||
index++;
|
||||
}
|
||||
return '${prefix}_$index';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,266 @@
|
||||
import 'dart:math';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:re_editor/re_editor.dart';
|
||||
import '../../services/lsp/lsp_diagnostics_store.dart';
|
||||
import '../../services/lsp/lsp_protocol.dart';
|
||||
import '../lsp_quick_fix_dialog.dart';
|
||||
|
||||
class _DiagnosticInterval {
|
||||
final int start;
|
||||
final int end;
|
||||
final Color color;
|
||||
|
||||
const _DiagnosticInterval(this.start, this.end, this.color);
|
||||
}
|
||||
|
||||
class _OffsetTracker {
|
||||
int value;
|
||||
_OffsetTracker(this.value);
|
||||
}
|
||||
|
||||
/// 编辑器代码诊断视图控制器
|
||||
///
|
||||
/// 专职负责:
|
||||
/// 1. 依据 LSP 与本地语法诊断,计算行内波浪下划线(保持语法高亮颜色);
|
||||
/// 2. 构建底部错误与警告横幅,支持一键唤起 LSP 快捷修复。
|
||||
class EditorDiagnosticController {
|
||||
const EditorDiagnosticController();
|
||||
|
||||
/// 依据实时诊断结果,为当前行的代码文本施加波浪下划线(保留已有代码高亮色彩)
|
||||
TextSpan buildDiagnosticSpans({
|
||||
required BuildContext context,
|
||||
required String? filePath,
|
||||
required int index,
|
||||
required CodeLine codeLine,
|
||||
required TextSpan textSpan,
|
||||
required TextStyle style,
|
||||
}) {
|
||||
if (filePath == null || codeLine.text.isEmpty) {
|
||||
return textSpan;
|
||||
}
|
||||
|
||||
final diagnostics = LspDiagnosticsStore.instance.getDiagnosticsForLine(filePath, index);
|
||||
if (diagnostics.isEmpty) {
|
||||
return textSpan;
|
||||
}
|
||||
|
||||
final lineText = codeLine.text;
|
||||
final lineLen = lineText.length;
|
||||
|
||||
final intervals = <_DiagnosticInterval>[];
|
||||
for (final d in diagnostics) {
|
||||
final startLine = d.range.start.line;
|
||||
final endLine = d.range.end.line;
|
||||
|
||||
int startCol = 0;
|
||||
if (startLine == index) {
|
||||
startCol = d.range.start.character;
|
||||
} else if (startLine > index) {
|
||||
continue;
|
||||
}
|
||||
|
||||
int endCol = lineLen;
|
||||
if (endLine == index) {
|
||||
endCol = d.range.end.character;
|
||||
} else if (endLine < index) {
|
||||
continue;
|
||||
}
|
||||
|
||||
startCol = startCol.clamp(0, lineLen);
|
||||
endCol = endCol.clamp(0, lineLen);
|
||||
|
||||
// LSP 有时可能返回单点(0 宽)区间,自动扩宽至至少 1 字符高亮以便视觉可见
|
||||
if (startCol == endCol) {
|
||||
if (startCol < lineLen) {
|
||||
endCol = min(startCol + 1, lineLen);
|
||||
} else if (startCol > 0) {
|
||||
startCol = startCol - 1;
|
||||
}
|
||||
}
|
||||
|
||||
if (startCol < endCol) {
|
||||
Color diagColor = Colors.redAccent;
|
||||
switch (d.severity) {
|
||||
case LspDiagnosticSeverity.error:
|
||||
diagColor = Colors.redAccent;
|
||||
break;
|
||||
case LspDiagnosticSeverity.warning:
|
||||
diagColor = Colors.amberAccent;
|
||||
break;
|
||||
case LspDiagnosticSeverity.information:
|
||||
diagColor = Colors.lightBlueAccent;
|
||||
break;
|
||||
case LspDiagnosticSeverity.hint:
|
||||
diagColor = Colors.grey;
|
||||
break;
|
||||
}
|
||||
intervals.add(_DiagnosticInterval(startCol, endCol, diagColor));
|
||||
}
|
||||
}
|
||||
|
||||
if (intervals.isEmpty) {
|
||||
return textSpan;
|
||||
}
|
||||
|
||||
return _applyDiagnosticIntervals(textSpan, intervals, _OffsetTracker(0), style);
|
||||
}
|
||||
|
||||
/// 构建底部单行诊断简报与快捷修复入口横幅
|
||||
Widget buildDiagnosticBanner({
|
||||
required BuildContext context,
|
||||
required String? filePath,
|
||||
required CodeLineEditingController? controller,
|
||||
required List<LspDiagnostic> diags,
|
||||
required int lineIndex,
|
||||
}) {
|
||||
final first = diags.first;
|
||||
final isErr = diags.any((d) => d.severity == LspDiagnosticSeverity.error);
|
||||
final color = isErr ? Colors.redAccent : Colors.amber;
|
||||
final icon = isErr ? Icons.error_outline : Icons.warning_amber_outlined;
|
||||
|
||||
return Material(
|
||||
color: Theme.of(context).colorScheme.surfaceContainerHighest,
|
||||
elevation: 2,
|
||||
child: InkWell(
|
||||
onTap: () {
|
||||
if (filePath != null && controller != null) {
|
||||
LspQuickFixDialog.show(
|
||||
context,
|
||||
filePath: filePath,
|
||||
lineIndex: lineIndex,
|
||||
diagnostics: diags,
|
||||
controller: controller,
|
||||
);
|
||||
}
|
||||
},
|
||||
child: Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 6),
|
||||
decoration: BoxDecoration(
|
||||
border: Border(
|
||||
top: BorderSide(color: color.withValues(alpha: 0.6), width: 1.5),
|
||||
),
|
||||
),
|
||||
child: Row(
|
||||
children: [
|
||||
Icon(icon, size: 16, color: color),
|
||||
const SizedBox(width: 8),
|
||||
Expanded(
|
||||
child: Text(
|
||||
'行 ${lineIndex + 1}: ${first.message}',
|
||||
maxLines: 1,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
style: TextStyle(
|
||||
fontSize: 12,
|
||||
color: Theme.of(context).colorScheme.onSurface,
|
||||
),
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 8),
|
||||
Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 6, vertical: 2),
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.amber.withValues(alpha: 0.2),
|
||||
borderRadius: BorderRadius.circular(4),
|
||||
),
|
||||
child: Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: const [
|
||||
Icon(Icons.lightbulb, size: 12, color: Colors.amber),
|
||||
SizedBox(width: 2),
|
||||
Text(
|
||||
'修复',
|
||||
style: TextStyle(fontSize: 11, fontWeight: FontWeight.w600, color: Colors.amber),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
static TextSpan _applyDiagnosticIntervals(
|
||||
TextSpan span,
|
||||
List<_DiagnosticInterval> intervals,
|
||||
_OffsetTracker tracker,
|
||||
TextStyle baseStyle,
|
||||
) {
|
||||
final effectiveStyle = span.style ?? baseStyle;
|
||||
|
||||
if (span.children != null && span.children!.isNotEmpty) {
|
||||
final newChildren = <InlineSpan>[];
|
||||
if (span.text != null && span.text!.isNotEmpty) {
|
||||
final leafSpan = TextSpan(text: span.text, style: effectiveStyle);
|
||||
newChildren.add(_processLeafTextSpan(leafSpan, intervals, tracker, effectiveStyle));
|
||||
}
|
||||
for (final child in span.children!) {
|
||||
if (child is TextSpan) {
|
||||
newChildren.add(_applyDiagnosticIntervals(child, intervals, tracker, effectiveStyle));
|
||||
} else {
|
||||
newChildren.add(child);
|
||||
}
|
||||
}
|
||||
return TextSpan(children: newChildren, style: span.style);
|
||||
}
|
||||
|
||||
return _processLeafTextSpan(span, intervals, tracker, effectiveStyle);
|
||||
}
|
||||
|
||||
static TextSpan _processLeafTextSpan(
|
||||
TextSpan span,
|
||||
List<_DiagnosticInterval> intervals,
|
||||
_OffsetTracker tracker,
|
||||
TextStyle baseStyle,
|
||||
) {
|
||||
final text = span.text;
|
||||
if (text == null || text.isEmpty) {
|
||||
return span;
|
||||
}
|
||||
|
||||
final startOffset = tracker.value;
|
||||
final endOffset = startOffset + text.length;
|
||||
tracker.value = endOffset;
|
||||
|
||||
final intersecting = intervals.where((it) => it.end > startOffset && it.start < endOffset).toList();
|
||||
if (intersecting.isEmpty) {
|
||||
return span;
|
||||
}
|
||||
|
||||
final splitPoints = <int>{startOffset, endOffset};
|
||||
for (final it in intersecting) {
|
||||
if (it.start > startOffset && it.start < endOffset) splitPoints.add(it.start);
|
||||
if (it.end > startOffset && it.end < endOffset) splitPoints.add(it.end);
|
||||
}
|
||||
final sortedPoints = splitPoints.toList()..sort();
|
||||
|
||||
final chunks = <InlineSpan>[];
|
||||
for (int i = 0; i < sortedPoints.length - 1; i++) {
|
||||
final segStart = sortedPoints[i];
|
||||
final segEnd = sortedPoints[i + 1];
|
||||
if (segStart >= segEnd) continue;
|
||||
|
||||
final chunkText = text.substring(segStart - startOffset, segEnd - startOffset);
|
||||
final activeInterval = intersecting.where((it) => it.start <= segStart && it.end >= segEnd).firstOrNull;
|
||||
|
||||
if (activeInterval != null) {
|
||||
chunks.add(TextSpan(
|
||||
text: chunkText,
|
||||
style: (span.style ?? baseStyle).copyWith(
|
||||
decoration: TextDecoration.underline,
|
||||
decorationStyle: TextDecorationStyle.wavy,
|
||||
decorationColor: activeInterval.color,
|
||||
),
|
||||
));
|
||||
} else {
|
||||
chunks.add(TextSpan(text: chunkText, style: span.style));
|
||||
}
|
||||
}
|
||||
|
||||
if (chunks.length == 1 && chunks.first is TextSpan) {
|
||||
return chunks.first as TextSpan;
|
||||
}
|
||||
return TextSpan(children: chunks, style: span.style);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
import 'package:re_editor/re_editor.dart';
|
||||
import '../../services/lsp/lsp_diagnostics_store.dart';
|
||||
import '../../services/lsp/lsp_manager.dart';
|
||||
|
||||
/// 编辑器 LSP 生命周期协调器
|
||||
///
|
||||
/// 负责:
|
||||
/// 1. 统一管理文档打开、变更、关闭与 LSP 语言服务器进程的同步;
|
||||
/// 2. 文档关闭时及时清理诊断缓存;
|
||||
/// 3. 所有语法纠错与诊断均严格来自真实 LSP 进程的 push 通知,不进行任何本地模糊伪检查。
|
||||
class EditorLspCoordinator {
|
||||
const EditorLspCoordinator();
|
||||
|
||||
/// 文档打开时同步至 LSP
|
||||
void onFileOpened(
|
||||
String filePath,
|
||||
String content,
|
||||
CodeLines codeLines, {
|
||||
String? workspaceRoot,
|
||||
}) {
|
||||
LspManager.instance.onFileOpened(
|
||||
filePath,
|
||||
content,
|
||||
workspaceRoot: workspaceRoot,
|
||||
);
|
||||
}
|
||||
|
||||
/// 文档文本内容变更时同步至 LSP 进程
|
||||
void onFileChanged(
|
||||
String filePath,
|
||||
String content,
|
||||
CodeLines codeLines,
|
||||
) {
|
||||
final session = LspManager.instance.getExistingSession(filePath);
|
||||
if (session != null && session.isInitialized) {
|
||||
LspManager.instance.onFileChanged(filePath, content);
|
||||
}
|
||||
}
|
||||
|
||||
/// 文档关闭时清理会话与诊断
|
||||
void onFileClosed(String filePath) {
|
||||
LspManager.instance.onFileClosed(filePath);
|
||||
LspDiagnosticsStore.instance.removeForFile(filePath);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
import 'package:flutter/widgets.dart';
|
||||
|
||||
/// 编辑器专属局部 Overlay 作用域
|
||||
///
|
||||
/// 核心职责:
|
||||
/// 1. 为代码补全弹窗(CodeAutocomplete)提供局部 Overlay 承载容器;
|
||||
/// 2. 控制弹窗的堆叠层级(Z-Order):使其位于编辑器内容之上,但在编辑区底栏(如虚拟小键盘 VirtualKeyboardWidget、诊断横幅)之下;
|
||||
/// 3. 当父级状态变更时,自动刷新内部根 OverlayEntry,确保代码内容与控制器无缝响应。
|
||||
class EditorOverlayScope extends StatefulWidget {
|
||||
final Widget child;
|
||||
final Clip clipBehavior;
|
||||
|
||||
const EditorOverlayScope({
|
||||
super.key,
|
||||
required this.child,
|
||||
this.clipBehavior = Clip.none,
|
||||
});
|
||||
|
||||
@override
|
||||
State<EditorOverlayScope> createState() => _EditorOverlayScopeState();
|
||||
}
|
||||
|
||||
class _EditorOverlayScopeState extends State<EditorOverlayScope> {
|
||||
late final OverlayEntry _entry;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_entry = OverlayEntry(
|
||||
builder: (context) => widget.child,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
void didUpdateWidget(covariant EditorOverlayScope oldWidget) {
|
||||
super.didUpdateWidget(oldWidget);
|
||||
_entry.markNeedsBuild();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Overlay(
|
||||
clipBehavior: widget.clipBehavior,
|
||||
initialEntries: [_entry],
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -34,7 +34,7 @@ class _LspLanguageEditDialogState extends State<LspLanguageEditDialog> {
|
||||
late final TextEditingController _extensionsController;
|
||||
late final TextEditingController _commandController;
|
||||
late final TextEditingController _argsController;
|
||||
late final TextEditingController _apkPackageController;
|
||||
late final TextEditingController _packageController;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
@@ -49,7 +49,7 @@ class _LspLanguageEditDialogState extends State<LspLanguageEditDialog> {
|
||||
_argsController = TextEditingController(
|
||||
text: init?.serverArgs.join(' ') ?? '',
|
||||
);
|
||||
_apkPackageController = TextEditingController(text: init?.apkPackage ?? '');
|
||||
_packageController = TextEditingController(text: init?.package ?? '');
|
||||
}
|
||||
|
||||
@override
|
||||
@@ -59,7 +59,7 @@ class _LspLanguageEditDialogState extends State<LspLanguageEditDialog> {
|
||||
_extensionsController.dispose();
|
||||
_commandController.dispose();
|
||||
_argsController.dispose();
|
||||
_apkPackageController.dispose();
|
||||
_packageController.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@@ -101,7 +101,7 @@ class _LspLanguageEditDialogState extends State<LspLanguageEditDialog> {
|
||||
fileExtensions: cleanedExtensions,
|
||||
serverCommand: _commandController.text.trim(),
|
||||
serverArgs: cleanedArgs,
|
||||
apkPackage: _apkPackageController.text.trim(),
|
||||
package: _packageController.text.trim(),
|
||||
enabled: widget.initialConfig?.enabled ?? true,
|
||||
);
|
||||
|
||||
@@ -175,7 +175,7 @@ class _LspLanguageEditDialogState extends State<LspLanguageEditDialog> {
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
TextFormField(
|
||||
controller: _apkPackageController,
|
||||
controller: _packageController,
|
||||
decoration: InputDecoration(
|
||||
labelText: l10n.apkPackageField,
|
||||
hintText: l10n.apkPackageFieldHint,
|
||||
|
||||
@@ -0,0 +1,153 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:re_editor/re_editor.dart';
|
||||
import '../services/lsp/lsp_manager.dart';
|
||||
import '../services/lsp/lsp_protocol.dart';
|
||||
import '../services/lsp/lsp_workspace_edit_applier.dart';
|
||||
|
||||
/// 快速修复操作弹窗(展示诊断信息及 LSP CodeAction 选项)
|
||||
class LspQuickFixDialog {
|
||||
static Future<void> show(
|
||||
BuildContext context, {
|
||||
required String filePath,
|
||||
required int lineIndex,
|
||||
required List<LspDiagnostic> diagnostics,
|
||||
required CodeLineEditingController controller,
|
||||
VoidCallback? onDismiss,
|
||||
}) async {
|
||||
final session = LspManager.instance.getExistingSession(filePath);
|
||||
List<LspCodeAction> actions = [];
|
||||
|
||||
if (session != null && diagnostics.isNotEmpty && lineIndex < controller.lineCount) {
|
||||
final lineLength = controller.codeLines[lineIndex].text.length;
|
||||
final range = LspRange(
|
||||
start: LspPosition.pos(lineIndex, 0),
|
||||
end: LspPosition.pos(lineIndex, lineLength),
|
||||
);
|
||||
try {
|
||||
actions = await session.getCodeActions(filePath, range, diagnostics);
|
||||
} catch (_) {}
|
||||
}
|
||||
|
||||
if (!context.mounted) return;
|
||||
|
||||
await showModalBottomSheet<void>(
|
||||
context: context,
|
||||
isScrollControlled: true,
|
||||
backgroundColor: Theme.of(context).colorScheme.surface,
|
||||
shape: const RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.vertical(top: Radius.circular(16)),
|
||||
),
|
||||
builder: (ctx) {
|
||||
return SafeArea(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Center(
|
||||
child: Container(
|
||||
width: 36,
|
||||
height: 4,
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.grey.withValues(alpha: 0.4),
|
||||
borderRadius: BorderRadius.circular(2),
|
||||
),
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
Row(
|
||||
children: [
|
||||
const Icon(Icons.lightbulb, color: Colors.amber, size: 20),
|
||||
const SizedBox(width: 8),
|
||||
Text(
|
||||
'代码问题与修复 (第 ${lineIndex + 1} 行)',
|
||||
style: const TextStyle(fontWeight: FontWeight.bold, fontSize: 16),
|
||||
),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
...diagnostics.map((d) {
|
||||
final isErr = d.severity == LspDiagnosticSeverity.error;
|
||||
return Container(
|
||||
margin: const EdgeInsets.only(bottom: 8),
|
||||
padding: const EdgeInsets.all(10),
|
||||
decoration: BoxDecoration(
|
||||
color: (isErr ? Colors.redAccent : Colors.amberAccent).withValues(alpha: 0.1),
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
border: Border.all(
|
||||
color: (isErr ? Colors.redAccent : Colors.amberAccent).withValues(alpha: 0.3),
|
||||
),
|
||||
),
|
||||
child: Row(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Icon(
|
||||
isErr ? Icons.error_outline : Icons.warning_amber_outlined,
|
||||
size: 18,
|
||||
color: isErr ? Colors.redAccent : Colors.amber,
|
||||
),
|
||||
const SizedBox(width: 8),
|
||||
Expanded(
|
||||
child: Text(
|
||||
d.message,
|
||||
style: TextStyle(
|
||||
fontSize: 13,
|
||||
color: Theme.of(context).colorScheme.onSurface,
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}),
|
||||
const Divider(height: 16),
|
||||
if (actions.isEmpty)
|
||||
Padding(
|
||||
padding: const EdgeInsets.symmetric(vertical: 12),
|
||||
child: Row(
|
||||
children: [
|
||||
Icon(
|
||||
Icons.info_outline,
|
||||
size: 16,
|
||||
color: Theme.of(context).colorScheme.onSurfaceVariant,
|
||||
),
|
||||
const SizedBox(width: 8),
|
||||
Text(
|
||||
'当前报错未提供自动修复动作',
|
||||
style: TextStyle(
|
||||
fontSize: 13,
|
||||
color: Theme.of(context).colorScheme.onSurfaceVariant,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
)
|
||||
else
|
||||
...actions.map((act) {
|
||||
return ListTile(
|
||||
contentPadding: EdgeInsets.zero,
|
||||
leading: const Icon(Icons.auto_fix_high, color: Colors.blueAccent),
|
||||
title: Text(act.title, style: const TextStyle(fontSize: 14)),
|
||||
trailing: act.isPreferred ? const Chip(label: Text('推荐', style: TextStyle(fontSize: 11))) : null,
|
||||
onTap: () {
|
||||
Navigator.pop(ctx);
|
||||
if (act.edit != null) {
|
||||
LspWorkspaceEditApplier.applyWorkspaceEdit(
|
||||
controller,
|
||||
act.edit!,
|
||||
currentFilePath: filePath,
|
||||
);
|
||||
}
|
||||
onDismiss?.call();
|
||||
},
|
||||
);
|
||||
}),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user