lexical-lsp / lexical

Lexical is a next-generation elixir language server
779 stars 77 forks source link

Fixed: Error while calculating block spans #687

Closed scohen closed 2 months ago

scohen commented 2 months ago

While trying to enhance the indexing of protocol modules, I found a fairly serious bug with alias / import detection. When we'd encounter a block of code (usually starting with do), we used soureror's detection of start and end. The problem is that it starts that block before the do, so for example, defmodule MyModule do.. would start at the defmodule call.

This meant that the module we were defining starts at the defmodule call, so our current module detection would be off by almost an entire line. This caused errors when builidng module names for protocols and when people would do defmodule __MODULE__.Child do.

We were also making nonsensical module names like Elixir.__MODULE__ when we called current_module despite the fact that not all code is inside a module.

Now, module detection is character accurate and can fail, and some code had to have error detection added. For future reference, all calls to current_module(analysis, position) can now fail, so doing a hard pattern match on {:ok, module} will fail in contexts outside of a module.