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

sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
nobody
How to bind a phoenix app to a specific ip address? could not find anything about that, nowhere, unfortunately, but for me this is quite...
New
beno
I will often find my self writing things similar to: case some_value do nil -> something() "" -> something() _ -> somethi...
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
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
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
yawaramin
In the Dialyzer docs ( dialyzer — OTP 29.0.2 (dialyzer 6.0.1) ), there is a way to turn off a specific warning for a function: -dialyzer...
New
jaysoifer
Is there a way to rollback a specific migration and only that one (“skipping” all the other ones)? Would mix ecto.rollback -v 200809061...
New

Other popular topics Top

sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 29603 241
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
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
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
boundedvariable
I am going through the kafka architecture. All the features what the kafka is providing are already in Erlang. I would like hear your opi...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement