Skip to content

Commit

Permalink
Remove redundant whitespace from symbol name (#1052)
Browse files Browse the repository at this point in the history
* Remove redundant whitespace from symbol name

* Apply suggestions from code review

make regexes unicode aware

---------

Co-authored-by: Łukasz Samson <[email protected]>
  • Loading branch information
oo6 and lukaszsamson committed Jan 12, 2024
1 parent e86c4de commit 7b3344c
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,10 @@ defmodule ElixirLS.LanguageServer.Providers.DocumentSymbols do

type_name =
type_name
|> String.replace("\n", "")
|> String.replace(~r/,*\n\s*/u, fn
"," <> _ -> ", "
_ -> ""
end)

type = if type_kind in [:type, :typep, :opaque], do: :class, else: :event

Expand Down Expand Up @@ -220,7 +223,12 @@ defmodule ElixirLS.LanguageServer.Providers.DocumentSymbols do
{defname, location, [{:when, _, [{_, head_location, _} = fn_head, _]} | _]}
)
when defname in @defs do
name = Macro.to_string(fn_head) |> String.replace("\n", "")
name =
Macro.to_string(fn_head)
|> String.replace(~r/,*\n\s*/u, fn
"," <> _ -> ", "
_ -> ""
end)

%Info{
type: if(defname in @macro_defs, do: :constant, else: :function),
Expand All @@ -235,7 +243,12 @@ defmodule ElixirLS.LanguageServer.Providers.DocumentSymbols do
# Function, macro, delegate
defp extract_symbol(_current_module, {defname, location, [{_, head_location, _} = fn_head | _]})
when defname in @defs do
name = Macro.to_string(fn_head) |> String.replace("\n", "")
name =
Macro.to_string(fn_head)
|> String.replace(~r/,*\n\s*/u, fn
"," <> _ -> ", "
_ -> ""
end)

%Info{
type: if(defname in @macro_defs, do: :constant, else: :function),
Expand Down
67 changes: 63 additions & 4 deletions apps/language_server/test/providers/document_symbols_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,17 @@ defmodule ElixirLS.LanguageServer.Providers.DocumentSymbolsTest do
def fun_multiple_when(_other) do
:something_else
end
def fun_multiline_args(
foo,
bar
)
when is_atom(foo),
do: foo
def fun_multiline_args(
foo,
bar
),
do: bar
end
]

Expand Down Expand Up @@ -196,12 +207,22 @@ defmodule ElixirLS.LanguageServer.Providers.DocumentSymbolsTest do
"end" => %{"character" => 37, "line" => 31},
"start" => %{"character" => 12, "line" => 31}
}
},
%Protocol.DocumentSymbol{
children: [],
kind: 12,
name: "def fun_multiline_args(foo, bar)"
},
%Protocol.DocumentSymbol{
children: [],
kind: 12,
name: "def fun_multiline_args(foo, bar)"
}
],
kind: 2,
name: "MyModule",
range: %{
"end" => %{"character" => 9, "line" => 34},
"end" => %{"character" => 9, "line" => 45},
"start" => %{"character" => 6, "line" => 1}
},
selectionRange: %{
Expand Down Expand Up @@ -238,6 +259,17 @@ defmodule ElixirLS.LanguageServer.Providers.DocumentSymbolsTest do
after
:ok
end
def fun_multiline_args(
foo,
bar
)
when is_atom(foo),
do: foo
def fun_multiline_args(
foo,
bar
),
do: bar
end
]

Expand All @@ -250,7 +282,7 @@ defmodule ElixirLS.LanguageServer.Providers.DocumentSymbolsTest do
kind: 2,
location: %{
range: %{
"end" => %{"character" => 9, "line" => 24},
"end" => %{"character" => 9, "line" => 35},
"start" => %{"character" => 6, "line" => 1}
}
}
Expand Down Expand Up @@ -344,6 +376,16 @@ defmodule ElixirLS.LanguageServer.Providers.DocumentSymbolsTest do
}
},
containerName: "MyModule"
},
%Protocol.SymbolInformation{
kind: 12,
name: "def fun_multiline_args(foo, bar)",
containerName: "MyModule"
},
%Protocol.SymbolInformation{
kind: 12,
name: "def fun_multiline_args(foo, bar)",
containerName: "MyModule"
}
]} = DocumentSymbols.symbols(uri, parser_context, false)
end
Expand Down Expand Up @@ -1215,6 +1257,10 @@ defmodule ElixirLS.LanguageServer.Providers.DocumentSymbolsTest do
@type my_with_args_when(key, value) :: [{key, value}] when value: integer
@type abc
@type
@type my_with_multiline_args(
key,
value
) :: [{key, value}]
end
"""

Expand Down Expand Up @@ -1271,6 +1317,11 @@ defmodule ElixirLS.LanguageServer.Providers.DocumentSymbolsTest do
children: [],
kind: 22,
name: "@type"
},
%Protocol.DocumentSymbol{
children: [],
kind: 5,
name: "@type my_with_multiline_args(key, value)"
}
],
kind: 2,
Expand All @@ -1292,6 +1343,10 @@ defmodule ElixirLS.LanguageServer.Providers.DocumentSymbolsTest do
@type my_with_args_when(key, value) :: [{key, value}] when value: integer
@type abc
@type
@type my_with_multiline_args(
key,
value
) :: [{key, value}]
end
"""

Expand Down Expand Up @@ -1348,6 +1403,11 @@ defmodule ElixirLS.LanguageServer.Providers.DocumentSymbolsTest do
kind: 22,
name: "@type",
containerName: "MyModule"
},
%Protocol.SymbolInformation{
kind: 5,
name: "@type my_with_multiline_args(key, value)",
containerName: "MyModule"
}
]} = DocumentSymbols.symbols(uri, parser_context, false)
end
Expand Down Expand Up @@ -2556,8 +2616,7 @@ defmodule ElixirLS.LanguageServer.Providers.DocumentSymbolsTest do

parser_context = ParserContextBuilder.from_string(text)

assert {:ok, []} =
DocumentSymbols.symbols(uri, parser_context, true)
assert {:ok, []} = DocumentSymbols.symbols(uri, parser_context, true)
end

test "returns def and defp as a prefix" do
Expand Down

0 comments on commit 7b3344c

Please sign in to comment.