@@ -23,6 +23,7 @@ export async function activate(context: vscode.ExtensionContext) {
2323 { language : "zig" , scheme : "file" } ,
2424 new ZigMainCodeLensProvider ( ) ,
2525 ) ,
26+ vscode . commands . registerCommand ( "zig.toggleMultilineStringLiteral" , toggleMultilineStringLiteral ) ,
2627 ) ;
2728
2829 void activateZls ( context ) ;
@@ -32,3 +33,38 @@ export async function activate(context: vscode.ExtensionContext) {
3233export async function deactivate ( ) {
3334 await deactivateZls ( ) ;
3435}
36+
37+ async function toggleMultilineStringLiteral ( ) {
38+ const editor = vscode . window . activeTextEditor ;
39+ if ( ! editor ) return ;
40+ const { document, selection } = editor ;
41+ if ( document . languageId !== "zig" ) return ;
42+
43+ let newText = "" ;
44+ let range = new vscode . Range ( selection . start , selection . end ) ;
45+
46+ const firstLine = document . lineAt ( selection . start . line ) ;
47+ const nonWhitespaceIndex = firstLine . firstNonWhitespaceCharacterIndex ;
48+
49+ for ( let lineNum = selection . start . line ; lineNum <= selection . end . line ; lineNum ++ ) {
50+ const line = document . lineAt ( lineNum ) ;
51+
52+ const isMLSL = line . text . slice ( line . firstNonWhitespaceCharacterIndex ) . startsWith ( "\\\\" ) ;
53+ const breakpoint = Math . min ( nonWhitespaceIndex , line . firstNonWhitespaceCharacterIndex ) ;
54+
55+ const newLine = isMLSL
56+ ? line . text . slice ( 0 , line . firstNonWhitespaceCharacterIndex ) +
57+ line . text . slice ( line . firstNonWhitespaceCharacterIndex ) . slice ( 2 )
58+ : line . isEmptyOrWhitespace
59+ ? " " . repeat ( nonWhitespaceIndex ) + "\\\\"
60+ : line . text . slice ( 0 , breakpoint ) + "\\\\" + line . text . slice ( breakpoint ) ;
61+ newText += newLine ;
62+ if ( lineNum < selection . end . line ) newText += "\n" ;
63+
64+ range = range . union ( line . range ) ;
65+ }
66+
67+ await editor . edit ( ( builder ) => {
68+ builder . replace ( range , newText ) ;
69+ } ) ;
70+ }
0 commit comments