Skip to content

Commit 6e0bc5a

Browse files
committed
fix #799
1 parent aae0495 commit 6e0bc5a

File tree

2 files changed

+8
-5
lines changed

2 files changed

+8
-5
lines changed

src/client/formatters/baseFormatter.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@ export abstract class BaseFormatter {
1616

1717
public abstract formatDocument(document: vscode.TextDocument, options: vscode.FormattingOptions, token: vscode.CancellationToken, range?: vscode.Range): Thenable<vscode.TextEdit[]>;
1818

19-
protected provideDocumentFormattingEdits(document: vscode.TextDocument, options: vscode.FormattingOptions, token: vscode.CancellationToken, command: string, args: string[]): Thenable<vscode.TextEdit[]> {
19+
protected provideDocumentFormattingEdits(document: vscode.TextDocument, options: vscode.FormattingOptions, token: vscode.CancellationToken, command: string, args: string[], cwd: string = null): Thenable<vscode.TextEdit[]> {
2020
this.outputChannel.clear();
21+
cwd = typeof cwd === 'string' && cwd.length > 0 ? cwd : (this.workspaceRootPath ? this.workspaceRootPath : vscode.workspace.rootPath);
2122

2223
// autopep8 and yapf have the ability to read from the process input stream and return the formatted code out of the output stream
2324
// However they don't support returning the diff of the formatted text when reading data from the input stream
@@ -29,8 +30,7 @@ export abstract class BaseFormatter {
2930
if (token && token.isCancellationRequested) {
3031
return [filePath, ''];
3132
}
32-
const workspaceRoot = this.workspaceRootPath ? this.workspaceRootPath : vscode.workspace.rootPath;
33-
return Promise.all<string>([Promise.resolve(filePath), execPythonFile(command, args.concat([filePath]), workspaceRoot)]);
33+
return Promise.all<string>([Promise.resolve(filePath), execPythonFile(command, args.concat([filePath]), cwd)]);
3434
}).then(data => {
3535
// Delete the temporary file created
3636
if (tmpFileCreated) {

src/client/formatters/yapfFormatter.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
'use strict';
22

33
import * as vscode from 'vscode';
4-
import {BaseFormatter} from './baseFormatter';
4+
import { BaseFormatter } from './baseFormatter';
55
import * as settings from './../common/configSettings';
66
import { Product } from '../common/installer';
7+
import * as path from 'path';
78

89
export class YapfFormatter extends BaseFormatter {
910
constructor(outputChannel: vscode.OutputChannel, pythonSettings: settings.IPythonSettings, workspaceRootPath?: string) {
@@ -17,6 +18,8 @@ export class YapfFormatter extends BaseFormatter {
1718
if (range && !range.isEmpty) {
1819
yapfArgs = yapfArgs.concat(['--lines', `${range.start.line + 1}-${range.end.line + 1}`]);
1920
}
20-
return super.provideDocumentFormattingEdits(document, options, token, yapfPath, yapfArgs);
21+
// Yapf starts looking for config file starting from the file path
22+
let cwd = path.dirname(document.fileName);
23+
return super.provideDocumentFormattingEdits(document, options, token, yapfPath, yapfArgs, cwd);
2124
}
2225
}

0 commit comments

Comments
 (0)