Skip to content

Commit

Permalink
Added projection times, tuned others
Browse files Browse the repository at this point in the history
  • Loading branch information
ArturHD committed Oct 22, 2023
1 parent 3778cd3 commit e68e274
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 6 deletions.
15 changes: 14 additions & 1 deletion apps/python-dsl/public/examples/example.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
from dsl import db
# Example of projections puts, increment (++), times

counter = 0
print("Hello, the counter is: ", end="")
print(counter)

counter = 1
for index in range(10):
counter += 1
print("Counter is now: ", end="")
print(counter)


# Example with dls for tables and variables
from dsl import db

old_pattern = "Mister"
new_pattern = "Mr."

Expand Down
7 changes: 4 additions & 3 deletions apps/python-dsl/src/projections/PutsProjection.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
import type { Match } from "@puredit/parser";
import type { FocusGroup } from "@puredit/projections/focus";
import TextInput from "@puredit/projections/TextInput.svelte";
// import { validateFromList } from "@puredit/projections/shared";
// import type { ContextGlobal } from "./context";
export let isNew: boolean;
export let view: EditorView | null;
Expand All @@ -26,7 +24,7 @@
</script>

<span class="inline-flex">
<span>puts </span>
<span class="noindent">puts </span>
<TextInput
className={highlightingFor(state, [tags.atom])}
node={match.args.anything}
Expand All @@ -38,4 +36,7 @@
</span>

<style>
.noindent {
margin-left: 0px;
}
</style>
51 changes: 51 additions & 0 deletions apps/python-dsl/src/projections/TimesProjection.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<script lang="ts">
import { onMount } from "svelte";
import { tags } from "@lezer/highlight";
import type { EditorState } from "@codemirror/state";
import type { EditorView } from "@codemirror/view";
import { highlightingFor } from "@codemirror/language";
import type { Match } from "@puredit/parser";
import type { FocusGroup } from "@puredit/projections/focus";
import TextInput from "@puredit/projections/TextInput.svelte";
// import { validateFromList } from "@puredit/projections/shared";
// import type { ContextGlobal } from "./context";
export let isNew: boolean;
export let view: EditorView | null;
export let match: Match;
export let state: EditorState;
export let focusGroup: FocusGroup;
onMount(() => {
if (isNew) {
requestAnimationFrame(() => {
focusGroup.first();
});
}
});
</script>

<span class="inline-flex">
<span class="noindent" />
<TextInput
className={highlightingFor(state, [tags.atom])}
node={match.args.times_value}
{state}
{view}
{focusGroup}
placeholder="# times"
/>
<span class="boundleft">.times:</span>
</span>

<style>
.boundleft {
margin-left: -10px;
}
.noindent {
margin-left: -10px;
}
.text-right {
text-align: right;
}
</style>
2 changes: 2 additions & 0 deletions apps/python-dsl/src/projections/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { trimProjection } from "./trimProjection";
import { swapProjection } from "./swapProjection";
import { incrementProjection } from "./incrementProjection";
import { putsProjection } from "./putsProjection";
import { timesProjection } from "./timesProjection";

export const projectionPluginConfig: ProjectionPluginConfig = {
parser: pythonParser,
Expand All @@ -17,6 +18,7 @@ export const projectionPluginConfig: ProjectionPluginConfig = {
swapProjection,
incrementProjection,
putsProjection,
timesProjection,
],
globalContextVariables,
globalContextValues,
Expand Down
4 changes: 2 additions & 2 deletions apps/python-dsl/src/projections/putsProjection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ const anything = arg("anything", "string");
// like: print(f"VarX is {VarX}").
// Just using 'print(${anything})' does not work.
export const [pattern, draft] = pythonParser.statementPattern`
print(${anything})
print(${anything}, end="")
`;

export const widget = svelteProjection(PutsProjection);

export const putsProjection: Projection = {
name: "puts",
description: "Prints string to console",
description: "Prints string to console without newline",
pattern,
draft,
requiredContextVariables: [],
Expand Down
27 changes: 27 additions & 0 deletions apps/python-dsl/src/projections/timesProjection.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import type { Text } from "@codemirror/state";
import { arg, block, contextVariable } from "@puredit/parser";
import type { Match } from "@puredit/parser";
import { stringLiteralValue } from "@puredit/projections/shared";
import { svelteProjection } from "@puredit/projections/svelte";
import type { Projection } from "@puredit/projections/types";
import TimesProjection from "./TimesProjection.svelte";
// import type { ContextColumns, ContextTables } from "./context";
import { pythonParser } from "./parser";

const times_value = arg("times_value", "integer");

export const [pattern, draft] = pythonParser.statementPattern`
for index in range(${times_value}):
${block({})}
`;

export const widget = svelteProjection(TimesProjection);

export const timesProjection: Projection = {
name: "times",
description: "Loop a block X times",
pattern,
draft,
requiredContextVariables: [],
widgets: [widget],
};

0 comments on commit e68e274

Please sign in to comment.