Feature Request | ig goes activated on import statement.
nkabbara opened this issue · 11 comments
Hi @kristijanhusak!
I was wondering if it would be possible to make <Leader>ig
also go to the definition if it's pressed over the include statement.
For example, if I have the cursor somewhere on over import or anywhere on that line and I hit <Leader>ig
, it would open the lib file buffer.
imp|ort { MyComponent } from './components/lib'
I introduced passing custom word to the import/goto commands, so this is something you can do yourself. Basically:
- Get the current line and parse the word you want to jump to
- Execute the command with that word, in this case it would be
:JsGotoDefinition lib
@kristijanhusak when I tried JsGotoDefinition lib
it says trailing characters. As if it's not expecting to have an argument. Is this already implemented?
That's odd. Is lib
a folder?
What happens if you write down lib
into the buffer and try to go to definition in the old way?
I can't reproduce it. Does it happen for some other words?
Also, do you have any more files or folders that are called lib
?
I would appreciate if you would debug this. Here are the steps:
- Find plugin in your plugins directory, open
autoload/jsfileimport.vim
, and comment out all lines that are try/catch. So basically line 28, and lines from 61 to 66. - Then try to reproduce this. It should give more uglier but detailed error.
I would appreciate if you would debug this.
Anytime. I commented them out, but they were on different lines for me: 58, 106 and 143.
Error is still the same. Nothing changed. I verified that the comments took effect by causing other errors and I see more detailed errors for the other valid errors.
Let me know if you need further debugging.
-Nash
How are the lines different? When did you do an update last time?
Can you test with some other words?
It seems to work after the latest update. Awesome, thanks, I'll figure out how to make it detect import line and go to imported file!
@kristijanhusak how can I call JsGotoDefinition(filename) in vimrc with the context of the current buffer?
The pseudocode for what I'm thinking:
function! LoadImportFileUnderCursor(word, currentLine)
"if word under cursor is 'import' parse out the path from currentLine
"assign to somevar
JsGotoDefinition a:somevar
endfunction
But I tried with hardcoding a filename and JsGotoDefinition returns 'Tag not found'. I'm assuming because it's not being ran under the context of the current buffer.
Assuming your line looks something like this:
import MyComponent from './components/MyComponent';
Something like this should do the trick:
function! GotoDefinition()
let current_line = getline('.')
let matches = matchlist(current_line, '^import \(\w\+\) from .*$')
if empty(matches)
return
endif
exe 'JsGotoDefinition '.matches[1]
endfunction
nnoremap <Leader>g :call GotoDefinition()<CR>
Haven't tested it though, so adjust accordingly if needed.
Thank you so much! I'll give it a try later.