Skip to content

updated code, fixed lints #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
{
"workbench.editor.scrollToSwitchTabs": true,
"workbench.editor.limit.enabled": true,
"dart.closingLabels": false,
// copied from: https://dartcode.org/docs/recommended-settings/
// Causes the debug view to automatically appear when a breakpoint is hit. This
// setting is global and not configurable per-language.
"debug.openDebug": "openOnDebugBreak",
"[yaml]": {
"editor.formatOnSave": true
},
"[dart]": {
// "editor.formatOnType": true,
// Disables built-in highlighting of words that match your selection. Without
// this, all instances of the selected text will be highlighted, interfering
// with Dart's ability to highlight only exact references to the selected variable.
"editor.selectionHighlight": false,
// By default, VS Code prevents code completion from popping open when in
// "snippet mode" (editing placeholders in inserted code). Setting this option
// to `false` stops that and allows completion to open as normal, as if you
// weren't in a snippet placeholder.
"editor.suggest.snippetsPreventQuickSuggestions": false,
// By default, VS Code will pre-select the most recently used item from code
// completion. This is usually not the most relevant item.
//
// "first" will always select top item
// "recentlyUsedByPrefix" will filter the recently used items based on the
// text immediately preceeding where completion was invoked.
"editor.suggestSelection": "first",
// Allows pressing <TAB> to complete snippets such as `for` even when the
// completion list is not visible.
"editor.tabCompletion": "onlySnippets",
// By default, VS Code will populate code completion with words found in the
// current file when a language service does not provide its own completions.
// This results in code completion suggesting words when editing comments and
// strings. This setting will prevent that.
"editor.wordBasedSuggestions": "off"
},
"editor.bracketPairColorization.enabled": true,
"editor.codeActionsOnSave": {
"source.fixAll": "explicit",
"source.organizeImports": "explicit",
"quickfix.insertSemicolon": "explicit",
"source.fixAll.eslint": "explicit"
},
"editor.formatOnSave": true,
"prettier.singleQuote": true,
"prettier.trailingComma": "all",
"dart.runPubGetOnPubspecChanges": "never",
"xmlTools.splitXmlnsOnFormat": false,
"xmlTools.splitAttributesOnFormat": true,
"arb-editor.suppressedWarnings": "all"
}
7 changes: 5 additions & 2 deletions analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
include: package:flutter_lints/flutter.yaml

# Additional information about this file can be found at
# https://dart.dev/guides/language/analysis-options
analyzer:
exclude:
- lib/languages/*.dart
- lib/styles/*.dart
- lib/styles/**/*.dart
131 changes: 57 additions & 74 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import 'package:re_highlight/styles/all.dart';
void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
const MyApp({super.key});

@override
Widget build(BuildContext context) {
return MaterialApp(
Expand All @@ -15,14 +17,13 @@ class MyApp extends StatelessWidget {
}

class MyHomePage extends StatefulWidget {
const MyHomePage({super.key});

@override
_MyHomePageState createState() => _MyHomePageState();

State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {

String? _language;
String? _theme;

Expand All @@ -36,77 +37,66 @@ class _MyHomePageState extends State<MyHomePage> {
),
body: Padding(
padding: const EdgeInsets.all(10),
child: Column(
children: [
Row(
children: [
SizedBox(
width: 100,
child: Text('Language'),
),
DropdownButton<String>(
items: builtinAllLanguages.keys.map(
(language) => DropdownMenuItem<String>(
value: language,
child: Text(language)
)
).toList(),
child: Column(children: [
Row(
children: [
SizedBox(
width: 100,
child: Text('Language'),
),
DropdownButton<String>(
items: builtinAllLanguages.keys
.map((language) => DropdownMenuItem<String>(
value: language, child: Text(language)))
.toList(),
value: _language,
onChanged: (value) {
setState(() {
_language = value;
_controller.languages = value == null ? const [] : [value];
_controller.languages =
value == null ? const [] : [value];
});
}
),
],
),
Row(
children: [
SizedBox(
width: 100,
child: Text('Theme'),
),
DropdownButton<String>(
items: builtinAllThemes.keys.map(
(theme) => DropdownMenuItem<String>(
value: theme,
child: Text(theme)
)
).toList(),
}),
],
),
Row(
children: [
SizedBox(
width: 100,
child: Text('Theme'),
),
DropdownButton<String>(
items: builtinAllThemes.keys
.map((theme) => DropdownMenuItem<String>(
value: theme, child: Text(theme)))
.toList(),
value: _theme,
onChanged: (value) {
setState(() {
_theme = value;
_controller.theme = value == null ? const {} : builtinAllThemes[value] ?? const {};
_controller.theme = value == null
? const {}
: builtinAllThemes[value] ?? const {};
});
}
),
],
),
Expanded(
child: Container(
child: TextField(
controller: _controller,
maxLines: null,
expands: true,
textAlign: TextAlign.start,
textAlignVertical: TextAlignVertical.top,
decoration: InputDecoration(
border: OutlineInputBorder()
)
),
)
)
]
),
}),
],
),
Expanded(
child: TextField(
controller: _controller,
maxLines: null,
expands: true,
textAlign: TextAlign.start,
textAlignVertical: TextAlignVertical.top,
decoration: InputDecoration(border: OutlineInputBorder())),
)
]),
),
);
}
}

