Skip to content
Draft
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
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ flutter pub add highlights_plugin
```dart
final plugin = HighlightsPlugin(debug: true);

final success = await plugin.initialize();
```

```dart
// ['kotlin', 'dart', 'swift', 'php', 'java', ...]
final languages = await plugin.getLanguages();
// ['monokai', 'darcula', 'notepad', ...]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,40 +31,33 @@ class HighlightsPlugin : FlutterPlugin, MethodCallHandler {
}

override fun onMethodCall(call: MethodCall, result: Result) {
when (call.method) {
"getHighlights" -> {
updateInstance(
code = call.argument("code"),
language = SyntaxLanguage.getByName(call.argument("language") ?: ""),
theme = SyntaxThemes.getByName(call.argument("theme") ?: "", useDarkMode),
emphasisLocations = tryGetEmphasisFromJson(call.argument("emphasisLocations"))
)
try {
when (call.method) {
"getHighlights" -> {
updateInstance(
code = call.argument("code"),
language = SyntaxLanguage.getByName(call.argument("language") ?: ""),
theme = SyntaxThemes.getByName(call.argument("theme") ?: "", useDarkMode),
emphasisLocations = tryGetEmphasisFromJson(call.argument("emphasisLocations"))
)

highlights.getHighlightsAsync(
object: DefaultHighlightsResultListener() {
override fun onSuccess(highlights: List<CodeHighlight>) {
result.success(highlights.toJson())
}
val output = highlights.getHighlights()
result.success(output.toJson())
}

override fun onCancel() {
result.success(null)
}
"getLanguages" -> result.success(SyntaxLanguage.getNames())
"getThemes" -> result.success(SyntaxThemes.getNames(useDarkMode))
"setDarkMode" -> {
useDarkMode = call.argument("useDarkMode") ?: false
result.success(null)
}

override fun onError(exception: Throwable) {
result.error("Error", exception.message, null)
}
}
)
}
"getLanguages" -> result.success(SyntaxLanguage.getNames())
"getThemes" -> result.success(SyntaxThemes.getNames(useDarkMode))
"setDarkMode" -> {
useDarkMode = call.argument("useDarkMode") ?: false
result.success(null)
}
else -> {
result.notImplemented()
else -> {
result.notImplemented()
}
}
} catch (e: Exception) {
result.error("Error", e.message, e)
}
}

Expand Down
25 changes: 14 additions & 11 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:async';

import 'package:flutter/material.dart';
import 'package:highlights_plugin/highlights_plugin.dart';
import 'package:highlights_plugin/model/phrase_location.dart';
Expand All @@ -23,7 +25,7 @@ class _MyAppState extends State<MyApp> {
final List<PhraseLocation> _emphasis = [];

Future<void> _updateDarkMode(bool isDark) async {
_highlightsPlugin.setDarkMode(isDark);
await _highlightsPlugin.setDarkMode(isDark);
_updateHighlights(_code ?? '');
}

Expand All @@ -41,19 +43,14 @@ class _MyAppState extends State<MyApp> {
_updateHighlights(_code ?? '');
}

Future<void> _updateHighlights(String code) async {
_updateHighlights(String code) {
_code = code;
_highlightsPlugin
.getHighlights(
_code ?? '',
_language,
_theme,
_emphasis,
)
.then((value) {
.getHighlights(_code ?? '', _language, _theme, _emphasis)
.then((highlights) {
setState(() {
value.sort((a, b) => a.location.start.compareTo(b.location.start));
_highlights = value.map((highlight) => highlight.toString()).toList();
_highlights =
highlights.map((highlight) => highlight.toString()).toList();
});
});
}
Expand All @@ -63,6 +60,12 @@ class _MyAppState extends State<MyApp> {
_updateHighlights(_code ?? '');
}

@override
void initState() {
super.initState();
unawaited(_highlightsPlugin.initialize());
}

@override
Widget build(BuildContext context) {
return MaterialApp(
Expand Down
6 changes: 0 additions & 6 deletions example/macos/Podfile.lock
Original file line number Diff line number Diff line change
@@ -1,21 +1,15 @@
PODS:
- FlutterMacOS (1.0.0)
- highlights_plugin (0.0.1):
- FlutterMacOS

DEPENDENCIES:
- FlutterMacOS (from `Flutter/ephemeral`)
- highlights_plugin (from `Flutter/ephemeral/.symlinks/plugins/highlights_plugin/macos`)

EXTERNAL SOURCES:
FlutterMacOS:
:path: Flutter/ephemeral
highlights_plugin:
:path: Flutter/ephemeral/.symlinks/plugins/highlights_plugin/macos

SPEC CHECKSUMS:
FlutterMacOS: 8f6f14fa908a6fb3fba0cd85dbd81ec4b251fb24
highlights_plugin: 8a7d54a1fffabf787c85c83f91317b3cc3bdd56c

PODFILE CHECKSUM: 353c8bcc5d5b0994e508d035b5431cfe18c1dea7

Expand Down
20 changes: 1 addition & 19 deletions example/macos/Runner.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,6 @@
33CC10EB2044A3C60003C045 /* Resources */,
33CC110E2044A8840003C045 /* Bundle Framework */,
3399D490228B24CF009A79C7 /* ShellScript */,
CD40F6F01B76A7B123DDBE56 /* [CP] Embed Pods Frameworks */,
);
buildRules = (
);
Expand All @@ -203,7 +202,7 @@
isa = PBXProject;
attributes = {
LastSwiftUpdateCheck = 0920;
LastUpgradeCheck = 1430;
LastUpgradeCheck = 1510;
ORGANIZATIONNAME = "";
TargetAttributes = {
33CC10EC2044A3C60003C045 = {
Expand Down Expand Up @@ -314,23 +313,6 @@
shellPath = /bin/sh;
shellScript = "\"$FLUTTER_ROOT\"/packages/flutter_tools/bin/macos_assemble.sh && touch Flutter/ephemeral/tripwire";
};
CD40F6F01B76A7B123DDBE56 /* [CP] Embed Pods Frameworks */ = {
isa = PBXShellScriptBuildPhase;
buildActionMask = 2147483647;
files = (
);
inputFileListPaths = (
"${PODS_ROOT}/Target Support Files/Pods-Runner/Pods-Runner-frameworks-${CONFIGURATION}-input-files.xcfilelist",
);
name = "[CP] Embed Pods Frameworks";
outputFileListPaths = (
"${PODS_ROOT}/Target Support Files/Pods-Runner/Pods-Runner-frameworks-${CONFIGURATION}-output-files.xcfilelist",
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
shellScript = "\"${PODS_ROOT}/Target Support Files/Pods-Runner/Pods-Runner-frameworks.sh\"\n";
showEnvVarsInLog = 0;
};
/* End PBXShellScriptBuildPhase section */

/* Begin PBXSourcesBuildPhase section */
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1430"
LastUpgradeVersion = "1510"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
2 changes: 1 addition & 1 deletion example/macos/Runner/AppDelegate.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Cocoa
import FlutterMacOS

@NSApplicationMain
@main
class AppDelegate: FlutterAppDelegate {
override func applicationShouldTerminateAfterLastWindowClosed(_ sender: NSApplication) -> Bool {
return true
Expand Down
4 changes: 2 additions & 2 deletions example/test/widget_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ void main() {
// Verify that platform version is retrieved.
expect(
find.byWidgetPredicate(
(Widget widget) => widget is Text &&
widget.data!.startsWith('Running on:'),
(Widget widget) =>
widget is Text && widget.data!.startsWith('Running on:'),
),
findsOneWidget,
);
Expand Down
24 changes: 2 additions & 22 deletions ios/Classes/SwiftHighlightsPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,30 +45,10 @@ public class SwiftHighlightsPlugin: NSObject, FlutterPlugin {
theme: theme,
emphasisLocations: emphasis
)

class Listener: DefaultHighlightsResultListener {
var flutterResult: FlutterResult

init(flutterResult: @escaping FlutterResult) {
self.flutterResult = flutterResult
}

override func onStart() {}

override func onCancel() {
flutterResult(nil)
}

override func onSuccess(result: [CodeHighlight]) {
flutterResult(ExtensionsKt.toJson(result))
}

override func onError(exception: KotlinThrowable) {
flutterResult(exception)
}
}
let highlightList = highlights.getHighlights();

highlights.getHighlightsAsync(listener: Listener(flutterResult: result));
result(ExtensionsKt.toJson(highlightList))
case "setDarkMode":
let map = call.arguments as! Dictionary<String, Any>
useDarkMode = map["useDarkMode"] as? Bool ?? false
Expand Down
2 changes: 2 additions & 0 deletions lib/highlights_interface.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import 'package:highlights_plugin/model/code_highlight.dart';
import 'package:highlights_plugin/model/phrase_location.dart';

abstract interface class HighlightsInterface {
Future<bool> initialize();

Future<List<CodeHighlight>> getHighlights(
String code,
String language,
Expand Down
59 changes: 59 additions & 0 deletions lib/highlights_platform_interface.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import 'package:highlights_plugin/model/code_highlight.dart';
import 'package:highlights_plugin/model/phrase_location.dart';
import 'package:highlights_plugin/native_platform_interface.dart';
import 'package:plugin_platform_interface/plugin_platform_interface.dart';

import 'highlights_interface.dart';
import 'method_index.dart' as methods;

abstract class HighlightsPlatformInterface extends PlatformInterface
implements HighlightsInterface {
HighlightsPlatformInterface() : super(token: _token);

static final Object _token = Object();

static bool debug = false;

static HighlightsPlatformInterface _instance =
NativePlatformInterface(debug: debug);

static HighlightsPlatformInterface get instance => _instance;

static set instance(HighlightsPlatformInterface instance) {
PlatformInterface.verifyToken(instance, _token);
_instance = instance;
}

@override
Future<bool> initialize() {
throw UnimplementedError('${methods.initialize} has not been implemented.');
}

@override
Future<List<CodeHighlight>> getHighlights(
String? code,
String? language,
String? theme,
List<PhraseLocation>? emphasisLocations,
) {
throw UnimplementedError(
'${methods.getHighlights} has not been implemented.');
}

@override
Future<List<String>> getLanguages() {
throw UnimplementedError(
'${methods.getLanguages} has not been implemented.');
}

@override
Future<List<String>> getThemes() {
throw UnimplementedError('${methods.getThemes} has not been implemented.');
}

@override
Future<void> setDarkMode(bool useDarkMode) {
throw UnimplementedError(
'${methods.setDarkMode} has not been implemented.');
}
}
21 changes: 13 additions & 8 deletions lib/highlights_plugin.dart
Original file line number Diff line number Diff line change
@@ -1,42 +1,47 @@
import 'package:flutter/cupertino.dart';
import 'package:highlights_plugin/highlights_platform_interface.dart';
import 'package:highlights_plugin/model/code_highlight.dart';
import 'package:highlights_plugin/model/phrase_location.dart';
import 'package:highlights_plugin/method_index.dart' as methods;
import 'highlights_interface.dart';
import 'highlights_plugin_platform_interface.dart';

class HighlightsPlugin implements HighlightsInterface {
HighlightsPlugin({this.debug = false}) {
// TODO Test logger flag
// Setup before first `instance` getter call
HighlightsPluginPlatform.debug = debug;
HighlightsPlatformInterface.debug = debug;
}

final bool debug;

@override
Future<bool> initialize() =>
HighlightsPlatformInterface.instance.initialize();

@override
Future<List<CodeHighlight>> getHighlights(
String code,
String? language,
String? theme,
List<PhraseLocation>? emphasisLocations,
) async {
) {
try {
return await HighlightsPluginPlatform.instance.getHighlights(
return HighlightsPlatformInterface.instance.getHighlights(
code,
language,
theme,
emphasisLocations,
);
} catch (e, st) {
_printDebugInfo(methods.getHighlights, e, st);
return [];
return Future.value([]);
}
}

@override
Future<List<String>> getLanguages() async {
try {
return await HighlightsPluginPlatform.instance.getLanguages();
return await HighlightsPlatformInterface.instance.getLanguages();
} catch (e, st) {
_printDebugInfo(methods.getLanguages, e, st);
return [];
Expand All @@ -46,7 +51,7 @@ class HighlightsPlugin implements HighlightsInterface {
@override
Future<List<String>> getThemes() async {
try {
return await HighlightsPluginPlatform.instance.getThemes();
return await HighlightsPlatformInterface.instance.getThemes();
} catch (e, st) {
_printDebugInfo(methods.getThemes, e, st);
return [];
Expand All @@ -56,7 +61,7 @@ class HighlightsPlugin implements HighlightsInterface {
@override
Future<void> setDarkMode(bool useDarkMode) {
try {
return HighlightsPluginPlatform.instance.setDarkMode(useDarkMode);
return HighlightsPlatformInterface.instance.setDarkMode(useDarkMode);
} catch (e, st) {
_printDebugInfo(methods.setDarkMode, e, st);
return Future.value();
Expand Down
Loading