justincjohnson

justincjohnson

Elixir Internals: Return all references to a function on a specific line of a file

I am trying to add rename support to the wonderful Elixir Language Server project in https://github.com/JakeBecker/elixir-ls/issues/22. I’m at a very early stage in this work, making sure I understand the Language Server Protocol spec as well as the capabilities within Elixir itself, and creating a list of all the work I believe will be involved. That said I’ve been able to implement a proof of concept that, within certain constraints, does rename function definitions and references.

I’m facing a challenge currently and am posting here in the hopes that someone can save me some time and point me in the right direction in the Elixir code base.

When I need to rename a function, I first find the function definition and all references to the function throughout the project’s code base. The granularity of this information (in part coming from Mix.Tasks.Xref) currently only includes files and line numbers. I don’t know the function name start and end character numbers on the given line, how many times the function is referenced on that line, or exactly what names are used for those references, be they fully qualified modules and function names, partially qualified modules relying on aliases, module names that rely on aliases with renamed modules via the :as keyword, or just function names via import. This makes it challenging to know what portion of text on the given line needs to be renamed.

It seems to me that I need to tap into the Elixir runtime in order to know exactly what valid ways exist to reference a given function on a given line of a file. Without tapping into the runtime it seems that I would have to reimplement a bunch of logic that would have already been implemented in Elixir itself.

Does that make sense? So what I’m hoping for is a function I can call where I can pass in an MFA tuple, a file path and a line number and get back a list of references to that function on that line along with metadata about those references so I can determine the exact start and end characters that need to be renamed.

From my initial research I’m assuming this at least doesn’t exist today in a public function, but perhaps there are internals I can use, and if so perhaps this is worth a proposal for a new public function to help with Elixir tooling.

Thanks in advance for any tips you can provide.

Most Liked

msaraiva

msaraiva

Broadway Core Team

Hi @justincjohnson!

If you set up ElixirLS to use this branch of ElixirSense, you’ll have everything you need. You can use ElixirSense.references/3 to retrieve the list of all references including function calls from aliased/imported modules. You’ll also get the exact ranges you need to replace in each file. Here’s an example:

iex> ElixirSense.references(code, line, column) |> Enum.take(2)
  [
    %{
      uri: "test/support/modules_with_references.ex",
      range: %{
        start: %{line: 26, column: 60},
        end: %{line: 26, column: 64}
      }
    },
    %{
      uri: "test/support/modules_with_references.ex",
      range: %{
        start: %{line: 42, column: 16},
        end: %{line: 42, column: 20}
      }
    }
  ]
justincjohnson

justincjohnson

Wonderful! I’ll give it a try over the weekend and let you know if I have any feedback.

Where Next?

Popular in Questions Top

Fl4m3Ph03n1x
Background Let’s assume I have a typical GenServer that receives messages as requests, does some operation in a DB and returns responses....
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New

Other popular topics 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
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 45766 226
New
quazar
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
New
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
AstonJ
by Lance Halvorsen Elixir and Phoenix are generating tremendous excitement as an unbeatable platform for building modern web application...
460 27162 124
New

We're in Beta

About us Mission Statement