fvictorio / vim-extract-variable

Vim plugin to extract an expression to a variable
MIT License
26 stars 6 forks source link

adding typescripreact filetype #12

Open NikolaDojic opened 2 years ago

NikolaDojic commented 2 years ago

if you are ok with giving me access, I will make a pull request, it is a two-line change or if you prefer

if exists("g:loaded_extract_variable") || &cp
  finish
endif
let g:loaded_extract_variable = 1

function! s:ExtractToVariable(visual_mode)
  " Check if 'filetype' is set
  if &filetype == ''
    echo "'filetype' is not set"
    return
  endif

  " Check if language is supported
  let l:supported_languages = ['elixir', 'go', 'javascript', 'r', 'typescript', 'typescriptreact', 'python', 'ruby', 'julia']
  let l:filetype = split(&filetype, '\.')[0]

  if index(l:supported_languages, l:filetype) == -1
    echo l:filetype . ' is not supported. Please open an issue at https://github.com/fvictorio/vim-extract-variable/issues/new'
    return
  endif

  " Yank expression to z register
  let saved_z = @z
  if a:visual_mode ==# 'v'
    execute "normal! `<v`>\"zy"
  else
    execute "normal! vib\"zy"
  endif

  " Ask for variable name
  let varname = input('Variable name? ')

  if varname != ''
    execute "normal! `<v`>s".varname."\<esc>"

    if l:filetype ==# 'javascript' || l:filetype ==# 'typescript' || l:filetype ==# 'typescriptreact'

      execute "normal! Oconst ".varname." = ".@z."\<esc>"
    elseif l:filetype ==# 'go'
      execute "normal! O".varname." := ".@z."\<esc>"
    elseif l:filetype ==# 'elixir' || l:filetype ==# 'python' || l:filetype ==# 'ruby' || l:filetype ==# 'julia'
      execute "normal! O".varname." = ".@z."\<esc>"
    elseif l:filetype ==# 'r' 
      execute "normal! O".varname." <- ".@z."\<esc>"
    endif
  else
    redraw
    echo 'Empty variable name, doing nothing'
  endif

  let @z = saved_z
endfunction

nnoremap <leader>ev :call <sid>ExtractToVariable('')<cr>
vnoremap <leader>ev :<c-u>call <sid>ExtractToVariable(visualmode())<cr>