edit_linting.sh 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. # @yaml
  2. # signature: |-
  3. # edit <start_line>:<end_line>
  4. # <replacement_text>
  5. # end_of_edit
  6. # docstring: replaces lines <start_line> through <end_line> (inclusive) with the given text in the open file. The replacement text is terminated by a line with only end_of_edit on it. All of the <replacement text> will be entered, so make sure your indentation is formatted properly. Python files will be checked for syntax errors after the edit. If the system detects a syntax error, the edit will not be executed. Simply try to edit the file again, but make sure to read the error message and modify the edit command you issue accordingly. Issuing the same command a second time will just lead to the same error message again.
  7. # end_name: end_of_edit
  8. # arguments:
  9. # start_line:
  10. # type: integer
  11. # description: the line number to start the edit at
  12. # required: true
  13. # end_line:
  14. # type: integer
  15. # description: the line number to end the edit at (inclusive)
  16. # required: true
  17. # replacement_text:
  18. # type: string
  19. # description: the text to replace the current selection with
  20. # required: true
  21. edit() {
  22. if [ -z "$CURRENT_FILE" ]
  23. then
  24. echo 'No file open. Use the `open` command first.'
  25. return
  26. fi
  27. local start_line="$(echo $1: | cut -d: -f1)"
  28. local end_line="$(echo $1: | cut -d: -f2)"
  29. if [ -z "$start_line" ] || [ -z "$end_line" ]
  30. then
  31. echo "Usage: edit <start_line>:<end_line>"
  32. return
  33. fi
  34. local re='^[0-9]+$'
  35. if ! [[ $start_line =~ $re ]]; then
  36. echo "Usage: edit <start_line>:<end_line>"
  37. echo "Error: start_line must be a number"
  38. return
  39. fi
  40. if ! [[ $end_line =~ $re ]]; then
  41. echo "Usage: edit <start_line>:<end_line>"
  42. echo "Error: end_line must be a number"
  43. return
  44. fi
  45. # Bash array starts at 0, so let's adjust
  46. local start_line=$((start_line - 1))
  47. local end_line=$((end_line))
  48. local line_count=0
  49. local replacement=()
  50. while IFS= read -r line
  51. do
  52. replacement+=("$line")
  53. ((line_count++))
  54. done
  55. # Create a backup of the current file
  56. cp "$CURRENT_FILE" "/root/$(basename "$CURRENT_FILE")_backup"
  57. # Read the file line by line into an array
  58. mapfile -t lines < "$CURRENT_FILE"
  59. local new_lines=("${lines[@]:0:$start_line}" "${replacement[@]}" "${lines[@]:$((end_line))}")
  60. # Write the new stuff directly back into the original file
  61. printf "%s\n" "${new_lines[@]}" >| "$CURRENT_FILE"
  62. # Run linter
  63. if [[ $CURRENT_FILE == *.py ]]; then
  64. lint_output=$(flake8 --isolated --select=F821,F822,F831,E111,E112,E113,E999,E902 "$CURRENT_FILE" 2>&1)
  65. else
  66. # do nothing
  67. lint_output=""
  68. fi
  69. # if there is no output, then the file is good
  70. if [ -z "$lint_output" ]; then
  71. export CURRENT_LINE=$start_line
  72. _constrain_line
  73. _print
  74. echo "File updated. Please review the changes and make sure they are correct (correct indentation, no duplicate lines, etc). Edit the file again if necessary."
  75. else
  76. echo "Your proposed edit has introduced new syntax error(s). Please read this error message carefully and then retry editing the file."
  77. echo ""
  78. echo "ERRORS:"
  79. _split_string "$lint_output"
  80. echo ""
  81. # Save original values
  82. original_current_line=$CURRENT_LINE
  83. original_window=$WINDOW
  84. # Update values
  85. export CURRENT_LINE=$(( (line_count / 2) + start_line )) # Set to "center" of edit
  86. export WINDOW=$((line_count + 10)) # Show +/- 5 lines around edit
  87. echo "This is how your edit would have looked if applied"
  88. echo "-------------------------------------------------"
  89. _constrain_line
  90. _print
  91. echo "-------------------------------------------------"
  92. echo ""
  93. # Restoring CURRENT_FILE to original contents.
  94. cp "/root/$(basename "$CURRENT_FILE")_backup" "$CURRENT_FILE"
  95. export CURRENT_LINE=$(( ((end_line - start_line + 1) / 2) + start_line ))
  96. export WINDOW=$((end_line - start_line + 10))
  97. echo "This is the original code before your edit"
  98. echo "-------------------------------------------------"
  99. _constrain_line
  100. _print
  101. echo "-------------------------------------------------"
  102. # Restore original values
  103. export CURRENT_LINE=$original_current_line
  104. export WINDOW=$original_window
  105. echo "Your changes have NOT been applied. Please fix your edit command and try again."
  106. echo "You either need to 1) Specify the correct start/end line arguments or 2) Correct your edit code."
  107. echo "DO NOT re-run the same failed edit command. Running it again will lead to the same error."
  108. fi
  109. # Remove backup file
  110. rm -f "/root/$(basename "$CURRENT_FILE")_backup"
  111. }