@@ -6,6 +6,7 @@ import {MODES,
66 LANGUAGES } from './clangMode' ;
77import { getBinPath } from './clangPath' ;
88import sax = require( 'sax' ) ;
9+ import * as shlex from 'shlex' ;
910
1011export 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