1
1
import * as cp from 'child_process' ;
2
-
3
2
import * as fs from 'fs' ;
4
-
5
3
import {
6
4
DocumentFormattingEditProvider ,
7
5
DocumentRangeFormattingEditProvider ,
@@ -16,6 +14,7 @@ import {
16
14
import { Configuration } from '../configuration/Configuration' ;
17
15
import { getDocumentFilter } from '../configuration/mod' ;
18
16
import { FileSystem } from '../file_system/FileSystem' ;
17
+ import { ChildLogger } from '../logging/child_logger' ;
19
18
20
19
const ansiRegex = / [ \u001b \u009b ] [ [ ( ) # ; ? ] * (?: [ 0 - 9 ] { 1 , 4 } (?: ; [ 0 - 9 ] { 0 , 4 } ) * ) ? [ 0 - 9 A - O R Z c f - n q r y = > < ] / g;
21
20
@@ -26,16 +25,20 @@ interface RustFmtDiff {
26
25
}
27
26
28
27
export class FormattingManager implements DocumentFormattingEditProvider , DocumentRangeFormattingEditProvider {
29
- private configuration : Configuration ;
30
-
31
- private newFormatRegex : RegExp = / ^ D i f f i n ( .* ) a t l i n e ( \d + ) : $ / ;
32
-
33
- public static async create ( context : ExtensionContext , configuration : Configuration ) : Promise < FormattingManager | undefined > {
28
+ private _newFormatRegex : RegExp = / ^ D i f f i n ( .* ) a t l i n e ( \d + ) : $ / ;
29
+ private _configuration : Configuration ;
30
+ private _logger : ChildLogger ;
31
+
32
+ public static async create (
33
+ context : ExtensionContext ,
34
+ configuration : Configuration ,
35
+ logger : ChildLogger
36
+ ) : Promise < FormattingManager | undefined > {
34
37
const rustfmtPath : string | undefined = await FileSystem . findExecutablePath ( configuration . getRustfmtPath ( ) ) ;
35
38
if ( rustfmtPath === undefined ) {
36
39
return undefined ;
37
40
}
38
- return new FormattingManager ( context , configuration ) ;
41
+ return new FormattingManager ( context , configuration , logger ) ;
39
42
}
40
43
41
44
public provideDocumentFormattingEdits ( document : TextDocument ) : Thenable < TextEdit [ ] > {
@@ -50,9 +53,15 @@ export class FormattingManager implements DocumentFormattingEditProvider, Docume
50
53
* To create an instance of the class use the method `create`
51
54
* @param context The extension context
52
55
* @param configuration The configuration
56
+ * @param logger the logger used to create a child logger to log messages
53
57
*/
54
- private constructor ( context : ExtensionContext , configuration : Configuration ) {
55
- this . configuration = configuration ;
58
+ private constructor (
59
+ context : ExtensionContext ,
60
+ configuration : Configuration ,
61
+ logger : ChildLogger
62
+ ) {
63
+ this . _configuration = configuration ;
64
+ this . _logger = logger . createChildLogger ( 'FormattingManager: ' ) ;
56
65
context . subscriptions . push (
57
66
languages . registerDocumentFormattingEditProvider (
58
67
getDocumentFilter ( ) ,
@@ -66,19 +75,22 @@ export class FormattingManager implements DocumentFormattingEditProvider, Docume
66
75
}
67
76
68
77
private formattingEdits ( document : TextDocument , range ?: Range ) : Thenable < TextEdit [ ] > {
78
+ const logger = this . _logger . createChildLogger ( 'formattingEdits: ' ) ;
69
79
return new Promise ( ( resolve , reject ) => {
70
80
const fileName = document . fileName + '.fmt' ;
71
81
fs . writeFileSync ( fileName , document . getText ( ) ) ;
72
-
82
+ const rustfmtPath = this . _configuration . getRustfmtPath ( ) ;
83
+ logger . debug ( `rustfmtPath=${ rustfmtPath } ` ) ;
73
84
const args = [ '--skip-children' , '--write-mode=diff' ] ;
74
85
if ( range !== undefined ) {
75
86
args . push ( '--file-lines' ,
76
87
`[{"file":"${ fileName } ","range":[${ range . start . line + 1 } , ${ range . end . line + 1 } ]}]` ) ;
77
88
} else {
78
89
args . push ( fileName ) ;
79
90
}
91
+ logger . debug ( `args=${ JSON . stringify ( args ) } ` ) ;
80
92
const env = Object . assign ( { TERM : 'xterm' } , process . env ) ;
81
- cp . execFile ( this . configuration . getRustfmtPath ( ) , args , { env : env } , ( err , stdout , stderr ) => {
93
+ cp . execFile ( rustfmtPath , args , { env : env } , ( err , stdout , stderr ) => {
82
94
try {
83
95
if ( err && ( < any > err ) . code === 'ENOENT' ) {
84
96
window . showInformationMessage ( 'The "rustfmt" command is not available. Make sure it is installed.' ) ;
@@ -92,12 +104,14 @@ export class FormattingManager implements DocumentFormattingEditProvider, Docume
92
104
const hasFatalError = ( err && ( err as any ) . code < 3 ) ;
93
105
94
106
if ( ( err || stderr . length ) && hasFatalError ) {
107
+ logger . debug ( 'cannot format due to syntax errors' ) ;
95
108
window . setStatusBarMessage ( '$(alert) Cannot format due to syntax errors' , 5000 ) ;
96
109
return reject ( ) ;
97
110
}
98
111
99
112
return resolve ( this . parseDiff ( document . uri , stdout ) ) ;
100
113
} catch ( e ) {
114
+ logger . error ( `e=${ e } ` ) ;
101
115
reject ( e ) ;
102
116
} finally {
103
117
fs . unlinkSync ( fileName ) ;
@@ -173,7 +187,7 @@ export class FormattingManager implements DocumentFormattingEditProvider, Docume
173
187
174
188
for ( const line of diff . split ( / \n / ) ) {
175
189
if ( line . startsWith ( 'Diff in' ) ) {
176
- const matches = this . newFormatRegex . exec ( line ) ;
190
+ const matches = this . _newFormatRegex . exec ( line ) ;
177
191
178
192
if ( ! matches ) {
179
193
continue ;
0 commit comments