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

Feature/4401 creating langium parser git graph #5664

Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
59 commits
Select commit Hold shift + click to select a range
1d0e98d
Added the langium module for gitGraph
Jul 21, 2024
6f7c291
Added gitGraphAst as typescript added gitGraphTypes and gitGraphParser
Jul 22, 2024
ebd4da9
Merge branch 'mermaid-js:develop' into feature/4401_creating_langium_…
Austin-Fulbright Jul 24, 2024
5460bc0
fixed checkout branch with no commits
Jul 24, 2024
d2e2017
fixed undefined for type errors
Jul 24, 2024
0b67cff
fixed cherrypicking tests
Jul 24, 2024
1a95d48
added Imperative state
Jul 24, 2024
d0eadeb
added parser
Jul 25, 2024
1af9094
fixed options e2e test
Jul 25, 2024
a386bd0
fixed tags for gitGraph
Jul 27, 2024
887e580
fixed some features and added propper default direction
Jul 27, 2024
3168084
fixed rendering
Jul 27, 2024
9f6a7b7
allows for custom merge type
Austin-Fulbright Jul 27, 2024
275dbe9
fixed all rendering differences
Jul 27, 2024
0d4c3e5
added unit tests for gitGraph parser
Jul 27, 2024
e57fee1
Update packages/mermaid/src/diagrams/git/gitGraphAst.ts
Austin-Fulbright Jul 27, 2024
62757c5
Update packages/mermaid/src/diagrams/git/gitGraphAst.ts
Austin-Fulbright Jul 27, 2024
281064f
Update packages/mermaid/src/diagrams/git/gitGraphAst.ts
Austin-Fulbright Jul 27, 2024
ec2d9c9
Update packages/mermaid/src/diagrams/git/gitGraphParser.ts
Austin-Fulbright Jul 27, 2024
5dfc94e
Update packages/mermaid/src/diagrams/git/gitGraphParser.ts
Austin-Fulbright Jul 27, 2024
871f047
Update packages/parser/src/language/gitGraph/module.ts
Austin-Fulbright Jul 27, 2024
6e5e5f9
Update packages/mermaid/src/diagrams/git/gitGraphAst.ts
Austin-Fulbright Jul 27, 2024
346efdd
Update packages/mermaid/src/diagrams/git/gitGraphAst.ts
Austin-Fulbright Jul 27, 2024
a0207f9
Update packages/mermaid/src/diagrams/git/gitGraphAst.ts
Austin-Fulbright Jul 27, 2024
6c1e5aa
Delete packages/mermaid/src/diagrams/git/parser/gitGraph.jison
Austin-Fulbright Jul 27, 2024
ef25160
Update packages/mermaid/src/diagrams/git/gitGraphAst.ts
Austin-Fulbright Jul 27, 2024
f30085c
Update packages/mermaid/src/diagrams/git/gitGraphAst.ts
Austin-Fulbright Jul 27, 2024
9ed38cc
Merge branch 'mermaid-js:develop' into feature/4401_creating_langium_…
Austin-Fulbright Aug 5, 2024
2a38d46
fixed the rest of the concerns, refactored portions of the gitGraphPa…
Aug 5, 2024
38e048b
fixed hash error loc & line properties
Aug 5, 2024
8fe0ed1
added parser test and combined the two gitGraph tests
Aug 6, 2024
00603e7
fix ts-ignore errors refactored large functions to use helpers
Aug 8, 2024
2218929
fixed types in gitGraphTypes
Aug 8, 2024
a93b832
made draw commit more readable, included more helper functions and in…
Aug 10, 2024
e0d0cdc
Merge branch 'mermaid-js:develop' into feature/4401_creating_langium_…
Austin-Fulbright Aug 10, 2024
62950c3
finished gitGraphRenderer.ts
Aug 10, 2024
269284c
added parser unit tests and organized config in gitGraphAst.ts
Aug 10, 2024
d684e0d
added more unit tests to gitGraphParser.ts and gitGraphRenderer.ts
Aug 10, 2024
c49a1bf
fixed custom type REVERSE for merge, fixed branch spacing for TB
Aug 10, 2024
aba306b
fixed highlight color
Aug 10, 2024
d73a090
fixed BT with parallel commits and added more unit tests
Aug 11, 2024
73aae9e
Merge branch 'develop' into feature/4401_creating_langium_parser_gitG…
Austin-Fulbright Aug 12, 2024
d50150c
Merge branch 'mermaid-js:develop' into feature/4401_creating_langium_…
Austin-Fulbright Aug 13, 2024
3539a35
[autofix.ci] apply automated fixes
autofix-ci[bot] Aug 13, 2024
299e559
added config as global
Aug 14, 2024
53798be
fixed gitgraphconfig problem
Aug 15, 2024
66e53df
added most suggested changes
Aug 20, 2024
b93691b
fixed small error
Aug 20, 2024
4ec0dcf
Merge branch 'develop' into feature/4401_creating_langium_parser_gitG…
Austin-Fulbright Aug 20, 2024
d9d9cc9
added objects to be transfered from parser to db
Aug 20, 2024
7a7b415
implemented transfer objects from parser to db
Aug 20, 2024
94ee076
fixed config for user configs
Aug 20, 2024
3ac2429
fixed merge
Aug 20, 2024
8ef30a2
Merge branch 'develop' into feature/4401_creating_langium_parser_gitG…
Austin-Fulbright Aug 20, 2024
24ba5b7
added gitgraph in imperative state
Aug 20, 2024
52f5d95
Merge branch 'develop' into feature/4401_creating_langium_parser_gitG…
Austin-Fulbright Aug 21, 2024
6c89763
Merge branch 'develop' into feature/4401_creating_langium_parser_gitG…
sidharthv96 Aug 23, 2024
a87f773
Merge branch 'develop' into pr/Austin-Fulbright/5664
sidharthv96 Aug 23, 2024
5deaef4
chore: add changeset
sidharthv96 Aug 23, 2024
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

