-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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
- Loading branch information
Showing
31 changed files
with
1,192 additions
and
330 deletions.
There are no files selected for viewing
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
from .position import Position | ||
from .protocol import ContextStrategyProtocol | ||
|
||
|
||
class FullFileStrategy(ContextStrategyProtocol): | ||
def get_contexts(self, src: list[str]) -> list[Position]: | ||
"""Return a list of Position objects representing the context of the source code. | ||
Args: | ||
src (list[str]): The source code as a list of strings. | ||
Returns: | ||
list[Position]: A list of Position objects representing the context of the source code. | ||
""" | ||
return [Position(start=0, end=len(src), start_col=0, end_col=len(src[-1]))] | ||
|
||
def get_context_indexes(self, src: list[str], start: int, end: int) -> Position: | ||
""" | ||
Calculate the context indexes based on the input source list and start/end positions. | ||
Args: | ||
src (list[str]): The source list of strings. | ||
start (int): The starting index. | ||
end (int): The ending index. | ||
Returns: | ||
Position: A Position object containing the calculated context indexes. | ||
""" | ||
return Position(start=0, end=len(src), start_col=0, end_col=len(src[-1])) | ||
|
||
def is_file_supported(self, filename: str, src: list[str]) -> bool: | ||
""" | ||
Checks if the given filename has a supported format based on a list of supported extensions. | ||
Args: | ||
filename (str): The name of the file to check. | ||
src (list[str]): A list of supported file extensions. | ||
Returns: | ||
bool: True if the file's extension is in the list of supported extensions, False otherwise. | ||
""" | ||
return True | ||
|
||
|
||
class NoopStrategy(ContextStrategyProtocol): | ||
def get_contexts(self, src: list[str]) -> list[Position]: | ||
""" | ||
Get the list of Position objects representing contexts based on the source list provided. | ||
Args: | ||
src (list[str]): A list of source strings. | ||
Returns: | ||
list[Position]: A list of Position objects representing contexts. | ||
""" | ||
return [] | ||
|
||
def get_context_indexes(self, src: list[str], start: int, end: int) -> Position: | ||
""" | ||
Get the context indexes for source code based on the start and end positions. | ||
Args: | ||
src (list[str]): The list of source code lines. | ||
start (int): The starting position. | ||
end (int): The ending position. | ||
Returns: | ||
Position: The context position with start and end indexes and start_col and end_col values. | ||
""" | ||
return Position(start=0, end=len(src), start_col=0, end_col=len(src[-1])) | ||
|
||
def is_file_supported(self, filename: str, src: list[str]) -> bool: | ||
""" | ||
Check if the given filename is restricted based on a list of restricted keywords. | ||
Args: | ||
filename (str): The name of the file to check. | ||
src (list[str]): A list of strings representing restricted keywords. | ||
Returns: | ||
bool: True if the file is restricted, False otherwise. | ||
""" | ||
return True |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
from patchwork.common.context_strategy.protocol import TreeSitterStrategy | ||
|
||
|
||
class JavaStrategy(TreeSitterStrategy): | ||
def __init__(self, query: str): | ||
""" | ||
Initialize the JavaSearcher instance. | ||
Args: | ||
query (str): The search query string to be used for Java file search. | ||
""" | ||
super().__init__("java", query, [".java"]) | ||
self.query = query | ||
|
||
|
||
class JavaClassStrategy(JavaStrategy): | ||
def __init__(self): | ||
""" | ||
Initialize the current class by calling the parent class's __init__ method. | ||
The specific class to be initialized should have a class_declaration marked by @node. | ||
""" | ||
super().__init__( | ||
""" | ||
(class_declaration) @node | ||
""".strip() | ||
) | ||
|
||
|
||
class JavaMethodStrategy(JavaStrategy): | ||
def __init__(self): | ||
""" | ||
Initialize the newly created object by inheriting properties and | ||
methods from the parent class. | ||
Parameters: | ||
- self: instance of the class | ||
Returns: | ||
- None | ||
""" | ||
super().__init__( | ||
""" | ||
[ | ||
(block_comment) @comment | ||
(method_declaration) @node | ||
] | ||
""".strip() | ||
) | ||
|
||
|
||
class JavaBlockStrategy(JavaStrategy): | ||
def __init__(self): | ||
""" | ||
Initialize the class by calling the parent class's constructor. | ||
Parameters: | ||
- self: The object instance. | ||
""" | ||
super().__init__( | ||
""" | ||
(block) @node | ||
""".strip() | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
from patchwork.common.context_strategy.protocol import TreeSitterStrategy | ||
|
||
_javascript_language = "typescript" | ||
_jsx_language = "tsx" | ||
|
||
_class_query = """ | ||
[ | ||
(class_declaration) | ||
(interface_declaration) | ||
(enum_declaration) | ||
] @node | ||
""".strip() | ||
_function_query = """ | ||
[ | ||
(comment) @comment | ||
[ | ||
( function ) | ||
( function_declaration ) | ||
( generator_function_declaration ) | ||
( arrow_function ) | ||
] @node | ||
] | ||
""".strip() | ||
_block_query = """ | ||
(statement_block) @node | ||
""".strip() | ||
|
||
_javascript_exts = [".js", ".ts"] | ||
_jsx_exts = [".jsx", ".tsx"] | ||
|
||
|
||
class JavascriptClassStrategy(TreeSitterStrategy): | ||
def __init__(self): | ||
""" | ||
Initializes the parent class with predefined attributes for handling JavaScript files. | ||
Attributes: | ||
_javascript_language (str): Language specification for JavaScript. | ||
_class_query (str): Query string used for class searches within JavaScript files. | ||
_javascript_exts (tuple): A tuple containing the file extensions for JavaScript files. | ||
""" | ||
super().__init__(_javascript_language, _class_query, _javascript_exts) | ||
|
||
|
||
class JavascriptFunctionStrategy(TreeSitterStrategy): | ||
def __init__(self): | ||
""" | ||
Initialize the current class instance as a subclass, passing specific parameters related to JavaScript handling to the superclass constructor. | ||
Parameters: | ||
- _javascript_language (str): The language being used for JavaScript. | ||
- _function_query (str): The query for functions in JavaScript. | ||
- _javascript_exts (list): List of extensions related to JavaScript. | ||
Returns: | ||
- None | ||
""" | ||
super().__init__(_javascript_language, _function_query, _javascript_exts) | ||
|
||
|
||
class JavascriptBlockStrategy(TreeSitterStrategy): | ||
def __init__(self): | ||
""" | ||
Initialize the object by calling the superclass constructor with specific arguments. | ||
Parameters: | ||
- _javascript_language (str): The language used for JavaScript. | ||
- _block_query (str): The query to block JavaScript functionalities. | ||
- _javascript_exts (list): List of JavaScript file extensions. | ||
""" | ||
super().__init__(_javascript_language, _block_query, _javascript_exts) | ||
|
||
|
||
class JsxClassStrategy(TreeSitterStrategy): | ||
def __init__(self): | ||
""" | ||
Initialize the object by calling the superclass's initializer with specific parameters. | ||
Parameters: | ||
- _jsx_language (str): The JSX language parameter. | ||
- _class_query (str): The class query parameter. | ||
- _jsx_exts (str): The JSX extensions parameter. | ||
""" | ||
super().__init__(_jsx_language, _class_query, _jsx_exts) | ||
|
||
|
||
class JsxFunctionStrategy(TreeSitterStrategy): | ||
def __init__(self): | ||
""" | ||
Initialize the instance by calling the parent class's constructor with predefined arguments. | ||
Parameters: | ||
- _jsx_language (str): The language for JSX. | ||
- _function_query (str): The query for functions. | ||
- _jsx_exts (str): The file extension for JSX files. | ||
""" | ||
super().__init__(_jsx_language, _function_query, _jsx_exts) | ||
|
||
|
||
class JsxBlockStrategy(TreeSitterStrategy): | ||
def __init__(self): | ||
""" | ||
Initializes the object by calling the superclass initializer with the | ||
provided language, block query, and file extensions for JSX processing. | ||
Parameters: | ||
- self: the object itself | ||
Returns: | ||
- None | ||
""" | ||
super().__init__(_jsx_language, _block_query, _jsx_exts) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import dataclasses | ||
|
||
|
||
@dataclasses.dataclass(slots=True) | ||
class Position: | ||
start: int | ||
end: int | ||
start_col: int | ||
end_col: int | ||
meta_positions: dict[str, "Position"] = dataclasses.field(default_factory=dict) | ||
|
||
# def extract_lines(self, src: list[str]) -> list[str]: | ||
# return src[self.start : self.end] | ||
# | ||
# def extract_text(self, src: list[str]) -> list[str]: | ||
# lines = self.extract_lines(src) | ||
# lines[0] = lines[0][self.start_col :] | ||
# lines[-1] = lines[-1][: self.end_col] | ||
# return lines | ||
# | ||
# @contextlib.contextmanager | ||
# def replace_text(self, src: list[str]) -> list[str]: | ||
# container = self.extract_text(src) | ||
# yield container | ||
# src[self.start] = src[self.start][: self.start_col + 1] + container[0] | ||
# src[self.start + 1 : self.end - 2] = container[1:-1] | ||
# src[self.end - 1] = src[self.end - 1][self.end_col - 1 :] + container[-1] | ||
# return | ||
|
||
|
||
# @dataclasses.dataclass(slots=True, frozen=True) | ||
# class FileSource: | ||
# filepath: Path | ||
# src: list[str] | ||
# | ||
# @contextlib.contextmanager | ||
# def replace_text(self, position: Position) -> list[str]: | ||
# with position.replace_text(self.src) as container: | ||
# yield container | ||
# return | ||
# | ||
# def write(self): | ||
# self.filepath.write_text("".join(self.src)) |
Oops, something went wrong.