From 625c6989d6907d0a607037015d3229042a5c450f Mon Sep 17 00:00:00 2001 From: Scott Ming Date: Fri, 21 Jun 2024 15:16:18 +0800 Subject: [PATCH] Determine immediate_parent is `:defmodule` or not --- .../code_action/handlers/organize_aliases.ex | 26 ++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/apps/remote_control/lib/lexical/remote_control/code_action/handlers/organize_aliases.ex b/apps/remote_control/lib/lexical/remote_control/code_action/handlers/organize_aliases.ex index e51d404ae..c8ea42b7b 100644 --- a/apps/remote_control/lib/lexical/remote_control/code_action/handlers/organize_aliases.ex +++ b/apps/remote_control/lib/lexical/remote_control/code_action/handlers/organize_aliases.ex @@ -83,7 +83,31 @@ defmodule Lexical.RemoteControl.CodeAction.Handlers.OrganizeAliases do # | case Analysis.module_scope(analysis, range) do %Scope{aliases: [_ | _]} -> - token_at_cursor_is_blank?(analysis, range.start) + cursor_path = Ast.cursor_path(analysis, range.start) + + immediate_parent = + Enum.find(cursor_path, fn + [{:__cursor__, _, _} | _] -> + false + + {:__block__, _, _} -> + false + + {{:__block__, _, _}, _} -> + false + + [{{:__block__, _, _}, _} | _] -> + false + + {:__cursor__, _, _} -> + false + + _ -> + true + end) + + immediate_parent_is_defmodule? = match?({:defmodule, _, _}, immediate_parent) + token_at_cursor_is_blank?(analysis, range.start) and immediate_parent_is_defmodule? _ -> false