@@ -20,12 +20,8 @@ defmodule ElixirLS.LanguageServer.Tracer do
20
20
GenServer . start_link ( __MODULE__ , args , name: __MODULE__ )
21
21
end
22
22
23
- def notify_settings_stored ( ) do
24
- GenServer . cast ( __MODULE__ , :notify_settings_stored )
25
- end
26
-
27
- def save ( ) do
28
- GenServer . cast ( __MODULE__ , :save )
23
+ def notify_settings_stored ( project_dir ) do
24
+ GenServer . cast ( __MODULE__ , { :notify_settings_stored , project_dir } )
29
25
end
30
26
31
27
defp get_project_dir ( ) do
@@ -57,19 +53,7 @@ defmodule ElixirLS.LanguageServer.Tracer do
57
53
] )
58
54
end
59
55
60
- project_dir = :persistent_term . get ( :language_server_project_dir , nil )
61
- state = % { project_dir: project_dir }
62
-
63
- if project_dir != nil do
64
- { us , _ } =
65
- :timer . tc ( fn ->
66
- for table <- @ tables do
67
- init_table ( table , project_dir )
68
- end
69
- end )
70
-
71
- Logger . info ( "Loaded DETS databases in #{ div ( us , 1000 ) } ms" )
72
- end
56
+ state = % { project_dir: nil }
73
57
74
58
{ :ok , state }
75
59
end
@@ -80,26 +64,12 @@ defmodule ElixirLS.LanguageServer.Tracer do
80
64
end
81
65
82
66
@ impl true
83
- def handle_cast ( :notify_settings_stored , state ) do
84
- project_dir = :persistent_term . get ( :language_server_project_dir )
85
- maybe_close_tables ( state )
86
-
67
+ def handle_cast ( { :notify_settings_stored , project_dir } , state ) do
87
68
for table <- @ tables do
88
69
table_name = table_name ( table )
89
70
:ets . delete_all_objects ( table_name )
90
71
end
91
72
92
- if project_dir != nil do
93
- { us , _ } =
94
- :timer . tc ( fn ->
95
- for table <- @ tables do
96
- init_table ( table , project_dir )
97
- end
98
- end )
99
-
100
- Logger . info ( "Loaded DETS databases in #{ div ( us , 1000 ) } ms" )
101
- end
102
-
103
73
{ :noreply , % { state | project_dir: project_dir } }
104
74
end
105
75
@@ -113,22 +83,8 @@ defmodule ElixirLS.LanguageServer.Tracer do
113
83
{ :noreply , state }
114
84
end
115
85
116
- def handle_cast ( :save , % { project_dir: project_dir } = state ) do
117
- for table <- @ tables do
118
- table_name = table_name ( table )
119
-
120
- sync ( table_name )
121
- end
122
-
123
- write_manifest ( project_dir )
124
-
125
- { :noreply , state }
126
- end
127
-
128
86
@ impl true
129
87
def terminate ( reason , state ) do
130
- maybe_close_tables ( state )
131
-
132
88
case reason do
133
89
:normal ->
134
90
:ok
@@ -168,112 +124,6 @@ defmodule ElixirLS.LanguageServer.Tracer do
168
124
end
169
125
end
170
126
171
- defp maybe_close_tables ( % { project_dir: nil } ) , do: :ok
172
-
173
- defp maybe_close_tables ( _state ) do
174
- for table <- @ tables do
175
- close_table ( table )
176
- end
177
-
178
- :ok
179
- end
180
-
181
- defp dets_path ( project_dir , table ) do
182
- Path . join ( [ project_dir , ".elixir_ls" , "#{ table } .dets" ] )
183
- end
184
-
185
- def init_table ( table , project_dir ) do
186
- table_name = table_name ( table )
187
- path = dets_path ( project_dir , table )
188
-
189
- opts = [ file: path |> String . to_charlist ( ) , auto_save: 60_000 , repair: true ]
190
-
191
- :ok = path |> Path . dirname ( ) |> File . mkdir_p ( )
192
-
193
- case :dets . open_file ( table_name , opts ) do
194
- { :ok , _ } ->
195
- :ok
196
-
197
- { :error , { :needs_repair , _ } = reason } ->
198
- Logger . warning ( "Unable to open DETS #{ path } : #{ inspect ( reason ) } " )
199
- File . rm_rf! ( path )
200
-
201
- { :ok , _ } = :dets . open_file ( table_name , opts )
202
-
203
- { :error , { :repair_failed , _ } = reason } ->
204
- Logger . warning ( "Unable to open DETS #{ path } : #{ inspect ( reason ) } " )
205
- File . rm_rf! ( path )
206
-
207
- { :ok , _ } = :dets . open_file ( table_name , opts )
208
-
209
- { :error , { :cannot_repair , _ } = reason } ->
210
- Logger . warning ( "Unable to open DETS #{ path } : #{ inspect ( reason ) } " )
211
- File . rm_rf! ( path )
212
-
213
- { :ok , _ } = :dets . open_file ( table_name , opts )
214
-
215
- { :error , { :not_a_dets_file , _ } = reason } ->
216
- Logger . warning ( "Unable to open DETS #{ path } : #{ inspect ( reason ) } " )
217
- File . rm_rf! ( path )
218
-
219
- { :ok , _ } = :dets . open_file ( table_name , opts )
220
-
221
- { :error , { :format_8_no_longer_supported , _ } = reason } ->
222
- Logger . warning ( "Unable to open DETS #{ path } : #{ inspect ( reason ) } " )
223
- File . rm_rf! ( path )
224
-
225
- { :ok , _ } = :dets . open_file ( table_name , opts )
226
- end
227
-
228
- case :dets . to_ets ( table_name , table_name ) do
229
- ^ table_name ->
230
- :ok
231
-
232
- { :error , reason } ->
233
- Logger . warning ( "Unable to read DETS #{ path } : #{ inspect ( reason ) } " )
234
- File . rm_rf! ( path )
235
-
236
- { :ok , _ } = :dets . open_file ( table_name , opts )
237
- ^ table_name = :dets . to_ets ( table_name , table_name )
238
- end
239
- catch
240
- kind , payload ->
241
- { payload , stacktrace } = Exception . blame ( kind , payload , __STACKTRACE__ )
242
- error_msg = Exception . format ( kind , payload , stacktrace )
243
-
244
- Logger . error (
245
- "Unable to init tracer table #{ table } in directory #{ project_dir } : #{ error_msg } "
246
- )
247
-
248
- JsonRpc . show_message (
249
- :error ,
250
- "Unable to init tracer tables in #{ project_dir } "
251
- )
252
-
253
- JsonRpc . telemetry (
254
- "lsp_server_error" ,
255
- % {
256
- "elixir_ls.lsp_process" => inspect ( __MODULE__ ) ,
257
- "elixir_ls.lsp_server_error" => error_msg
258
- } ,
259
- % { }
260
- )
261
-
262
- unless :persistent_term . get ( :language_server_test_mode , false ) do
263
- Process . sleep ( 2000 )
264
- System . halt ( 1 )
265
- else
266
- IO . warn ( "Unable to init tracer table #{ table } in directory #{ project_dir } : #{ error_msg } " )
267
- end
268
- end
269
-
270
- def close_table ( table ) do
271
- table_name = table_name ( table )
272
- sync ( table_name )
273
-
274
- :ok = :dets . close ( table_name )
275
- end
276
-
277
127
defp modules_by_file_matchspec ( file , return ) do
278
128
[
279
129
{ { :"$1" , :"$2" } ,
@@ -434,11 +284,6 @@ defmodule ElixirLS.LanguageServer.Tracer do
434
284
end
435
285
end
436
286
437
- defp sync ( table_name ) do
438
- :ok = :dets . from_ets ( table_name , table_name )
439
- :ok = :dets . sync ( table_name )
440
- end
441
-
442
287
defp in_project_sources? ( path ) do
443
288
project_dir = get_project_dir ( )
444
289
@@ -486,37 +331,4 @@ defmodule ElixirLS.LanguageServer.Tracer do
486
331
:ets . safe_fixtable ( table , false )
487
332
end
488
333
end
489
-
490
- defp manifest_path ( project_dir ) do
491
- Path . join ( [ project_dir , ".elixir_ls" , "tracer_db.manifest" ] )
492
- end
493
-
494
- def write_manifest ( project_dir ) do
495
- path = manifest_path ( project_dir )
496
- File . rm_rf! ( path )
497
-
498
- File . write! ( path , "#{ @ version } " , [ :write ] )
499
- end
500
-
501
- def read_manifest ( project_dir ) do
502
- with { :ok , text } <- File . read ( manifest_path ( project_dir ) ) ,
503
- { version , "" } <- Integer . parse ( text ) do
504
- version
505
- else
506
- other ->
507
- IO . warn ( "Manifest: #{ inspect ( other ) } " )
508
- nil
509
- end
510
- end
511
-
512
- def manifest_version_current? ( project_dir ) do
513
- read_manifest ( project_dir ) == @ version
514
- end
515
-
516
- def clean_dets ( project_dir ) do
517
- for path <-
518
- Path . join ( [ SourceFile.Path . escape_for_wildcard ( project_dir ) , ".elixir_ls/*.dets" ] )
519
- |> Path . wildcard ( ) ,
520
- do: File . rm_rf! ( path )
521
- end
522
334
end
0 commit comments