Skip to content

Commit

Permalink
feat: improve c/c++ struct field tokenizing (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
blurfx authored Nov 26, 2023
1 parent 4983e68 commit 8036046
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 1 deletion.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
"main": "index.js",
"scripts": {
"test": "pnpm recursive run test:ci",
"dev": "pnpm run -r --parallel dev",
"pre-commit": "pnpm run --filter \"@calor/*\" pre-commit",
"build": "pnpm recursive run build",
"build": "pnpm run -r build",
"lint": "pnpm build && pnpm run --filter \"@calor/*\" lint",
"ci:version": "changeset version",
"ci:publish": "pnpm build && changeset publish",
Expand Down
1 change: 1 addition & 0 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
},
"scripts": {
"build": "tsup",
"dev": "tsup --watch",
"test": "vitest",
"test:ci": "vitest run",
"pre-commit": "lint-staged",
Expand Down
5 changes: 5 additions & 0 deletions packages/core/src/rules/cpp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ const cppRules: ParseRule[] = [
kind: 'keyword',
pattern: /(#\s*(?:define|if|elif|else|endif|import|error|line))\b/g,
},
{
kind: 'class',
pattern: /\b(struct)(\s+)([a-zA-Z$_][\w$_]*)\b/g,
matchHints: ['keyword', 'plain', 'class'],
},
{
kind: 'keyword',
pattern:
Expand Down
17 changes: 17 additions & 0 deletions packages/core/tests/tokenizer/c-cpp.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,4 +129,21 @@ describe('c/cpp tokenizer', () => {
expect(includeTokens.length).toBe(1);
expect(includeTokens[0].value).toBe('#include');
});
it('struct', () => {
const tokens = tokenize(
`
struct MyStruct {
int num;
struct foo *bar;
};
`,
cppRules,
);
const structNameTokens = tokens.filter((token) => token.kind === 'class');
expect(structNameTokens.length).toBe(2);
expect(structNameTokens.some((token) => token.value === 'MyStruct')).toBe(
true,
);
expect(structNameTokens.some((token) => token.value === 'foo')).toBe(true);
});
});
1 change: 1 addition & 0 deletions packages/highlighter/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
},
"scripts": {
"build": "tsup",
"dev": "tsup --watch",
"pre-commit": "lint-staged",
"typecheck": "tsc --noEmit --project .",
"dry:eslint": "eslint --ext .ts,.tsx ./src",
Expand Down

0 comments on commit 8036046

Please sign in to comment.