1
1
defmodule NextLS.Snippet do
2
2
@ moduledoc false
3
3
4
- def get ( "defmodule/2" , nil ) do
4
+ def get ( label , trigger_character , opts \\ [ ] )
5
+
6
+ def get ( "defmodule/2" , nil , opts ) do
7
+ uri = Keyword . get ( opts , :uri )
8
+
9
+ modulename =
10
+ if uri do
11
+ infer_module_name ( uri )
12
+ else
13
+ "ModuleName"
14
+ end
15
+
5
16
% {
6
17
kind: GenLSP.Enumerations.CompletionItemKind . snippet ( ) ,
7
18
insert_text_format: GenLSP.Enumerations.InsertTextFormat . snippet ( ) ,
8
19
insert_text: """
9
- defmodule ${1:ModuleName } do
20
+ defmodule ${1:#{ modulename } } do
10
21
$0
11
22
end
12
23
"""
13
24
}
14
25
end
15
26
16
- def get ( "defstruct/1" , nil ) do
27
+ def get ( "defstruct/1" , nil , _opts ) do
17
28
% {
18
29
kind: GenLSP.Enumerations.CompletionItemKind . snippet ( ) ,
19
30
insert_text_format: GenLSP.Enumerations.InsertTextFormat . snippet ( ) ,
@@ -23,7 +34,7 @@ defmodule NextLS.Snippet do
23
34
}
24
35
end
25
36
26
- def get ( "defprotocol/2" , nil ) do
37
+ def get ( "defprotocol/2" , nil , _opts ) do
27
38
% {
28
39
kind: GenLSP.Enumerations.CompletionItemKind . snippet ( ) ,
29
40
insert_text_format: GenLSP.Enumerations.InsertTextFormat . snippet ( ) ,
@@ -35,7 +46,7 @@ defmodule NextLS.Snippet do
35
46
}
36
47
end
37
48
38
- def get ( "defimpl/2" , nil ) do
49
+ def get ( "defimpl/2" , nil , _opts ) do
39
50
% {
40
51
kind: GenLSP.Enumerations.CompletionItemKind . snippet ( ) ,
41
52
insert_text_format: GenLSP.Enumerations.InsertTextFormat . snippet ( ) ,
@@ -49,7 +60,7 @@ defmodule NextLS.Snippet do
49
60
}
50
61
end
51
62
52
- def get ( "defimpl/3" , nil ) do
63
+ def get ( "defimpl/3" , nil , _opts ) do
53
64
% {
54
65
kind: GenLSP.Enumerations.CompletionItemKind . snippet ( ) ,
55
66
insert_text_format: GenLSP.Enumerations.InsertTextFormat . snippet ( ) ,
@@ -63,7 +74,7 @@ defmodule NextLS.Snippet do
63
74
}
64
75
end
65
76
66
- def get ( "def/" <> _ , nil ) do
77
+ def get ( "def/" <> _ , nil , _opts ) do
67
78
% {
68
79
kind: GenLSP.Enumerations.CompletionItemKind . snippet ( ) ,
69
80
insert_text_format: GenLSP.Enumerations.InsertTextFormat . snippet ( ) ,
@@ -75,7 +86,7 @@ defmodule NextLS.Snippet do
75
86
}
76
87
end
77
88
78
- def get ( "defp/" <> _ , nil ) do
89
+ def get ( "defp/" <> _ , nil , _opts ) do
79
90
% {
80
91
kind: GenLSP.Enumerations.CompletionItemKind . snippet ( ) ,
81
92
insert_text_format: GenLSP.Enumerations.InsertTextFormat . snippet ( ) ,
@@ -87,7 +98,7 @@ defmodule NextLS.Snippet do
87
98
}
88
99
end
89
100
90
- def get ( "defmacro/" <> _ , nil ) do
101
+ def get ( "defmacro/" <> _ , nil , _opts ) do
91
102
% {
92
103
kind: GenLSP.Enumerations.CompletionItemKind . snippet ( ) ,
93
104
insert_text_format: GenLSP.Enumerations.InsertTextFormat . snippet ( ) ,
@@ -101,7 +112,7 @@ defmodule NextLS.Snippet do
101
112
}
102
113
end
103
114
104
- def get ( "defmacrop/" <> _ , nil ) do
115
+ def get ( "defmacrop/" <> _ , nil , _opts ) do
105
116
% {
106
117
kind: GenLSP.Enumerations.CompletionItemKind . snippet ( ) ,
107
118
insert_text_format: GenLSP.Enumerations.InsertTextFormat . snippet ( ) ,
@@ -115,7 +126,7 @@ defmodule NextLS.Snippet do
115
126
}
116
127
end
117
128
118
- def get ( "for/" <> _ , nil ) do
129
+ def get ( "for/" <> _ , nil , _opts ) do
119
130
% {
120
131
kind: GenLSP.Enumerations.CompletionItemKind . snippet ( ) ,
121
132
insert_text_format: GenLSP.Enumerations.InsertTextFormat . snippet ( ) ,
@@ -127,7 +138,7 @@ defmodule NextLS.Snippet do
127
138
}
128
139
end
129
140
130
- def get ( "with/" <> _ , nil ) do
141
+ def get ( "with/" <> _ , nil , _opts ) do
131
142
% {
132
143
kind: GenLSP.Enumerations.CompletionItemKind . snippet ( ) ,
133
144
insert_text_format: GenLSP.Enumerations.InsertTextFormat . snippet ( ) ,
@@ -139,7 +150,7 @@ defmodule NextLS.Snippet do
139
150
}
140
151
end
141
152
142
- def get ( "case/" <> _ , nil ) do
153
+ def get ( "case/" <> _ , nil , _opts ) do
143
154
% {
144
155
kind: GenLSP.Enumerations.CompletionItemKind . snippet ( ) ,
145
156
insert_text_format: GenLSP.Enumerations.InsertTextFormat . snippet ( ) ,
@@ -155,7 +166,7 @@ defmodule NextLS.Snippet do
155
166
}
156
167
end
157
168
158
- def get ( "cond/" <> _ , nil ) do
169
+ def get ( "cond/" <> _ , nil , _opts ) do
159
170
% {
160
171
kind: GenLSP.Enumerations.CompletionItemKind . snippet ( ) ,
161
172
insert_text_format: GenLSP.Enumerations.InsertTextFormat . snippet ( ) ,
@@ -171,7 +182,47 @@ defmodule NextLS.Snippet do
171
182
}
172
183
end
173
184
174
- def get ( _label , _trigger_character ) do
185
+ def get ( _label , _trigger_character , _opts ) do
175
186
nil
176
187
end
188
+
189
+ defp infer_module_name ( uri ) do
190
+ result =
191
+ uri
192
+ |> Path . split ( )
193
+ |> Enum . reduce ( false , fn
194
+ "lib" , _ ->
195
+ { :lib , [ ] }
196
+
197
+ "test" , _ ->
198
+ { :test , [ ] }
199
+
200
+ "support" , { :test , _ } ->
201
+ { :lib , [ ] }
202
+
203
+ _ , false ->
204
+ false
205
+
206
+ element , { type , elements } ->
207
+ camelized =
208
+ element
209
+ |> Path . rootname ( )
210
+ |> Macro . camelize ( )
211
+
212
+ { type , [ camelized | elements ] }
213
+ end )
214
+
215
+ case result do
216
+ { _ , parts } ->
217
+ parts
218
+ |> Enum . reverse ( )
219
+ |> Enum . join ( "." )
220
+
221
+ false ->
222
+ uri
223
+ |> Path . basename ( )
224
+ |> Path . rootname ( )
225
+ |> Macro . camelize ( )
226
+ end
227
+ end
177
228
end
0 commit comments