Skip to content

Commit 989ceb0

Browse files
authored
Allow for type hinting in construct() method (#132)
* Optionally allow "-> None" type for construct() method detection * Add test for new "-> None" syntax
1 parent 03bb842 commit 989ceb0

File tree

3 files changed

+13
-5
lines changed

3 files changed

+13
-5
lines changed

src/pythonParsing.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as crypto from "crypto";
2-
import { TextDocument, Range } from "vscode";
2+
import { Range, TextDocument } from "vscode";
33

44
/**
55
* Cache is a simple key-value store that keeps a maximum number of entries.
@@ -206,7 +206,7 @@ export class ManimClass {
206206
/**
207207
* Regular expression to match the construct() method definition.
208208
*/
209-
private static CONSTRUCT_METHOD_REGEX = /^\s*def\s+construct\s*\(self\)\s*:/;
209+
private static CONSTRUCT_METHOD_REGEX = /^\s*def\s+construct\s*\(self\)\s*(->\s*None)?\s*:/;
210210

211211
/**
212212
* The 0-based line number where the Manim Class is defined.

tests/cellRanges.test.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
import { workspace, Range } from "vscode";
21
import { describe, it } from "mocha";
3-
import { uriInWorkspace } from "./utils/testRunner";
2+
import { Range, workspace } from "vscode";
43
import { ManimCellRanges } from "../src/pythonParsing";
4+
import { uriInWorkspace } from "./utils/testRunner";
55

66
describe("Manim Cell Ranges", function () {
77
// in the expected ranges we only care about the start and end lines
88
// line numbers are 0-based here
99
const tests = [
1010
{
1111
filename: "detection_basic.py",
12-
expectedRanges: [[5, 7], [9, 10]],
12+
expectedRanges: [[5, 7], [9, 10], [16, 18]],
1313
},
1414
{
1515
filename: "detection_class_definition.py",

tests/fixtures/detection_basic.py

+8
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,14 @@ def construct(self):
1111
print("And even more code")
1212

1313

14+
class BasicNotebookWithType(Scene):
15+
16+
def construct(self) -> None:
17+
## Cell inside construct(self) marked with "None" type
18+
print("With some code None")
19+
print("With some more code None")
20+
21+
1422
class NoManimScene(Scene):
1523
def constructtttt(self):
1624
## Should not be detected as Manim Cell

0 commit comments

Comments
 (0)