Skip to content

Commit 5b0f9a9

Browse files
authored
Return correct location for defs with when (#464)
previously the location pointed to when keyword (which may be on the next line) not to def's head like in other cases
1 parent 7cd7615 commit 5b0f9a9

File tree

2 files changed

+41
-25
lines changed

2 files changed

+41
-25
lines changed

apps/language_server/lib/language_server/providers/document_symbols.ex

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,23 @@ defmodule ElixirLS.LanguageServer.Providers.DocumentSymbols do
186186
%Info{type: :constant, name: "@#{name}", location: location, children: []}
187187
end
188188

189-
# Function, macro, guard, delegate
189+
# Function, macro, guard with when
190+
defp extract_symbol(
191+
_current_module,
192+
{defname, _, [{:when, _, [{_, location, _} = fn_head, _]} | _]}
193+
)
194+
when defname in @defs do
195+
name = Macro.to_string(fn_head)
196+
197+
%Info{
198+
type: :function,
199+
name: "#{defname} #{name}",
200+
location: location,
201+
children: []
202+
}
203+
end
204+
205+
# Function, macro, delegate
190206
defp extract_symbol(_current_module, {defname, _, [{_, location, _} = fn_head | _]})
191207
when defname in @defs do
192208
name = Macro.to_string(fn_head)

apps/language_server/test/providers/document_symbols_test.exs

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -90,21 +90,21 @@ defmodule ElixirLS.LanguageServer.Providers.DocumentSymbolsTest do
9090
%Protocol.DocumentSymbol{
9191
children: [],
9292
kind: 12,
93-
name: "defguard my_guard(a) when is_integer(a)",
94-
range: %{end: %{character: 29, line: 7}, start: %{character: 29, line: 7}},
93+
name: "defguard my_guard(a)",
94+
range: %{end: %{character: 17, line: 7}, start: %{character: 17, line: 7}},
9595
selectionRange: %{
96-
end: %{character: 29, line: 7},
97-
start: %{character: 29, line: 7}
96+
end: %{character: 17, line: 7},
97+
start: %{character: 17, line: 7}
9898
}
9999
},
100100
%Protocol.DocumentSymbol{
101101
children: [],
102102
kind: 12,
103-
name: "defguardp my_private_guard(a) when is_integer(a)",
104-
range: %{end: %{character: 38, line: 8}, start: %{character: 38, line: 8}},
103+
name: "defguardp my_private_guard(a)",
104+
range: %{end: %{character: 18, line: 8}, start: %{character: 18, line: 8}},
105105
selectionRange: %{
106-
end: %{character: 38, line: 8},
107-
start: %{character: 38, line: 8}
106+
end: %{character: 18, line: 8},
107+
start: %{character: 18, line: 8}
108108
}
109109
},
110110
%Protocol.DocumentSymbol{
@@ -120,11 +120,11 @@ defmodule ElixirLS.LanguageServer.Providers.DocumentSymbolsTest do
120120
%Protocol.DocumentSymbol{
121121
children: [],
122122
kind: 12,
123-
name: "defguard my_guard when 1 == 1",
124-
range: %{end: %{character: 26, line: 10}, start: %{character: 26, line: 10}},
123+
name: "defguard my_guard",
124+
range: %{end: %{character: 17, line: 10}, start: %{character: 17, line: 10}},
125125
selectionRange: %{
126-
end: %{character: 26, line: 10},
127-
start: %{character: 26, line: 10}
126+
end: %{character: 17, line: 10},
127+
start: %{character: 17, line: 10}
128128
}
129129
},
130130
%Protocol.DocumentSymbol{
@@ -140,11 +140,11 @@ defmodule ElixirLS.LanguageServer.Providers.DocumentSymbolsTest do
140140
%Protocol.DocumentSymbol{
141141
children: [],
142142
kind: 12,
143-
name: "def my_fn_with_guard(arg) when is_integer(arg)",
144-
range: %{end: %{character: 34, line: 12}, start: %{character: 34, line: 12}},
143+
name: "def my_fn_with_guard(arg)",
144+
range: %{end: %{character: 12, line: 12}, start: %{character: 12, line: 12}},
145145
selectionRange: %{
146-
end: %{character: 34, line: 12},
147-
start: %{character: 34, line: 12}
146+
end: %{character: 12, line: 12},
147+
start: %{character: 12, line: 12}
148148
}
149149
},
150150
%Protocol.DocumentSymbol{
@@ -245,18 +245,18 @@ defmodule ElixirLS.LanguageServer.Providers.DocumentSymbolsTest do
245245
containerName: "MyModule"
246246
},
247247
%Protocol.SymbolInformation{
248-
name: "defguard my_guard(a) when is_integer(a)",
248+
name: "defguard my_guard(a)",
249249
kind: 12,
250250
location: %{
251-
range: %{end: %{character: 29, line: 7}, start: %{character: 29, line: 7}}
251+
range: %{end: %{character: 17, line: 7}, start: %{character: 17, line: 7}}
252252
},
253253
containerName: "MyModule"
254254
},
255255
%Protocol.SymbolInformation{
256-
name: "defguardp my_private_guard(a) when is_integer(a)",
256+
name: "defguardp my_private_guard(a)",
257257
kind: 12,
258258
location: %{
259-
range: %{end: %{character: 38, line: 8}, start: %{character: 38, line: 8}}
259+
range: %{end: %{character: 18, line: 8}, start: %{character: 18, line: 8}}
260260
},
261261
containerName: "MyModule"
262262
},
@@ -269,10 +269,10 @@ defmodule ElixirLS.LanguageServer.Providers.DocumentSymbolsTest do
269269
containerName: "MyModule"
270270
},
271271
%Protocol.SymbolInformation{
272-
name: "defguard my_guard when 1 == 1",
272+
name: "defguard my_guard",
273273
kind: 12,
274274
location: %{
275-
range: %{end: %{character: 26, line: 10}, start: %{character: 26, line: 10}}
275+
range: %{end: %{character: 17, line: 10}, start: %{character: 17, line: 10}}
276276
},
277277
containerName: "MyModule"
278278
},
@@ -285,10 +285,10 @@ defmodule ElixirLS.LanguageServer.Providers.DocumentSymbolsTest do
285285
containerName: "MyModule"
286286
},
287287
%Protocol.SymbolInformation{
288-
name: "def my_fn_with_guard(arg) when is_integer(arg)",
288+
name: "def my_fn_with_guard(arg)",
289289
kind: 12,
290290
location: %{
291-
range: %{end: %{character: 34, line: 12}, start: %{character: 34, line: 12}}
291+
range: %{end: %{character: 12, line: 12}, start: %{character: 12, line: 12}}
292292
},
293293
containerName: "MyModule"
294294
},

0 commit comments

Comments
 (0)