@@ -12,36 +12,19 @@ local BufferedWriter = require('hopcsharp.database.buffer')
1212
1313local M = {}
1414
15- local PROCESSING_ERROR_MESSAGE = ' init_database is running, try again later. '
16- .. ' If init_database failed - restart or manually set vim.g.hopcsharp_processing to false'
17-
1815vim .g .hopcsharp_processing = false
1916
20- local function log (message , prefix )
21- prefix = prefix or ' hopcsharp: '
22- print (prefix .. message )
23- end
24-
25- local function scheduled_iteration (i , iterable , callback )
26- if i > # iterable then
27- return
28- end
29-
30- callback (i , iterable )
31-
32- vim .schedule (function ()
33- scheduled_iteration (i + 1 , iterable , callback )
34- end )
35- end
36-
3717M .__init_database = function ()
3818 -- drop existing schema
3919 database .__drop_db ()
4020 local writer = BufferedWriter :new (database .__get_db (), 1000 )
4121
22+ local files = parse .__get_source_files ()
23+
4224 local counter = 0
43- scheduled_iteration (1 , parse .__get_source_files (), function (i , items )
44- parse .__parse_tree (items [i ], function (tree , file_path_id , file_content , wr )
25+
26+ utils .__scheduled_iteration (files , function (i , item , items )
27+ parse .__parse_tree (item , function (tree , file_path_id , file_content , wr )
4528 local root = tree :root ()
4629 definition .__parse_definitions (root , file_path_id , file_content , wr )
4730 inheritance .__parse_inheritance (root , file_path_id , file_content , wr )
@@ -50,7 +33,7 @@ M.__init_database = function()
5033 counter = counter + 1
5134
5235 if counter % 100 == 0 then
53- log (string.format (' processed %s/%s of files' , counter , # items ), ' ' )
36+ utils . __log (string.format (' processed %s/%s of files' , counter , # items ), ' ' )
5437 end
5538
5639 if counter == # items then
@@ -60,93 +43,82 @@ M.__init_database = function()
6043end
6144
6245M .init_database = function ()
63- if vim .g .hopcsharp_processing then
64- vim .notify (PROCESSING_ERROR_MESSAGE )
65- return
66- end
67-
68- vim .g .hopcsharp_processing = true
69-
70- local command = {
71- ' nvim' ,
72- ' --headless' ,
73- ' -c' ,
74- ' lua require("hopcsharp").__init_database()' ,
75- ' -c' ,
76- ' qa' ,
77- }
78-
79- local line_buffer = {}
80- -- using table here, for quirks of different OS's
81- -- to add different line separators
82- local line_separators = { ' \r\r ' , ' \r ' }
83-
84- local function flush_line_buffer ()
85- if # line_buffer > 0 then
86- log (table.concat (line_buffer , ' ' ))
87- line_buffer = {}
46+ utils .__block_on_processing (function ()
47+ vim .g .hopcsharp_processing = true
48+
49+ local command = {
50+ ' nvim' ,
51+ ' --headless' ,
52+ ' -c' ,
53+ ' lua require("hopcsharp").__init_database()' ,
54+ ' -c' ,
55+ ' qa' ,
56+ }
57+
58+ local line_buffer = {}
59+ -- using table here, for quirks of different OS's
60+ -- to add different line separators
61+ local line_separators = { ' \r\r ' , ' \r ' }
62+
63+ local function flush_line_buffer ()
64+ if # line_buffer > 0 then
65+ utils .__log (table.concat (line_buffer , ' ' ))
66+ line_buffer = {}
67+ end
8868 end
89- end
90-
91- local start = os.time ( )
92- local on_stdout = function ( _ , data )
93- for _ , entry in ipairs ( data ) do
94- if utils . __contains ( line_separators , entry ) then
95- flush_line_buffer ()
96- else
97- table.insert ( line_buffer , entry )
69+
70+ local start = os.time ()
71+ local on_stdout = function ( _ , data )
72+ for _ , entry in ipairs ( data ) do
73+ if utils . __contains ( line_separators , entry ) then
74+ flush_line_buffer ()
75+ else
76+ table.insert ( line_buffer , entry )
77+ end
9878 end
9979 end
100- end
101-
102- local on_exit = function ( _ )
103- flush_line_buffer ()
104- vim . g . hopcsharp_processing = false
105- local elapsed = os.difftime ( os.time (), start )
106- log ( ' finished processing files ' .. elapsed .. ' s elapsed ' )
107- end
108-
109- -- spawn actual parsing in a separate instance of neovim
110- vim .fn .jobstart ( command , {
111- cwd = vim . fn . getcwd () ,
112- on_stdout = on_stdout ,
113- on_exit = on_exit ,
114- pty = true ,
115- stdin = nil ,
116- } )
80+
81+ local on_exit = function ( _ )
82+ flush_line_buffer ( )
83+ vim . g . hopcsharp_processing = false
84+ local elapsed = os.difftime ( os.time (), start )
85+ utils . __log ( ' finished processing files ' .. elapsed .. ' s elapsed ' )
86+ end
87+
88+ -- spawn actual parsing in a separate instance of neovim
89+ vim . fn . jobstart ( command , {
90+ cwd = vim .fn .getcwd (),
91+ on_stdout = on_stdout ,
92+ on_exit = on_exit ,
93+ pty = true ,
94+ stdin = nil ,
95+ })
96+ end )
11797end
11898
11999M .hop_to_definition = function (config )
120- if vim .g .hopcsharp_processing then
121- vim .notify (PROCESSING_ERROR_MESSAGE )
122- return
123- end
124- hop .__hop_to_definition (config )
100+ utils .__block_on_processing (function ()
101+ hop .__hop_to_definition (config )
102+ end )
125103end
126104
127105M .hop_to_implementation = function (config )
128- if vim .g .hopcsharp_processing then
129- vim .notify (PROCESSING_ERROR_MESSAGE )
130- return
131- end
132- hop .__hop_to_implementation (config )
106+ utils .__block_on_processing (function ()
107+ hop .__hop_to_implementation (config )
108+ end )
133109end
134110
135111M .get_type_hierarchy = function ()
136- if vim .g .hopcsharp_processing then
137- vim .notify (PROCESSING_ERROR_MESSAGE )
138- return
139- end
140- hierarchy .__get_type_hierarchy ()
112+ utils .__block_on_processing (function ()
113+ hierarchy .__get_type_hierarchy ()
114+ end )
141115end
142116
143117--- @return sqlite_db
144118M .get_db = function ()
145- if vim .g .hopcsharp_processing then
146- vim .notify (PROCESSING_ERROR_MESSAGE )
147- return
148- end
149- return database .__get_db ()
119+ return utils .__block_on_processing (function ()
120+ return database .__get_db ()
121+ end )
150122end
151123
152124return M
0 commit comments