From 5509b4e8a6e551c0eab29bb591cf0b4ca98dad17 Mon Sep 17 00:00:00 2001 From: Mitchell Hanberg <mitch@mitchellhanberg.com> Date: Thu, 4 Apr 2024 16:49:54 -0400 Subject: [PATCH] fix(completions): correctly insert cursor position (#408) The cursor was being inserted in a strange matter, which made the modified ast not have a `__cursor__()` function, so we couldn't find the cursor. Really, all we need to do is insert the cursor on its own line between the line. For example, if you were to put the cursor into the middle of this line of code ```elixir Enum.map(some_list, fn row -> end) ``` It would insert the cursor like so ```elixir Enum.map(some_list, fn row -> __cursor__() end) ``` Fixes #406 --- flake.nix | 2 +- lib/next_ls.ex | 5 ++++- mix.lock | 2 +- priv/monkey/_next_ls_private_compiler.ex | 5 ----- 4 files changed, 6 insertions(+), 8 deletions(-) diff --git a/flake.nix b/flake.nix index 82659ee4..02f123fd 100644 --- a/flake.nix +++ b/flake.nix @@ -115,7 +115,7 @@ src = self.outPath; inherit version elixir; pname = "next-ls-deps"; - hash = "sha256-9LKtrNvNU8swcaaW3OuOCNKCa76+mvtu7MsWuy204/E="; + hash = "sha256-aGVoJJPK+2phB9HLoIp50Qz0s+3tA9PU+yg8nvOGNRY="; mixEnv = "prod"; }; diff --git a/lib/next_ls.ex b/lib/next_ls.ex index 3334aea7..bcb690c8 100644 --- a/lib/next_ls.ex +++ b/lib/next_ls.ex @@ -581,7 +581,10 @@ defmodule NextLS do document |> List.update_at(position.line, fn row -> {front, back} = String.split_at(row, position.character) - String.slice(front, -1..1) <> "__cursor__()" <> back + # all we need to do is insert the cursor so we can find the spot to then + # calculate the environment, it doens't really matter if its valid code, + # it probably isn't already + front <> "\n__cursor__()\n" <> back end) |> Enum.join("\n") diff --git a/mix.lock b/mix.lock index 5b1564b4..8b809bb6 100644 --- a/mix.lock +++ b/mix.lock @@ -47,7 +47,7 @@ "req": {:hex, :req, "0.4.0", "1c759054dd64ef1b1a0e475c2d2543250d18f08395d3174c371b7746984579ce", [:mix], [{:brotli, "~> 0.3.1", [hex: :brotli, repo: "hexpm", optional: true]}, {:ezstd, "~> 1.0", [hex: :ezstd, repo: "hexpm", optional: true]}, {:finch, "~> 0.9", [hex: :finch, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}, {:mime, "~> 1.6 or ~> 2.0", [hex: :mime, repo: "hexpm", optional: false]}, {:nimble_csv, "~> 1.0", [hex: :nimble_csv, repo: "hexpm", optional: true]}, {:plug, "~> 1.0", [hex: :plug, repo: "hexpm", optional: true]}], "hexpm", "f53eadc32ebefd3e5d50390356ec3a59ed2b8513f7da8c6c3f2e14040e9fe989"}, "schematic": {:hex, :schematic, "0.2.1", "0b091df94146fd15a0a343d1bd179a6c5a58562527746dadd09477311698dbb1", [:mix], [{:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "0b255d65921e38006138201cd4263fd8bb807d9dfc511074615cd264a571b3b1"}, "sourceror": {:hex, :sourceror, "1.0.2", "c5e86fdc14881f797749d1fe5df017ca66727a8146e7ee3e736605a3df78f3e6", [:mix], [], "hexpm", "832335e87d0913658f129d58b2a7dc0490ddd4487b02de6d85bca0169ec2bd79"}, - "spitfire": {:git, "https://github.com/elixir-tools/spitfire.git", "26e68d4b184caf32ccee840ef0e862c911ff51ef", []}, + "spitfire": {:git, "https://github.com/elixir-tools/spitfire.git", "d39f91d6e66f70cb3093bad4947d7e46bba40b43", []}, "ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.7", "354c321cf377240c7b8716899e182ce4890c5938111a1296add3ec74cf1715df", [:make, :mix, :rebar3], [], "hexpm", "fe4c190e8f37401d30167c8c405eda19469f34577987c76dde613e838bbc67f8"}, "styler": {:hex, :styler, "0.8.1", "f3c0f65023e4bfbf7e7aa752d128b8475fdabfd30f96ee7314b84480cc56e788", [:mix], [], "hexpm", "1aa48d3aa689a639289af3d8254d40e068e98c083d6e5e3d1a695e71a147b344"}, "telemetry": {:hex, :telemetry, "1.2.1", "68fdfe8d8f05a8428483a97d7aab2f268aaff24b49e0f599faa091f1d4e7f61c", [:rebar3], [], "hexpm", "dad9ce9d8effc621708f99eac538ef1cbe05d6a874dd741de2e689c47feafed5"}, diff --git a/priv/monkey/_next_ls_private_compiler.ex b/priv/monkey/_next_ls_private_compiler.ex index c8e075e7..3eab0d60 100644 --- a/priv/monkey/_next_ls_private_compiler.ex +++ b/priv/monkey/_next_ls_private_compiler.ex @@ -254,11 +254,6 @@ defmodule :_next_ls_private_formatter do ins: [text: :green, space: :green_background] ] - @callback features(Keyword.t()) :: [sigils: [atom()], extensions: [binary()]] - - @callback format(String.t(), Keyword.t()) :: String.t() - - @impl true def run(args) do cwd = File.cwd!() {opts, args} = OptionParser.parse!(args, strict: @switches)