Skip to content
Open
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

- Added doxygen format support ([#120](https://github.com/NilsJPWerner/autoDocstring/issues/120)) (@daluar @PhilipNelson5)
- Disable HTML-escaping behavior globally ([#235](https://github.com/NilsJPWerner/autoDocstring/issues/235)) (@PhilipNelson5)
- Improve definition detection to allow reserved words in the default arguments ([#190](https://github.com/NilsJPWerner/autoDocstring/issues/190)) (@ylfzq @PhilipNelson5)

[All Changes](https://github.com/NilsJPWerner/autoDocstring/compare/v0.6.1...master)

Expand Down
11 changes: 7 additions & 4 deletions src/parse/get_definition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,15 @@ export function getDefinition(document: string, linePosition: number): string {
return "";
}

const pattern = /\b(((async\s+)?\s*def)|\s*class)\b/g;
// Match (async)? def name (...) and class name (...)
const pattern = /\b(((async\s+)?\s*def)|\s*class)\s+\w+\s*\(.*?\)/g;

// Get starting index of last def match in the preceding text
// Get starting index of last def or class match in the preceding text
let index: number;
while (pattern.test(precedingText)) {
index = pattern.lastIndex - RegExp.lastMatch.length;
let match = pattern.exec(precedingText);
while (match) {
index = match.index;
match = pattern.exec(precedingText);
}

if (index == undefined) {
Expand Down
20 changes: 20 additions & 0 deletions src/test/parse/get_definition.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ describe("getDefinition()", () => {
expect(result).to.equal("def basic_function(param1, param2 = abc):");
});

it("should get a function definition that uses reserved words as default arguments", () => {
const result = getDefinition(functionWithReservedWordDefaults, 10);

expect(result).to.equal('def function_with_reserved_word_defaults( a: int, b="def", c="class" ) -> None:');
});

it("should get an indented function definition", () => {
const result = getDefinition(indentedFunction, 4);

Expand Down Expand Up @@ -84,6 +90,20 @@ def basic_function(param1, param2 = abc):
def something_else():
`;

const functionWithReservedWordDefaults = `
def another_func(a: str = "class, b: int): -> str
return a
class Foo(object):
pass
def function_with_reserved_word_defaults(
a: int, b="def", c="class"
) -> None:
"""
pass
`;

const indentedFunction = `
Something Else
Expand Down