cursors_edit_linting.sh 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. # @yaml
  2. # signature: |-
  3. # edit
  4. # <replacement_text>
  5. # end_of_edit
  6. # docstring: replaces *all* of the text between the START CURSOR and the END CURSOR with the replacement_text. 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. To enter text at the beginning of the file, set START CURSOR and END CURSOR to 0. Use set_cursors to move the cursors around. Python files will be checked for syntax errors after the edit.
  7. # end_name: end_of_edit
  8. # arguments:
  9. # replacement_text:
  10. # type: string
  11. # description: the text to replace the current selection with
  12. # required: true
  13. edit() {
  14. if [ -z "$CURRENT_FILE" ]
  15. then
  16. echo 'No file open. Use the `open` command first.'
  17. return
  18. fi
  19. local start_line=$((START_CURSOR - 1))
  20. start_line=$((start_line < 0 ? 0 : start_line))
  21. local end_line=$((END_CURSOR))
  22. end_line=$((end_line < 0 ? 0 : end_line))
  23. local replacement=()
  24. while IFS= read -r line
  25. do
  26. replacement+=("$line")
  27. done
  28. local num_lines=${#replacement[@]}
  29. # Create a backup of the current file
  30. cp "$CURRENT_FILE" "/root/$(basename "$CURRENT_FILE")_backup"
  31. # Read the file line by line into an array
  32. mapfile -t lines < "$CURRENT_FILE"
  33. local new_lines=("${lines[@]:0:$start_line}" "${replacement[@]}" "${lines[@]:$((end_line))}")
  34. # Write the new stuff directly back into the original file
  35. printf "%s\n" "${new_lines[@]}" >| "$CURRENT_FILE"
  36. # Run linter
  37. if [[ $CURRENT_FILE == *.py ]]; then
  38. lint_output=$(flake8 --select=F821,F822,F831,E111,E112,E113,E999,E902 "$CURRENT_FILE" 2>&1)
  39. else
  40. # do nothing
  41. lint_output=""
  42. fi
  43. # if there is no output, then the file is good
  44. if [ -z "$lint_output" ]; then
  45. _constrain_line
  46. # set to START + num_lines - 1, unless num_lines is 0, then set to START
  47. export END_CURSOR=$((num_lines == 0 ? START_CURSOR : START_CURSOR + num_lines - 1))
  48. export START_CURSOR=$START_CURSOR
  49. _print
  50. 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."
  51. else
  52. echo "Your proposed edit has introduced new syntax error(s). Please understand the fixes and retry your edit commmand."
  53. echo ""
  54. echo "ERRORS:"
  55. _split_string "$lint_output"
  56. echo ""
  57. # Save original values
  58. original_current_line=$CURRENT_LINE
  59. original_window=$WINDOW
  60. original_end_cursor=$END_CURSOR
  61. # Update values
  62. export CURRENT_LINE=$(( (num_lines / 2) + start_line )) # Set to "center" of edit
  63. export WINDOW=$((num_lines + 10)) # Show +/- 5 lines around edit
  64. export END_CURSOR=$((num_lines == 0 ? START_CURSOR : START_CURSOR + num_lines - 1))
  65. echo "This is how your edit would have looked if applied"
  66. echo "-------------------------------------------------"
  67. _constrain_line
  68. _print
  69. echo "-------------------------------------------------"
  70. echo ""
  71. # Restoring CURRENT_FILE to original contents.
  72. cp "/root/$(basename "$CURRENT_FILE")_backup" "$CURRENT_FILE"
  73. export CURRENT_LINE=$(( ((end_line - start_line) / 2) + start_line )) # Set to "center" of edit
  74. export WINDOW=$((end_line - start_line + 10))
  75. export END_CURSOR=$original_end_cursor
  76. echo "This is the original code before your edit"
  77. echo "-------------------------------------------------"
  78. _constrain_line
  79. _print
  80. echo "-------------------------------------------------"
  81. # Restore original values
  82. export CURRENT_LINE=$original_current_line
  83. export WINDOW=$original_window
  84. export END_CURSOR=$original_end_cursor
  85. echo "Your changes have NOT been applied. Please fix your edit command and try again."
  86. echo "You either need to 1) Specify the correct start/end line arguments or 2) Correct your edit code."
  87. echo "DO NOT re-run the same failed edit command. Running it again will lead to the same error."
  88. fi
  89. # Remove backup file
  90. rm -f "/root/$(basename "$CURRENT_FILE")_backup"
  91. }