Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
This repository was archived by the owner on Dec 8, 2020. It is now read-only.

Commit cd2b213

Browse filesBrowse files
authored
Added logging to formatting manager (#320)
1 parent 5c1e803 commit cd2b213
Copy full SHA for cd2b213

File tree

3 files changed

+32
-16
lines changed
Filter options

3 files changed

+32
-16
lines changed

‎src/components/formatting/formatting_manager.ts

Copy file name to clipboardExpand all lines: src/components/formatting/formatting_manager.ts
+27-13Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import * as cp from 'child_process';
2-
32
import * as fs from 'fs';
4-
53
import {
64
DocumentFormattingEditProvider,
75
DocumentRangeFormattingEditProvider,
@@ -16,6 +14,7 @@ import {
1614
import { Configuration } from '../configuration/Configuration';
1715
import { getDocumentFilter } from '../configuration/mod';
1816
import { FileSystem } from '../file_system/FileSystem';
17+
import { ChildLogger } from '../logging/child_logger';
1918

2019
const ansiRegex = /[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g;
2120

@@ -26,16 +25,20 @@ interface RustFmtDiff {
2625
}
2726

2827
export class FormattingManager implements DocumentFormattingEditProvider, DocumentRangeFormattingEditProvider {
29-
private configuration: Configuration;
30-
31-
private newFormatRegex: RegExp = /^Diff in (.*) at line (\d+):$/;
32-
33-
public static async create(context: ExtensionContext, configuration: Configuration): Promise<FormattingManager | undefined> {
28+
private _newFormatRegex: RegExp = /^Diff in (.*) at line (\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> {
3437
const rustfmtPath: string | undefined = await FileSystem.findExecutablePath(configuration.getRustfmtPath());
3538
if (rustfmtPath === undefined) {
3639
return undefined;
3740
}
38-
return new FormattingManager(context, configuration);
41+
return new FormattingManager(context, configuration, logger);
3942
}
4043

4144
public provideDocumentFormattingEdits(document: TextDocument): Thenable<TextEdit[]> {
@@ -50,9 +53,15 @@ export class FormattingManager implements DocumentFormattingEditProvider, Docume
5053
* To create an instance of the class use the method `create`
5154
* @param context The extension context
5255
* @param configuration The configuration
56+
* @param logger the logger used to create a child logger to log messages
5357
*/
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: ');
5665
context.subscriptions.push(
5766
languages.registerDocumentFormattingEditProvider(
5867
getDocumentFilter(),
@@ -66,19 +75,22 @@ export class FormattingManager implements DocumentFormattingEditProvider, Docume
6675
}
6776

6877
private formattingEdits(document: TextDocument, range?: Range): Thenable<TextEdit[]> {
78+
const logger = this._logger.createChildLogger('formattingEdits: ');
6979
return new Promise((resolve, reject) => {
7080
const fileName = document.fileName + '.fmt';
7181
fs.writeFileSync(fileName, document.getText());
72-
82+
const rustfmtPath = this._configuration.getRustfmtPath();
83+
logger.debug(`rustfmtPath=${rustfmtPath}`);
7384
const args = ['--skip-children', '--write-mode=diff'];
7485
if (range !== undefined) {
7586
args.push('--file-lines',
7687
`[{"file":"${fileName}","range":[${range.start.line + 1}, ${range.end.line + 1}]}]`);
7788
} else {
7889
args.push(fileName);
7990
}
91+
logger.debug(`args=${JSON.stringify(args)}`);
8092
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) => {
8294
try {
8395
if (err && (<any>err).code === 'ENOENT') {
8496
window.showInformationMessage('The "rustfmt" command is not available. Make sure it is installed.');
@@ -92,12 +104,14 @@ export class FormattingManager implements DocumentFormattingEditProvider, Docume
92104
const hasFatalError = (err && (err as any).code < 3);
93105

94106
if ((err || stderr.length) && hasFatalError) {
107+
logger.debug('cannot format due to syntax errors');
95108
window.setStatusBarMessage('$(alert) Cannot format due to syntax errors', 5000);
96109
return reject();
97110
}
98111

99112
return resolve(this.parseDiff(document.uri, stdout));
100113
} catch (e) {
114+
logger.error(`e=${e}`);
101115
reject(e);
102116
} finally {
103117
fs.unlinkSync(fileName);
@@ -173,7 +187,7 @@ export class FormattingManager implements DocumentFormattingEditProvider, Docume
173187

174188
for (const line of diff.split(/\n/)) {
175189
if (line.startsWith('Diff in')) {
176-
const matches = this.newFormatRegex.exec(line);
190+
const matches = this._newFormatRegex.exec(line);
177191

178192
if (!matches) {
179193
continue;

‎src/extension.ts

Copy file name to clipboardExpand all lines: src/extension.ts
+4-2Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,15 +153,17 @@ class RlsMode {
153153
logger.debug('User decided to use rustfmt');
154154
const formattingManager = await FormattingManager.create(
155155
this._extensionContext,
156-
this._configuration
156+
this._configuration,
157+
this._logger
157158
);
158159
if (formattingManager === undefined) {
159160
await this.handleMissingRustfmt();
160161
// The user may have decided not to use rustfmt
161162
if (this._rlsConfiguration.getUseRustfmt()) {
162163
const anotherFormattingManager = await FormattingManager.create(
163164
this._extensionContext,
164-
this._configuration
165+
this._configuration,
166+
this._logger
165167
);
166168
if (anotherFormattingManager === undefined) {
167169
window.showErrorMessage('Formatting: some error happened');

‎src/legacy_mode_manager.ts

Copy file name to clipboardExpand all lines: src/legacy_mode_manager.ts
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export class LegacyModeManager {
3333
currentWorkingDirectoryManager: CurrentWorkingDirectoryManager,
3434
logger: ChildLogger
3535
): Promise<LegacyModeManager> {
36-
const formattingManager: FormattingManager | undefined = await FormattingManager.create(context, configuration);
36+
const formattingManager: FormattingManager | undefined = await FormattingManager.create(context, configuration, logger);
3737
return new LegacyModeManager(
3838
context,
3939
configuration,

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.