Skip to content

Commit

Permalink
Remove legacy settings support (#174)
Browse files Browse the repository at this point in the history
* Remove legacy settings support

* Formatting
  • Loading branch information
karthiknadig committed Oct 10, 2023
1 parent 1edde1e commit 8ef7d44
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 39 deletions.
1 change: 1 addition & 0 deletions src/common/constants.ts
Expand Up @@ -3,6 +3,7 @@

import * as path from 'path';

export const EXTENSION_ID = 'ms-python.autopep8';
const folderName = path.basename(__dirname);
export const EXTENSION_ROOT_DIR =
folderName === 'common' ? path.dirname(path.dirname(__dirname)) : path.dirname(__dirname);
Expand Down
86 changes: 48 additions & 38 deletions src/common/settings.ts
Expand Up @@ -4,7 +4,8 @@
import { ConfigurationChangeEvent, ConfigurationScope, WorkspaceConfiguration, WorkspaceFolder } from 'vscode';
import { getInterpreterDetails } from './python';
import { getConfiguration, getWorkspaceFolders } from './vscodeapi';
import { traceLog } from './logging';
import { traceInfo, traceLog, traceWarn } from './logging';
import { EXTENSION_ID } from './constants';

export interface ISettings {
cwd: string;
Expand Down Expand Up @@ -42,39 +43,6 @@ function resolveVariables(value: string[], workspace?: WorkspaceFolder): string[
});
}

function getArgs(namespace: string, workspace: WorkspaceFolder): string[] {
const config = getConfiguration(namespace, workspace.uri);
const args = config.get<string[]>('args', []);

if (args.length > 0) {
return args;
}

const legacyConfig = getConfiguration('python', workspace.uri);
const legacyArgs = legacyConfig.get<string[]>('formatting.autopep8Args', []);
if (legacyArgs.length > 0) {
traceLog(`Using legacy configuration form 'python.formatting.autopep8Args': ${legacyArgs.join(' ')}.`);
}
return legacyArgs;
}

function getPath(namespace: string, workspace: WorkspaceFolder): string[] {
const config = getConfiguration(namespace, workspace.uri);
const path = config.get<string[]>('path', []);

if (path.length > 0) {
return path;
}

const legacyConfig = getConfiguration('python', workspace.uri);
const legacyPath = legacyConfig.get<string>('formatting.autopep8Path', '');
if (legacyPath.length > 0 && legacyPath !== 'autopep8') {
traceLog(`Using legacy configuration form 'python.formatting.autopep8Path': ${legacyPath}`);
return [legacyPath];
}
return [];
}

export function getInterpreterFromSetting(namespace: string, scope?: ConfigurationScope) {
const config = getConfiguration(namespace, scope);
return config.get<string[]>('interpreter');
Expand Down Expand Up @@ -109,13 +77,11 @@ export async function getWorkspaceSettings(
}
}

const args = getArgs(namespace, workspace);
const path = getPath(namespace, workspace);
const workspaceSetting = {
cwd: workspace.uri.fsPath,
workspace: workspace.uri.toString(),
args: resolveVariables(args, workspace),
path: resolveVariables(path, workspace),
args: resolveVariables(config.get<string[]>('args', []), workspace),
path: resolveVariables(config.get<string[]>('path', []), workspace),
interpreter: resolveVariables(interpreter, workspace),
importStrategy: config.get<string>('importStrategy', 'fromEnvironment'),
showNotifications: config.get<string>('showNotifications', 'off'),
Expand Down Expand Up @@ -162,3 +128,47 @@ export function checkIfConfigurationChanged(e: ConfigurationChangeEvent, namespa
const changed = settings.map((s) => e.affectsConfiguration(s));
return changed.includes(true);
}

export function logDefaultFormatter(): void {
getWorkspaceFolders().forEach((workspace) => {
let config = getConfiguration('editor', { uri: workspace.uri, languageId: 'python' });
if (!config) {
config = getConfiguration('editor', workspace.uri);
if (!config) {
traceInfo('Unable to get editor configuration');
}
}
const formatter = config.get<string>('defaultFormatter', '');
traceInfo(`Default formatter is set to ${formatter} for workspace ${workspace.uri.fsPath}`);
if (formatter !== EXTENSION_ID) {
traceWarn(`autopep8 Formatter is NOT set as the default formatter for workspace ${workspace.uri.fsPath}`);
traceWarn(
'To set autopep8 Formatter as the default formatter, add the following to your settings.json file:',
);
traceWarn(`\n"[python]": {\n "editor.defaultFormatter": "${EXTENSION_ID}"\n}`);
}
});
}

export function logLegacySettings(): void {
getWorkspaceFolders().forEach((workspace) => {
try {
const legacyConfig = getConfiguration('python', workspace.uri);
const legacyArgs = legacyConfig.get<string[]>('formatting.autopep8Args', []);
const legacyPath = legacyConfig.get<string>('formatting.autopep8Path', '');
if (legacyArgs.length > 0) {
traceWarn(`"python.formatting.autopep8Args" is deprecated. Use "autopep8.args" instead.`);
traceWarn(`"python.formatting.autopep8Args" for workspace ${workspace.uri.fsPath}:`);
traceWarn(`\n${JSON.stringify(legacyArgs, null, 4)}`);
}

if (legacyPath.length > 0 && legacyPath !== 'autopep8') {
traceWarn(`"python.formatting.autopep8Path" is deprecated. Use "autopep8.path" instead.`);
traceWarn(`"python.formatting.autopep8Path" for workspace ${workspace.uri.fsPath}:`);
traceWarn(`\n${JSON.stringify(legacyPath, null, 4)}`);
}
} catch (err) {
traceWarn(`Error while logging legacy settings: ${err}`);
}
});
}
22 changes: 21 additions & 1 deletion src/extension.ts
Expand Up @@ -6,7 +6,13 @@ import { LanguageClient } from 'vscode-languageclient/node';
import { restartServer } from './common/server';
import { registerLogger, traceError, traceLog, traceVerbose } from './common/logging';
import { initializePython, onDidChangePythonInterpreter } from './common/python';
import { checkIfConfigurationChanged, getInterpreterFromSetting, getWorkspaceSettings } from './common/settings';
import {
checkIfConfigurationChanged,
getInterpreterFromSetting,
getWorkspaceSettings,
logDefaultFormatter,
logLegacySettings,
} from './common/settings';
import { loadServerDefaults } from './common/setup';
import { getProjectRoot } from './common/utilities';
import { createOutputChannel, onDidChangeConfiguration, registerCommand } from './common/vscodeapi';
Expand Down Expand Up @@ -63,8 +69,22 @@ export async function activate(context: vscode.ExtensionContext): Promise<void>
registerLanguageStatusItem(serverId, serverName, `${serverId}.showLogs`),
);

// This is needed to ensure that the formatter is registered before the
// language server is started. If the language server takes too long to start,
// and user triggers formatting, they can see a weird message that formatting
// although registered by extension it is not supported.
registerEmptyFormatter();

// This is needed to inform users that they might have not set this extension
// as the formatter. Instructions are printed in the output channel on how to
// set it.
logDefaultFormatter();

// This is needed to inform users that they might have some legacy settings that
// are no longer supported. Instructions are printed in the output channel on how
// to update them.
logLegacySettings();

setImmediate(async () => {
const interpreter = getInterpreterFromSetting(serverId);
if (interpreter === undefined || interpreter.length === 0) {
Expand Down

0 comments on commit 8ef7d44

Please sign in to comment.