0

I'm trying to create function and a mapping to insert/remove breakpoints in python code.

So far I got this

function! AddBreakPoint()
    let l:line = line('.')
    let l:indentChar = ' '
    call append(l:line - 1, repeat(l:indentChar, indent(l:line)) . "import pdb;pdb.set_trace()")
endfunction


function! RemoveAllBreakPoints()
    execute 'args *.py'
    silent! KeepView execute 'argdo g/\v^\s+(import pdb|pdb.set_trace)/de'
    write
endfunction

I've tried both bufdo and argdo and none of them works properly. With bufdo it breaks with neovim terminal emulator buffers, saing buffer doesn't have some property. With argo do it shows some error very quickly and while it removes breakpoints it also messes my splits. For example it can change the active buffer in split to another.

Also argdo is really wouldn't work for me, because I don't need to search all python files, for large projects this would be too slow. I only need files that have been modified recently or more likely in that same editing session.

EDIT1

schedules = {
    1: True,
    import pdb;pdb.set_trace()
    2: True,
    3: True,
    4: True,
    5: True,
    6: False,
    7: False,
}

There's a test file for you.

8
  • 1
    in the same editing session. Are these files still open in another tab or buffer?
    – DJMcMayhem
    Commented May 25, 2016 at 18:38
  • @DJMcMayhem Highly probable. Commented May 25, 2016 at 18:40
  • I don't use python, but - since you want to remove all breakpoints, doesn't pdb have a command that does just that? I.e. without the need to specify any file. Something like pdb.clear, say (if such command exists).
    – VanLaser
    Commented May 25, 2016 at 21:59
  • I would probably end up using a global list that tracked all the files that had been edited. i.e. autocmd BufWritePost * add(g:edditedFiles, expand('%:p') and then use a for loop over that list whenever I wanted to do the "global" edit.
    – Tumbler41
    Commented May 25, 2016 at 22:02
  • @Tumbler41 good idea, haven't thought of that, because I'm not really familiar with vimscript. Commented May 26, 2016 at 17:19

1 Answer 1

1

Give this a shot:

let g:eddited_python_files = []
autocmd BufWritePost *.py if (count(g:eddited_python_files, expand('%:p')) == 0) | call add(g:eddited_python_files, expand('%:p')) | endif

command! RB call RemoveAllBreakPoints()

function! RemoveAllBreakPoints()
   tabnew " Don't clobber the current tab
   for file in g:eddited_python_files
      exe "edit " . l:file
      g/\v^\s+(import pdb|pdb.set_trace)/d
      write
   endfor
   quit " Go back to original tab
endfunction

I changed the autocmd to only add unique files. I just used your regular expression, I'm not familiar with python brakpoints. I'm also not sure how you wanted to call this function so I just put in a command for it. This means you can do a :RB to do the operation.

3
  • I've got it a try, it doesn't work. Commented Jun 3, 2016 at 20:02
  • Could you elaborate? What happens, is there an error? Does it do the wrong thing? I need something more than this to help you.
    – Tumbler41
    Commented Jun 3, 2016 at 20:08
  • I tried it on your example and it works for me.
    – Tumbler41
    Commented Jun 3, 2016 at 20:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.