Large diffs are not rendered by default.

21 changes: 21 additions & 0 deletions packages/mermaid/src/diagrams/git/gitGraphParser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import type { GitGraph } from '@mermaid-js/parser';
import { parse } from '@mermaid-js/parser';
import type { ParserDefinition } from '../../diagram-api/types.js';
import { log } from '../../logger.js';
import { populateCommonDb } from '../common/populateCommonDb.js';
import db from './gitGraphAst.js';

const populate = (ast: any) => {
populateCommonDb(ast, db);
for (const statement of ast.statements) {
log.debug(statement);
}
};

export const parser: ParserDefinition = {
parse: async (input: string): Promise<void> => {
const ast: GitGraph = await parse('gitGraph', input);
log.debug(ast);
populate(ast);
},
};
55 changes: 55 additions & 0 deletions packages/mermaid/src/diagrams/git/gitGraphTypes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
export type CommitType = 'NORMAL' | 'REVERSE' | 'HIGHLIGHT' | 'MERGE' | 'CHERRY_PICK';

export interface Commit {
id: string;
message: string;
seq: number;
type: number;
tags: string[] | undefined;
parents: (string | null)[];
branch: string;
customType?: number;
customId?: boolean;
}

export interface GitGraph {
statements: Statement[];
}

export type Statement = CommitAst | Branch | Merge | Checkout | CherryPicking;

export interface CommitAst {
$type: 'Commit';
id: string;
message?: string;
tags?: string[];
type?: 'NORMAL' | 'REVERSE' | 'HIGHLIGHT';
}

export interface Branch {
$type: 'Branch';
name: string;
order?: number;
}

export interface Merge {
$type: 'Merge';
branch: string;
id?: string;
tags?: string[];
type?: 'NORMAL' | 'REVERSE' | 'HIGHLIGHT';
}

export interface Checkout {
$type: 'Checkout';
branch: string;
}

export interface CherryPicking {
$type: 'CherryPicking';
id: string;
tags?: string[];
parent: string;
Austin-Fulbright marked this conversation as resolved.
Show resolved Hide resolved
}

export type DiagramOrientation = 'LR' | 'TB';
1 change: 1 addition & 0 deletions packages/mermaid/src/diagrams/git/parser/gitGraph.jison
Austin-Fulbright marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ mergeStatement
| MERGE ref commitTags COMMIT_ID STR COMMIT_TYPE commitType {yy.merge($2, $5, $7, $3)}
;


