<commit_before>let s:http_method_pattern = '\<\%(get\|post\|put\|delete\|patch\)\>'

function rimplement#route#Main()
  let description = s:FindRouteDescription()

  if description == ''
    return
  endif

  if description !~ '^\k\+#\k\+$'
    echomsg "Description doesn't look like controller#action: ".description
    return
  endif

  let [controller, action] = split(description, '#')
  let filename = 'app/controllers/'.controller.'_controller.rb'
  exe 'edit '.filename

  if !filereadable(filename)
    " then it doesn't exist yet, fill it in
    call append(0, [
          \ 'class '.rimplement#CapitalCamelCase(controller).'Controller < ApplicationController',
          \ '  def '.action,
          \ '  end',
          \ 'end',
          \ ])
    $delete _
    exe 2
  else
    let class_line = search('^\s*class\>')

    if class_line < 0
      echoerr "Class line not found"
      return
    endif

    if search('^\s*def \zs'.action)
      return
    endif

    let indent = repeat(' ', indent(class_line))

    call append(class_line, [
          \ indent.'  def '.action,
          \ indent.'  end',
          \ '',
          \ ])
  endif

  write
endfunction

function! s:FindRouteDescription()
  if rimplement#SearchUnderCursor('''[^'']\+''') > 0
    return rimplement#GetMotion("vi'")
  elseif rimplement#SearchUnderCursor('"[^"]\+"') > 0
    return rimplement#GetMotion('vi"')
  elseif rimplement#SearchUnderCursor('resources :\k\+') > 0
    call search(':\k')
    let resource = expand('<cword>')
    return resource.'#index'
  elseif rimplement#SearchUnderCursor('resource :\k\+') > 0
    call search(':\k')
    let resource = expand('<cword>')
    return resource.'#show'
  elseif rimplement#SearchUnderCursor(s:http_method_pattern.'\s\+:\k\+') > 0
    call search(':\k')
    let action = expand('<cword>')
    if search('^\s*resources\= :\zs\k\+\ze do$', 'b') < 0
      echomsg "Found the action '".action."', but can't find a containing resource."
      return ''
    endif
    let controller = expand('<cword>')
    return controller.'#'.action
  endif

  echomsg "Couldn't find string description"
  return ''
endfunction
<commit_msg>Handle nested routes<commit_after>let s:http_method_pattern = '\<\%(get\|post\|put\|delete\|patch\)\>'

function rimplement#route#Main()
  let description = s:FindRouteDescription()

  if description == ''
    return
  endif

  if description !~ '^\k\+#\k\+$'
    echomsg "Description doesn't look like controller#action: ".description
    return
  endif

  let nesting = s:FindRouteNesting()
  echomsg string(nesting)
  if len(nesting) > 0
    let file_prefix = join(nesting, '/').'/'
    let module_prefix = join(map(nesting, 'rimplement#CapitalCamelCase(v:val)'), '::').'::'
  else
    let file_prefix = ''
    let module_prefix = ''
  endif

  let [controller, action] = split(description, '#')
  let filename = 'app/controllers/'.file_prefix.controller.'_controller.rb'
  exe 'edit '.filename

  if !filereadable(filename)
    " then it doesn't exist yet, fill it in
    call append(0, [
          \ 'class '.module_prefix.rimplement#CapitalCamelCase(controller).'Controller < ApplicationController',
          \ '  def '.action,
          \ '  end',
          \ 'end',
          \ ])
    $delete _
    exe 2
  else
    let class_line = search('^\s*class\>')

    if class_line < 0
      echoerr "Class line not found"
      return
    endif

    if search('^\s*def \zs'.action)
      return
    endif

    let indent = repeat(' ', indent(class_line))

    call append(class_line, [
          \ indent.'  def '.action,
          \ indent.'  end',
          \ '',
          \ ])
  endif

  write
endfunction

function! s:FindRouteDescription()
  if rimplement#SearchUnderCursor('''[^'']\+''') > 0
    return rimplement#GetMotion("vi'")
  elseif rimplement#SearchUnderCursor('"[^"]\+"') > 0
    return rimplement#GetMotion('vi"')
  elseif rimplement#SearchUnderCursor('resources :\k\+') > 0
    call search(':\k')
    let resource = expand('<cword>')
    return resource.'#index'
  elseif rimplement#SearchUnderCursor('resource :\k\+') > 0
    call search(':\k')
    let resource = expand('<cword>')
    return resource.'#show'
  elseif rimplement#SearchUnderCursor(s:http_method_pattern.'\s\+:\k\+') > 0
    call search(':\k')
    let action = expand('<cword>')
    if search('^\s*resources\= :\zs\k\+\ze do$', 'b') < 0
      echomsg "Found the action '".action."', but can't find a containing resource."
      return ''
    endif
    let controller = expand('<cword>')
    return controller.'#'.action
  endif

  echomsg "Couldn't find string description"
  return ''
endfunction

function! s:FindRouteNesting()
  " Find any parent routes
  let indent = indent('.')
  let route_path = []
  let namespace_pattern = 'namespace :\zs\k\+'

  while search('^ \{'.(indent - &sw).'}'.namespace_pattern, 'bW')
    let route = expand('<cword>')
    call insert(route_path, route, 0)
    let indent = indent('.')
  endwhile

  return route_path
endfunction

{'fraction_non_alphanumeric': 0.1597526924611089, 'fraction_numerical': 0.003988831272437176, 'mean_word_length': 2.9550473186119874, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3840403', 'n_tokens_mistral': 1657, 'n_tokens_neox': 1575, 'n_words': 383}