mhanberg

mhanberg

Expert LSP Core Team

I use the compilation tracers for Next LS to power its symbol/reference database used for definition/references/hover/workspace symbols etc.

Is there a way (or could there be a way) to know if a function/macro reference is from generated code?

Example

Consider the following code

%GenLSP.Structures.Hover{
  contents: %GenLSP.Structures.MarkupContent{
    kind: GenLSP.Enumerations.MarkupKind.markdown(),
    value: """
    # #{module}
      ^ hover on this location

    #{docs}
    """
  },
  range: %Range{
    start: %Position{line: startl - 1, character: startc - 1},
    end: %Position{line: endl - 1, character: endc - 1}
  }
}

With the tracers as they are today, if you were to hover on the above location, you would see the documentation for Kernel.to_string/1 as seen here

Now this particular example is because string interpolation syntax is compiled into something similar to <<"# ", to_string(module)::binary>> (I think the particulars of this is incorrect, but it is the gist of it).

Now, I wouldn’t consider this a huge problem, except that the range for this “reference” overlaps with the text of the variable binding module, which will interfere with using definition/references on that.

Question

So my question to end this is, is there a way the code from the trace is actually present in the source code?

cc @josevalim

Thanks!

PS: The mailing list details itself as the place to open proposals, and GitHub issues are for actual bugs, so I think Elixir Forum is still the place to ask clarifying questions like this. Please let me know if that understanding is incorrect

Showing Posts 1 to 4

josevalim

josevalim

Creator of Elixir

What you could try to do is to set columns: true in the parser options and then see if there is a column in the metadata. IIRC, generated code does not have column information attached to it.

mhanberg

mhanberg OP

Expert LSP Core Team

That doesn’t seem to be the case from what I’ve observed.

Just to confirm, these are the traces generated from the following piece of code

Code

defmodule TracerDemo do
  defmacrop some_macro(list) do
    quote do
      Enum.map(unquote(list), fn str -> String.upcase(str) end)
    end
  end

  def hello(arg) do
    "hello #{arg}!"
    some_macro(["hello", arg, "!"])
  end
end

Traces

Compiling 1 file (.ex)
[lib/tracer_demo/tracer.ex:3: TracerDemo.Tracer.trace/2]
it #=> :start

[lib/tracer_demo/tracer.ex:3: TracerDemo.Tracer.trace/2]
it #=> {:imported_macro,
 [
   end_of_expression: [newlines: 2, line: 6, column: 6],
   do: [line: 2, column: 30],
   end: [line: 6, column: 3],
   line: 2,
   column: 3
 ], Kernel, :defmacrop, 2}

[lib/tracer_demo/tracer.ex:3: TracerDemo.Tracer.trace/2]
it #=> {:remote_function, [line: 2], :elixir_def, :store_definition, 3}

[lib/tracer_demo/tracer.ex:3: TracerDemo.Tracer.trace/2]
it #=> {:imported_macro,
 [do: [line: 8, column: 18], end: [line: 11, column: 3], line: 8, column: 3],
 Kernel, :def, 2}

[lib/tracer_demo/tracer.ex:3: TracerDemo.Tracer.trace/2]
it #=> {:remote_function, [line: 8], :elixir_def, :store_definition, 3}

[lib/tracer_demo/tracer.ex:3: TracerDemo.Tracer.trace/2]
it #=> {:remote_function, [line: 1], :elixir_utils, :noop, 0}

[lib/tracer_demo/tracer.ex:3: TracerDemo.Tracer.trace/2]
it #=> {:remote_macro, [closing: [line: 9, column: 17], line: 9, column: 12], Kernel,
 :to_string, 1}

[lib/tracer_demo/tracer.ex:3: TracerDemo.Tracer.trace/2]
it #=> {:remote_function, [line: 9], String.Chars, :to_string, 1}

[lib/tracer_demo/tracer.ex:3: TracerDemo.Tracer.trace/2]
it #=> {:local_macro, [closing: [line: 10, column: 35], line: 10, column: 5],
 :some_macro, 1}

[lib/tracer_demo/tracer.ex:3: TracerDemo.Tracer.trace/2]
it #=> {:alias_reference,
 [
   line: 10,
   counter: {TracerDemo, 4},
   last: [line: 4, column: 7],
   column: 7,
   alias: false
 ], Enum}

[lib/tracer_demo/tracer.ex:3: TracerDemo.Tracer.trace/2]
it #=> {:remote_function, [line: 10, closing: [line: 4, column: 63], column: 12], Enum,
 :map, 2}

[lib/tracer_demo/tracer.ex:3: TracerDemo.Tracer.trace/2]
it #=> {:alias_reference,
 [
   line: 10,
   counter: {TracerDemo, 4},
   last: [line: 4, column: 41],
   column: 41,
   alias: false
 ], String}

[lib/tracer_demo/tracer.ex:3: TracerDemo.Tracer.trace/2]
it #=> {:remote_function, [line: 10, closing: [line: 4, column: 58], column: 48],
 String, :upcase, 1}

[lib/tracer_demo/tracer.ex:3: TracerDemo.Tracer.trace/2]
it #=> {:on_module,
 <<70, 79, 82, 49, 0, 0, 8, 176, 66, 69, 65, 77, 65, 116, 85, 56, 0, 0, 1, 19,
   0, 0, 0, 27, 17, 69, 108, 105, 120, 105, 114, 46, 84, 114, 97, 99, 101, 114,
   68, 101, 109, 111, 8, 95, 95, 105, 110, 102, ...>>, :none}

[lib/tracer_demo/tracer.ex:3: TracerDemo.Tracer.trace/2]
it #=> :stop

You can note that the Kernel.to_string trace from the string interpolation is emitted as {:remote_macro, [closing: [line: 9, column: 17], line: 9, column: 12], Kernel, :to_string, 1} and the function and alias traces from the macro are emitted with column information as well.

Full code located here.

josevalim

josevalim

Creator of Elixir

Thank you, apparently it was on my mind to implement this, but I never did. I just pushed a commit to main that addresses the Enum case.

For the string one, there is now a special key called from_interpolation. Note from_brackets is also used when foo[bar] becomes Access.get. The full list is here: Macro — Elixir v1.20.2

mhanberg

mhanberg OP

Expert LSP Core Team

You are amazing :person_bowing:t2:.

Thank you!

— All posts loaded —

Where Next? Top

Trending in Questions Top

water
I’m aware of 2 such projects, are there others? git: GitHub - team-alembic/realworld: A fullstack Phoenix LiveView application with back...
New
katta
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
peck
Hi everyone. My team and I have been working on a fairly modest app based around video streaming and chat, but we’ve landed a customer t...
New
bradley
I really like the adapter patterns that ecto, nebulex, waffle, etc. use and would love find something similar for a key management servic...
New
unaware8150
Hello folks! So at work, we are seeing some situations where we have to define some “fixed” strings that are used across the codebase in...
New
Alvinkariuki
How Can I Optimise Compile Time Dependencies I have been building an elixir application for about 2 years now. Many modules and files ha...
New
leoschweizer
Hi, we run different LLM models across a cluster, with each node loading a configurable subset. Model-specific queues let nodes consume ...
New

Other Trending Topics Top

budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
mudasobwa
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
juhalehtonen
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New
budgie
I love Elixir. It’s one of 2 programming languages I’ve ever fallen in love with. But I don’t use it anymore. Serverless was the promis...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews