Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create alternate or relative file if it's not found command #503

Open
sadovnik opened this issue Nov 26, 2017 · 14 comments
Open

Create alternate or relative file if it's not found command #503

sadovnik opened this issue Nov 26, 2017 · 14 comments

Comments

@sadovnik
Copy link

Hello!

Is there any command I can use to create an alternate or relative file if it's not created yet? If there isn't, it would be cool if you add some :A! command or something which will behave similar to :Emodel user!.

@sadovnik sadovnik changed the title Create spec if it's not found Create alternate or relative file if it's not found command Nov 26, 2017
@juanibiapina
Copy link

This works with vim-projectionist already. I'm currently looking for an easy way to disabled alternation on vim-rails only.

@tpope
Copy link
Owner

tpope commented Mar 15, 2018

There's an experimental feature where it will prompt you for the file to create with :set confirm.

@tap349
Copy link

tap349 commented May 8, 2018

after updating the plugin set confirm no longer works :(

E345: Can't find file "<spec_file_path>" in path

UPDATE: this is because I created new Rails project with --no-skip-test which creates test directory instead of spec one. after renaming test to spec creating alternate file works as usual )

@Mehonoshin
Copy link

Any news about this feature?

@tap349
Copy link

tap349 commented Jun 11, 2018

@Mehonoshin Even though Tim Pope says it's an experimental feature, :set confirm works for me (and it's been around for about 2 years already).

@tpope
Copy link
Owner

tpope commented Jun 11, 2018

Yeah it does still work, but only for things defined as projections (which can be confusing, and why I still consider it experimental).

@igbanam
Copy link

igbanam commented Mar 9, 2019

I could understand that using :A could be some sort of magic if this file is not around.

Could this be a new command, like :C, which would require the explicit "projection"? — p.s: I do not know much about projections and projectionist, so if I am using this wrongly here, please correct me.

I was thinking the following
:Cmodel car — creates a file at app/models/car.rb
:Cspec models/car — creates a file at spec/models/car_spec.rb
:Ccontroller
:Clayout
:Cview cars/index — creates a file at app/views/cars/index.html.erb

…and so on

@chalmagean
Copy link

I'd also to see this working. It's very useful to be able to create missing spec files, especially for long file names.

@take
Copy link

take commented Apr 2, 2020

looks like #135 is related

@bmulholland
Copy link

bmulholland commented May 18, 2021

I don't really care about what command does this, I just want to auto-create the spec - especially because I often make typos when creating those files (e.g. forgetting _spec).

I'm using a spec folder and :set confirm doesn't seem to work; when I try, I get the same E345 as before. That appears to be because the projection only goes in one direction, so a file in app can be created from a file in spec, but not vice versa.

Looks like this is something supported in vim-projectionist, but the vim-rails code is just too old to add it (and that was written 6 years ago!). Maybe someone will write a fresh code base now that Neovim is re-vitalizing things?

For now, seems like the only way to auto-create a spec is to add the projectionist plugin. I'm moving on to other work today, and will update with some setup steps & config if I get around to adding projectionist.

Writing out my debug steps here in case it helps others:

  1. Check :echo rails#buffer().alternate_candidates() - does it return a path?
  2. Does the folder structure in that path exist?
  3. Is there a projection for it?
  4. Is the projection in the correct direction?

@otavioschwanck
Copy link

otavioschwanck commented Mar 21, 2022

i know this issue is old, but ill share my setup for this:

function OpenTestAlternateAndSplit()
  let win_count = winnr()
  let test_path = eval('rails#buffer().alternate()')

  execute "normal! \<C-w>o"

  execute "norm \<C-w>v"

  execute "call OpenTestAlternate()"

  if test_path =~ 'app/'
    execute "norm \<C-w>x"
  endif
endfunction

function OpenTestAlternate()
  let test_path = eval('rails#buffer().alternate()')

  execute "e " . test_path

  if !filereadable(test_path) && join(getline(1,'$'), "\n") == ''
    if test_path =~ "spec/"
      execute "norm itemplate_test\<Tab>"
    else
      execute "norm iminitest\<Tab>"
    endif
  endif
endfunction

nmap <leader>a :call OpenTestAlternate()<CR>
nmap <leader>A :call OpenTestAlternateAndSplit()<CR>

If you create an snippet called template_test for rspec and minitest for minitest, it automatically will expand a snippet for you.

To remove this behaviour, just remove this part:

  if !filereadable(test_path) && join(getline(1,'$'), "\n") == ''
    if test_path =~ "spec/"
      execute "norm itemplate_test\<Tab>"
    else
      execute "norm iminitest\<Tab>"
    endif
  endif

The snippets (Ultisnips format):

snippet minitest "minitest spec file" i
require 'test_helper'

class ${1:ClassName} < ActiveSupport::TestCase
  $0
end
endsnippet

snippet template_test "Rspec.describe Class, type: :model do ... end"
require 'rails_helper'

RSpec.describe ${1:Class}, type: :${2:model} do
  $0
end
endsnippet

@davidsilveira
Copy link

@otavioschwanck this code is very useful.

@styrmis
Copy link

styrmis commented Jun 17, 2022

I just wanted to have something incredibly basic, to avoid typing/typos when the alternate file didn't exist, so I have defined a single command in my vimrc:

command AC :execute "e " . eval('rails#buffer().alternate()')

So when :A complains about the file not existing, I run :AC and continue work from there.

@jiz4oh
Copy link

jiz4oh commented Nov 3, 2022

function! s:ac() abort
  let pre = &confirm
  try
    set confirm
    A
  finally
    let &confirm = pre
  endtry
endfunction

command! AC :call <SID>ac()

I defined this for myself

eebs added a commit to eebs/dotfiles that referenced this issue Mar 14, 2023
Based on this [blog post] from Josh Clayton.

Still trying to get `:AV` to automatically create the alternate file, so
far no luck. More [detail].

[blog post]: https://joshuaclayton.me/posts/automatic-projections-for-quick-vim-navigation/
[detail]: tpope/vim-rails#503
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests