@@ -4,20 +4,29 @@ local JdtlsClient = require('java-core.ls.clients.jdtls-client')
4
4
local List = require (' java-core.utils.list' )
5
5
local ui = require (' java.utils.ui' )
6
6
7
+ local selections_needed_refactoring_commands = {
8
+ ' convertVariableToField' ,
9
+ ' extractConstant' ,
10
+ ' extractField' ,
11
+ ' extractMethod' ,
12
+ ' extractVariable' ,
13
+ ' extractVariableAllOccurrence' ,
14
+ }
15
+
7
16
local available_commands = {
8
- -- 'assignField',
9
- -- 'assignVariable',
17
+ ' assignField' ,
18
+ ' assignVariable' ,
10
19
-- 'changeSignature',
11
- -- 'convertAnonymousClassToNestedCommand',
12
- -- 'convertVariableToField',
20
+ ' convertAnonymousClassToNestedCommand' ,
21
+ ' convertVariableToField' ,
13
22
' extractConstant' ,
14
23
' extractField' ,
15
24
-- 'extractInterface',
16
25
' extractMethod' ,
17
26
' extractVariable' ,
18
27
' extractVariableAllOccurrence' ,
19
- -- 'introduceParameter',
20
- -- 'invertVariable',
28
+ ' introduceParameter' ,
29
+ ' invertVariable' ,
21
30
}
22
31
23
32
--- @class java-refactor.RefactorCommands
31
40
32
41
--- Run refactor command
33
42
--- @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 )
36
45
if not vim .tbl_contains (available_commands , refactor_type ) then
37
46
notify .error (
38
47
string.format (' Refactoring command "%s" is not supported' , refactor_type )
39
48
)
40
49
return
41
50
end
42
51
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
73
55
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 )
80
58
end
81
59
82
60
local edit = self .jdtls_client :java_get_refactor_edit (
83
61
refactor_type ,
84
- context ,
62
+ params ,
85
63
formatting_options ,
86
64
selections ,
87
- buffer
65
+ vim . api . nvim_get_current_buf ()
88
66
)
89
67
90
68
if not edit then
@@ -100,6 +78,9 @@ function RefactorCommands:refactor(refactor_type, context)
100
78
)
101
79
end
102
80
81
+ --- @private
82
+ --- @param command_name string
83
+ --- @param arguments any
103
84
function RefactorCommands .run_lsp_client_command (command_name , arguments )
104
85
local command = vim .lsp .commands [command_name ]
105
86
@@ -111,4 +92,66 @@ function RefactorCommands.run_lsp_client_command(command_name, arguments)
111
92
command (arguments )
112
93
end
113
94
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
+
114
157
return RefactorCommands
0 commit comments