axelson

axelson

Scenic Core Team

CLI tool to get function name from line of source code

Has anyone written a CLI tool/script that when given a file path (to a .ex or .exs) file and a line number, will return the module name and function? i.e.

$ extract_line_number.bash ~/code/my_cool_proj/lib/utils.ex:15
MyCoolProj.Utils.find_by_id/2

This could be probably be built using the Elixir treesitter grammar (are there already any CLI tools that use the grammar in similar ways?). Before I embark on writing such a tool/script I wanted to see if anyone has already written one.

Most Liked

sodapopcan

sodapopcan

You should be able to use ast-grep for this.

sodapopcan

sodapopcan

It would have to be in conjunction with a scripting language. Maybe a terser way but I was thinking along these lines:

ast-grep scan --inline-rules '
id: find-def
language: Elixir
rule:
  any: 
    - pattern: def $$$
    - pattern: def $$$()
    - pattern: def $$$($$$)
' --json=compact -- path/to/file.ex

giving you all the (public) function definitions in path/to/file.ex. The json includes their start and end lines.

axelson

axelson

Scenic Core Team

Ah, that’s a good idea. I’ll give that a try when I have a chance. Thanks!

Where Next?

Popular in Questions Top

minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

Other popular topics Top

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New

We're in Beta

About us Mission Statement