-
Notifications
You must be signed in to change notification settings - Fork 5
Simplify to register additional/custom Typir services #78
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
Merged
Merged
Changes from 14 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
5f88820
simplified kind registry
JohannesMeierSE afea1cc
improved error message
JohannesMeierSE bb4a173
made test utility more flexible
JohannesMeierSE a6d81a2
fixed bug: the logic to ensure that types are not created multiple ti…
JohannesMeierSE 65d0faf
new feature: make the $names of kinds configurable
JohannesMeierSE 6ba8bef
enable additional services in Typir core with `createTypirServicesWit…
JohannesMeierSE 4ce6969
enable additional services in Typir-Langium with `createTypirLangiumS…
JohannesMeierSE 65aefc5
Langium services are stored in the TypirLangium services now
JohannesMeierSE d5d1421
rebasing fixes
JohannesMeierSE 2c7b673
demonstrate API for additional Typir services for the custom matrix t…
JohannesMeierSE faee192
polished the CHANGELOG.md
JohannesMeierSE d810785
specify sub-super-relationships of language nodes for the predefined …
JohannesMeierSE e027d6f
wrote documentation for customization
JohannesMeierSE ab78f9d
sketched alternative function to support additional services
JohannesMeierSE 01d1557
improvements according to the review
JohannesMeierSE 0c00785
removed skipped alternative solution, added forth customization param…
JohannesMeierSE File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,75 @@ | ||
| # Customize default implementations | ||
| # Customize Typir | ||
|
|
||
| This describes how the default implementations of Typir can be customized. | ||
| This describes how the default behaviour of Typir can be customized. | ||
| How to use custom types in Typir is described [in this section](./kinds/custom-types.md). | ||
|
|
||
| If you are already familar with Langium and its [strategies for customization](https://langium.org/docs/reference/configuration-services/#customization), feel free to skip this section, since the strategies and even the implementation are nearly the same. | ||
|
|
||
| As described in the [design section](./design.md), nearly all features of Typir are exposed by APIs in form of interfaces, | ||
| for which Typir provides classes implementing these interfaces as default implementations. These interfaces and implementations are composed in `typir.ts`. | ||
| for which Typir provides classes implementing these interfaces as default implementations. These interfaces and implementations are composed in ... | ||
|
|
||
| TODO | ||
| - `typir.ts` for Typir (core) | ||
| - `typir-langium.ts` for Typir-Langium | ||
|
|
||
| Some examples how to customize existing services and how to add new services are sketched in `customization-example.test.ts`. | ||
|
|
||
|
|
||
| ## Customize the implementation of existing services | ||
|
|
||
| To customize or replace the default implementation for an existing Typir service, just provide another implementation when initializing the Typir services. | ||
| As an example, the existing factory to create classes is replaced to allow two super classes now (default is one super class only): | ||
|
|
||
| ```typescript | ||
| const customizedTypir = createTypirServices({ | ||
| factory: { | ||
| Classes: services => new ClassKind(services, { maximumNumberOfSuperClasses: 2 }), | ||
| }, | ||
| // ... customize as many existing services as you like ... | ||
| }); | ||
| ``` | ||
|
|
||
| ## Add additional services | ||
|
|
||
| Additional services need to be explicitly specified. | ||
| In general, you can add an arbitrary number of services, which might be deeply grouped. | ||
| It is even possible to add new services to already existing groups. | ||
| In the following example, an additional factory for classes is exposed as service: | ||
|
|
||
| ```typescript | ||
| type AdditionalExampleTypirServices = { | ||
| readonly factory: { | ||
| readonly OtherClasses: ClassFactoryService<TestLanguageNode>; | ||
| }, | ||
| }; | ||
| ``` | ||
|
|
||
| Provide implementations for all added services when you instantiate the Typir services. | ||
| Instead of `createTypirServices`, use `createTypirServicesWithAdditionalServices` instead: | ||
|
|
||
| ```typescript | ||
| const customizedTypir: TypirServices<TestLanguageNode> & AdditionalExampleTypirServices = createTypirServicesWithAdditionalServices<TestLanguageNode, AdditionalExampleTypirServices>({ | ||
| factory: { | ||
| OtherClasses: services => new ClassKind(services, { maximumNumberOfSuperClasses: 2, $name: 'OtherClass' }), | ||
| }, | ||
| }); | ||
| ``` | ||
|
|
||
| TypeScript don't force you to write `TypirServices<TestLanguageNode> & AdditionalExampleTypirServices` in the code snipped above, but makes explicit what is going on here. | ||
| To simplify the code, it is possible (but not mandatory) to introduce a TypeScript type like the following and to use it instead: | ||
|
|
||
| ```typescript | ||
| type ExampleTypirServices = TypirServices<TestLanguageNode> & AdditionalExampleTypirServices; | ||
| ``` | ||
|
|
||
| Newly added services are usable by all other services, including new services and existing services. | ||
| The latter is important when customizing default implementations, when the custom implementation depends on the new services. | ||
|
|
||
| It is possible to provide implementations for new services together with customizations for existing services: | ||
|
|
||
| ```typescript | ||
| const customizedTypir: ExampleTypirServices = createTypirServicesWithAdditionalServices<TestLanguageNode, AdditionalExampleTypirServices>({ | ||
| // 1st argument: mandatory implementations for all new services | ||
| }, { | ||
| // 2nd argument: customize some existing services here | ||
| }); | ||
| ``` | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.