pezra / rspec-mode

An RSpec minor mode for Emacs
258 stars 112 forks source link

Improve FactoryBot snippets #201

Closed kzkn closed 2 years ago

kzkn commented 2 years ago

This pullreq is build upon #200 . So please check #200 first :pray:

dgutov commented 2 years ago

Hi!

Separate placeholders of variable name and factory name

Are you sure this is a good idea? Fairly often the variable name is the same or similar to the factory name. So it would make sense to save time in those cases when they do match (or differ very little).

kzkn commented 2 years ago

@dgutov I often write code like this:

let(:alice) { create(:user) }
let(:bob) { create(:user) }

However, as you point out, it may be lesser than the case where the variable name and the factory name match.

In yasnippet, can the default value of $2 be the string entered in $1, do you know? If it is possible, I think it would keep efficiency in both cases where the variable name and the factory name match and where they do not.

dgutov commented 2 years ago

I'm not sure I understand. What would be the change in behavior then?

If $2 uses the same default as what the user has entered in the first field, isn't that just how the current snippets work?

kzkn commented 2 years ago

@dgutov Sorry for the confusing explanation. I imagine the following operation. But I'm not sure if this can be done with yasnippet .


A case of the variable name and factory name is match:

lfgb<TAB>
;; expanded as next line

let(:name) { build(:) }
;;   ^-- focus here

;; enter "user" and <TAB>
let(:user) { build(:user) }
;;                  ^-- focus here

;; enter <TAB>, the snippet is completed
let(:user) { build(:user) }

A case of the variable name and factory name is not match:

lfgb<TAB>
;; expanded as next line

let(:name) { build(:) }
;;   ^-- focus here

;; enter "alice" and <TAB>
let(:alice) { build(:alice) }
;;                   ^-- focus here

;; enter "user"
let(:alice) { build(:user) }
;;                       ^-- focus here

;; enter <TAB>, the snippet is completed
let(:alice) { build(:user) }
kzkn commented 2 years ago

But I'm not sure if this can be done with yasnippet .

I have found a way to do this!

let(:${1:name}) { `(rspec-snippets-fg-method-call "build")`(:${2:$1}) }
dgutov commented 2 years ago

I imagine the following operation. But I'm not sure if this can be done with yasnippet .

I though when I tested it yesterday, I saw this exact behavior. But apparently not. :-(

I have found a way to do this!

Please update the PR, and I'll happily merge it.

kzkn commented 2 years ago

@dgutov Updated :smiley:

dgutov commented 2 years ago

Thanks!