Skip to content

Commit

Permalink
fix(references,definition): better references of symbols
Browse files Browse the repository at this point in the history
Closes #342
Closes #184
Closes #304
  • Loading branch information
mhanberg committed Apr 21, 2024
1 parent 3ea0fb7 commit fdf528c
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 14 deletions.
3 changes: 2 additions & 1 deletion lib/next_ls.ex
Original file line number Diff line number Diff line change
Expand Up @@ -1351,6 +1351,7 @@ defmodule NextLS do
FROM "symbols" sym
WHERE sym.file = ?
AND sym.line = ?
AND ? BETWEEN sym.column AND sym.end_column
ORDER BY sym.id ASC
LIMIT 1
"""
Expand All @@ -1365,7 +1366,7 @@ defmodule NextLS do
LIMIT 1
"""

case DB.query(database, definition_query, [file, line]) do
case DB.query(database, definition_query, [file, line, col]) do
[[module, "defmodule", _]] ->
{:module, module}

Expand Down
32 changes: 21 additions & 11 deletions lib/next_ls/db.ex
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,10 @@ defmodule NextLS.DB do
__query__(
{conn, s.logger},
~Q"""
INSERT INTO symbols (module, file, type, name, line, 'column', source)
VALUES (?, ?, ?, ?, ?, ?, ?);
INSERT INTO symbols (module, file, type, name, line, 'column', 'end_column', source)
VALUES (?, ?, ?, ?, ?, ?, ?, ?);
""",
[mod, file, "defmodule", mod, module_line, 1, source]
[mod, file, "defmodule", mod, module_line, 1, String.length(Macro.to_string(mod)), source]
)

if struct do
Expand All @@ -120,19 +120,28 @@ defmodule NextLS.DB do
__query__(
{conn, s.logger},
~Q"""
INSERT INTO symbols (module, file, type, name, line, 'column', source)
VALUES (?, ?, ?, ?, ?, ?, ?);
INSERT INTO symbols (module, file, type, name, line, 'column', 'end_column', source)
VALUES (?, ?, ?, ?, ?, ?, ?, ?);
""",
[mod, file, "defstruct", "%#{Macro.to_string(mod)}{}", meta[:line], 1, source]
[
mod,
file,
"defstruct",
"%#{Macro.to_string(mod)}{}",
meta[:line],
meta[:column] || 1,
meta[:column] || 1,
source
]
)
end

for {name, {:v1, type, _meta, clauses}} <- defs, {meta, params, _, _} <- clauses do
__query__(
{conn, s.logger},
~Q"""
INSERT INTO symbols (module, file, type, name, params, line, 'column', source)
VALUES (?, ?, ?, ?, ?, ?, ?, ?);
INSERT INTO symbols (module, file, type, name, params, line, 'column', end_column, source)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?);
""",
[
mod,
Expand All @@ -142,6 +151,7 @@ defmodule NextLS.DB do
:erlang.term_to_binary(params),
meta[:line],
meta[:column] || 1,
(meta[:column] || 1) + String.length(to_string(name)) - 1,
source
]
)
Expand All @@ -151,10 +161,10 @@ defmodule NextLS.DB do
__query__(
{conn, s.logger},
~Q"""
INSERT INTO symbols (module, file, type, name, line, 'column', source)
VALUES (?, ?, ?, ?, ?, ?, ?);
INSERT INTO symbols (module, file, type, name, line, 'column', 'end_column', source)
VALUES (?, ?, ?, ?, ?, ?, ?, ?);
""",
[mod, file, type, name, line, column, source]
[mod, file, type, name, line, column, column + String.length(to_string(name)) - 1, source]
)
end

Expand Down
3 changes: 2 additions & 1 deletion lib/next_ls/db/schema.ex
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ defmodule NextLS.DB.Schema do

alias NextLS.DB

@version 6
@version 7

def init(conn) do
# FIXME: this is odd tech debt. not a big deal but is confusing
Expand Down Expand Up @@ -83,6 +83,7 @@ defmodule NextLS.DB.Schema do
params blob,
line integer NOT NULL,
column integer NOT NULL,
end_column integer NOT NULL,
source text NOT NULL DEFAULT 'user',
inserted_at text NOT NULL DEFAULT CURRENT_TIMESTAMP
);
Expand Down
7 changes: 6 additions & 1 deletion lib/next_ls/definition.ex
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@ defmodule NextLS.Definition do
AND ? <= refs.end_line
AND refs.start_column <= ?
AND ? <= refs.end_column
ORDER BY refs.id asc
ORDER BY
(CASE refs.type
WHEN 'function' THEN 0
WHEN 'module' THEN 1
ELSE 2
END) asc
LIMIT 1;
""",
[file, line, line, col, col]
Expand Down

0 comments on commit fdf528c

Please sign in to comment.