Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -218,8 +218,8 @@ LineNumberStyle({
```dart
CodeController({
String? text,
this.language,
this.theme,
Mode? language,
Map<String, TextStyle>? theme,
this.patternMap,
this.stringMap,
this.params = const EditorParams(),
Expand Down
61 changes: 44 additions & 17 deletions lib/src/code_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,38 @@ class EditorParams {
}

class CodeController extends TextEditingController {
Mode? _language;

/// A highlight language to parse the text with
final Mode? language;
Mode? get language => _language;

set language(Mode? language) {
if (language == _language) {
return;
}

if (language != null) {
_languageId = _genId();
highlight.registerLanguage(_languageId, language);
}

_language = language;
notifyListeners();
}

Map<String, TextStyle>? _theme;

/// The theme to apply to the [language] parsing result
final Map<String, TextStyle>? theme;
Map<String, TextStyle>? get theme => _theme;

set theme(Map<String, TextStyle>? theme) {
if (theme == _theme) {
return;
}

_theme = theme;
notifyListeners();
}

/// A map of specific regexes to style
final Map<String, TextStyle>? patternMap;
Expand All @@ -43,15 +70,18 @@ class CodeController extends TextEditingController {
final void Function(String)? onChange;

/* Computed members */
final String languageId = _genId();
String _languageId = _genId();

String get languageId => _languageId;

final styleList = <TextStyle>[];
final modifierMap = <String, CodeModifier>{};
RegExp? styleRegExp;

CodeController({
String? text,
this.language,
this.theme,
Mode? language,
Map<String, TextStyle>? theme,
this.patternMap,
this.stringMap,
this.params = const EditorParams(),
Expand All @@ -62,13 +92,10 @@ class CodeController extends TextEditingController {
],
this.webSpaceFix = true,
this.onChange,
}) : super(text: text) {
// PatternMap
if (language != null && theme == null) throw Exception("A theme must be provided for language parsing");
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we allow to change language and theme, then I suggest we allow language without theme. If they both present, then we will format the code. If either one is not present, then not.

Otherwise there will be too much checks, and a nasty method like setThemeAndLanguage to set both in one call.

// Register language
if (language != null) {
highlight.registerLanguage(languageId, language!);
}
}) : _theme = theme,
super(text: text) {
this.language = language;

// Create modifier map
modifiers.forEach((el) {
modifierMap[el.char] = el;
Expand Down Expand Up @@ -207,7 +234,7 @@ class CodeController extends TextEditingController {

TextSpan _processLanguage(String text, TextStyle? style) {
final rawText = _webSpaceFix ? _middleDotsToSpaces(text) : text;
final result = highlight.parse(rawText, language: languageId);
final result = highlight.parse(rawText, language: _languageId);

final nodes = result.nodes;

Expand All @@ -220,14 +247,14 @@ class CodeController extends TextEditingController {
final nodeChildren = node.children;
if (val != null) {
if (_webSpaceFix) val = _spacesToMiddleDots(val);
var child = TextSpan(text: val, style: theme?[node.className]);
if (styleRegExp != null) child = _processPatterns(val, theme?[node.className]);
var child = TextSpan(text: val, style: _theme?[node.className]);
if (styleRegExp != null) child = _processPatterns(val, _theme?[node.className]);
currentSpans.add(child);
} else if (nodeChildren != null) {
List<TextSpan> tmp = [];
currentSpans.add(TextSpan(
children: tmp,
style: theme?[node.className],
style: _theme?[node.className],
));
stack.add(currentSpans);
currentSpans = tmp;
Expand Down Expand Up @@ -263,7 +290,7 @@ class CodeController extends TextEditingController {
styleRegExp = RegExp(patternList.join('|'), multiLine: true);

// Return parsing
if (language != null)
if (_language != null && _theme != null)
return _processLanguage(text, style);
else if (styleRegExp != null)
return _processPatterns(text, style);
Expand Down