Where is the module defined?

How do you find where some module or macro is defined? For example in python there is the keyword import, in C++ there is include. In Elixir how can we find where something is defined?

1 Like

You cannot use something without defining where it is. You either have to fully qualify it:

SomeModule.Something.func(42)

Or you can alias it:

alias SomeModule.Something
Something.func(42)

Or you can import it:

import SomeModule.Something
func(42)

And those are the only ways. However Macro’s, like when use SomeModule can inject whatever they want in to your space, including imports and aliases, but everything always comes from somewhere defined in your module, whether via a macro like use, import, alias, or etc… The only thing exempt from this is the Kernel, where it is always imported, however you can also override it if you want. :slight_smile:

4 Likes

But what if I want to see modules source code. How can I find where it came from? For example in phoenix I have this code

defmodule Rumbl.PageView do
   use Rumbl.Web, :view
end

how can I quickly find the source code of Web?

1 Like

By convention it should be in an appropriately named file. In Atom I hit Ctrl+t, start typing the name of the thing I want to open and it auto-completes out to me with a selectable list. If on the command-line and there is no web/views/page_view.ex and your IDE cannot find it then it is time to grep for defmodule[^P]*PageView. :slight_smile:

Erlang does not have the concept of submodules, packages, or anything of the sort, everything is a flat namespace, and thus importing modules in it is obvious, every file is named as the module name itself, exactly. In Elixir a module is namespaced, can have multiple modules in a file, and so forth, so they compile-down to a munged set of base files, but the source files can be named anything, however convention holds. :slight_smile:

2 Likes

Thanks. So it seems I have to get familiar with grep :smiley:

1 Like

I’d definitely recommend a good IDE, Atom is decent, VIM or (space)emacs might be best. :slight_smile:

1 Like

I use mostly Atom to code in Elixir due to superb plugins. One of them is https://github.com/msaraiva/atom-elixir With it after I can ALT+Click to go to de modulue definition. So clicking on Web in line use Rumbl.Web, :view would open PROJECT_ROOT/web/web.ex file for me :wink:

PS. Due to the details I don’t want to go into, you have to have module compiled before using that go-to functionality, ideally if you want just to look what is what and where in code just do mix compile beforehand :slight_smile:

PPS. I’m usually in love with Jetbrains editors, but there is none for elixir. Luke Imhoff does the https://github.com/KronicDeth/intellij-elixir plugin for Jetbrains Idea, and it’s getting so much better from version to version, so you might give it a try if you are used to Jetbrains IDE. I tend to have both atom and Idea with Elixir plugin opened when I work on Elixir code :wink: Downside? Idea does not come cheap :stuck_out_tongue:

2 Likes

I am using spacemacs and usually you go to definition with , g g and with ctrl-o you jump back

1 Like

Thanks. I also use Atom and I do have installed that plugin. I didn’t know about that feature. Nice. For me it works with Alt + Down.

1 Like

If you are looking for some CLI tools which lookup the file modules/functions are defiened, alchemist-vim’s alchemist_client command might be your help.

It’s not about the usage, but the issue might be help because there are some comments about the command: https://github.com/slashmili/alchemist.vim/issues/54

s/alchemist_client/alchemist_server/g