Skip to content

Commit

Permalink
feat: Source Maps for Code Generation (#714)
Browse files Browse the repository at this point in the history
Closes #30

### Summary of Changes

- changed code generation to return traced nodes instead of text
- add option to generate source maps by providing an optional path

---------

Co-authored-by: megalinter-bot <129584137+megalinter-bot@users.noreply.github.com>
Co-authored-by: Lars Reimann <mail@larsreimann.com>
  • Loading branch information
3 people authored Nov 2, 2023
1 parent ac6d42a commit 64b9e07
Show file tree
Hide file tree
Showing 38 changed files with 407 additions and 154 deletions.
27 changes: 23 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 8 additions & 3 deletions packages/safe-ds-cli/src/cli/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@ import fs from 'node:fs';
import path from 'node:path';
import { extractDocument } from './cli-util.js';

export const generate = async (fileName: string, opts: GenerateOptions): Promise<void> => {
export const generate = async (fileName: string, opts: CliGenerateOptions): Promise<void> => {
const services = (await createSafeDsServicesWithBuiltins(NodeFileSystem)).SafeDs;
const document = await extractDocument(fileName, services);
const destination = opts.destination ?? path.join(path.dirname(fileName), 'generated');
const generatedFiles = services.generation.PythonGenerator.generate(document, URI.file(path.resolve(destination)));
const generatedFiles = services.generation.PythonGenerator.generate(document, {
destination: URI.file(path.resolve(destination)),
createSourceMaps: opts.sourcemaps,
});

for (const file of generatedFiles) {
const fsPath = URI.parse(file.uri).fsPath;
Expand All @@ -25,6 +28,8 @@ export const generate = async (fileName: string, opts: GenerateOptions): Promise
console.log(chalk.green(`Python code generated successfully.`));
};

export interface GenerateOptions {
export interface CliGenerateOptions {
destination?: string;
sourcemaps: boolean;
quiet: boolean;
}
1 change: 1 addition & 0 deletions packages/safe-ds-cli/src/cli/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ program
.option('-d, --destination <dir>', 'destination directory of generation')
.option('-r, --root <dir>', 'source root folder')
.option('-q, --quiet', 'whether the program should print something', false)
.option('-s, --sourcemaps', 'whether source maps should be generated', false)
.description('generate Python code')
.action(generate);

Expand Down
1 change: 1 addition & 0 deletions packages/safe-ds-lang/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"chevrotain": "^11.0.3",
"glob": "^10.3.10",
"langium": "^2.1.0",
"source-map": "^0.7.4",
"vscode-languageserver": "^9.0.1",
"vscode-languageserver-textdocument": "^1.0.11"
},
Expand Down
Loading

0 comments on commit 64b9e07

Please sign in to comment.