Skip to content

Commit e5b2c6b

Browse files
authored
Generate Docstring Patchflow (#156)
* sav * sav * dogfood context strategy * fix autofix * fix autofix * fix generic contextes * lint * fix docs * add force * allow force * fix affectedCode * sav * sav * finalize python docs * lint and finalize
1 parent a045bbe commit e5b2c6b

File tree

31 files changed

+1192
-330
lines changed

31 files changed

+1192
-330
lines changed
File renamed without changes.

patchwork/steps/ExtractCode/context_strategy/context_strategies.py renamed to patchwork/common/context_strategy/context_strategies.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ class ContextStrategies:
4545
NOOP,
4646
]
4747

48+
FUNCTION = [PYTHON_FUNCTION, JAVA_METHOD, JAVASCRIPT_FUNCTION, JSX_FUNCTION]
49+
4850
__MAPPING: dict[str, ContextStrategyProtocol] = {
4951
FULL_FILE: FullFileStrategy(),
5052
NOOP: NoopStrategy(),
@@ -63,10 +65,32 @@ class ContextStrategies:
6365

6466
@staticmethod
6567
def get_context_strategy(name: str) -> ContextStrategyProtocol | None:
68+
"""
69+
Retrieve a context strategy based on the given name.
70+
71+
Args:
72+
name (str): The name of the context strategy to retrieve.
73+
74+
Returns:
75+
ContextStrategyProtocol | None: The context strategy associated with the given name,
76+
or None if not found.
77+
"""
6678
return ContextStrategies.__MAPPING.get(name, None)
6779

6880
@staticmethod
6981
def get_context_strategies(name: str, *args) -> list[ContextStrategyProtocol]:
82+
"""
83+
Collect context strategies based on the provided arguments.
84+
85+
Args:
86+
name (str): The initial strategy name to look up.
87+
*args: Additional strategy names to look up.
88+
89+
Returns:
90+
list[ContextStrategyProtocol]: A list of found context strategy instances that match the
91+
provided names. If a strategy can't be found, it is not included
92+
in the list.
93+
"""
7094
rv = []
7195
for arg in [name, *args]:
7296
element = ContextStrategies.get_context_strategy(arg)
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
from .position import Position
2+
from .protocol import ContextStrategyProtocol
3+
4+
5+
class FullFileStrategy(ContextStrategyProtocol):
6+
def get_contexts(self, src: list[str]) -> list[Position]:
7+
"""Return a list of Position objects representing the context of the source code.
8+
9+
Args:
10+
src (list[str]): The source code as a list of strings.
11+
12+
Returns:
13+
list[Position]: A list of Position objects representing the context of the source code.
14+
"""
15+
return [Position(start=0, end=len(src), start_col=0, end_col=len(src[-1]))]
16+
17+
def get_context_indexes(self, src: list[str], start: int, end: int) -> Position:
18+
"""
19+
Calculate the context indexes based on the input source list and start/end positions.
20+
21+
Args:
22+
src (list[str]): The source list of strings.
23+
start (int): The starting index.
24+
end (int): The ending index.
25+
26+
Returns:
27+
Position: A Position object containing the calculated context indexes.
28+
"""
29+
return Position(start=0, end=len(src), start_col=0, end_col=len(src[-1]))
30+
31+
def is_file_supported(self, filename: str, src: list[str]) -> bool:
32+
"""
33+
Checks if the given filename has a supported format based on a list of supported extensions.
34+
35+
Args:
36+
filename (str): The name of the file to check.
37+
src (list[str]): A list of supported file extensions.
38+
39+
Returns:
40+
bool: True if the file's extension is in the list of supported extensions, False otherwise.
41+
"""
42+
return True
43+
44+
45+
class NoopStrategy(ContextStrategyProtocol):
46+
def get_contexts(self, src: list[str]) -> list[Position]:
47+
"""
48+
Get the list of Position objects representing contexts based on the source list provided.
49+
50+
Args:
51+
src (list[str]): A list of source strings.
52+
53+
Returns:
54+
list[Position]: A list of Position objects representing contexts.
55+
"""
56+
return []
57+
58+
def get_context_indexes(self, src: list[str], start: int, end: int) -> Position:
59+
"""
60+
Get the context indexes for source code based on the start and end positions.
61+
62+
Args:
63+
src (list[str]): The list of source code lines.
64+
start (int): The starting position.
65+
end (int): The ending position.
66+
67+
Returns:
68+
Position: The context position with start and end indexes and start_col and end_col values.
69+
"""
70+
return Position(start=0, end=len(src), start_col=0, end_col=len(src[-1]))
71+
72+
def is_file_supported(self, filename: str, src: list[str]) -> bool:
73+
"""
74+
Check if the given filename is restricted based on a list of restricted keywords.
75+
76+
Args:
77+
filename (str): The name of the file to check.
78+
src (list[str]): A list of strings representing restricted keywords.
79+
80+
Returns:
81+
bool: True if the file is restricted, False otherwise.
82+
"""
83+
return True
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
from patchwork.common.context_strategy.protocol import TreeSitterStrategy
2+
3+
4+
class JavaStrategy(TreeSitterStrategy):
5+
def __init__(self, query: str):
6+
"""
7+
Initialize the JavaSearcher instance.
8+
9+
Args:
10+
query (str): The search query string to be used for Java file search.
11+
"""
12+
super().__init__("java", query, [".java"])
13+
self.query = query
14+
15+
16+
class JavaClassStrategy(JavaStrategy):
17+
def __init__(self):
18+
"""
19+
Initialize the current class by calling the parent class's __init__ method.
20+
The specific class to be initialized should have a class_declaration marked by @node.
21+
"""
22+
super().__init__(
23+
"""
24+
(class_declaration) @node
25+
""".strip()
26+
)
27+
28+
29+
class JavaMethodStrategy(JavaStrategy):
30+
def __init__(self):
31+
"""
32+
Initialize the newly created object by inheriting properties and
33+
methods from the parent class.
34+
35+
Parameters:
36+
- self: instance of the class
37+
38+
Returns:
39+
- None
40+
"""
41+
super().__init__(
42+
"""
43+
[
44+
(block_comment) @comment
45+
(method_declaration) @node
46+
]
47+
""".strip()
48+
)
49+
50+
51+
class JavaBlockStrategy(JavaStrategy):
52+
def __init__(self):
53+
"""
54+
Initialize the class by calling the parent class's constructor.
55+
56+
Parameters:
57+
- self: The object instance.
58+
"""
59+
super().__init__(
60+
"""
61+
(block) @node
62+
""".strip()
63+
)
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
from patchwork.common.context_strategy.protocol import TreeSitterStrategy
2+
3+
_javascript_language = "typescript"
4+
_jsx_language = "tsx"
5+
6+
_class_query = """
7+
[
8+
(class_declaration)
9+
(interface_declaration)
10+
(enum_declaration)
11+
] @node
12+
""".strip()
13+
_function_query = """
14+
[
15+
(comment) @comment
16+
[
17+
( function )
18+
( function_declaration )
19+
( generator_function_declaration )
20+
( arrow_function )
21+
] @node
22+
]
23+
""".strip()
24+
_block_query = """
25+
(statement_block) @node
26+
""".strip()
27+
28+
_javascript_exts = [".js", ".ts"]
29+
_jsx_exts = [".jsx", ".tsx"]
30+
31+
32+
class JavascriptClassStrategy(TreeSitterStrategy):
33+
def __init__(self):
34+
"""
35+
Initializes the parent class with predefined attributes for handling JavaScript files.
36+
37+
Attributes:
38+
_javascript_language (str): Language specification for JavaScript.
39+
_class_query (str): Query string used for class searches within JavaScript files.
40+
_javascript_exts (tuple): A tuple containing the file extensions for JavaScript files.
41+
"""
42+
super().__init__(_javascript_language, _class_query, _javascript_exts)
43+
44+
45+
class JavascriptFunctionStrategy(TreeSitterStrategy):
46+
def __init__(self):
47+
"""
48+
Initialize the current class instance as a subclass, passing specific parameters related to JavaScript handling to the superclass constructor.
49+
50+
Parameters:
51+
- _javascript_language (str): The language being used for JavaScript.
52+
- _function_query (str): The query for functions in JavaScript.
53+
- _javascript_exts (list): List of extensions related to JavaScript.
54+
55+
Returns:
56+
- None
57+
"""
58+
super().__init__(_javascript_language, _function_query, _javascript_exts)
59+
60+
61+
class JavascriptBlockStrategy(TreeSitterStrategy):
62+
def __init__(self):
63+
"""
64+
Initialize the object by calling the superclass constructor with specific arguments.
65+
66+
Parameters:
67+
- _javascript_language (str): The language used for JavaScript.
68+
- _block_query (str): The query to block JavaScript functionalities.
69+
- _javascript_exts (list): List of JavaScript file extensions.
70+
"""
71+
super().__init__(_javascript_language, _block_query, _javascript_exts)
72+
73+
74+
class JsxClassStrategy(TreeSitterStrategy):
75+
def __init__(self):
76+
"""
77+
Initialize the object by calling the superclass's initializer with specific parameters.
78+
79+
Parameters:
80+
- _jsx_language (str): The JSX language parameter.
81+
- _class_query (str): The class query parameter.
82+
- _jsx_exts (str): The JSX extensions parameter.
83+
"""
84+
super().__init__(_jsx_language, _class_query, _jsx_exts)
85+
86+
87+
class JsxFunctionStrategy(TreeSitterStrategy):
88+
def __init__(self):
89+
"""
90+
Initialize the instance by calling the parent class's constructor with predefined arguments.
91+
92+
Parameters:
93+
- _jsx_language (str): The language for JSX.
94+
- _function_query (str): The query for functions.
95+
- _jsx_exts (str): The file extension for JSX files.
96+
"""
97+
super().__init__(_jsx_language, _function_query, _jsx_exts)
98+
99+
100+
class JsxBlockStrategy(TreeSitterStrategy):
101+
def __init__(self):
102+
"""
103+
Initializes the object by calling the superclass initializer with the
104+
provided language, block query, and file extensions for JSX processing.
105+
106+
Parameters:
107+
- self: the object itself
108+
109+
Returns:
110+
- None
111+
"""
112+
super().__init__(_jsx_language, _block_query, _jsx_exts)
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import dataclasses
2+
3+
4+
@dataclasses.dataclass(slots=True)
5+
class Position:
6+
start: int
7+
end: int
8+
start_col: int
9+
end_col: int
10+
meta_positions: dict[str, "Position"] = dataclasses.field(default_factory=dict)
11+
12+
# def extract_lines(self, src: list[str]) -> list[str]:
13+
# return src[self.start : self.end]
14+
#
15+
# def extract_text(self, src: list[str]) -> list[str]:
16+
# lines = self.extract_lines(src)
17+
# lines[0] = lines[0][self.start_col :]
18+
# lines[-1] = lines[-1][: self.end_col]
19+
# return lines
20+
#
21+
# @contextlib.contextmanager
22+
# def replace_text(self, src: list[str]) -> list[str]:
23+
# container = self.extract_text(src)
24+
# yield container
25+
# src[self.start] = src[self.start][: self.start_col + 1] + container[0]
26+
# src[self.start + 1 : self.end - 2] = container[1:-1]
27+
# src[self.end - 1] = src[self.end - 1][self.end_col - 1 :] + container[-1]
28+
# return
29+
30+
31+
# @dataclasses.dataclass(slots=True, frozen=True)
32+
# class FileSource:
33+
# filepath: Path
34+
# src: list[str]
35+
#
36+
# @contextlib.contextmanager
37+
# def replace_text(self, position: Position) -> list[str]:
38+
# with position.replace_text(self.src) as container:
39+
# yield container
40+
# return
41+
#
42+
# def write(self):
43+
# self.filepath.write_text("".join(self.src))

0 commit comments

Comments
 (0)