Skip to content

Commit 823d00f

Browse files
committed
Add alternate commands
1 parent c196f55 commit 823d00f

File tree

3 files changed

+44
-14
lines changed

3 files changed

+44
-14
lines changed

package-lock.json

Lines changed: 7 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "clang-format",
33
"displayName": "Clang-Format",
44
"description": "Use Clang-Format in Visual Studio Code",
5-
"version": "1.9.0",
5+
"version": "99.9.0",
66
"publisher": "xaver",
77
"engines": {
88
"vscode": "^1.1.0"
@@ -12,7 +12,8 @@
1212
"theme": "dark"
1313
},
1414
"dependencies": {
15-
"sax": "^1.2.1"
15+
"sax": "^1.2.1",
16+
"shlex": "^2.0.1"
1617
},
1718
"categories": [
1819
"Formatters"
@@ -48,6 +49,11 @@
4849
"command": "clang-format.formatSelection",
4950
"title": "Format selection or around cursor",
5051
"category": "Clang-Format"
52+
},
53+
{
54+
"command": "clang-format.formatSelectionAlternate",
55+
"title": "Format selection or around cursor (alternate)",
56+
"category": "Clang-Format"
5157
}
5258
],
5359
"configuration": {
@@ -79,10 +85,15 @@
7985
"default": "",
8086
"description": "clang-format fallback style for C++, left empty to use clang-format.style"
8187
},
88+
"clang-format.language.cpp.alternate.style": {
89+
"type": "string",
90+
"default": "",
91+
"description": "clang-format fallback style for C++, left empty to use clang-format.style"
92+
},
8293
"clang-format.language.cpp.fallbackStyle": {
8394
"type": "string",
8495
"default": "",
85-
"description": "clang-format fallback style for C++, left empty to use clang-format.fallbackStyle"
96+
"description": "clang-format alternate style for C++"
8697
},
8798
"clang-format.language.c.enable": {
8899
"type": "boolean",
@@ -94,6 +105,11 @@
94105
"default": "",
95106
"description": "clang-format fallback style for C, left empty to use clang-format.style"
96107
},
108+
"clang-format.language.c.alternate.style": {
109+
"type": "string",
110+
"default": "",
111+
"description": "clang-format alternate style for C"
112+
},
97113
"clang-format.language.c.fallbackStyle": {
98114
"type": "string",
99115
"default": "",

src/extension.ts

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {MODES,
66
LANGUAGES} from './clangMode';
77
import {getBinPath} from './clangPath';
88
import sax = require('sax');
9+
import * as shlex from 'shlex';
910

1011
export let outputChannel = vscode.window.createOutputChannel('Clang-Format');
1112

@@ -39,7 +40,7 @@ export class ClangDocumentFormattingEditProvider implements vscode.DocumentForma
3940
});
4041
}
4142

42-
public formatSelection(): void {
43+
public formatSelection(alternate = false): void {
4344
let editor = vscode.window.activeTextEditor;
4445
let document = editor.document;
4546

@@ -48,7 +49,7 @@ export class ClangDocumentFormattingEditProvider implements vscode.DocumentForma
4849
return;
4950
}
5051

51-
this.doFormatDocument(editor.document, editor.selection, undefined, undefined).then(
52+
this.doFormatDocument(editor.document, editor.selection, undefined, undefined, alternate).then(
5253
(result) => {
5354
editor.edit((editBuilder) => {
5455
for (let edit of result.edits)
@@ -192,8 +193,9 @@ export class ClangDocumentFormattingEditProvider implements vscode.DocumentForma
192193
return ALIAS[document.languageId] || document.languageId;
193194
}
194195

195-
private getStyle(document: vscode.TextDocument) {
196-
let ret = vscode.workspace.getConfiguration('clang-format').get<string>(`language.${this.getLanguage(document)}.style`);
196+
private getStyle(document: vscode.TextDocument, alternate = false) {
197+
const styleOpt = alternate ? 'alternate.style' : 'style';
198+
let ret = vscode.workspace.getConfiguration('clang-format').get<string>(`language.${this.getLanguage(document)}.${styleOpt}`);
197199
if (ret.trim()) {
198200
return ret.trim();
199201
}
@@ -228,7 +230,7 @@ export class ClangDocumentFormattingEditProvider implements vscode.DocumentForma
228230
return assumedFilename;
229231
}
230232

231-
private doFormatDocument(document: vscode.TextDocument, range: vscode.Range, options: vscode.FormattingOptions, token: vscode.CancellationToken): Thenable<FormatResult> {
233+
private doFormatDocument(document: vscode.TextDocument, range: vscode.Range, options: vscode.FormattingOptions, token: vscode.CancellationToken, alternate = false): Thenable<FormatResult> {
232234
return new Promise((resolve, reject) => {
233235
let filename = document.fileName;
234236

@@ -237,7 +239,7 @@ export class ClangDocumentFormattingEditProvider implements vscode.DocumentForma
237239

238240
let formatArgs = [
239241
'-output-replacements-xml',
240-
`-style=${this.getStyle(document)}`,
242+
`-style=${this.getStyle(document, alternate)}`,
241243
`-fallback-style=${this.getFallbackStyle(document)}`,
242244
`-assume-filename=${this.getAssumedFilename(document)}`
243245
];
@@ -263,6 +265,9 @@ export class ClangDocumentFormattingEditProvider implements vscode.DocumentForma
263265

264266
let stdout = '';
265267
let stderr = '';
268+
const argsString = formatArgs.map(shlex.quote).join(' ');
269+
outputChannel.clear();
270+
outputChannel.appendLine(`Calling with arguments: ${argsString}`)
266271
let child = cp.spawn(formatCommandBinPath, formatArgs, { cwd: workingPath });
267272
child.stdin.end(codeContent);
268273
child.stdout.on('data', chunk => stdout += chunk);
@@ -277,13 +282,12 @@ export class ClangDocumentFormattingEditProvider implements vscode.DocumentForma
277282
child.on('close', code => {
278283
try {
279284
if (stderr.length != 0) {
280-
outputChannel.show();
281-
outputChannel.clear();
282285
outputChannel.appendLine(stderr);
283-
return reject('Cannot format due to syntax errors.');
284286
}
285287

286288
if (code != 0) {
289+
outputChannel.show();
290+
outputChannel.appendLine(`Process exited with code ${code}`);
287291
return reject();
288292
}
289293

@@ -321,4 +325,9 @@ export function activate(ctx: vscode.ExtensionContext): void {
321325
() => {
322326
formatter.formatSelection();
323327
}));
328+
ctx.subscriptions.push(
329+
vscode.commands.registerCommand('clang-format.formatSelectionAlternate',
330+
() => {
331+
formatter.formatSelection(true /* alternate */);
332+
}));
324333
}

0 commit comments

Comments
 (0)