Skip to content

Parse task name from decorator. #3

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 1 commit into from
Jan 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
25 changes: 24 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,27 @@
{
"editor.defaultFormatter": "esbenp.prettier-vscode",
"[javascriptreact]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
"[typescriptreact]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
"[typescript]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
"[scss]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
"[javascript]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
"[html]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
"[css]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
"[yaml]": {
"editor.defaultFormatter": null
},
"editor.formatOnSave": true
}
16 changes: 13 additions & 3 deletions src/providers/taskProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,15 +230,24 @@ export class PyTaskProvider implements vscode.TreeDataProvider<TreeItemType> {
const lines = content.split("\n");

let isDecorated = false;
let taskName: string | undefined;
for (let i = 0; i < lines.length; i++) {
const line = lines[i].trim();

// Check for decorators
if (line.startsWith("@")) {
// Handle both @task and @pytask.task(...) patterns
isDecorated =
(hasTaskImport && line === `@${taskAlias}`) ||
(hasTaskImport && (line === `@${taskAlias}` || line.startsWith(`@${taskAlias}(`))) ||
(hasPytaskImport && line.startsWith(`@${pytaskAlias}.task`));

// Extract name from decorator if present
if (isDecorated) {
const nameMatch = line.match(/name\s*=\s*["']([^"']+)["']/);
if (nameMatch) {
taskName = nameMatch[1];
}
}
continue;
}

Expand All @@ -248,9 +257,10 @@ export class PyTaskProvider implements vscode.TreeDataProvider<TreeItemType> {
const funcName = funcMatch[1];
// Add if it's a task_* function or has a task decorator
if (funcName.startsWith("task_") || isDecorated) {
tasks.push(new TaskItem(funcName, filePath, i + 1));
tasks.push(new TaskItem(taskName || funcName, filePath, i + 1));
}
isDecorated = false; // Reset decorator flag
isDecorated = false;
taskName = undefined; // Reset task name
}
}

Expand Down
18 changes: 18 additions & 0 deletions src/test/providers/taskProvider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -363,4 +363,22 @@ def decorated_task(): # line 8
expect(tasks[1].label).to.equal("task_one");
expect(tasks[1].lineNumber).to.equal(4);
});

test("Should handle tasks that set a new name via the decorator", () => {
const content = `
from pytask import task

@task(name="new_name")
def task_one():
pass

@task(kwargs={}, name="second_new_name")
def task_two():
pass
`;
const tasks = provider.findTaskFunctions(dummyPath, content);
expect(tasks).to.have.lengthOf(2, "Should find both tasks");
expect(tasks[0].label).to.equal("new_name");
expect(tasks[1].label).to.equal("second_new_name");
});
});
Loading