Skip to content

Commit a05fcd7

Browse files
Add a setting to allow server transport selection (#449)
Co-authored-by: Courtney Webster <60238438+cwebster-99@users.noreply.github.com>
1 parent 632adc2 commit a05fcd7

File tree

5 files changed

+35
-4
lines changed

5 files changed

+35
-4
lines changed

README.md

+5
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,11 @@ There are several settings you can configure to customize the behavior of this e
8080
<td><code>off</code></td>
8181
<td> Controls when notifications are shown by this extension. Accepted values are <code>onError</code>, <code>onWarning</code>, <code>always</code> and <code>off</code>.</td>
8282
</tr>
83+
<tr>
84+
<td>black-formatter.serverTransport</td>
85+
<td><code>stdio</code></td>
86+
<td> Selects the transport protocol to be used by the Black server. When set to <code>stdio</code>, the extension will use the standard input/output streams to communicate with the Black server. When set to <code>pipe</code>, the extension will use a named pipe (on Windows) or Unix Domain Socket (on Linux/Mac) to communicate with the Black server. The <code>stdio</code> transport protocol is the default and recommended option for most users.</td>
87+
</tr>
8388
</tbody>
8489
</table>
8590

package.json

+14
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,20 @@
156156
],
157157
"scope": "machine",
158158
"type": "string"
159+
},
160+
"black-formatter.serverTransport": {
161+
"default": "stdio",
162+
"enum": [
163+
"stdio",
164+
"pipe"
165+
],
166+
"markdownDescription": "%settings.serverTransport.description%",
167+
"markdownEnumDescriptions": [
168+
"%settings.serverTransport.stdio.description%",
169+
"%settings.serverTransport.pipe.description%"
170+
],
171+
"scope": "window",
172+
"type": "string"
159173
}
160174
}
161175
},

package.nls.json

+3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
"settings.importStrategy.useBundled.description": "Always use the bundled version of Black to format Python files.",
99
"settings.importStrategy.fromEnvironment.description": "Use the Black binary from the selected Python environment. If the extension fails to find a valid Black binary, it will fallback to using the bundled version of Black.",
1010
"settings.interpreter.description": "Path to a Python executable or a command that will be used to launch the Black server and any subprocess. Accepts an array of a single or multiple strings. When set to `[]`, the extension will use the path to the selected Python interpreter. If passing a command, each argument should be provided as a separate string in the array.",
11+
"settings.serverTransport.description": "Selects the transport protocol to be used by the Black server. When set to `stdio`, the extension will use the standard input/output streams to communicate with the Black server. When set to `pipe`, the extension will use a named pipe (on Windows) or Unix Domain Socket (on Linux/Mac) to communicate with the Black server. The `stdio` transport protocol is the default and recommended option for most users.",
12+
"settings.serverTransport.stdio.description": "Use the standard input/output streams to communicate with the Black server.",
13+
"settings.serverTransport.pipe.description": "Use a named pipe (on windows) and Unix Domain Socket (on linux/mac) to communicate with the Black server.",
1114
"settings.showNotifications.description": "Controls when notifications are shown by this extension.",
1215
"settings.showNotifications.off.description": "All notifications are turned off, any errors or warnings when formatting Python files are still available in the logs.",
1316
"settings.showNotifications.onError.description": "Notifications are shown only in the case of an error when formatting Python files.",

src/common/server.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import {
1414
import { DEBUG_SERVER_SCRIPT_PATH, SERVER_SCRIPT_PATH } from './constants';
1515
import { traceError, traceInfo, traceVerbose } from './logging';
1616
import { getDebuggerPath } from './python';
17-
import { getExtensionSettings, getGlobalSettings, ISettings } from './settings';
17+
import { getExtensionSettings, getGlobalSettings, getServerTransport, ISettings } from './settings';
1818
import { getDocumentSelector, getLSClientTraceLevel } from './utilities';
1919
import { updateStatus } from './status';
2020
import { unregisterEmptyFormatter } from './nullFormatter';
@@ -29,7 +29,8 @@ async function createServer(
2929
initializationOptions: IInitOptions,
3030
): Promise<LanguageClient> {
3131
const command = settings.interpreter[0];
32-
const cwd = settings.cwd === '${fileDirname}' ? Uri.parse(settings.workspace).fsPath : settings.cwd;
32+
const workspaceUri = Uri.parse(settings.workspace);
33+
const cwd = settings.cwd === '${fileDirname}' ? workspaceUri.fsPath : settings.cwd;
3334

3435
// Set debugger path needed for debugging python code.
3536
const newEnv = { ...process.env };
@@ -57,7 +58,7 @@ async function createServer(
5758
command,
5859
args,
5960
options: { cwd, env: newEnv },
60-
transport: TransportKind.pipe,
61+
transport: getServerTransport(serverId, workspaceUri),
6162
};
6263

6364
// Options to control the language client

src/common/settings.ts

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT License.
33

4-
import { ConfigurationChangeEvent, ConfigurationScope, WorkspaceConfiguration, WorkspaceFolder } from 'vscode';
4+
import { ConfigurationChangeEvent, ConfigurationScope, Uri, WorkspaceConfiguration, WorkspaceFolder } from 'vscode';
55
import { getInterpreterDetails } from './python';
66
import { getConfiguration, getWorkspaceFolders } from './vscodeapi';
77
import { traceInfo, traceLog, traceWarn } from './logging';
88
import { EXTENSION_ID } from './constants';
9+
import { TransportKind } from 'vscode-languageclient/node';
910

1011
export interface ISettings {
1112
cwd: string;
@@ -17,6 +18,12 @@ export interface ISettings {
1718
showNotifications: string;
1819
}
1920

21+
export function getServerTransport(namespace: string, uri: Uri): TransportKind {
22+
const config = getConfiguration(namespace, uri);
23+
const value = config.get<string>('serverTransport', 'stdio');
24+
return value === 'pipe' ? TransportKind.pipe : TransportKind.stdio;
25+
}
26+
2027
export function getExtensionSettings(namespace: string, includeInterpreter?: boolean): Promise<ISettings[]> {
2128
return Promise.all(getWorkspaceFolders().map((w) => getWorkspaceSettings(namespace, w, includeInterpreter)));
2229
}
@@ -152,6 +159,7 @@ export function checkIfConfigurationChanged(e: ConfigurationChangeEvent, namespa
152159
`${namespace}.interpreter`,
153160
`${namespace}.importStrategy`,
154161
`${namespace}.showNotifications`,
162+
`${namespace}.serverTransport`,
155163
];
156164
const changed = settings.map((s) => e.affectsConfiguration(s));
157165
return changed.includes(true);

0 commit comments

Comments
 (0)