@@ -5,17 +5,16 @@ import 'package:flutter/material.dart';
55import 'package:flutter/services.dart' ;
66import 'package:highlight/highlight_core.dart' ;
77
8- import 'code_modifier.dart' ;
9- import 'theme.dart' ;
8+ import '../code_modifiers/code_block_modifier.dart' ;
9+ import '../code_modifiers/code_modifier.dart' ;
10+ import '../code_modifiers/indent_code_modifier.dart' ;
11+ import '../code_modifiers/tab_code_modifier.dart' ;
12+ import '../code_theme/code_theme.dart' ;
13+ import '../code_theme/code_theme_data.dart' ;
14+ import 'editor_params.dart' ;
1015
1116const _MIDDLE_DOT = '·' ;
1217
13- class EditorParams {
14- final int tabSpaces;
15-
16- const EditorParams ({this .tabSpaces = 2 });
17- }
18-
1918class CodeController extends TextEditingController {
2019 Mode ? _language;
2120
@@ -86,7 +85,8 @@ class CodeController extends TextEditingController {
8685 CodeController ({
8786 String ? text,
8887 Mode ? language,
89- Map <String , TextStyle >? theme,
88+ @Deprecated ('Use CodeTheme widget to provide theme to CodeField.' )
89+ Map <String , TextStyle >? theme,
9090 this .patternMap,
9191 this .stringMap,
9292 this .params = const EditorParams (),
@@ -117,6 +117,7 @@ class CodeController extends TextEditingController {
117117 final sel = selection;
118118 text = text.replaceRange (selection.start, selection.end, str);
119119 final len = str.length;
120+
120121 selection = sel.copyWith (
121122 baseOffset: sel.start + len,
122123 extentOffset: sel.start + len,
@@ -125,9 +126,13 @@ class CodeController extends TextEditingController {
125126
126127 /// Remove the char just before the cursor or the selection
127128 void removeChar () {
128- if (selection.start < 1 ) return ;
129+ if (selection.start < 1 ) {
130+ return ;
131+ }
132+
129133 final sel = selection;
130134 text = text.replaceRange (selection.start - 1 , selection.start, "" );
135+
131136 selection = sel.copyWith (
132137 baseOffset: sel.start - 1 ,
133138 extentOffset: sel.start - 1 ,
@@ -138,6 +143,7 @@ class CodeController extends TextEditingController {
138143 void removeSelection () {
139144 final sel = selection;
140145 text = text.replaceRange (selection.start, selection.end, "" );
146+
141147 selection = sel.copyWith (
142148 baseOffset: sel.start,
143149 extentOffset: sel.start,
@@ -146,17 +152,19 @@ class CodeController extends TextEditingController {
146152
147153 /// Remove the selection or last char if the selection is empty
148154 void backspace () {
149- if (selection.start < selection.end)
155+ if (selection.start < selection.end) {
150156 removeSelection ();
151- else
157+ } else {
152158 removeChar ();
159+ }
153160 }
154161
155162 KeyEventResult onKey (RawKeyEvent event) {
156163 if (event.isKeyPressed (LogicalKeyboardKey .tab)) {
157164 text = text.replaceRange (selection.start, selection.end, "\t " );
158165 return KeyEventResult .handled;
159166 }
167+
160168 return KeyEventResult .ignored;
161169 }
162170
@@ -173,7 +181,10 @@ class CodeController extends TextEditingController {
173181 /// Get untransformed text
174182 /// See webSpaceFix
175183 String get rawText {
176- if (! _webSpaceFix) return super .text;
184+ if (! _webSpaceFix) {
185+ return super .text;
186+ }
187+
177188 return _middleDotsToSpaces (super .text);
178189 }
179190
@@ -183,24 +194,34 @@ class CodeController extends TextEditingController {
183194 static String _genId () {
184195 const _chars = 'abcdefghijklmnopqrstuvwxyz1234567890' ;
185196 final _rnd = Random ();
197+
186198 return String .fromCharCodes (
187- Iterable .generate (10 , (_) => _chars.codeUnitAt (_rnd.nextInt (_chars.length))),
199+ Iterable .generate (
200+ 10 ,
201+ (_) => _chars.codeUnitAt (_rnd.nextInt (_chars.length)),
202+ ),
188203 );
189204 }
190205
191206 int ? _insertedLoc (String a, String b) {
192207 final sel = selection;
193- if (a.length + 1 != b.length || sel.start != sel.end) return null ;
208+
209+ if (a.length + 1 != b.length || sel.start != sel.end) {
210+ return null ;
211+ }
212+
194213 return sel.start;
195214 }
196215
197216 @override
198217 set value (TextEditingValue newValue) {
199218 final loc = _insertedLoc (text, newValue.text);
219+
200220 if (loc != null ) {
201221 final char = newValue.text[loc];
202222 final modifier = modifierMap[char];
203223 final val = modifier? .updateString (rawText, selection, params);
224+
204225 if (val != null ) {
205226 // Update newValue
206227 newValue = newValue.copyWith (
@@ -209,20 +230,38 @@ class CodeController extends TextEditingController {
209230 );
210231 }
211232 }
233+
212234 // Now fix the textfield for web
213- if (_webSpaceFix) newValue = newValue.copyWith (text: _spacesToMiddleDots (newValue.text));
214- if (onChange != null ) onChange !(_webSpaceFix ? _middleDotsToSpaces (newValue.text) : newValue.text);
235+ if (_webSpaceFix) {
236+ newValue = newValue.copyWith (text: _spacesToMiddleDots (newValue.text));
237+ }
238+
239+ if (onChange != null ) {
240+ onChange !(
241+ _webSpaceFix ? _middleDotsToSpaces (newValue.text) : newValue.text,
242+ );
243+ }
244+
215245 super .value = newValue;
216246 }
217247
218248 TextSpan _processPatterns (String text, TextStyle ? style) {
219249 final children = < TextSpan > [];
250+
220251 text.splitMapJoin (
221252 styleRegExp! ,
222253 onMatch: (Match m) {
223- if (styleList.isEmpty) return '' ;
254+ if (styleList.isEmpty) {
255+ return '' ;
256+ }
257+
224258 int idx;
225- for (idx = 1 ; idx < m.groupCount && idx <= styleList.length && m.group (idx) == null ; idx++ ) {}
259+ for (idx = 1 ;
260+ idx < m.groupCount &&
261+ idx <= styleList.length &&
262+ m.group (idx) == null ;
263+ idx++ ) {}
264+
226265 children.add (TextSpan (
227266 text: m[0 ],
228267 style: styleList[idx - 1 ],
@@ -234,10 +273,15 @@ class CodeController extends TextEditingController {
234273 return '' ;
235274 },
236275 );
276+
237277 return TextSpan (style: style, children: children);
238278 }
239279
240- TextSpan _processLanguage (String text, CodeThemeData ? widgetTheme, TextStyle ? style) {
280+ TextSpan _processLanguage (
281+ String text,
282+ CodeThemeData ? widgetTheme,
283+ TextStyle ? style,
284+ ) {
241285 final rawText = _webSpaceFix ? _middleDotsToSpaces (text) : text;
242286 final result = highlight.parse (rawText, language: _languageId);
243287
@@ -250,21 +294,32 @@ class CodeController extends TextEditingController {
250294 void _traverse (Node node) {
251295 var val = node.value;
252296 final nodeChildren = node.children;
253- final nodeStyle = widgetTheme? .styles[node.className] ?? _theme? [node.className];
297+ final nodeStyle =
298+ widgetTheme? .styles[node.className] ?? _theme? [node.className];
254299
255300 if (val != null ) {
256- if (_webSpaceFix) val = _spacesToMiddleDots (val);
301+ if (_webSpaceFix) {
302+ val = _spacesToMiddleDots (val);
303+ }
304+
257305 var child = TextSpan (text: val, style: nodeStyle);
258- if (styleRegExp != null ) child = _processPatterns (val, nodeStyle);
306+
307+ if (styleRegExp != null ) {
308+ child = _processPatterns (val, nodeStyle);
309+ }
310+
259311 currentSpans.add (child);
260312 } else if (nodeChildren != null ) {
261313 List <TextSpan > tmp = [];
314+
262315 currentSpans.add (TextSpan (
263316 children: tmp,
264317 style: nodeStyle,
265318 ));
319+
266320 stack.add (currentSpans);
267321 currentSpans = tmp;
322+
268323 nodeChildren.forEach ((n) {
269324 _traverse (n);
270325 if (n == nodeChildren.last) {
@@ -274,26 +329,39 @@ class CodeController extends TextEditingController {
274329 }
275330 }
276331
277- if (nodes != null ) for (var node in nodes) _traverse (node);
332+ if (nodes != null ) {
333+ for (var node in nodes) {
334+ _traverse (node);
335+ }
336+ }
337+
278338 return TextSpan (style: style, children: children);
279339 }
280340
281341 @override
282- TextSpan buildTextSpan ({required BuildContext context, TextStyle ? style, bool ? withComposing}) {
342+ TextSpan buildTextSpan ({
343+ required BuildContext context,
344+ TextStyle ? style,
345+ bool ? withComposing,
346+ }) {
283347 // Retrieve pattern regexp
284348 final patternList = < String > [];
349+
285350 if (_webSpaceFix) {
286351 patternList.add ("(" + _MIDDLE_DOT + ")" );
287352 styleList.add (TextStyle (color: Colors .transparent));
288353 }
354+
289355 if (stringMap != null ) {
290356 patternList.addAll (stringMap! .keys.map ((e) => r'(\b' + e + r'\b)' ));
291357 styleList.addAll (stringMap! .values);
292358 }
359+
293360 if (patternMap != null ) {
294361 patternList.addAll (patternMap! .keys.map ((e) => "(" + e + ")" ));
295362 styleList.addAll (patternMap! .values);
296363 }
364+
297365 styleRegExp = RegExp (patternList.join ('|' ), multiLine: true );
298366
299367 // Return parsing
0 commit comments