Skip to content

Commit

Permalink
add fix for vscode issue on macOS where locale info is stripped from env
Browse files Browse the repository at this point in the history
  • Loading branch information
joslarson committed May 8, 2018
1 parent 281f488 commit b3c6a09
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 6 deletions.
9 changes: 8 additions & 1 deletion src/BlackEditProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ export class BlackEditProvider
implements DocumentRangeFormattingEditProvider, DocumentFormattingEditProvider {
channel?: OutputChannel;
hasCompatibleBlackVersion?: boolean;
commandPrefix: string;

constructor(commandPrefix = '') {
this.commandPrefix = commandPrefix;
}

debug(msg: string, newLine = true) {
const debug: boolean = workspace.getConfiguration('black', null).get('debug') as boolean;
Expand Down Expand Up @@ -79,7 +84,9 @@ export class BlackEditProvider
const pythonPrefix =
pythonPath && pythonPath !== 'python' && !hasCustomPath ? `${pythonPath} -m ` : '';

return `${pythonPrefix}${blackPath} -l ${lineLength}${fast ? ' --fast' : ''} -`;
return `${this.commandPrefix}${pythonPrefix}${blackPath} -l ${lineLength}${
fast ? ' --fast' : ''
} -`;
}

async provideEdits(
Expand Down
28 changes: 26 additions & 2 deletions src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { exec } from 'child_process';
import { Disposable, ExtensionContext, languages, workspace, window } from 'vscode';
import { BlackEditProvider } from './BlackEditProvider';
import { blackVersionIsIncompatible } from './utils';
Expand All @@ -18,6 +19,7 @@ async function registerFormatter(provider: BlackEditProvider) {
disposeHandlers();
languages.registerDocumentFormattingEditProvider('python', provider);
languages.registerDocumentRangeFormattingEditProvider('python', provider);

// check black version compatibility
const versionErrorMessage = await blackVersionIsIncompatible(provider);
if (versionErrorMessage) {
Expand All @@ -27,8 +29,30 @@ async function registerFormatter(provider: BlackEditProvider) {
}
}

export function activate(context: ExtensionContext) {
const provider = new BlackEditProvider();
export async function activate(context: ExtensionContext) {
const providerArgs: string[] = [];

// workaround for vscode issue: https://github.com/Microsoft/vscode/issues/16261
if (process.platform === 'darwin' && !process.env.LANG) {
await new Promise((resolve, reject) =>
exec(
`echo $(defaults read -g AppleLanguages | sed '/"/!d;s/["[:space:]]//g;s/-/_/').UTF-8`,
(error, stdout, stderr) => {
// if there's an unexpected error, skip this
if (!error) {
const langCode = stdout.trim();
// make sure stdout matches a valid language code pattern
if (langCode.match(/^[a-z]{2}_[A-Z]{2}\.UTF-8$/)) {
providerArgs.push(`LANG=${langCode} `);
}
}
resolve();
}
)
);
}

const provider = new BlackEditProvider(...providerArgs);

// initial formatter registration
registerFormatter(provider);
Expand Down
6 changes: 3 additions & 3 deletions src/test/test-project/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"python.pythonPath": "${workspaceFolder}/.venv/bin/python"
"python.pythonPath": "${workspaceFolder}/.venv/bin/python",
// "python.linting.pylintEnabled": false,
// "python.linting.flake8Enabled": true,
// "python.linting.enabled": true,
// "python.formatting.provider": "none",
// "black.lineLength": 79,
// "black.debug": true,
// "black.path": "/Users/brojoe/.pyenv/versions/3.6.3/bin/black",
"black.debug": true,
"black.path": "/Users/brojoe/.pyenv/versions/3.6.3/bin/black",
// "python.pythonPath": "${workspaceFolder}/.venv/bin/python",
}

0 comments on commit b3c6a09

Please sign in to comment.