Skip to content

Commit

Permalink
feat: add language version support to formatting options
Browse files Browse the repository at this point in the history
  • Loading branch information
magic-akari committed Dec 10, 2024
1 parent f4e0f57 commit 6601b80
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 4 deletions.
1 change: 1 addition & 0 deletions extra/dart_fmt.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export function format(input: string, filename: string, config?: LayoutConfig):
interface LayoutConfig {
line_width?: number;
line_ending?: "lf" | "crlf";
language_version?: string;
}

export type InitInput = RequestInfo | URL | Response | BufferSource | WebAssembly.Module;
Expand Down
3 changes: 3 additions & 0 deletions extra/dart_fmt.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ export function format(source, filename = "stdin.dart", config = {}) {
if (options.line_ending === "crlf") {
options.lineEnding = "\r\n";
}
if(options.language_version) {
options.languageVersion = options.language_version;
}

const result = dart_fmt(source, filename, JSON.stringify(options));
const err = result[0] === "x";
Expand Down
6 changes: 4 additions & 2 deletions lib/binding.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,19 @@ import 'package:dart_fmt/dart_fmt.dart';
class Options {
final int? pageWidth;
final String? lineEnding;
final String? languageVersion;

Options.fromJson(Map<String, dynamic> json)
: pageWidth = json['pageWidth'] as int?,
lineEnding = json['lineEnding'] as String?;
lineEnding = json['lineEnding'] as String?,
languageVersion = json['languageVersion'] as String?;
}

String formatWrapper(String source, String filename, String options) {
final config = Options.fromJson(jsonDecode(options));

try {
return "o${format(source, filename, pageWidth: config.pageWidth, lineEnding: config.lineEnding)}";
return "o${format(source, filename, pageWidth: config.pageWidth, lineEnding: config.lineEnding, version: config.languageVersion)}";
} catch (e) {
return "x$e";
}
Expand Down
11 changes: 9 additions & 2 deletions lib/dart_fmt.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
import 'package:dart_style/dart_style.dart';
import 'package:pub_semver/pub_semver.dart';

String format(String source, String filename,
{int? pageWidth, String? lineEnding}) {
final formatter = DartFormatter(pageWidth: pageWidth, lineEnding: lineEnding);
{int? pageWidth, String? lineEnding, String? version}) {
final languageVersion = version != null
? Version.parse(version)
: DartFormatter.latestLanguageVersion;
final formatter = DartFormatter(
pageWidth: pageWidth,
lineEnding: lineEnding,
languageVersion: languageVersion);
return formatter.format(source, uri: filename);
}
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ environment:
# Add regular dependencies here.
dependencies:
dart_style: ^2.3.7
pub_semver: ^2.1.4

dev_dependencies:
lints: ^5.0.0
Expand Down

0 comments on commit 6601b80

Please sign in to comment.