Skip to content

Commit 53324f7

Browse files
authored
Tab symbol is replaced with spaces on fullText set (#62) (#66)
* _replaceTabsIfNeeded (#62) * fvm in gitignore (#62) * Symbols class (#62) * tab replacement tests * Tab replacement test minor refinement (#62) * _areIdenticalTexts (#62) * moved tab and space constants (#62)
1 parent 8690a2d commit 53324f7

File tree

4 files changed

+94
-6
lines changed

4 files changed

+94
-6
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,5 @@ app.*.map.json
4646
/android/app/release
4747

4848
pubspec.lock
49+
.fvm/flutter_sdk
50+
.fvm/fvm_config.json

example/lib/custom_code_box.dart

+5-5
Original file line numberDiff line numberDiff line change
@@ -167,11 +167,11 @@ class _InnerFieldState extends State<InnerField> {
167167
language: allLanguages[widget.language],
168168
text: '''
169169
class MyClass {
170-
void readOnlyMethod() {// [START section1]
171-
}// [END section1]
172-
// [START section2]
173-
void method() {
174-
}// [END section2]
170+
\tvoid readOnlyMethod() {// [START section1]
171+
\t}// [END section1]
172+
\t// [START section2]
173+
\tvoid method() {
174+
\t}// [END section2]
175175
}
176176
''',
177177
namedSectionParser: const BracketsStartEndNamedSectionParser(),

lib/src/code_field/code_controller.dart

+10-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ import 'editor_params.dart';
2121
import 'span_builder.dart';
2222

2323
const _middleDot = '·';
24+
const _tab = '\t';
25+
const _space = ' ';
2426

2527
class CodeController extends TextEditingController {
2628
Mode? _language;
@@ -260,7 +262,7 @@ class CodeController extends TextEditingController {
260262
String get fullText => _lastCode.text;
261263

262264
set fullText(String fullText) {
263-
_updateLastCodeIfChanged(fullText);
265+
_updateLastCodeIfChanged(_replaceTabsWithSpacesIfNeeded(fullText));
264266
super.value = TextEditingValue(text: _lastCode.visibleText);
265267
}
266268

@@ -378,6 +380,13 @@ class CodeController extends TextEditingController {
378380
);
379381
}
380382

383+
String _replaceTabsWithSpacesIfNeeded(String text) {
384+
if (modifiers.contains(const TabModifier())) {
385+
return text.replaceAll(_tab, _space * params.tabSpaces);
386+
}
387+
return text;
388+
}
389+
381390
TextSpan _processPatterns(String text, TextStyle? style) {
382391
final children = <TextSpan>[];
383392

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import 'package:flutter_code_editor/flutter_code_editor.dart';
2+
import 'package:flutter_test/flutter_test.dart';
3+
import 'package:highlight/languages/go.dart';
4+
5+
void main() {
6+
const snippetWithTabs = '''
7+
public class MyClass {
8+
\tpublic void main() {
9+
\t}
10+
}
11+
''';
12+
const snippetWithDoubleSpaces = '''
13+
public class MyClass {
14+
public void main() {
15+
}
16+
}
17+
''';
18+
const snippetWithTripleSpaces = '''
19+
public class MyClass {
20+
public void main() {
21+
}
22+
}
23+
''';
24+
25+
bool _areIdenticalTexts(String text1, String text2) {
26+
return text1.compareTo(text2) == 0;
27+
}
28+
29+
group('Tab replacement', () {
30+
test(
31+
'applied if TabModifier is present',
32+
() {
33+
final controller = CodeController(
34+
text: snippetWithTabs,
35+
language: go,
36+
modifiers: [const TabModifier()],
37+
);
38+
expect(
39+
_areIdenticalTexts(controller.text, snippetWithDoubleSpaces),
40+
true,
41+
);
42+
},
43+
);
44+
45+
test(
46+
'not applied if TabModifier is not present',
47+
() {
48+
final controller = CodeController(
49+
text: snippetWithTabs,
50+
language: go,
51+
modifiers: [],
52+
);
53+
expect(
54+
_areIdenticalTexts(controller.text, snippetWithDoubleSpaces),
55+
false,
56+
);
57+
},
58+
);
59+
60+
test(
61+
'works with custom tabSpaces',
62+
() {
63+
const tabSpaces = 3;
64+
final controller = CodeController(
65+
params: const EditorParams(tabSpaces: tabSpaces),
66+
text: snippetWithTabs,
67+
language: go,
68+
modifiers: [const TabModifier()],
69+
);
70+
expect(
71+
_areIdenticalTexts(controller.text, snippetWithTripleSpaces),
72+
true,
73+
);
74+
},
75+
);
76+
});
77+
}

0 commit comments

Comments
 (0)