Fix completion crash on higher unicode characters (#466)

* Fix completion crash on higher unicode characters

* Safely rescue from intellisense errors
This commit is contained in:
Jonatan Kłosko 2021-07-26 11:34:22 +02:00 committed by GitHub
parent 943d8b6059
commit f9ec058e43
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 19 additions and 2 deletions

View file

@ -13,6 +13,8 @@ defmodule Livebook.Evaluator do
use GenServer, restart: :temporary
require Logger
alias Livebook.Evaluator
@type t :: GenServer.server()
@ -214,7 +216,15 @@ defmodule Livebook.Evaluator do
def handle_cast({:handle_intellisense, send_to, ref, request, evaluation_ref}, state) do
context = get_context(state, evaluation_ref)
response = Livebook.Intellisense.handle_request(request, context.binding, context.env)
# Safely rescue from intellisense errors
response =
try do
Livebook.Intellisense.handle_request(request, context.binding, context.env)
rescue
error -> Logger.error(Exception.format(:error, error, __STACKTRACE__))
end
send(send_to, {:intellisense_response, ref, response})
{:noreply, state}

View file

@ -172,6 +172,7 @@ defmodule Livebook.Intellisense do
defp subject_range(line, index) do
{left, right} = String.split_at(line, index)
bytes_until_index = byte_size(left)
left =
left
@ -186,7 +187,7 @@ defmodule Livebook.Intellisense do
|> consume_until(@space ++ @operators ++ @punctuation, @closing_identifier)
|> List.to_string()
{index - byte_size(left), index + byte_size(right)}
{bytes_until_index - byte_size(left), bytes_until_index + byte_size(right)}
end
defp consume_until(acc \\ [], chars, stop, stop_include)

View file

@ -1064,5 +1064,11 @@ defmodule Livebook.IntellisenseTest do
assert %{contents: [file_read]} = Intellisense.get_details(":file.read()", 8, binding, env)
assert file_read =~ "Typical error reasons:"
end
test "properly parses unicode" do
{binding, env} = eval(do: nil)
assert nil == Intellisense.get_details("msg = '🍵'", 8, binding, env)
end
end
end