@@ -364,7 +364,7 @@ test "ignore autofix comment whitespace" {
364364}
365365
366366test "string literal to multiline string literal" {
367- try testUserCodeAction (
367+ try testUserCodeAction (.{ . str_kind_conv = .@"string literal to multiline string" },
368368 \\const foo = <cursor>"line one\nline two\nline three";
369369 ,
370370 \\const foo =
@@ -373,15 +373,15 @@ test "string literal to multiline string literal" {
373373 \\\\line three
374374 \\;
375375 );
376- try testUserCodeAction (
376+ try testUserCodeAction (.{ . str_kind_conv = .@"string literal to multiline string" },
377377 \\const foo = "Hello, <cursor>World!\n";
378378 ,
379379 \\const foo =
380380 \\\\Hello, World!
381381 \\\\
382382 \\;
383383 );
384- try testUserCodeAction (
384+ try testUserCodeAction (.{ . str_kind_conv = .@"string literal to multiline string" },
385385 \\std.debug.print(<cursor>"Hi\nHey\nHello\n", .{});
386386 ,
387387 \\std.debug.print(
@@ -391,18 +391,44 @@ test "string literal to multiline string literal" {
391391 \\\\
392392 \\, .{});
393393 );
394- try testUserCodeAction (
394+ try testUserCodeAction (.{ . str_kind_conv = .@"string literal to multiline string" },
395395 \\const blank = <cursor>""
396396 \\;
397397 ,
398398 \\const blank =
399399 \\\\
400400 \\;
401401 );
402+ try testUserCodeAction (.{ .str_kind_conv = .@"string literal to multiline string" },
403+ \\for (0..42) |idx| {
404+ \\ std.debug.print("{}: {}\n<cursor>", .{ idx, my_foos[idx] });
405+ \\}
406+ ,
407+ \\for (0..42) |idx| {
408+ \\ std.debug.print(
409+ \\\\{}: {}
410+ \\\\
411+ \\, .{ idx, my_foos[idx] });
412+ \\}
413+ );
414+ try testUserCodeAction (.{ .str_kind_conv = .@"string literal to multiline string" },
415+ \\const s1 = <cursor>"\t";
416+ ,
417+ \\const s1 =
418+ \\\\
419+ \\;
420+ );
421+ try testUserCodeAction (.{ .str_kind_conv = .@"string literal to multiline string" },
422+ \\const s1 = <cursor>"pre text\tpost text";
423+ ,
424+ \\const s1 =
425+ \\\\pre text post text
426+ \\;
427+ );
402428}
403429
404430test "multiline string literal to string literal" {
405- try testUserCodeAction (
431+ try testUserCodeAction (.{ . str_kind_conv = .@"multiline string to string literal" },
406432 \\const bleh =
407433 \\ \\hello
408434 \\ \\world<cursor>
@@ -415,7 +441,7 @@ test "multiline string literal to string literal" {
415441 \\ \\oh?
416442 \\;
417443 );
418- try testUserCodeAction (
444+ try testUserCodeAction (.{ . str_kind_conv = .@"multiline string to string literal" },
419445 \\std.debug.print(
420446 \\\\Hi<cursor>
421447 \\\\Hey
@@ -427,7 +453,7 @@ test "multiline string literal to string literal" {
427453 \\"Hi\nHey\nHello\n"
428454 \\, .{});
429455 );
430- try testUserCodeAction (
456+ try testUserCodeAction (.{ . str_kind_conv = .@"multiline string to string literal" },
431457 \\const nums =
432458 \\ \\123
433459 \\ \\456<cursor>
@@ -436,17 +462,19 @@ test "multiline string literal to string literal" {
436462 ,
437463 \\const nums = "123\n456\n789";
438464 );
439- try testUserCodeAction (
440- \\for (0..42) |idx| {
441- \\ std.debug.print("{}: {}\n <cursor>", .{ idx, my_foos[idx] });
442- \\}
465+ try testUserCodeAction (.{ . str_kind_conv = .@"multiline string to string literal" },
466+ \\const s3 =
467+ \\ <cursor>\\"
468+ \\;
443469 ,
444- \\for (0..42) |idx| {
445- \\ std.debug.print(
446- \\\\{}: {}
447- \\\\
448- \\, .{ idx, my_foos[idx] });
449- \\}
470+ \\const s3 = "\"";
471+ );
472+ try testUserCodeAction (.{ .str_kind_conv = .@"multiline string to string literal" },
473+ \\const s3 =
474+ \\ <cursor>\\\
475+ \\;
476+ ,
477+ \\const s3 = "\\";
450478 );
451479}
452480
@@ -503,7 +531,7 @@ fn testAutofixOptions(before: []const u8, after: []const u8, want_zir: bool) !vo
503531 try std .testing .expectEqualStrings (after , handle .tree .source );
504532}
505533
506- fn testUserCodeAction (source : []const u8 , expected : []const u8 ) ! void {
534+ fn testUserCodeAction (action_kind : zls.code_actions.UserActionKind , source : []const u8 , expected : []const u8 ) ! void {
507535 var ctx = try Context .init ();
508536 defer ctx .deinit ();
509537
@@ -514,7 +542,6 @@ fn testUserCodeAction(source: []const u8, expected: []const u8) !void {
514542 const uri = try ctx .addDocument (text );
515543 const handle = ctx .server .document_store .getHandle (uri ).? ;
516544 const pos = offsets .indexToPosition (text , cursor_idx , ctx .server .offset_encoding );
517-
518545 const params = types.CodeActionParams {
519546 .textDocument = .{ .uri = uri },
520547 .range = .{
@@ -524,29 +551,26 @@ fn testUserCodeAction(source: []const u8, expected: []const u8) !void {
524551 .context = .{ .diagnostics = &[_ ]zls.types.Diagnostic {} },
525552 };
526553
527- @setEvalBranchQuota (5000 );
528- const response = try ctx .server .sendRequestSync (ctx .arena .allocator (), "textDocument/codeAction" , params ) orelse {
529- std .debug .print ("Server returned `null` as the result\n " , .{});
530- return error .InvalidResponse ;
554+ var analyser = ctx .server .initAnalyser (handle );
555+ defer analyser .deinit ();
556+ var builder = zls.code_actions.Builder {
557+ .arena = ctx .arena .allocator (),
558+ .analyser = & analyser ,
559+ .handle = handle ,
560+ .offset_encoding = ctx .server .offset_encoding ,
531561 };
562+ var actions = std .ArrayListUnmanaged (types .CodeAction ){};
532563
533- var text_edits : std .ArrayListUnmanaged (types .TextEdit ) = .{};
534- defer text_edits .deinit (allocator );
535-
536- for (response ) | action | {
537- const code_action = action .CodeAction ;
538- if (code_action .kind .? == .@"source.fixAll" ) continue ;
539- const workspace_edit = code_action .edit .? ;
540- const changes = workspace_edit .changes .? .map ;
541- try std .testing .expectEqual (@as (usize , 1 ), changes .count ());
542- try std .testing .expect (changes .contains (uri ));
543-
544- try text_edits .appendSlice (allocator , changes .get (uri ).? );
545- }
564+ try builder .addCodeAction (action_kind , params , & actions );
565+ try std .testing .expect (actions .items .len == 1 );
566+ const code_action = actions .items [0 ];
567+ const workspace_edit = code_action .edit .? ;
568+ const changes = workspace_edit .changes .? .map ;
569+ try std .testing .expectEqual (@as (usize , 1 ), changes .count ());
570+ try std .testing .expect (changes .contains (uri ));
546571
547- const actual = try zls .diff .applyTextEdits (allocator , text , text_edits . items , ctx .server .offset_encoding );
572+ const actual = try zls .diff .applyTextEdits (allocator , text , changes . get ( uri ) .? , ctx .server .offset_encoding );
548573 defer allocator .free (actual );
549574 try ctx .server .document_store .refreshDocument (uri , try allocator .dupeZ (u8 , actual ));
550-
551575 try std .testing .expectEqualStrings (expected , handle .tree .source );
552576}
0 commit comments