Skip to content
This repository was archived by the owner on Nov 25, 2025. It is now read-only.

Commit 70c2cc5

Browse files
authored
add toggleMultilineStringLiteral command
1 parent 845de8b commit 70c2cc5

2 files changed

Lines changed: 48 additions & 0 deletions

File tree

package.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,11 @@
349349
"title": "Install Zig",
350350
"category": "Zig Setup"
351351
},
352+
{
353+
"command": "zig.toggleMultilineStringLiteral",
354+
"title": "Toggle Multiline String Literal",
355+
"category": "Zig"
356+
},
352357
{
353358
"command": "zig.zls.enable",
354359
"title": "Enable Language Server",
@@ -365,6 +370,13 @@
365370
"category": "ZLS language server"
366371
}
367372
],
373+
"keybindings": [
374+
{
375+
"command": "zig.toggleMultilineStringLiteral",
376+
"key": "alt+m alt+s",
377+
"when": "editorTextFocus && editorLangId == 'zig'"
378+
}
379+
],
368380
"jsonValidation": [
369381
{
370382
"fileMatch": "zls.json",

src/extension.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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) {
3233
export 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

Comments
 (0)