diff --git a/lib/next_ls.ex b/lib/next_ls.ex index 50ba5026..4a5cc898 100644 --- a/lib/next_ls.ex +++ b/lib/next_ls.ex @@ -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 """ @@ -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} diff --git a/lib/next_ls/db.ex b/lib/next_ls/db.ex index 6f1d63a2..1af61022 100644 --- a/lib/next_ls/db.ex +++ b/lib/next_ls/db.ex @@ -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 @@ -120,10 +120,19 @@ 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 @@ -131,8 +140,8 @@ defmodule NextLS.DB 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, @@ -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 ] ) @@ -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 diff --git a/lib/next_ls/db/schema.ex b/lib/next_ls/db/schema.ex index 455ded65..00737b7d 100644 --- a/lib/next_ls/db/schema.ex +++ b/lib/next_ls/db/schema.ex @@ -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 @@ -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 ); diff --git a/lib/next_ls/definition.ex b/lib/next_ls/definition.ex index 0fe7cb46..03503114 100644 --- a/lib/next_ls/definition.ex +++ b/lib/next_ls/definition.ex @@ -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]