Skip to content

Commit fdf528c

Browse files
committed
fix(references,definition): better references of symbols
Closes #342 Closes #184 Closes #304
1 parent 3ea0fb7 commit fdf528c

File tree

4 files changed

+31
-14
lines changed

4 files changed

+31
-14
lines changed

lib/next_ls.ex

+2-1
Original file line numberDiff line numberDiff line change
@@ -1351,6 +1351,7 @@ defmodule NextLS do
13511351
FROM "symbols" sym
13521352
WHERE sym.file = ?
13531353
AND sym.line = ?
1354+
AND ? BETWEEN sym.column AND sym.end_column
13541355
ORDER BY sym.id ASC
13551356
LIMIT 1
13561357
"""
@@ -1365,7 +1366,7 @@ defmodule NextLS do
13651366
LIMIT 1
13661367
"""
13671368

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

lib/next_ls/db.ex

+21-11
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,10 @@ defmodule NextLS.DB do
108108
__query__(
109109
{conn, s.logger},
110110
~Q"""
111-
INSERT INTO symbols (module, file, type, name, line, 'column', source)
112-
VALUES (?, ?, ?, ?, ?, ?, ?);
111+
INSERT INTO symbols (module, file, type, name, line, 'column', 'end_column', source)
112+
VALUES (?, ?, ?, ?, ?, ?, ?, ?);
113113
""",
114-
[mod, file, "defmodule", mod, module_line, 1, source]
114+
[mod, file, "defmodule", mod, module_line, 1, String.length(Macro.to_string(mod)), source]
115115
)
116116

117117
if struct do
@@ -120,19 +120,28 @@ defmodule NextLS.DB do
120120
__query__(
121121
{conn, s.logger},
122122
~Q"""
123-
INSERT INTO symbols (module, file, type, name, line, 'column', source)
124-
VALUES (?, ?, ?, ?, ?, ?, ?);
123+
INSERT INTO symbols (module, file, type, name, line, 'column', 'end_column', source)
124+
VALUES (?, ?, ?, ?, ?, ?, ?, ?);
125125
""",
126-
[mod, file, "defstruct", "%#{Macro.to_string(mod)}{}", meta[:line], 1, source]
126+
[
127+
mod,
128+
file,
129+
"defstruct",
130+
"%#{Macro.to_string(mod)}{}",
131+
meta[:line],
132+
meta[:column] || 1,
133+
meta[:column] || 1,
134+
source
135+
]
127136
)
128137
end
129138

130139
for {name, {:v1, type, _meta, clauses}} <- defs, {meta, params, _, _} <- clauses do
131140
__query__(
132141
{conn, s.logger},
133142
~Q"""
134-
INSERT INTO symbols (module, file, type, name, params, line, 'column', source)
135-
VALUES (?, ?, ?, ?, ?, ?, ?, ?);
143+
INSERT INTO symbols (module, file, type, name, params, line, 'column', end_column, source)
144+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?);
136145
""",
137146
[
138147
mod,
@@ -142,6 +151,7 @@ defmodule NextLS.DB do
142151
:erlang.term_to_binary(params),
143152
meta[:line],
144153
meta[:column] || 1,
154+
(meta[:column] || 1) + String.length(to_string(name)) - 1,
145155
source
146156
]
147157
)
@@ -151,10 +161,10 @@ defmodule NextLS.DB do
151161
__query__(
152162
{conn, s.logger},
153163
~Q"""
154-
INSERT INTO symbols (module, file, type, name, line, 'column', source)
155-
VALUES (?, ?, ?, ?, ?, ?, ?);
164+
INSERT INTO symbols (module, file, type, name, line, 'column', 'end_column', source)
165+
VALUES (?, ?, ?, ?, ?, ?, ?, ?);
156166
""",
157-
[mod, file, type, name, line, column, source]
167+
[mod, file, type, name, line, column, column + String.length(to_string(name)) - 1, source]
158168
)
159169
end
160170

lib/next_ls/db/schema.ex

+2-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ defmodule NextLS.DB.Schema do
2323

2424
alias NextLS.DB
2525

26-
@version 6
26+
@version 7
2727

2828
def init(conn) do
2929
# FIXME: this is odd tech debt. not a big deal but is confusing
@@ -83,6 +83,7 @@ defmodule NextLS.DB.Schema do
8383
params blob,
8484
line integer NOT NULL,
8585
column integer NOT NULL,
86+
end_column integer NOT NULL,
8687
source text NOT NULL DEFAULT 'user',
8788
inserted_at text NOT NULL DEFAULT CURRENT_TIMESTAMP
8889
);

lib/next_ls/definition.ex

+6-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,12 @@ defmodule NextLS.Definition do
1919
AND ? <= refs.end_line
2020
AND refs.start_column <= ?
2121
AND ? <= refs.end_column
22-
ORDER BY refs.id asc
22+
ORDER BY
23+
(CASE refs.type
24+
WHEN 'function' THEN 0
25+
WHEN 'module' THEN 1
26+
ELSE 2
27+
END) asc
2328
LIMIT 1;
2429
""",
2530
[file, line, line, col, col]

0 commit comments

Comments
 (0)