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

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
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
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
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New

Other popular topics Top

WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
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
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
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
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36432 110
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
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 54250 245
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
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

We're in Beta

About us Mission Statement