66import os
77import json
88import io
9- from completor import Completor , vim , import_completer , get
9+ from completor import Completor , vim , get
1010from completor .compat import to_unicode
1111
1212from . import ext
1313from .models import Initialize , DidOpen , Completion , DidChange , DidSave , \
1414 Definition , Format , Rename , Hover , Initialized , Implementation , \
1515 References , DidChangeConfiguration , Symbol , Signature , DocumentSymbol , \
16- CodeAction
16+ CodeAction , CodeResolve
1717from .action import gen_jump_list , get_completion_word , gen_hover_doc , \
1818 filter_items , parse_symbols , rename
1919from .utils import gen_uri
@@ -128,7 +128,46 @@ def rename_request(self, name):
128128
129129 def code_action (self , action ):
130130 uri = gen_uri (self .filename )
131- c = CodeAction (uri , action )
131+ meta = (self .current_options or {}).get (b'meta' , {})
132+ c = CodeAction (uri , action , text_range = meta .get ('text_range' ))
133+ req_id , req = c .to_request ()
134+ self .current_id = req_id
135+ return req
136+
137+ def code_action_callback (self , data ):
138+ if not data :
139+ return ''
140+
141+ try :
142+ data = json .loads (data [0 ])
143+ except Exception as e :
144+ logger .exception (e )
145+ return ''
146+
147+ edit = data .get ('edit' )
148+ if edit :
149+ uri = gen_uri (self .filename )
150+ changes = edit .get ('changes' )
151+ edits = None
152+ if changes :
153+ edits = changes .get (uri )
154+ else :
155+ changes = edit .get ('documentChanges' )
156+ if not changes :
157+ return
158+ for change in changes :
159+ if change ['textDocument' ]['uri' ] == uri :
160+ edits = change ['edits' ]
161+ break
162+
163+ if not edits :
164+ return ''
165+
166+ res = vim .Dictionary (data = [edits ], action = 'format' )
167+ self .trigger_action (res )
168+ return ''
169+
170+ c = CodeResolve (params = data )
132171 req_id , req = c .to_request ()
133172 self .current_id = req_id
134173 return req
@@ -173,6 +212,11 @@ def _gen_action_args(self, action, args):
173212 return ''
174213 return self .code_action ([args [0 ]])
175214
215+ if action == b'code_action_callback' :
216+ if not args :
217+ return ''
218+ return self .code_action_callback ([args [0 ]])
219+
176220 if action == b'hover' :
177221 return self .position_request (Hover )
178222
@@ -185,7 +229,7 @@ def _gen_action_args(self, action, args):
185229 return ''
186230
187231 def gen_request (self , action , args ):
188- self .current_options = None
232+ self .current_options = {}
189233 if args :
190234 last = args [- 1 ]
191235 if isinstance (last , (dict , vim .Dictionary )):
@@ -350,12 +394,38 @@ def on_code_action(self, data):
350394 if not item :
351395 return []
352396
353- item = item [0 ]
354- if not item or 'edit' not in item :
397+ items = []
398+ try :
399+ for v in item :
400+ data = json .dumps (v )
401+ items .append ({'title' : v ['title' ], 'data' : json .dumps (v )})
402+ except Exception as e :
403+ logger .exception (e )
355404 return []
356405
357- rename (item ['edit' ])
358- return vim .Dictionary (data = [], action = 'rename' )
406+ return vim .Dictionary (data = items , action = 'menu' )
407+
408+ # item = item[0]
409+ # if not item or 'edit' not in item:
410+ # return []
411+ #
412+ # rename(item['edit'])
413+ # return vim.Dictionary(data=[], action='rename')
414+
415+ def on_code_action_callback (self , data ):
416+ logger .info ("code_action_callback -> %r" , data )
417+ if not data :
418+ return []
419+
420+ try :
421+ changes = data [0 ]['edit' ]['documentChanges' ]
422+ if len (changes ) != 1 :
423+ return []
424+ except Exception as e :
425+ logger .exception (e )
426+ return []
427+
428+ return vim .Dictionary (data = [changes [0 ]['edits' ]], action = 'format' )
359429
360430 def on_hover (self , data ):
361431 logger .info ("hover -> %r" , data )
0 commit comments