Skip to content

Commit 8515c37

Browse files
committed
fix completions crash on too long regex
1 parent 53096f7 commit 8515c37

File tree

1 file changed

+32
-14
lines changed

1 file changed

+32
-14
lines changed

apps/language_server/lib/language_server/providers/completion.ex

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -153,18 +153,22 @@ defmodule ElixirLS.LanguageServer.Providers.Completion do
153153
end
154154

155155
def_before =
156-
cond do
157-
Regex.match?(~r/(defdelegate|defp?)\s*#{Regex.escape(prefix)}$/u, text_before_cursor) ->
158-
:def
159-
160-
Regex.match?(
161-
~r/(defguardp?|defmacrop?)\s*#{Regex.escape(prefix)}$/u,
162-
text_before_cursor
163-
) ->
164-
:defmacro
165-
166-
true ->
167-
nil
156+
try do
157+
cond do
158+
Regex.match?(~r/(defdelegate|defp?)\s*#{Regex.escape(prefix)}$/u, text_before_cursor) ->
159+
:def
160+
161+
Regex.match?(
162+
~r/(defguardp?|defmacrop?)\s*#{Regex.escape(prefix)}$/u,
163+
text_before_cursor
164+
) ->
165+
:defmacro
166+
167+
true ->
168+
nil
169+
end
170+
rescue
171+
Regex.CompileError -> nil
168172
end
169173

170174
container_cursor_quoted =
@@ -221,15 +225,29 @@ defmodule ElixirLS.LanguageServer.Providers.Completion do
221225

222226
line_indent = String.length(line_text) - String.length(String.trim_leading(line_text))
223227

228+
pipe_before? =
229+
try do
230+
Regex.match?(~r/\|>\s*#{Regex.escape(prefix)}$/u, text_before_cursor)
231+
rescue
232+
Regex.CompileError -> false
233+
end
234+
235+
capture_before? =
236+
try do
237+
Regex.match?(~r/&#{Regex.escape(prefix)}$/u, text_before_cursor)
238+
rescue
239+
Regex.CompileError -> false
240+
end
241+
224242
context = %{
225243
container_cursor_to_quoted: container_cursor_quoted,
226244
text_before_cursor: text_before_cursor,
227245
text_after_cursor: text_after_cursor,
228246
prefix: prefix,
229247
remote_calls?: match?({:dot, _, _}, NormalizedCode.Fragment.cursor_context(prefix)),
230248
def_before: def_before,
231-
pipe_before?: Regex.match?(~r/\|>\s*#{Regex.escape(prefix)}$/u, text_before_cursor),
232-
capture_before?: Regex.match?(~r/&#{Regex.escape(prefix)}$/u, text_before_cursor),
249+
pipe_before?: pipe_before?,
250+
capture_before?: capture_before?,
233251
scope: scope,
234252
module: env.module,
235253
line: line,

0 commit comments

Comments
 (0)