Skip to content

Commit

Permalink
Merge pull request #52 from akvelon/issue51_prettier
Browse files Browse the repository at this point in the history
Prettier (#51)
  • Loading branch information
alexeyinkin authored Apr 7, 2023
2 parents 00a2d52 + 3114455 commit af74171
Show file tree
Hide file tree
Showing 16 changed files with 130 additions and 113 deletions.
29 changes: 6 additions & 23 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ tasks.register("port") {
}

tasks.register("portLanguages") {
dependsOn("npxTsc")
dependsOn("tool:npxTsc")

doLast {
exec {
Expand All @@ -14,28 +14,11 @@ tasks.register("portLanguages") {

exec {
executable("dart")
args("format", "highlighting/lib/languages")
}

exec {
executable("dart")
args("format", "highlighting/lib/src/common_modes.dart")
}
}
}

tasks.register("npxTsc") {
doLast {
exec {
executable("npm")
args("install")
workingDir("tool")
}

exec {
executable("npx")
args("tsc")
workingDir("tool")
args(
"format",
"highlighting/lib/languages",
"highlighting/lib/src/common_modes.dart",
)
}
}
}
1 change: 1 addition & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ rootProject.name = "dart-highlighting"

include(":flutter_highlighting")
include(":highlighting")
include(":tool")
3 changes: 3 additions & 0 deletions tool/.prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"trailingComma": "all"
}
27 changes: 27 additions & 0 deletions tool/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
tasks.register("precommit") {
dependsOn("npxTsc")
dependsOn("format")
}

tasks.register("npxTsc") {
doLast {
exec {
executable("npm")
args("install")
}

exec {
executable("npx")
args("tsc")
}
}
}

tasks.register("format") {
doLast {
exec {
executable("npx")
args("prettier", "--write", "src")
}
}
}
3 changes: 2 additions & 1 deletion tool/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
"devDependencies": {
"@types/circular-json": "^0.4.0",
"@types/lodash": "^4.14.192",
"@types/node": "^12.6.9"
"@types/node": "^12.6.9",
"prettier": "^2.8.7"
},
"scripts": {
"start": "gulp"
Expand Down
31 changes: 11 additions & 20 deletions tool/src/callback_dictionary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,13 @@ import typescript from "highlight.js/lib/languages/typescript";
import ruby from "highlight.js/lib/languages/ruby";
import _ from "lodash";

import {Mode, ModeCallback} from "./types.js";

const commonCallbacks = new Map<string, string>(
[
[
hljs.END_SAME_AS_BEGIN({})["on:begin"]!.toString(),
"endSameAsBeginOnBegin"
],
[
hljs.END_SAME_AS_BEGIN({})["on:end"]!.toString(),
"endSameAsBeginOnEnd"
],
[
hljs.SHEBANG()["on:begin"]!.toString(),
"shebangOnBegin",
],
]
);
import { Mode, ModeCallback } from "./types.js";

const commonCallbacks = new Map<string, string>([
[hljs.END_SAME_AS_BEGIN({})["on:begin"]!.toString(), "endSameAsBeginOnBegin"],
[hljs.END_SAME_AS_BEGIN({})["on:end"]!.toString(), "endSameAsBeginOnEnd"],
[hljs.SHEBANG()["on:begin"]!.toString(), "shebangOnBegin"],
]);

class LanguagesCallbackParser {
languages: Mode[];
Expand All @@ -38,7 +27,9 @@ class LanguagesCallbackParser {
private process() {
for (const language of this.languages) {
const parser = new LanguageCallbackParser(language);
parser.entries.forEach((dartFunctionName, code) => this.entries.set(code, dartFunctionName));
parser.entries.forEach((dartFunctionName, code) =>
this.entries.set(code, dartFunctionName),
);
}
}
}
Expand Down Expand Up @@ -122,7 +113,7 @@ class LanguageCallbackParser {
if (!commonCallbacks.has(code)) {
this.entries.set(code, entry.dartFunctionName);
}
};
}
}

export const callbackDictionary = new Map<string, string>([
Expand Down
31 changes: 16 additions & 15 deletions tool/src/languages/mathematica.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,22 @@ import { readFileSync, writeFileSync } from "fs";
import { NOTICE_COMMENT } from "../common.js";

export function portMathematicaSpecific(): void {
const fileName = '../node_modules/highlight.js/lib/languages/mathematica.js';
const data = readFileSync(fileName, 'utf8');
const fileName = "../node_modules/highlight.js/lib/languages/mathematica.js";
const data = readFileSync(fileName, "utf8");

const symbolSet = data.match(/SYSTEM_SYMBOLS\s*=\s*(\[[^\]]*\])/s);
if (symbolSet === null || symbolSet[1] === null) {
throw Error("Couldn't find symbol set of Mathematica.");
}
const symbolSet = data.match(/SYSTEM_SYMBOLS\s*=\s*(\[[^\]]*\])/s);
if (symbolSet === null || symbolSet[1] === null) {
throw Error("Couldn't find symbol set of Mathematica.");
}

const output =
NOTICE_COMMENT
+ "final SYSTEM_SYMBOLS = "
+ symbolSet[1]
.replace(/\[/, '{')
.replace(/\]/, '}')
.replace(/\s"/g, 'r"')
+ ";";
writeFileSync("../../highlighting/lib/languages/common/mathematica_symbols.dart", output);
const output =
NOTICE_COMMENT +
"final SYSTEM_SYMBOLS = " +
symbolSet[1].replace(/\[/, "{").replace(/\]/, "}").replace(/\s"/g, 'r"') +
";";

writeFileSync(
"../../highlighting/lib/languages/common/mathematica_symbols.dart",
output,
);
}
19 changes: 13 additions & 6 deletions tool/src/porting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import _, { PropertyPath } from "lodash";

/**
* Gets the path to the pbject by reference string
*
*
* @param str reference string `~contains~0` -> `*.contains[0]`
* @returns
* @returns
*/
export function getLodashGetKey(str: String): PropertyPath {
let lodashGetKey = "";
Expand All @@ -22,16 +22,19 @@ export function getLodashGetKey(str: String): PropertyPath {

/**
* Recursively takes all of the references from [rootObject] and return them in a set.
*/
*/
export function expandRefs(rootObject: Object): Set<String> {
let commonSet = new Set<String>();
expandRefsInternal(rootObject, commonSet, rootObject);

return commonSet;
}

function expandRefsInternal(rootObject: Object, commonSet: Set<String>, currentObject: Object): void {

function expandRefsInternal(
rootObject: Object,
commonSet: Set<String>,
currentObject: Object,
): void {
if (typeof currentObject === "string") {
if (commonSet.has(currentObject)) {
return;
Expand All @@ -40,7 +43,11 @@ function expandRefsInternal(rootObject: Object, commonSet: Set<String>, currentO
commonSet.add(currentObject);
let lodashGetKey: PropertyPath = getLodashGetKey(currentObject);

expandRefsInternal(rootObject, commonSet, _.get(rootObject, lodashGetKey));
expandRefsInternal(
rootObject,
commonSet,
_.get(rootObject, lodashGetKey),
);
}
}

Expand Down
77 changes: 40 additions & 37 deletions tool/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,50 +1,53 @@
// TODO: Import types from highlight.js instead of this, https://github.com/akvelon/dart-highlighting/issues/50

export interface CallbackResponse {
data: Record<string, any>
ignoreMatch: () => void
isMatchIgnored: boolean
data: Record<string, any>;
ignoreMatch: () => void;
isMatchIgnored: boolean;
}

export type ModeCallback = (match: RegExpMatchArray, response: CallbackResponse) => void;
export type ModeCallback = (
match: RegExpMatchArray,
response: CallbackResponse,
) => void;

interface ModeCallbacks {
"on:end"?: Function,
"on:begin"?: ModeCallback
"on:end"?: Function;
"on:begin"?: ModeCallback;
}

interface ModeDetails {
begin?: RegExp | string | (RegExp | string)[]
match?: RegExp | string | (RegExp | string)[]
end?: RegExp | string | (RegExp | string)[]
// deprecated in favor of `scope`
className?: string
scope?: string | Record<number, string>
beginScope?: string | Record<number, string>
endScope?: string | Record<number, string>
contains?: ("self" | Mode)[]
endsParent?: boolean
endsWithParent?: boolean
endSameAsBegin?: boolean
skip?: boolean
excludeBegin?: boolean
excludeEnd?: boolean
returnBegin?: boolean
returnEnd?: boolean
__beforeBegin?: Function
parent?: Mode
starts?:Mode
lexemes?: string | RegExp
keywords?: Record<string, any> | string
beginKeywords?: string
relevance?: number
illegal?: string | RegExp | Array<string | RegExp>
variants?: Mode[]
cachedVariants?: Mode[]
// parsed
subLanguage?: string | string[]
isCompiled?: boolean
label?: string
begin?: RegExp | string | (RegExp | string)[];
match?: RegExp | string | (RegExp | string)[];
end?: RegExp | string | (RegExp | string)[];
// deprecated in favor of `scope`
className?: string;
scope?: string | Record<number, string>;
beginScope?: string | Record<number, string>;
endScope?: string | Record<number, string>;
contains?: ("self" | Mode)[];
endsParent?: boolean;
endsWithParent?: boolean;
endSameAsBegin?: boolean;
skip?: boolean;
excludeBegin?: boolean;
excludeEnd?: boolean;
returnBegin?: boolean;
returnEnd?: boolean;
__beforeBegin?: Function;
parent?: Mode;
starts?: Mode;
lexemes?: string | RegExp;
keywords?: Record<string, any> | string;
beginKeywords?: string;
relevance?: number;
illegal?: string | RegExp | Array<string | RegExp>;
variants?: Mode[];
cachedVariants?: Mode[];
// parsed
subLanguage?: string | string[];
isCompiled?: boolean;
label?: string;
}

export interface Mode extends ModeCallbacks, ModeDetails {}
2 changes: 1 addition & 1 deletion tool/src/types/highlight.js.d.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
// This is a placeholder so that we can import the below module that has no type information.
declare module 'highlight.js';
declare module "highlight.js";
2 changes: 1 addition & 1 deletion tool/src/types/javascript.d.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
// This is a placeholder so that we can import the below module that has no type information.
declare module 'highlight.js/lib/languages/javascript';
declare module "highlight.js/lib/languages/javascript";
2 changes: 1 addition & 1 deletion tool/src/types/mathematica.d.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
// This is a placeholder so that we can import the below module that has no type information.
declare module 'highlight.js/lib/languages/mathematica';
declare module "highlight.js/lib/languages/mathematica";
2 changes: 1 addition & 1 deletion tool/src/types/php.d.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
// This is a placeholder so that we can import the below module that has no type information.
declare module 'highlight.js/lib/languages/php';
declare module "highlight.js/lib/languages/php";
2 changes: 1 addition & 1 deletion tool/src/types/ruby.d.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
// This is a placeholder so that we can import the below module that has no type information.
declare module 'highlight.js/lib/languages/ruby';
declare module "highlight.js/lib/languages/ruby";
2 changes: 1 addition & 1 deletion tool/src/types/typescript.d.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
// This is a placeholder so that we can import the below module that has no type information.
declare module 'highlight.js/lib/languages/typescript';
declare module "highlight.js/lib/languages/typescript";
10 changes: 5 additions & 5 deletions tool/src/util.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
export type StringObject<T> = { [key: string]: T };

export function expand<T>(arrays: T[][]): T[] {
const result: T[] = [];
const result: T[] = [];

for (const arr of arrays) {
result.push(...arr);
}
for (const arr of arrays) {
result.push(...arr);
}

return result;
return result;
}

0 comments on commit af74171

Please sign in to comment.