-
Notifications
You must be signed in to change notification settings - Fork 96
Expand file tree
/
Copy pathgutter.dart
More file actions
171 lines (144 loc) · 4.95 KB
/
gutter.dart
File metadata and controls
171 lines (144 loc) · 4.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
// TODO(alexeyinkin): Remove when dropping support for Flutter < 3.10, https://github.com/akvelon/flutter-code-editor/issues/245
// ignore_for_file: unnecessary_non_null_assertion
import 'package:flutter/material.dart';
import '../code_field/code_controller.dart';
import '../line_numbers/gutter_style.dart';
import 'error.dart';
import 'fold_toggle.dart';
const _issueColumnWidth = 16.0;
const _foldingColumnWidth = 16.0;
const _lineNumberColumn = 0;
const _issueColumn = 1;
const _foldingColumn = 2;
class GutterWidget extends StatelessWidget {
const GutterWidget({
required this.codeController,
required this.style,
required this.scrollController,
});
final CodeController codeController;
final GutterStyle style;
final ScrollController? scrollController;
@override
Widget build(BuildContext context) {
return SingleChildScrollView(
controller: scrollController,
child: AnimatedBuilder(
animation: codeController,
builder: _buildOnChange,
),
);
}
Widget _buildOnChange(BuildContext context, Widget? child) {
final code = codeController.code;
final gutterWidth = style.width -
(style.showErrors ? 0 : _issueColumnWidth) -
(style.showFoldingHandles ? 0 : _foldingColumnWidth);
final issueColumnWidth = style.showErrors ? _issueColumnWidth : 0.0;
final foldingColumnWidth =
style.showFoldingHandles ? _foldingColumnWidth : 0.0;
final tableRows = List.generate(
code.hiddenLineRanges.visibleLineNumbers.length,
// ignore: prefer_const_constructors
(i) => TableRow(
// ignore: prefer_const_literals_to_create_immutables
children: [
const SizedBox(),
const SizedBox(),
const SizedBox(),
],
),
);
_fillLineNumbers(tableRows);
if (style.showErrors) {
_fillIssues(tableRows);
}
if (style.showFoldingHandles) {
_fillFoldToggles(tableRows);
}
return Container(
padding: EdgeInsets.only(right: style.margin),
width: style.showLineNumbers ? gutterWidth : null,
child: Table(
columnWidths: {
_lineNumberColumn: const FlexColumnWidth(),
_issueColumn: FixedColumnWidth(issueColumnWidth),
_foldingColumn: FixedColumnWidth(foldingColumnWidth),
},
defaultVerticalAlignment: TableCellVerticalAlignment.middle,
children: tableRows,
),
);
}
void _fillLineNumbers(List<TableRow> tableRows) {
final code = codeController.code;
final numberStyle = style.textStyle;
final numberStrutStyle = numberStyle == null
? null
: StrutStyle.fromTextStyle(
numberStyle,
forceStrutHeight: true,
);
for (final i in code.hiddenLineRanges.visibleLineNumbers) {
final lineIndex = _lineIndexToTableRowIndex(i);
if (lineIndex == null) {
continue;
}
tableRows[lineIndex].children![_lineNumberColumn] = Text(
style.showLineNumbers ? '${i + 1}' : ' ',
style: numberStyle,
strutStyle: numberStrutStyle,
textAlign: style.textAlign,
);
}
}
void _fillIssues(List<TableRow> tableRows) {
for (final issue in codeController.analysisResult.issues) {
if (issue.line >= codeController.code.lines.length) {
continue;
}
final lineIndex = _lineIndexToTableRowIndex(issue.line);
if (lineIndex == null || lineIndex >= tableRows.length) {
continue;
}
tableRows[lineIndex].children![_issueColumn] = GutterErrorWidget(
issue,
style.errorPopupTextStyle ??
(throw Exception('Error popup style should never be null')),
);
}
}
void _fillFoldToggles(List<TableRow> tableRows) {
final code = codeController.code;
for (final block in code.foldableBlocks) {
final lineIndex = _lineIndexToTableRowIndex(block.firstLine);
if (lineIndex == null) {
continue;
}
final isFolded = code.foldedBlocks.contains(block);
tableRows[lineIndex].children![_foldingColumn] = FoldToggle(
color: style.textStyle?.color,
isFolded: isFolded,
onTap: isFolded
? () => codeController.unfoldAt(block.firstLine)
: () => codeController.foldAt(block.firstLine),
);
}
// Add folded blocks that are not considered as a valid foldable block,
// but should be folded because they were folded before becoming invalid.
for (final block in code.foldedBlocks) {
final lineIndex = _lineIndexToTableRowIndex(block.firstLine);
if (lineIndex == null || lineIndex >= tableRows.length) {
continue;
}
tableRows[lineIndex].children![_foldingColumn] = FoldToggle(
color: style.textStyle?.color,
isFolded: true,
onTap: () => codeController.unfoldAt(block.firstLine),
);
}
}
int? _lineIndexToTableRowIndex(int line) {
return codeController.code.hiddenLineRanges.cutLineIndexIfVisible(line);
}
}