Skip to content

Commit

Permalink
Convert JavaScript string column to Elixir (livebook-dev#467)
Browse files Browse the repository at this point in the history
* Fix column/index wording

* Convert JavaScript string column to Elixir
  • Loading branch information
jonatanklosko authored Jul 26, 2021
1 parent f9ec058 commit af50646
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 16 deletions.
4 changes: 2 additions & 2 deletions assets/js/cell/live_editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -238,9 +238,9 @@ class LiveEditor {

this.editor.getModel().__getHover = (model, position) => {
const line = model.getLineContent(position.lineNumber);
const index = position.column - 1;
const column = position.column;

return this.__asyncIntellisenseRequest("details", { line, index })
return this.__asyncIntellisenseRequest("details", { line, column })
.then((response) => {
const contents = response.contents.map((content) => ({
value: content,
Expand Down
20 changes: 10 additions & 10 deletions lib/livebook/intellisense.ex
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ defmodule Livebook.Intellisense do
%{items: items}
end

def handle_request({:details, line, index}, binding, env) do
get_details(line, index, binding, env)
def handle_request({:details, line, column}, binding, env) do
get_details(line, column, binding, env)
end

def handle_request({:format, code}, _binding, _env) do
Expand Down Expand Up @@ -140,12 +140,12 @@ defmodule Livebook.Intellisense do

@doc """
Returns detailed information about identifier being
at `index` in `line`.
in `column` in `line`.
"""
@spec get_details(String.t(), non_neg_integer(), Code.binding(), Macro.Env.t()) ::
@spec get_details(String.t(), pos_integer(), Code.binding(), Macro.Env.t()) ::
Livebook.Runtime.details() | nil
def get_details(line, index, binding, env) do
{from, to} = subject_range(line, index)
def get_details(line, column, binding, env) do
{from, to} = subject_range(line, column)

if from < to do
subject = binary_part(line, from, to - from)
Expand All @@ -170,9 +170,9 @@ defmodule Livebook.Intellisense do
@closing_identifier '?!'
@punctuation @non_closing_punctuation ++ @closing_punctuation

defp subject_range(line, index) do
{left, right} = String.split_at(line, index)
bytes_until_index = byte_size(left)
defp subject_range(line, column) do
{left, right} = String.split_at(line, column)
bytes_until_column = byte_size(left)

left =
left
Expand All @@ -187,7 +187,7 @@ defmodule Livebook.Intellisense do
|> consume_until(@space ++ @operators ++ @punctuation, @closing_identifier)
|> List.to_string()

{bytes_until_index - byte_size(left), bytes_until_index + byte_size(right)}
{bytes_until_column - byte_size(left), bytes_until_column + byte_size(right)}
end

defp consume_until(acc \\ [], chars, stop, stop_include)
Expand Down
15 changes: 15 additions & 0 deletions lib/livebook/js_interop.ex
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,21 @@ defmodule Livebook.JSInterop do
apply_to_code_units(ops, Enum.slice(code_units, n..-1))
end

@doc """
Returns a column number in the Elixir string corresponding to
the given column interpreted in terms of UTF-16 code units as
JavaScript does.
"""
@spec convert_column_to_elixir(pos_integer(), String.t()) :: pos_integer()
def convert_column_to_elixir(column, line) do
line
|> string_to_utf16_code_units()
|> Enum.take(column - 1)
|> utf16_code_units_to_string()
|> String.length()
|> Kernel.+(1)
end

# ---

defp string_to_utf16_code_units(string) do
Expand Down
4 changes: 2 additions & 2 deletions lib/livebook/runtime.ex
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ defprotocol Livebook.Runtime do
@type completion_item_kind :: :function | :module | :type | :variable | :field

@typedoc """
Looks up more details about an identifier found at `index` in `line`.
Looks up more details about an identifier found in `column` in `line`.
"""
@type details_request :: {:details, line :: String.t(), index :: non_neg_integer()}
@type details_request :: {:details, line :: String.t(), column :: pos_integer()}

@type details_response :: %{
range: %{
Expand Down
5 changes: 3 additions & 2 deletions lib/livebook_web/live/session_live.ex
Original file line number Diff line number Diff line change
Expand Up @@ -632,8 +632,9 @@ defmodule LivebookWeb.SessionLive do
%{"type" => "completion", "hint" => hint} ->
{:completion, hint}

%{"type" => "details", "line" => line, "index" => index} ->
{:details, line, index}
%{"type" => "details", "line" => line, "column" => column} ->
column = Livebook.JSInterop.convert_column_to_elixir(column, line)
{:details, line, column}

%{"type" => "format", "code" => code} ->
{:format, code}
Expand Down
23 changes: 23 additions & 0 deletions test/livebook/js_interop_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,27 @@ defmodule Livebook.JSInteropTest do
assert JSInterop.apply_delta_to_string(delta, string) == " cats"
end
end

describe "convert_column_to_elixir/2" do
test "keeps the column as is for ASCII characters" do
column = 4
line = "String.replace"
assert JSInterop.convert_column_to_elixir(column, line) == 4
end

test "shifts the column given characters spanning multiple UTF-16 code units" do
# 🚀 consists of 2 UTF-16 code units, so JavaScript assumes "🚀".length is 2
column = 7
line = "🚀🚀 String.replace"
assert JSInterop.convert_column_to_elixir(column, line) == 5
end

test "returns proper column if a middle UTF-16 code unit is given" do
# 🚀 consists of 2 UTF-16 code units, so JavaScript assumes "🚀".length is 2
# 3th and 4th code unit correspond to the second 🚀
column = 3
line = "🚀🚀 String.replace"
assert JSInterop.convert_column_to_elixir(column, line) == 2
end
end
end

0 comments on commit af50646

Please sign in to comment.