Skip to content
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

test(textmate): Show used theme colour #2255

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
36 changes: 32 additions & 4 deletions test/reason-textmate/bin/TextmateCli.re
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,22 @@
// Tool for testing the textmate grammars
//
// Can be run via:
// `esy @test x TextmateCli --file=package.json --grammar=test/reason-textmate/json.json --line=25
// `esy @test x TextmateCli --file=package.json --grammar=extensions/json/JSON.tmLanguage.json --theme=extensions/onedark-pro/themes/OneDark-Pro.json --line=25

open Textmate;

module Cli = {
type t = {
sourceFile: string,
grammarFile: string,
themeFile: string,
lineNumber: int,
};

let parse = args => {
let sourceFile = ref(None);
let grammarFile = ref(None);
let themeFile = ref(None);
let lineNumber = ref(0);
let () =
Arg.parse_argv(
Expand All @@ -35,6 +37,11 @@ module Cli = {
"Grammar file to use",
),
("--line", Int(i => lineNumber := i), "Line number to show"),
(
"--theme",
String(s => themeFile := Some(s)),
"Theme file to use",
),
],
_ => (),
"",
Expand All @@ -52,20 +59,23 @@ module Cli = {
{
sourceFile: getRequiredParameter("file", sourceFile),
grammarFile: getRequiredParameter("grammar", grammarFile),
themeFile: getRequiredParameter("theme", themeFile),
lineNumber: lineNumber^,
};
};

let toString = ({sourceFile, grammarFile, lineNumber}) =>
let toString = ({sourceFile, grammarFile, themeFile, lineNumber}) =>
Printf.sprintf(
{|
Using arguments:
- sourceFile: %s
- grammarFile: %s
- themeFile: %s
- lineNumber: %d
|},
sourceFile,
grammarFile,
themeFile,
lineNumber,
);
};
Expand All @@ -80,10 +90,21 @@ let getOkOrFail = (~msg, res) => {
};
};

let getTokenWithColour = (theme, token) => {
let colour =
TokenTheme.match(Theme.getTokenColors(theme), Token.show(token));

Printf.sprintf(
{|%s, Colour: (%s)|},
Token.show(token),
colour.TokenTheme.ResolvedStyle.foreground,
);
};

let run = () => {
print_endline("** Welcome to TextmateCli **");

let Cli.{sourceFile, grammarFile, lineNumber} as args =
let Cli.{sourceFile, grammarFile, lineNumber, themeFile} as args =
Cli.parse(Sys.argv);
print_endline(args |> Cli.toString);

Expand All @@ -99,6 +120,13 @@ let run = () => {
let scopeName = Grammar.getScopeName(grammar);
print_endline("Grammar loaded - using scope: " ++ scopeName);

print_endline("Loading theme: " ++ themeFile);

let theme =
themeFile
|> Theme.from_file
|> getOkOrFail(~msg="Unable to load theme " ++ grammarFile);

print_endline("Trying to read file: " ++ sourceFile);
let lines =
sourceFile |> Oni_Core.Utility.File.readAllLines |> Array.of_list;
Expand Down Expand Up @@ -126,7 +154,7 @@ let run = () => {
print_endline(Printf.sprintf("Line %d: %s", lineNumber, latestLine^));
print_endline("** Tokens: **");

latestTokens^ |> List.map(Token.show) |> List.iter(print_endline);
latestTokens^ |> List.map(getTokenWithColour(theme)) |> List.iter(print_endline);

print_endline("Done!");
};
Expand Down