@@ -4,20 +4,29 @@ local JdtlsClient = require('java-core.ls.clients.jdtls-client')
44local List = require (' java-core.utils.list' )
55local ui = require (' java.utils.ui' )
66
7+ local selections_needed_refactoring_commands = {
8+ ' convertVariableToField' ,
9+ ' extractConstant' ,
10+ ' extractField' ,
11+ ' extractMethod' ,
12+ ' extractVariable' ,
13+ ' extractVariableAllOccurrence' ,
14+ }
15+
716local available_commands = {
8- -- 'assignField',
9- -- 'assignVariable',
17+ ' assignField' ,
18+ ' assignVariable' ,
1019 -- 'changeSignature',
11- -- 'convertAnonymousClassToNestedCommand',
12- -- 'convertVariableToField',
20+ ' convertAnonymousClassToNestedCommand' ,
21+ ' convertVariableToField' ,
1322 ' extractConstant' ,
1423 ' extractField' ,
1524 -- 'extractInterface',
1625 ' extractMethod' ,
1726 ' extractVariable' ,
1827 ' extractVariableAllOccurrence' ,
19- -- 'introduceParameter',
20- -- 'invertVariable',
28+ ' introduceParameter' ,
29+ ' invertVariable' ,
2130}
2231
2332--- @class java-refactor.RefactorCommands
3140
3241--- Run refactor command
3342--- @param refactor_type jdtls.CodeActionCommand
34- --- @param context lsp.CodeActionContext
35- function RefactorCommands :refactor (refactor_type , context )
43+ --- @param params lsp.CodeActionParams
44+ function RefactorCommands :refactor (refactor_type , params )
3645 if not vim .tbl_contains (available_commands , refactor_type ) then
3746 notify .error (
3847 string.format (' Refactoring command "%s" is not supported' , refactor_type )
3948 )
4049 return
4150 end
4251
43- if not context then
44- context = vim .lsp .util .make_range_params (0 )
45- context .context = {}
46- context .context .diagnostics = vim .lsp .diagnostic .get_line_diagnostics (0 )
47- end
48-
49- local formatting_options = {
50- tabSize = vim .bo .tabstop ,
51- insertSpaces = vim .bo .expandtab ,
52- }
53-
54- local buffer = vim .api .nvim_get_current_buf ()
55-
56- local selections = List :new ()
57-
58- if
59- context .range .start .character == context .range [' end' ].character
60- and context .range .start .line == context .range [' end' ].line
61- then
62- local selection =
63- self .jdtls_client :java_infer_selection (refactor_type , context , buffer )[1 ]
64-
65- if refactor_type == ' extractField' then
66- if selection .params and vim .islist (selection .params ) then
67- local initialize_in =
68- ui .select (' Initialize the field in' , selection .params )
69-
70- if not initialize_in then
71- return
72- end
52+ params = params or RefactorCommands .make_action_params ()
53+ local formatting_options = RefactorCommands .make_formatting_options ()
54+ local selections
7355
74- selections :push (initialize_in )
75- end
76- end
77-
78- selections :push (selection )
79- vim .print (selections )
56+ if selections_needed_refactoring_commands then
57+ selections = self :get_selections (refactor_type , params )
8058 end
8159
8260 local edit = self .jdtls_client :java_get_refactor_edit (
8361 refactor_type ,
84- context ,
62+ params ,
8563 formatting_options ,
8664 selections ,
87- buffer
65+ vim . api . nvim_get_current_buf ()
8866 )
8967
9068 if not edit then
@@ -100,6 +78,9 @@ function RefactorCommands:refactor(refactor_type, context)
10078 )
10179end
10280
81+ --- @private
82+ --- @param command_name string
83+ --- @param arguments any
10384function RefactorCommands .run_lsp_client_command (command_name , arguments )
10485 local command = vim .lsp .commands [command_name ]
10586
@@ -111,4 +92,66 @@ function RefactorCommands.run_lsp_client_command(command_name, arguments)
11192 command (arguments )
11293end
11394
95+ --- Returns action params
96+ --- @private
97+ --- @return lsp.CodeActionParams
98+ function RefactorCommands .make_action_params ()
99+ --- @type lsp.CodeActionParams
100+ local params = vim .lsp .util .make_range_params (0 )
101+
102+ --- @type lsp.CodeActionContext
103+ local context = { diagnostics = vim .lsp .diagnostic .get_line_diagnostics (0 ) }
104+
105+ params .context = context
106+
107+ return params
108+ end
109+
110+ --- @private
111+ --- @return lsp.FormattingOptions
112+ function RefactorCommands .make_formatting_options ()
113+ return {
114+ tabSize = vim .bo .tabstop ,
115+ insertSpaces = vim .bo .expandtab ,
116+ }
117+ end
118+
119+ --- @private
120+ --- @param refactor_type jdtls.CodeActionCommand
121+ --- @param params lsp.CodeActionParams
122+ --- @return jdtls.SelectionInfo[]
123+ function RefactorCommands :get_selections (refactor_type , params )
124+ local selections = List :new ()
125+ local buffer = vim .api .nvim_get_current_buf ()
126+
127+ if
128+ params .range .start .character == params .range [' end' ].character
129+ and params .range .start .line == params .range [' end' ].line
130+ then
131+ local selection_res =
132+ self .jdtls_client :java_infer_selection (refactor_type , params , buffer )
133+
134+ if not selection_res then
135+ return selections
136+ end
137+
138+ local selection = selection_res [1 ]
139+
140+ if selection .params and vim .islist (selection .params ) then
141+ local initialize_in =
142+ ui .select (' Initialize the field in' , selection .params )
143+
144+ if not initialize_in then
145+ return selections
146+ end
147+
148+ selections :push (initialize_in )
149+ end
150+
151+ selections :push (selection )
152+ end
153+
154+ return selections
155+ end
156+
114157return RefactorCommands
0 commit comments