Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion lib/elixir/lib/code/fragment.ex
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ defmodule Code.Fragment do
def cursor_context(fragment, opts \\ [])

def cursor_context(binary, opts) when is_binary(binary) and is_list(opts) do
# CRLF not relevant here - we discard everything before last `\n`
binary =
case :binary.matches(binary, "\n") do
[] ->
Expand All @@ -151,6 +152,7 @@ defmodule Code.Fragment do
end

def cursor_context(charlist, opts) when is_list(charlist) and is_list(opts) do
# CRLF not relevant here - we discard everything before last `\n`
charlist =
case charlist |> Enum.chunk_by(&(&1 == ?\n)) |> List.last([]) do
[?\n | _] -> []
Expand Down Expand Up @@ -508,14 +510,16 @@ defmodule Code.Fragment do

def surround_context(binary, {line, column}, opts) when is_binary(binary) do
binary
|> String.split("\n")
|> String.split(["\r\n", "\n"])
|> Enum.at(line - 1, '')
|> String.to_charlist()
|> position_surround_context(line, column, opts)
end

def surround_context(charlist, {line, column}, opts) when is_list(charlist) do
charlist
|> :string.replace('\r\n', '\n', :all)
|> :string.join('')
|> :string.split('\n', :all)
|> Enum.at(line - 1, '')
|> position_surround_context(line, column, opts)
Expand Down
18 changes: 18 additions & 0 deletions lib/elixir/test/elixir/code_fragment_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ defmodule CodeFragmentTest do
assert CF.cursor_context('\n') == :expr
assert CF.cursor_context("\n\n") == :expr
assert CF.cursor_context('\n\n') == :expr
assert CF.cursor_context("\r\n") == :expr
assert CF.cursor_context('\r\n') == :expr
assert CF.cursor_context("\r\n\r\n") == :expr
assert CF.cursor_context('\r\n\r\n') == :expr
end

test "local_or_var" do
Expand Down Expand Up @@ -267,6 +271,8 @@ defmodule CodeFragmentTest do
test "newlines" do
assert CF.cursor_context("this+does-not*matter\nHello.") == {:dot, {:alias, 'Hello'}, ''}
assert CF.cursor_context('this+does-not*matter\nHello.') == {:dot, {:alias, 'Hello'}, ''}
assert CF.cursor_context("this+does-not*matter\r\nHello.") == {:dot, {:alias, 'Hello'}, ''}
assert CF.cursor_context('this+does-not*matter\r\nHello.') == {:dot, {:alias, 'Hello'}, ''}
end
end

Expand All @@ -278,6 +284,18 @@ defmodule CodeFragmentTest do
begin: {3, 1},
end: {3, 9}
}

assert CF.surround_context("\r\n\r\nhello_wo\r\n", {3, i}) == %{
context: {:local_or_var, 'hello_wo'},
begin: {3, 1},
end: {3, 9}
}

assert CF.surround_context('\r\n\r\nhello_wo\r\n', {3, i}) == %{
context: {:local_or_var, 'hello_wo'},
begin: {3, 1},
end: {3, 9}
}
end
end

Expand Down