본문 바로가기
utils

Vimspector (neovim으로 디버깅 하기)

by p4cho 2023. 12. 28.
728x90

neovim 에서 디버깅까지 할 수 있다면 IDE를 벗어날 수 있을 것 같다.

그래서 1년쯤 전에 내 “귀인”께 추천받았던 vimspector를 써보기로했다.

https://github.com/puremourning/vimspector

 

GitHub - puremourning/vimspector: vimspector - A multi-language debugging system for Vim

vimspector - A multi-language debugging system for Vim - GitHub - puremourning/vimspector: vimspector - A multi-language debugging system for Vim

github.com

간지..

Installation

vim-plug 로 설치해보자.

call plug#begin('~/.vim/plugged')
...
Plug 'puremourning/vimspector'
...
call plug#end()
:source %
:PlugInstall

python 디버깅을 위한 디버거(debugpy) 설치

" set base dir
let g:vimspector_base_dir = '/Users/han/.vim/plugged/vimspector'

" nvim command mode
:VimspectorInstall debugpy

Configuration

neovim은 Vimspector에서 의존하는 일부 기능을 구현하지 않는다.

  • WinBar - 코드 창 상단의 버튼 및 출력 창의 현재 출력을 변경하는 데 사용됨.
  • Prompt Buffers - 콘솔에서 명령을 보내고 Watch를 추가하는 데 사용됨. (참고: prompt buffers는 neovim nightly에서 사용 가능)
  • Balloons - 이는 마우스를 가리킬 때 변수 평가 팝업이 표시되도록하는 기능. 키보드 매핑을 생성하는 방법은 아래를 참조.

다음과 같은 대체 방법이 있습니다:

  • WinBar - :VimspectorShowOutput 및 :VimspectorReset 사용 가능
  • Prompt Buffers - :VimspectorEval 및 :VimspectorWatch 사용 가능
  • Balloons - <Plug>VimspectorBalloonEval 매핑 사용가능. 기본 매핑은 없으므로 다음과 같이 키보드 매핑을 설정:
" ~/.config/nvim/init.vim
" 기억하기 쉬운 'di' = 'debug inspect' (원하는 대로 선택하세요!)

" normal 모드에서 커서 아래 단어
nmap <Leader>di <Plug>VimspectorBalloonEval
" visual 모드에서 선택된 텍스트
xmap <Leader>di <Plug>VimspectorBalloonEval

Human Mode

손이 2개, 손가락이 총 10개 뿐인 일반적인 휴먼이라면, 여러 조합을 한 번에 누르기 불편할 것이다. 그래서 Human Mode 라는 하나의 단축키로만 맵핑한 모드를 소개한다.

" ~/.config/nvim/init.vim

let g:vimspector_enable_mappings = 'HUMAN'

Key Mapping Function

F5 <Plug>VimspectorContinue When debugging, continue. Otherwise start debugging.
F3 <Plug>VimspectorStop Stop debugging.
F4 <Plug>VimspectorRestart Restart debugging with the same configuration.
F6 <Plug>VimspectorPause Pause debuggee.
F9 <Plug>VimspectorToggleBreakpoint Toggle line breakpoint on the current line.
<leader>F9 <Plug>VimspectorToggleConditionalBreakpoint Toggle conditional line breakpoint on the current line.
F8 <Plug>VimspectorAddFunctionBreakpoint Add a function breakpoint for the expression under cursor
<leader>F8 <Plug>VimspectorRunToCursor Run to Cursor
F10 <Plug>VimspectorStepOver Step Over
F11 <Plug>VimspectorStepInto Step Into
F12 <Plug>VimspectorStepOut Step out of current function scope

.vimspector.json

프로젝트의 root 경로에 추가해주면 vimspector가 파일을 읽고 초기화 된다.

{
  "configurations": {
    "Python-vimspector: Launch": {
      "adapter": "debugpy",
      "filetypes": [ "python" ],
      "configuration": {
        "name": "Python-vimspector: Launch",
        "type": "python",
        "request": "launch",
        "cwd": "${workspaceRoot}",
        "python": "<${poetry env info -p}/bin/python">,
        "stopOnEntry": true,
        "console": "externalTerminal",
        "debugOptions": ["-i"],
        "program": "${file}"
      }
    }
  }
}

→ 주로 poetry를 사용하기 때문에 configuration.python 에 해당 프로젝트에서 참조하는 python 경로를 적어 준다. debugOptions 의 -i 는 stdin(표준입력)을 받는 것을 활성화.

간지..

 


Poetry venv

poetry 환경에서 계속 `Vimspector unavailable: Requires Vim compiled with +python3` 메세지가 발생했다.

원인은 python package에 pynvim이 설치되어있지 않아서인데, poetry를 사용하는경우 poetry add pynvim; poetry install; 을 통해 설치 후 poetry 환경에서 neovim을 실행해야 poetry venv에 설치한 library들이 적용된다.

poetry run nvim

pvi 로 alias 등록해서 사용하니 편함.

728x90

'utils' 카테고리의 다른 글

Vim 에 대한 간단한 소개와 사용법, 숙달하기 위한 연습방법  (1) 2024.12.08
fuzzy finder + ripgrep  (0) 2024.01.17
Treesitter  (0) 2023.12.26
lazygit  (1) 2023.12.21
Neovim  (0) 2023.12.19