forked from akvelon/flutter-code-editor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcode_line.dart
77 lines (65 loc) · 1.68 KB
/
code_line.dart
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
import 'dart:ui';
import 'package:charcode/ascii.dart';
import 'package:meta/meta.dart';
@immutable
class CodeLine {
final String text;
final TextRange textRange;
final bool isReadOnly;
final int indent;
const CodeLine({
required this.text,
required this.textRange,
required this.indent,
this.isReadOnly = false,
});
CodeLine.fromTextAndRange({
required this.text,
required this.textRange,
this.isReadOnly = false,
}) : indent = _calculateIndent(text);
CodeLine.fromTextAndStart(
this.text,
int start, {
this.isReadOnly = false,
}) : textRange = TextRange(start: start, end: start + text.length),
indent = _calculateIndent(text);
@override
String toString() =>
'CodeLine(ro: $isReadOnly, textRange: $textRange, text: "$text")';
CodeLine copyWith({
String? text,
TextRange? textRange,
bool? isReadOnly,
}) =>
CodeLine(
text: text ?? this.text,
textRange: textRange ?? this.textRange,
isReadOnly: isReadOnly ?? this.isReadOnly,
indent: text == null ? 0 : _calculateIndent(text),
);
@override
bool operator ==(Object other) {
return other is CodeLine &&
text == other.text &&
textRange == other.textRange &&
isReadOnly == other.isReadOnly;
}
@override
int get hashCode => Object.hash(
text,
textRange,
isReadOnly,
);
static int _calculateIndent(String text) {
int indentation = 0;
for (final character in text.runes) {
if (character == $space || character == $tab || character == $lf) {
indentation++;
} else {
break;
}
}
return indentation;
}
}