-
Notifications
You must be signed in to change notification settings - Fork 9
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
Refactor navigation commands into its own tool #35
Open
ryanhoangt
wants to merge
15
commits into
main
Choose a base branch
from
ht/refactor-nav-commands
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
6d89a48
migrate ts-parser, utils module and navigator
ryanhoangt afa1cda
add missing package
ryanhoangt 4fc2697
fix lint
ryanhoangt 7830710
add cli
ryanhoangt 82944ee
refactor
ryanhoangt 2119440
finish refactor
ryanhoangt 96527db
Merge branch 'main' into ht/refactor-nav-commands
ryanhoangt 7eb2856
add tool & param desc
ryanhoangt c4ae06a
add tool & param desc for nav
ryanhoangt b067d2a
add nav tip
ryanhoangt b3e2c0d
Add unit tests for TreeSitterParser
openhands-agent 537da75
remove nav tip
ryanhoangt 4453461
add tests for navigator
ryanhoangt b5c8b84
use None check instead of falsy
ryanhoangt a8f7c08
Merge branch 'main' into ht/refactor-nav-commands
ryanhoangt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
from .editor import file_editor | ||
from .navigator import symbol_navigator | ||
|
||
__all__ = ['file_editor'] | ||
__all__ = ['file_editor', 'symbol_navigator'] |
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
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,113 @@ | ||
import argparse | ||
import json | ||
import re | ||
import sys | ||
from pathlib import Path | ||
from typing import Any, NoReturn | ||
|
||
from .editor import Command, get_args | ||
|
||
|
||
def parse_view_range(value: str) -> list[int]: | ||
try: | ||
start, end = map(int, value.split(',')) | ||
return [start, end] | ||
except ValueError: | ||
raise argparse.ArgumentTypeError( | ||
'view-range must be two comma-separated integers, e.g. "1,10"' | ||
) | ||
|
||
|
||
def create_parser() -> argparse.ArgumentParser: | ||
parser = argparse.ArgumentParser( | ||
description='OpenHands Editor CLI - A tool for viewing and editing files' | ||
) | ||
parser.add_argument( | ||
'command', | ||
type=str, | ||
choices=list(get_args(Command)), | ||
help='The command to execute', | ||
) | ||
parser.add_argument( | ||
'path', | ||
type=str, | ||
help='Path to the file or directory to operate on', | ||
) | ||
parser.add_argument( | ||
'--file-text', | ||
type=str, | ||
help='Content for the file when using create command', | ||
) | ||
parser.add_argument( | ||
'--view-range', | ||
type=parse_view_range, | ||
help='Line range to view in format "start,end", e.g. "1,10"', | ||
) | ||
parser.add_argument( | ||
'--old-str', | ||
type=str, | ||
help='String to replace when using str_replace command', | ||
) | ||
parser.add_argument( | ||
'--new-str', | ||
type=str, | ||
help='New string to insert when using str_replace or insert commands', | ||
) | ||
parser.add_argument( | ||
'--insert-line', | ||
type=int, | ||
help='Line number after which to insert when using insert command', | ||
) | ||
parser.add_argument( | ||
'--enable-linting', | ||
action='store_true', | ||
help='Enable linting for file modifications', | ||
) | ||
parser.add_argument( | ||
'--raw', | ||
action='store_true', | ||
help='Output raw JSON response instead of formatted text', | ||
) | ||
return parser | ||
|
||
|
||
def extract_result(output: str) -> dict[str, Any]: | ||
match = re.search( | ||
r'<oh_aci_output_[0-9a-f]{32}>(.*?)</oh_aci_output_[0-9a-f]{32}>', | ||
output, | ||
re.DOTALL, | ||
) | ||
assert match, f'Output does not contain the expected <oh_aci_output_> tags in the correct format: {output}' | ||
result_dict = json.loads(match.group(1)) | ||
return result_dict | ||
|
||
|
||
def main() -> NoReturn: | ||
parser = create_parser() | ||
args = parser.parse_args() | ||
|
||
# Import here to avoid circular imports | ||
from . import file_editor | ||
|
||
try: | ||
output = file_editor( | ||
command=args.command, | ||
path=str(Path(args.path).absolute()), | ||
file_text=args.file_text, | ||
view_range=args.view_range, | ||
old_str=args.old_str, | ||
new_str=args.new_str, | ||
insert_line=args.insert_line, | ||
enable_linting=args.enable_linting, | ||
) | ||
|
||
result = extract_result(output) | ||
print(result['formatted_output_and_error']) | ||
sys.exit(0) | ||
except Exception as e: | ||
print(f'ERROR: {str(e)}', file=sys.stderr) | ||
sys.exit(1) | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |
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 |
---|---|---|
@@ -1 +1,3 @@ | ||
CONTENT_TRUNCATED_NOTICE: str = '<response clipped><NOTE>To save on context only part of this file has been shown to you. You should retry this tool after you have searched inside the file with `grep -n` in order to find the line numbers of what you are looking for.</NOTE>' | ||
|
||
NAVIGATION_TIPS: str = '<TIP>Use the navigation tool to investigate more about a particular class, function/method and how it is used in the codebase.</TIP>' |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not sure if there's a formal board and backlog item to link these TODO items to, but in my experience these kinds of things often never get done otherwise