Skip to content

Commit b50fc2f

Browse files
authored
Replace comments instead of whole functions (#217)
* replace comments only instead of functions * fix tests * add ci test * version bump * update prompt * add newline to formats * add newline to formats * fix spaces
1 parent b9bd099 commit b50fc2f

File tree

22 files changed

+418
-98
lines changed

22 files changed

+418
-98
lines changed

.github/workflows/test.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,13 @@ jobs:
7171
--github_api_key=${{ secrets.SCM_GITHUB_KEY }} \
7272
--pr_url=https://github.com/patched-codes/patchwork/pull/${{ steps.findPr.outputs.number }}
7373
74+
- name: Generate Docstring
75+
run: |
76+
poetry run patchwork GenerateDocstring --log debug \
77+
--openai_api_key=${{ secrets.OPENAI_KEY }} \
78+
--github_api_key=${{ secrets.SCM_GITHUB_KEY }} \
79+
--base_path=tests/cicd/generate_docstring
80+
7481
- name: Generate README
7582
run: |
7683
# Specify the parent folder you want to check

patchwork/common/context_strategy/generic.py

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
from __future__ import annotations
22

3-
from .position import Position
4-
from .protocol import ContextStrategyProtocol
3+
from patchwork.common.context_strategy.langugues import GenericLanguage
4+
from patchwork.common.context_strategy.position import Position
5+
from patchwork.common.context_strategy.protocol import ContextStrategyProtocol
56

67

78
class FullFileStrategy(ContextStrategyProtocol):
@@ -14,7 +15,7 @@ def get_contexts(self, src: list[str]) -> list[Position]:
1415
Returns:
1516
list[Position]: A list of Position objects representing the context of the source code.
1617
"""
17-
return [Position(start=0, end=len(src), start_col=0, end_col=len(src[-1]))]
18+
return [Position(start=0, end=len(src), start_col=0, end_col=len(src[-1]), language=self.language)]
1819

1920
def get_context_indexes(self, src: list[str], start: int, end: int) -> Position:
2021
"""
@@ -28,7 +29,7 @@ def get_context_indexes(self, src: list[str], start: int, end: int) -> Position:
2829
Returns:
2930
Position: A Position object containing the calculated context indexes.
3031
"""
31-
return Position(start=0, end=len(src), start_col=0, end_col=len(src[-1]))
32+
return Position(start=0, end=len(src), start_col=0, end_col=len(src[-1]), language=self.language)
3233

3334
def is_file_supported(self, filename: str, src: list[str]) -> bool:
3435
"""
@@ -43,6 +44,16 @@ def is_file_supported(self, filename: str, src: list[str]) -> bool:
4344
"""
4445
return True
4546

47+
@property
48+
def language(self) -> GenericLanguage:
49+
"""
50+
Retrieve the language for the current context strategy.
51+
52+
Returns:
53+
str: The language for the current context strategy.
54+
"""
55+
return GenericLanguage()
56+
4657

4758
class NoopStrategy(ContextStrategyProtocol):
4859
def get_contexts(self, src: list[str]) -> list[Position]:
@@ -69,7 +80,7 @@ def get_context_indexes(self, src: list[str], start: int, end: int) -> Position:
6980
Returns:
7081
Position: The context position with start and end indexes and start_col and end_col values.
7182
"""
72-
return Position(start=0, end=len(src), start_col=0, end_col=len(src[-1]))
83+
return Position(start=0, end=len(src), start_col=0, end_col=len(src[-1]), language=self.language)
7384

7485
def is_file_supported(self, filename: str, src: list[str]) -> bool:
7586
"""
@@ -83,3 +94,13 @@ def is_file_supported(self, filename: str, src: list[str]) -> bool:
8394
bool: True if the file is restricted, False otherwise.
8495
"""
8596
return True
97+
98+
@property
99+
def language(self) -> GenericLanguage:
100+
"""
101+
Retrieve the language for the current context strategy.
102+
103+
Returns:
104+
str: The language for the current context strategy.
105+
"""
106+
return GenericLanguage()

patchwork/common/context_strategy/java.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from patchwork.common.context_strategy.langugues import JavaLanguage
12
from patchwork.common.context_strategy.protocol import TreeSitterStrategy
23

34

@@ -9,7 +10,7 @@ def __init__(self, query: str):
910
Args:
1011
query (str): The search query string to be used for Java file search.
1112
"""
12-
super().__init__("java", query, [".java"])
13+
super().__init__("java", query, [".java"], JavaLanguage())
1314
self.query = query
1415

1516

patchwork/common/context_strategy/javascript.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from patchwork.common.context_strategy.langugues import JavascriptLanguage
12
from patchwork.common.context_strategy.protocol import TreeSitterStrategy
23

34
_javascript_language = "typescript"
@@ -39,7 +40,7 @@ def __init__(self):
3940
_class_query (str): Query string used for class searches within JavaScript files.
4041
_javascript_exts (tuple): A tuple containing the file extensions for JavaScript files.
4142
"""
42-
super().__init__(_javascript_language, _class_query, _javascript_exts)
43+
super().__init__(_javascript_language, _class_query, _javascript_exts, JavascriptLanguage())
4344

4445

4546
class JavascriptFunctionStrategy(TreeSitterStrategy):
@@ -55,7 +56,7 @@ def __init__(self):
5556
Returns:
5657
- None
5758
"""
58-
super().__init__(_javascript_language, _function_query, _javascript_exts)
59+
super().__init__(_javascript_language, _function_query, _javascript_exts, JavascriptLanguage())
5960

6061

6162
class JavascriptBlockStrategy(TreeSitterStrategy):
@@ -68,7 +69,7 @@ def __init__(self):
6869
- _block_query (str): The query to block JavaScript functionalities.
6970
- _javascript_exts (list): List of JavaScript file extensions.
7071
"""
71-
super().__init__(_javascript_language, _block_query, _javascript_exts)
72+
super().__init__(_javascript_language, _block_query, _javascript_exts, JavascriptLanguage())
7273

7374

7475
class JsxClassStrategy(TreeSitterStrategy):
@@ -81,7 +82,7 @@ def __init__(self):
8182
- _class_query (str): The class query parameter.
8283
- _jsx_exts (str): The JSX extensions parameter.
8384
"""
84-
super().__init__(_jsx_language, _class_query, _jsx_exts)
85+
super().__init__(_jsx_language, _class_query, _jsx_exts, JavascriptLanguage())
8586

8687

8788
class JsxFunctionStrategy(TreeSitterStrategy):
@@ -94,7 +95,7 @@ def __init__(self):
9495
- _function_query (str): The query for functions.
9596
- _jsx_exts (str): The file extension for JSX files.
9697
"""
97-
super().__init__(_jsx_language, _function_query, _jsx_exts)
98+
super().__init__(_jsx_language, _function_query, _jsx_exts, JavascriptLanguage())
9899

99100

100101
class JsxBlockStrategy(TreeSitterStrategy):
@@ -109,4 +110,4 @@ def __init__(self):
109110
Returns:
110111
- None
111112
"""
112-
super().__init__(_jsx_language, _block_query, _jsx_exts)
113+
super().__init__(_jsx_language, _block_query, _jsx_exts, JavascriptLanguage())
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
from typing_extensions import Protocol
2+
3+
4+
class LanguageProtocol(Protocol):
5+
@property
6+
def docstring_format(self) -> str:
7+
"""
8+
Retrieve the comment format for the language.
9+
10+
Returns:
11+
str: The comment format for the language.
12+
"""
13+
...
14+
15+
16+
class GenericLanguage(LanguageProtocol):
17+
def __init__(self):
18+
"""
19+
Initialize the NoopLanguage instance.
20+
"""
21+
self._comment_format = ""
22+
23+
@property
24+
def docstring_format(self) -> str:
25+
"""
26+
Retrieve the comment format for the language.
27+
28+
Returns:
29+
str: The comment format for the language.
30+
"""
31+
return self._comment_format
32+
33+
34+
class JavaLanguage(LanguageProtocol):
35+
def __init__(self):
36+
"""
37+
Initialize the JavaLanguage instance.
38+
"""
39+
self._comment_format = """\
40+
/**
41+
* <Method description>
42+
*
43+
* @param <Parameter name> <Parameter description>
44+
* @return <Return description>
45+
*/
46+
"""
47+
48+
@property
49+
def docstring_format(self) -> str:
50+
"""
51+
Retrieve the comment format for the language.
52+
53+
Returns:
54+
str: The comment format for the language.
55+
"""
56+
return self._comment_format
57+
58+
59+
class PythonLanguage(LanguageProtocol):
60+
def __init__(self):
61+
"""
62+
Initialize the PythonLanguage instance.
63+
"""
64+
# This is currently google docstring format
65+
self._comment_format = '''\
66+
"""<Method description>
67+
68+
Args:
69+
<Parameter name> <Parameter type>: <Parameter description>
70+
71+
Returns:
72+
<Return type>: <Return description>
73+
"""
74+
'''
75+
76+
@property
77+
def docstring_format(self) -> str:
78+
"""
79+
Retrieve the comment format for the language.
80+
81+
Returns:
82+
str: The comment format for the language.
83+
"""
84+
return self._comment_format
85+
86+
87+
class JavascriptLanguage(LanguageProtocol):
88+
def __init__(self):
89+
"""
90+
Initialize the JavascriptLanguage instance.
91+
"""
92+
self._comment_format = """\
93+
/**
94+
* <Method description>
95+
* @param {<Parameter type>} <Parameter Name> - <Parameter description>
96+
* @returns {<Return type>} <Return description>
97+
*/
98+
"""
99+
100+
@property
101+
def docstring_format(self) -> str:
102+
"""
103+
Retrieve the comment format for the language.
104+
105+
Returns:
106+
str: The comment format for the language.
107+
"""
108+
return self._comment_format

patchwork/common/context_strategy/position.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,16 @@
22

33
from attrs import Factory, define
44

5+
from patchwork.common.context_strategy.langugues import LanguageProtocol
6+
57

68
@define
79
class Position:
810
start: int
911
end: int
1012
start_col: int
1113
end_col: int
14+
language: LanguageProtocol
1215
meta_positions: dict[str, "Position"] = Factory(dict)
1316

1417
# def extract_lines(self, src: list[str]) -> list[str]:

patchwork/common/context_strategy/protocol.py

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from tree_sitter_languages.core import get_language, get_parser
44
from typing_extensions import Protocol
55

6+
from patchwork.common.context_strategy.langugues import LanguageProtocol
67
from patchwork.common.context_strategy.position import Position
78

89

@@ -47,20 +48,32 @@ def is_file_supported(self, filename: str, src: list[str]) -> bool:
4748
"""
4849
...
4950

51+
@property
52+
def language(self) -> LanguageProtocol:
53+
"""
54+
Retrieve the language for the current context strategy.
55+
56+
Returns:
57+
str: The language for the current context strategy.
58+
"""
59+
...
60+
5061

5162
class TreeSitterStrategy(ContextStrategyProtocol):
52-
def __init__(self, language: str, query: str, exts: list[str]):
63+
def __init__(self, language: str, query: str, exts: list[str], language_protocol: LanguageProtocol):
5364
"""
5465
Initialize the instance with specified language, query, and file extensions.
5566
5667
Args:
5768
language (str): The programming language for the search.
5869
query (str): The search query.
5970
exts (list[str]): The list of file extensions to consider for the search.
71+
language_protocol (LanguageProtocol): The language protocol associated.
6072
"""
61-
self.language = language
73+
self.tree_sitter_language = language
6274
self.query = query
6375
self.exts = exts
76+
self.language_protocol = language_protocol
6477

6578
def query_src(self, src: list[str]):
6679
"""
@@ -72,8 +85,8 @@ def query_src(self, src: list[str]):
7285
Returns:
7386
list: Returns a list of captures that match the query in the source code's abstract syntax tree (AST).
7487
"""
75-
language = get_language(self.language)
76-
parser = get_parser(self.language)
88+
language = get_language(self.tree_sitter_language)
89+
parser = get_parser(self.tree_sitter_language)
7790
tree = parser.parse("".join(src).encode("utf-8-sig"))
7891
return language.query(self.query).captures(tree.root_node)
7992

@@ -100,6 +113,7 @@ def get_contexts(self, src: list[str]) -> list[Position]:
100113
end=node.end_point[0] + 1,
101114
start_col=node.start_point[1],
102115
end_col=node.end_point[1] + 1,
116+
language=self.language,
103117
)
104118
positions.append(position)
105119

