Skip to content

Commit

Permalink
add code suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
milachae committed Nov 20, 2024
1 parent 2d8328c commit 891d255
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 36 deletions.
2 changes: 1 addition & 1 deletion cli/src/cli/commands/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ export function runCommand(program: Command): Command {
Options.defaultKgramsInWindow
)
.option(
"-ic, --include-comments",
"-C, --include-comments",
Utils.indent(
"Include the comments during the tokenization process."
)
Expand Down
6 changes: 3 additions & 3 deletions lib/src/lib/tokenizer/codeTokenizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ export class CodeTokenizer extends Tokenizer {
node.endPosition.column
);

const isComment = node.type.includes("comment");
if (!isComment || this.options.includeComments) {
const includeToken = !node.type.includes("comment") || this.options.includeComments;
if (includeToken) {
tokens.push(this.newToken("(", location));
tokens.push(this.newToken(node.type, location));
}
Expand All @@ -78,7 +78,7 @@ export class CodeTokenizer extends Tokenizer {
}
}

if (!isComment || this.options.includeComments) {
if (includeToken) {
tokens.push(this.newToken(")", location));
}

Expand Down
9 changes: 1 addition & 8 deletions lib/src/lib/tokenizer/tokenizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,7 @@ export type TokenizerOptions = Partial<{

export abstract class Tokenizer {

protected options: TokenizerOptions = {};

constructor(public readonly language: Language, options?: TokenizerOptions) {
if (options !== undefined) {
this.options = options;
}
}

constructor(public readonly language: Language, protected readonly options: TokenizerOptions = {}) {}

/**
* Runs the parser on a given string. Returns a list of Tokens
Expand Down
33 changes: 9 additions & 24 deletions lib/src/test/tokenizer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,29 +26,6 @@ const languageFiles = {
"verilog": "../samples/verilog/module.v"
} as {[key: string]: string};

const tokenLength = {
"../samples/bash/caesar.sh": 1179,
"../samples/c/caesar.c": 582,
"../samples/c-sharp/Caesar.cs": 603,
"../samples/char/caesar.txt": 3700,
"../samples/cpp/caesar.cpp": 795,
"../samples/elm/Caesar.elm": 753,
"../samples/go/Caesar.go": 1032,
"../samples/groovy/caesar.groovy": 282,
"../samples/java/Caesar.java": 519,
"../samples/javascript/sample.js": 861,
"../samples/python/caesar.py": 306,
"../samples/php/caesar.php": 411,
"../samples/modelica/sample.mo": 7533,
"../samples/r/caesar.R": 585,
"../samples/rust/caesar.rs": 774,
"../samples/scala/Caesar.scala": 366,
"../samples/sql/sample.sql": 540,
"../samples/tsx/sample.tsx": 1656,
"../samples/typescript/caesar.ts": 375,
"../samples/verilog/module.v": 2448
} as {[key: string]: number};

for (const [languageName, languageFile] of Object.entries(languageFiles)) {
test(`LanguagePicker can find ${languageName} correctly by name`, async t => {
const language = await new LanguagePicker().findLanguage(languageName);
Expand All @@ -74,7 +51,6 @@ for (const [languageName, languageFile] of Object.entries(languageFiles)) {
const { tokens } = tokenizer.tokenizeFile(file);
t.truthy(tokens);
t.snapshot(tokens, "stable tokenization");
t.is(tokens.length, tokenLength[languageFile]);
});
}

Expand Down Expand Up @@ -201,3 +177,12 @@ test("should be able to correctly tokenize a loop", async t => {
);
});


test("tokens should contain comments when includeComments is true", async t => {
const file = new File("comments.js", "let i = 0;\nwhile (i < 10) { // comment\n i += 1;\n}");
const language = await (new LanguagePicker().findLanguage("javascript"));

const tokenizer = await language.createTokenizer({ includeComments: true });
const { tokens } = tokenizer.tokenizeFile(file);
t.true(tokens.includes("comment"));
});

0 comments on commit 891d255

Please sign in to comment.