class CodeThemeController extends TextEditingController {

List<String> languages;
Map<String, TextStyle> theme;

Expand All @@ -122,26 +112,19 @@ class CodeThemeController extends TextEditingController {
}

@override
TextSpan buildTextSpan({
required BuildContext context,
TextStyle? style,
required bool withComposing
}) {
TextSpan buildTextSpan(
{required BuildContext context,
TextStyle? style,
required bool withComposing}) {
if (languages.isEmpty || theme.isEmpty) {
return super.buildTextSpan(
context: context,
style: style,
withComposing: withComposing
);
context: context, style: style, withComposing: withComposing);
}
final HighlightResult result = _highlight.highlightAuto(text, languages);
final TextSpanRenderer renderer = TextSpanRenderer(style, theme);
result.render(renderer);
return renderer.span ?? super.buildTextSpan(
context: context,
style: style,
withComposing: withComposing
);
return renderer.span ??
super.buildTextSpan(
context: context, style: style, withComposing: withComposing);
}

}
}
34 changes: 13 additions & 21 deletions example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@ packages:
dependency: transitive
description:
name: characters
sha256: "04a925763edad70e8443c99234dc3328f442e811f1d8fd1a72f1c8ad0f69a605"
sha256: f71061c654a3380576a52b451dd5532377954cf9dbd272a78fc8479606670803
url: "https://pub.dev"
source: hosted
version: "1.3.0"
version: "1.4.0"
collection:
dependency: transitive
description:
name: collection
sha256: ee67cb0715911d28db6bf4af1026078bd6f0128b07a5f66fb2ed94ec6783c09a
sha256: "2f5709ae4d3d59dd8f7cd309b4e023046b57d8a6c82130785d2b0e5868084e76"
url: "https://pub.dev"
source: hosted
version: "1.18.0"
version: "1.19.1"
flutter:
dependency: "direct main"
description: flutter
Expand All @@ -26,38 +26,38 @@ packages:
dependency: transitive
description:
name: material_color_utilities
sha256: "9528f2f296073ff54cb9fee677df673ace1218163c3bc7628093e7eed5203d41"
sha256: f7142bb1154231d7ea5f96bc7bde4bda2a0945d2806bb11670e30b850d56bdec
url: "https://pub.dev"
source: hosted
version: "0.5.0"
version: "0.11.1"
meta:
dependency: transitive
description:
name: meta
sha256: a6e590c838b18133bb482a2745ad77c5bb7715fb0451209e1a7567d416678b8e
sha256: e3641ec5d63ebf0d9b41bd43201a66e3fc79a65db5f61fc181f04cd27aab950c
url: "https://pub.dev"
source: hosted
version: "1.10.0"
version: "1.16.0"
path:
dependency: transitive
description:
name: path
sha256: "087ce49c3f0dc39180befefc60fdb4acd8f8620e5682fe2476afd0b3688bb4af"
sha256: "75cca69d1490965be98c73ceaea117e8a04dd21217b37b292c9ddbec0d955bc5"
url: "https://pub.dev"
source: hosted
version: "1.9.0"
version: "1.9.1"
re_highlight:
dependency: "direct main"
description:
path: ".."
relative: true
source: path
version: "0.0.1"
version: "0.0.3"
sky_engine:
dependency: transitive
description: flutter
source: sdk
version: "0.0.99"
version: "0.0.0"
vector_math:
dependency: transitive
description:
Expand All @@ -66,14 +66,6 @@ packages:
url: "https://pub.dev"
source: hosted
version: "2.1.4"
web:
dependency: transitive
description:
name: web
sha256: afe077240a270dcfd2aafe77602b4113645af95d0ad31128cc02bce5ac5d5152
url: "https://pub.dev"
source: hosted
version: "0.3.0"
sdks:
dart: ">=3.2.0-194.0.dev <4.0.0"
dart: ">=3.7.2 <4.0.0"
flutter: ">=1.17.0"
30 changes: 18 additions & 12 deletions lib/languages/lib/javascript.dart
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
import 'package:re_highlight/re_highlight.dart';

ModeCallback callbackOnBegin = (EnhancedMatch match, ModeCallbackResponse response) {
ModeCallback callbackOnBegin = (
EnhancedMatch match,
ModeCallbackResponse response,
) {
final String? match0 = match[0];
if (match0 == null) {
return;
}
final int afterMatchIndex = match0.length + match.index;
final String nextChar = match.input.substring(afterMatchIndex, afterMatchIndex + 1);
final String nextChar = match.input.substring(
afterMatchIndex,
afterMatchIndex + 1,
);
if (
// HTML should not include another raw `<` inside a tag
// nested type?
// `<Array<Array<number>>`, etc.
nextChar == "<" ||
// the , gives away that this is not HTML
// `<T, A extends keyof T, V>`
nextChar == ",") {
// HTML should not include another raw `<` inside a tag
// nested type?
// `<Array<Array<number>>`, etc.
nextChar == "<" ||
// the , gives away that this is not HTML
// `<T, A extends keyof T, V>`
nextChar == ",") {
response.ignoreMatch();
return;
}
Expand All @@ -35,15 +41,15 @@ ModeCallback callbackOnBegin = (EnhancedMatch match, ModeCallbackResponse respon

// some more template typing stuff
// <T = any>(key?: string) => Modify<
if (RegExp(r'^\s*=').hasMatch(afterMatch)) {
if (getRegExp(r'^\s*=').hasMatch(afterMatch)) {
response.ignoreMatch();
return;
}

// `<From extends string>`
// technically this could be HTML, but it smells like a type
// NOTE: This is ugh, but added specifically for https://github.com/highlightjs/highlight.js/issues/3276
final RegExpMatch? m = RegExp(r'^\s+extends\s+').firstMatch(afterMatch);
final RegExpMatch? m = getRegExp(r'^\s+extends\s+').firstMatch(afterMatch);
if (m != null) {
if (m.start == 0) {
response.ignoreMatch();
Expand All @@ -57,4 +63,4 @@ bool _hasClosingTag(EnhancedMatch match, int after) {
final String tag = "</${match[0]!.substring(1)}";
final int pos = match.input.indexOf(tag, after);
return pos != -1;
}
}
Loading