Skip to content

Commit

Permalink
gf on a property in the template goes to the definition
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewRadev committed Jul 30, 2016
1 parent c7aca6d commit c35d7d5
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 0 deletions.
1 change: 1 addition & 0 deletions autoload/ember_tools.vim
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ function! ember_tools#Includeexpr()
\ 'ember_tools#gf#TransitionRoute',
\ 'ember_tools#gf#Controller',
\ 'ember_tools#gf#Action',
\ 'ember_tools#gf#Property',
\ 'ember_tools#gf#ServiceInjection',
\ 'ember_tools#gf#ServiceProperty',
\ 'ember_tools#gf#Model',
Expand Down
43 changes: 43 additions & 0 deletions autoload/ember_tools/gf.vim
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,49 @@ function! ember_tools#gf#Action()
endif
endfunction

function! ember_tools#gf#Property()
if !ember_tools#IsTemplateFiletype()
return ''
endif

let current_file = expand('%:.')
let property_name = expand('<cword>')

if s:IsComponentTemplate(current_file)
let component_name = s:ExtractComponentName(current_file)
let result = s:FindComponentLogic(component_name)
elseif s:IsTemplate(current_file)
let controller_name = s:ExtractControllerName(current_file)
let result = s:FindController(controller_name)
else
let result = ''
endif

if result == ''
" no file was found, try something else
return ''
endif

let property_pattern = '^\s*\zs'.property_name.':'
let property_found_in_file = 0

" Check if the property really is that file
for line in readfile(result)
if line =~ property_pattern
let property_found_in_file = 1
break
endif
endfor

if !property_found_in_file
" we should try something else
return ''
endif

call ember_tools#SetFileOpenCallback(result, property_pattern)
return result
endfunction

function! s:FindService(property)
let property = a:property
let service_name = split(property, '\.')[0]
Expand Down
38 changes: 38 additions & 0 deletions spec/plugin/gf_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,44 @@
expect(current_line.strip).to eq 'exampleAction() {'
end

specify "finding a controller property" do
edit_file 'app/controllers/foo.js', <<-EOF
export default Ember.Controller.extend({
showSomething: true
});
EOF
edit_file 'app/templates/foo.hbs', <<-EOF
{{#if showSomething}}
<div>Something</div>
{{/if}}
EOF
vim.search 'showSomething'

vim.normal 'gf'

expect(current_file).to eq 'app/controllers/foo.js'
expect(current_line.strip).to eq 'showSomething: true'
end

specify "finding a component property" do
edit_file 'app/components/foo/bar-baz/component.js', <<-EOF
export default Ember.Component.extend({
showSomething: true
});
EOF
edit_file 'app/components/foo/bar-baz/template.hbs', <<-EOF
{{#if showSomething}}
<div>Something</div>
{{/if}}
EOF
vim.search 'showSomething'

vim.normal 'gf'

expect(current_file).to eq 'app/components/foo/bar-baz/component.js'
expect(current_line.strip).to eq 'showSomething: true'
end

describe "finding a service" do
before :each do
touch_file 'app/services/example-service.js'
Expand Down

0 comments on commit c35d7d5

Please sign in to comment.