commitStatement
: COMMIT commit_arg {yy.commit($2)}
| COMMIT commitTags {yy.commit('','',yy.commitType.NORMAL,$2)}
Expand Down
5 changes: 5 additions & 0 deletions packages/parser/langium-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@
"id": "pie",
"grammar": "src/language/pie/pie.langium",
"fileExtensions": [".mmd", ".mermaid"]
},
{
"id": "gitGraph",
"grammar": "src/language/gitGraph/gitGraph.langium",
"fileExtensions": [".mmd", ".mermaid"]
}
],
"mode": "production",
Expand Down
68 changes: 68 additions & 0 deletions packages/parser/src/language/gitGraph/gitGraph.langium
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
grammar GitGraph

import "../common/common";

entry GitGraph:
NEWLINE*
'gitGraph' Direction? ':'?
NEWLINE*
(
Options?
NEWLINE*
(TitleAndAccessibilities |
statements+=Statement |
NEWLINE)*
)
;

Statement
: Commit
| Branch
| Merge
| Checkout
| CherryPicking
;


Direction:
dir=('LR' | 'TB' | 'BT') EOL;

Options:
'options' '{' rawOptions+=STRING* '}' EOL;

Commit:
'commit'
(
'id:' id=STRING
|'msg:'? message=STRING
|'tag:' tags=STRING
|'type:' name=('NORMAL' | 'REVERSE' | 'HIGHLIGHT')
)* EOL;
Branch:
'branch' name=(ID|STRING)
('order:' order=INT)?
EOL;

Merge:
'merge' branch=(ID|STRING)
(
'id:' id=STRING
|'tag:' tags=STRING
|'type:' name=('NORMAL' | 'REVERSE' | 'HIGHLIGHT')
)* EOL;

Checkout:
('checkout'|'switch') branch=(ID|STRING) EOL;

CherryPicking:
'cherry-pick'
(
'id:' id=STRING
|'tag:' tags=STRING
|'parent:' id=STRING
)* EOL;

terminal INT returns number: /[0-9]+(?=\s)/;
terminal ID returns string: /\w([-\./\w]*[-\w])?/;
terminal STRING: /"[^"]*"|'[^']*'/;

1 change: 1 addition & 0 deletions packages/parser/src/language/gitGraph/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './module.js';
53 changes: 53 additions & 0 deletions packages/parser/src/language/gitGraph/module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import type {
DefaultSharedCoreModuleContext,
LangiumCoreServices,
LangiumSharedCoreServices,
Module,
PartialLangiumCoreServices,
} from 'langium';
import {
inject,
createDefaultCoreModule,
createDefaultSharedCoreModule,
EmptyFileSystem,
} from 'langium';

import { CommonValueConverter } from '../common/valueConverter.js';
Austin-Fulbright marked this conversation as resolved.
Show resolved Hide resolved
import { MermaidGeneratedSharedModule, GitGraphGeneratedModule } from '../generated/module.js';
import { GitGraphTokenBuilder } from './tokenBuilder.js';

interface GitGraphAddedServices {
parser: {
TokenBuilder: GitGraphTokenBuilder;
ValueConverter: CommonValueConverter;
};
}

export type GitGraphServices = LangiumCoreServices & GitGraphAddedServices;

export const GitGraphModule: Module<
GitGraphServices,
PartialLangiumCoreServices & GitGraphAddedServices
> = {
parser: {
TokenBuilder: () => new GitGraphTokenBuilder(),
ValueConverter: () => new CommonValueConverter(),
},
};

export function createGitGraphServices(context: DefaultSharedCoreModuleContext = EmptyFileSystem): {
shared: LangiumSharedCoreServices;
GitGraph: GitGraphServices;
} {
const shared: LangiumSharedCoreServices = inject(
createDefaultSharedCoreModule(context),
MermaidGeneratedSharedModule
);
const GitGraph: GitGraphServices = inject(
createDefaultCoreModule({ shared }),
GitGraphGeneratedModule,
GitGraphModule
);
shared.ServiceRegistry.register(GitGraph);
return { shared, GitGraph };
}
7 changes: 7 additions & 0 deletions packages/parser/src/language/gitGraph/tokenBuilder.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { AbstractMermaidTokenBuilder } from '../common/index.js';

