@@ -259,6 +259,13 @@ async def surround_with_command(
259259 if document is None :
260260 return
261261
262+ lines = document .get_lines ()
263+ need_return = False
264+ if insert_range .end .line >= len (lines ):
265+ insert_range .end .line == len (lines ) - 1
266+ insert_range .end .character = len (lines [- 1 ])
267+ need_return = True
268+
262269 spaces = "" .join (itertools .takewhile (str .isspace , document .get_lines ()[insert_range .start .line ]))
263270
264271 edits : List [Union [TextEdit , AnnotatedTextEdit ]] = [
@@ -281,7 +288,9 @@ async def surround_with_command(
281288 AnnotatedTextEdit (
282289 "add_clause" ,
283290 Range (start = Position (insert_range .end .line , 0 ), end = Position (insert_range .end .line , 0 )),
284- f"{ spaces } EXCEPT message\n { spaces } Fail implement this\n { spaces } END\n " ,
291+ ("\n " if need_return else "" )
292+ + f"{ spaces } EXCEPT message\n { spaces } Fail implement this\n { spaces } END"
293+ + ("\n " if not need_return else "" ),
285294 )
286295 )
287296 p = Position (insert_range .end .line + 1 , len (spaces ) + 6 + 4 )
@@ -293,7 +302,9 @@ async def surround_with_command(
293302 AnnotatedTextEdit (
294303 "add_clause" ,
295304 Range (start = Position (insert_range .end .line , 0 ), end = Position (insert_range .end .line , 0 )),
296- f"{ spaces } FINALLY\n { spaces } Fail implement this\n { spaces } END\n " ,
305+ ("\n " if need_return else "" )
306+ + f"{ spaces } FINALLY\n { spaces } Fail implement this\n { spaces } END"
307+ + ("\n " if not need_return else "" ),
297308 )
298309 )
299310 p = Position (insert_range .end .line + 2 , len (spaces ) + 4 )
@@ -304,11 +315,11 @@ async def surround_with_command(
304315 AnnotatedTextEdit (
305316 "add_clause" ,
306317 Range (start = Position (insert_range .end .line , 0 ), end = Position (insert_range .end .line , 0 )),
307- f"{ spaces } EXCEPT message\n "
318+ ( " \n " if need_return else "" ) + f"{ spaces } EXCEPT message\n "
308319 f"{ spaces } Fail implement this\n "
309320 f"{ spaces } FINALLY\n "
310321 f"{ spaces } Fail implement this\n "
311- f"{ spaces } END\n " ,
322+ f"{ spaces } END" + ( " \n " if not need_return else "" ) ,
312323 )
313324 )
314325 p = Position (insert_range .end .line + 1 , len (spaces ) + 6 + 4 )
0 commit comments