@@ -908,6 +908,62 @@ defmodule ElixirLS.LanguageServer.Providers.CompletionTest do
908
908
"""
909
909
}
910
910
end
911
+
912
+ test "will suggest defmodule with module_name snippet when file path matches **/lib/**/*.ex" do
913
+ text = """
914
+ defmod
915
+ # ^
916
+ """
917
+
918
+ { line , char } = { 0 , 6 }
919
+
920
+ TestUtils . assert_has_cursor_char ( text , line , char )
921
+
922
+ assert { :ok , % { "items" => [ first | _ ] = _items } } =
923
+ Completion . completion (
924
+ text ,
925
+ line ,
926
+ char ,
927
+ @ supports
928
+ |> Keyword . put (
929
+ :file_path ,
930
+ "/some/path/my_project/lib/my_project/sub_folder/my_file.ex"
931
+ )
932
+ )
933
+
934
+ assert % {
935
+ "label" => "defmodule" ,
936
+ "insertText" => "defmodule MyProject.SubFolder.MyFile$1 do\n \t $0\n end"
937
+ } = first
938
+ end
939
+
940
+ test "will suggest defmodule without module_name snippet when file path does not match expected patterns" do
941
+ text = """
942
+ defmod
943
+ # ^
944
+ """
945
+
946
+ { line , char } = { 0 , 6 }
947
+
948
+ TestUtils . assert_has_cursor_char ( text , line , char )
949
+
950
+ assert { :ok , % { "items" => [ first | _ ] = _items } } =
951
+ Completion . completion (
952
+ text ,
953
+ line ,
954
+ char ,
955
+ @ supports
956
+ |> Keyword . put (
957
+ :file_path ,
958
+ "/some/path/my_project/lib/my_project/sub_folder/my_file.heex"
959
+ )
960
+ )
961
+
962
+ assert % {
963
+ "label" => "defmodule" ,
964
+ "insertText" => "defmodule $1 do\n \t $0\n end"
965
+ } = first
966
+ end
911
967
end
912
968
913
969
describe "generic suggestions" do
@@ -1053,4 +1109,79 @@ defmodule ElixirLS.LanguageServer.Providers.CompletionTest do
1053
1109
assert insert_text =~ "if do\n \t "
1054
1110
end
1055
1111
end
1112
+
1113
+ describe "suggest_module_name/1" do
1114
+ import Completion , only: [ suggest_module_name: 1 ]
1115
+
1116
+ test "returns nil if current file_path is empty" do
1117
+ assert nil == suggest_module_name ( "" )
1118
+ end
1119
+
1120
+ test "returns nil if current file is not an .ex file" do
1121
+ assert nil == suggest_module_name ( "some/path/lib/dir/file.heex" )
1122
+ end
1123
+
1124
+ test "returns nil if current file is an .ex file but no lib folder exists in path" do
1125
+ assert nil == suggest_module_name ( "some/path/not_lib/dir/file.ex" )
1126
+ end
1127
+
1128
+ test "returns nil if current file is an *_test.exs file but no test folder exists in path" do
1129
+ assert nil == suggest_module_name ( "some/path/not_test/dir/file_test.exs" )
1130
+ end
1131
+
1132
+ test "returns an appropriate suggestion if file directly under lib" do
1133
+ assert "MyProject" == suggest_module_name ( "some/path/my_project/lib/my_project.ex" )
1134
+ end
1135
+
1136
+ test "returns an appropriate suggestion if file arbitrarily nested under lib/" do
1137
+ assert "MyProject.Foo.Bar.Baz.MyFile" =
1138
+ suggest_module_name ( "some/path/my_project/lib/my_project/foo/bar/baz/my_file.ex" )
1139
+ end
1140
+
1141
+ test "returns an appropriate suggestion if file directly under test/" do
1142
+ assert "MyProjectTest" ==
1143
+ suggest_module_name ( "some/path/my_project/test/my_project_test.exs" )
1144
+ end
1145
+
1146
+ test "returns an appropriate suggestion if file arbitrarily nested under test" do
1147
+ assert "MyProject.Foo.Bar.Baz.MyFileTest" ==
1148
+ suggest_module_name (
1149
+ "some/path/my_project/test/my_project/foo/bar/baz/my_file_test.exs"
1150
+ )
1151
+ end
1152
+
1153
+ test "returns an appropriate suggestion if file is part of an umbrella project" do
1154
+ assert "MySubApp.Foo.Bar.Baz" ==
1155
+ suggest_module_name (
1156
+ "some/path/my_umbrella_project/apps/my_sub_app/lib/my_sub_app/foo/bar/baz.ex"
1157
+ )
1158
+ end
1159
+
1160
+ test "returns appropriate suggestions for modules nested under known phoenix dirs" do
1161
+ [
1162
+ { "MyProjectWeb.MyController" , "controllers/my_controller.ex" } ,
1163
+ { "MyProjectWeb.MyPlug" , "plugs/my_plug.ex" } ,
1164
+ { "MyProjectWeb.MyView" , "views/my_view.ex" } ,
1165
+ { "MyProjectWeb.MyChannel" , "channels/my_channel.ex" } ,
1166
+ { "MyProjectWeb.MyEndpoint" , "endpoints/my_endpoint.ex" } ,
1167
+ { "MyProjectWeb.MySocket" , "sockets/my_socket.ex" }
1168
+ ]
1169
+ |> Enum . each ( fn { expected_module_name , partial_path } ->
1170
+ path = "some/path/my_project/lib/my_project_web/#{ partial_path } "
1171
+ assert expected_module_name == suggest_module_name ( path )
1172
+ end )
1173
+ end
1174
+
1175
+ test "uses known Phoenix dirs as part of a module's name if these are not located directly beneath the *_web folder" do
1176
+ assert "MyProject.Controllers.MyController" ==
1177
+ suggest_module_name (
1178
+ "some/path/my_project/lib/my_project/controllers/my_controller.ex"
1179
+ )
1180
+
1181
+ assert "MyProjectWeb.SomeNestedDir.Controllers.MyController" ==
1182
+ suggest_module_name (
1183
+ "some/path/my_project/lib/my_project_web/some_nested_dir/controllers/my_controller.ex"
1184
+ )
1185
+ end
1186
+ end
1056
1187
end
0 commit comments