export class GitGraphTokenBuilder extends AbstractMermaidTokenBuilder {
public constructor() {
super(['gitGraph']);
}
}
10 changes: 10 additions & 0 deletions packages/parser/src/language/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,30 @@ export {
PacketBlock,
Pie,
PieSection,
GitGraph,
Branch,
Commit,
Merge,
isCommon,
isInfo,
isPacket,
isPacketBlock,
isPie,
isPieSection,
isGitGraph,
isBranch,
isCommit,
isMerge,
} from './generated/ast.js';
export {
InfoGeneratedModule,
MermaidGeneratedSharedModule,
PacketGeneratedModule,
PieGeneratedModule,
GitGraphGeneratedModule,
} from './generated/module.js';

export * from './gitGraph/index.js';
export * from './common/index.js';
export * from './info/index.js';
export * from './packet/index.js';
Expand Down
11 changes: 9 additions & 2 deletions packages/parser/src/parse.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { LangiumParser, ParseResult } from 'langium';

import type { Info, Packet, Pie } from './index.js';
import type { Info, Packet, Pie, GitGraph } from './index.js';

export type DiagramAST = Info | Packet | Pie;
export type DiagramAST = Info | Packet | Pie | GitGraph;

const parsers: Record<string, LangiumParser> = {};
const initializers = {
Expand All @@ -21,11 +21,18 @@ const initializers = {
const parser = createPieServices().Pie.parser.LangiumParser;
parsers.pie = parser;
},
gitGraph: async () => {
const { createGitGraphServices } = await import('./language/gitGraph/index.js');
const parser = createGitGraphServices().GitGraph.parser.LangiumParser;
parsers.gitGraph = parser;
},
} as const;

export async function parse(diagramType: 'info', text: string): Promise<Info>;
export async function parse(diagramType: 'packet', text: string): Promise<Packet>;
export async function parse(diagramType: 'pie', text: string): Promise<Pie>;
export async function parse(diagramType: 'gitGraph', text: string): Promise<GitGraph>;

export async function parse<T extends DiagramAST>(
diagramType: keyof typeof initializers,
text: string
Expand Down
18 changes: 18 additions & 0 deletions packages/parser/tests/gitGraph.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { describe, expect, it } from 'vitest';
import { GitGraph } from '../src/language/index.js';
import { gitGraphParse as parse } from './test-util.js';

describe('gitGraph', () => {
describe('Basic Parsing', () => {
it('should handle empty gitGraph', () => {
const result = parse(`gitGraph`);
expect(result.value.$type).toBe(GitGraph);
expect(result.value.statements).toHaveLength(0);
});

it('should handle gitGraph with one statement', () => {
const result = parse(`gitGraph\n A`);
expect(result.value.$type).toBe(GitGraph);
});
});
});
26 changes: 24 additions & 2 deletions packages/parser/tests/test-util.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
import type { LangiumParser, ParseResult } from 'langium';
import { expect, vi } from 'vitest';
import type { Info, InfoServices, Pie, PieServices } from '../src/language/index.js';
import { createInfoServices, createPieServices } from '../src/language/index.js';
import type {
Info,
InfoServices,
Pie,
PieServices,
GitGraph,
GitGraphServices,
} from '../src/language/index.js';
import {
createInfoServices,
createPieServices,
createGitGraphServices,
} from '../src/language/index.js';

const consoleMock = vi.spyOn(console, 'log').mockImplementation(() => undefined);

Expand Down Expand Up @@ -40,3 +51,14 @@ export function createPieTestServices() {
return { services: pieServices, parse };
}
export const pieParse = createPieTestServices().parse;

const gitGraphServices: GitGraphServices = createGitGraphServices().GitGraph;
const gitGraphParser: LangiumParser = gitGraphServices.parser.LangiumParser;
export function createGitGraphTestServices() {
const parse = (input: string) => {
return gitGraphParser.parse<GitGraph>(input);
};

return { services: gitGraphServices, parse };
}
export const gitGraphParse = createGitGraphTestServices().parse;
Loading