@@ -115,6 +129,7 @@ def get_contexts(self, src: list[str]) -> list[Position]:
115129
end=node.end_point[0] + 1,
116130
start_col=node.start_point[1],
117131
end_col=node.end_point[1] + 1,
132+
language=self.language,
118133
)
119134
break
120135

@@ -150,3 +165,7 @@ def is_file_supported(self, filename: str, src: list[str]) -> bool:
150165
bool: True if the file's extension is in the list of supported extensions and `src` is not empty, otherwise False.
151166
"""
152167
return any(filename.endswith(ext) for ext in self.exts) and len(src) > 0
168+
169+
@property
170+
def language(self) -> LanguageProtocol:
171+
return self.language_protocol

patchwork/common/context_strategy/python.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
from patchwork.common.context_strategy.position import Position
1818

19+
from .langugues import PythonLanguage
1920
from .protocol import ContextStrategyProtocol
2021

2122

@@ -58,6 +59,7 @@ def _visit(self, node: libcst.CSTNode) -> Position:
5859
end=code_range.end.line,
5960
start_col=code_range.start.column - 1,
6061
end_col=code_range.end.column,
62+
language=PythonLanguage(),
6163
)
6264
self.positions.append(position)
6365
return position
@@ -98,6 +100,7 @@ def visit_FunctionDef(self, node: FunctionDef) -> Optional[bool]:
98100
end=code_range.end.line,
99101
start_col=code_range.start.column - 1,
100102
end_col=code_range.end.column,
103+
language=PythonLanguage(),
101104
)
102105
position.meta_positions["comment"] = comment_position
103106
return True
@@ -226,6 +229,10 @@ def is_file_supported(self, filename: str, src: list[str]) -> bool:
226229
return False
227230
return filename.endswith(".py") and len(src) > 0
228231

232+
@property
233+
def language(self) -> PythonLanguage:
234+
return PythonLanguage()
235+
229236

230237
class PythonFunctionStrategy(PythonStrategy):
231238
def __init__(self):

0 commit comments

Comments
 (0)