一、核心命名模式
二、事件命名规范
1. 时态区分
// 即将发生(Will)
vscode.workspace.onWillSaveTextDocument(e => {
console.log('文档即将保存');
});
// 已经完成(Did)
vscode.workspace.onDidSaveTextDocument(e => {
console.log('文档已保存');
});
2. 事件前缀
// 观察者模式
panel.onDidDispose(() => { /* 面板已销毁 */ });
vscode.window.onDidChangeActiveTextEditor(e => { /* 编辑器已切换 */ });
// 自定义事件
context.subscriptions.push(vscode.commands.registerCommand('myEvent', () => {
vscode.workspace.onDidChangeConfiguration(e => {
if (e.affectsConfiguration('myExtension')) {
/* 配置已变更 */
}
});
}));
三、方法命名规范
1. 动词开头
// 创建类
vscode.window.createWebviewPanel(...);
vscode.workspace.createFileSystemWatcher(...);
// 获取类
vscode.workspace.getConfiguration(...);
vscode.window.getActiveTextEditor(...);
// 设置类
vscode.window.setStatusBarMessage(...);
vscode.workspace.updateState(...);
2. 异步方法
// 明确Async后缀
async function fetchDataAsync(uri: vscode.Uri): Promise<string> {
const data = await vscode.workspace.fs.readFile(uri);
return new TextDecoder().decode(data);
}
// 事件监听异步处理
vscode.workspace.onDidOpenTextDocument(async document => {
if (document.languageId === 'typescript') {
const content = await fetchDataAsync(document.uri);
/* 处理内容 */
}
});
四、参数与变量命名
1. 驼峰命名法
// 正确示例
function processDocument(textDocument: vscode.TextDocument) {
const lineCount = textDocument.lineCount;
const firstLine = textDocument.lineAt(0).text;
}
// 错误示例
function ProcessDoc(doc: any) { /* 模糊类型 + 错误命名 */ }
2. 描述性命名
// 良好实践
interface ExtensionConfig {
enableAdvancedFeatures: boolean;
pollingIntervalSeconds: number;
}
// 避免使用
interface Config {
flag: boolean;
time: number;
}
五、配置项命名
1. 分层命名
// package.json
{
"contributes": {
"configuration": {
"title": "My Extension",
"properties": {
"myExtension.enableLogging": {
"type": "boolean",
"default": false
},
"myExtension.serverUrl": {
"type": "string",
"format": "uri"
}
}
}
}
}
2. 访问配置
const config = vscode.workspace.getConfiguration('myExtension');
const isLoggingEnabled = config.get('enableLogging');
const serverUrl = config.get('serverUrl');
六、高级命名模式
1. 工厂方法
// 创建复杂对象
vscode.languages.createDiagnosticCollection('myLinter');
vscode.debug.createDebugAdapterDescriptorFactory(...);
// 对比普通构造
new vscode.StatusBarItem(vscode.StatusBarAlignment.Left);
2. 选项对象
// 明确参数含义
vscode.window.showInformationMessage('Update available', 'Install', 'Later')
.then(selection => {
if (selection === 'Install') {
vscode.commands.executeCommand('extension.installUpdate');
}
});
// 复杂选项模式
vscode.workspace.applyEdit(edit => {
edit.replace(uri, range, newValue);
}, {
undoStopBefore: true,
undoStopAfter: true
});
七、最佳实践建议
- 保持一致性
// 统一使用动词开头
function activateExtension() { /* ... */ }
function deactivateExtension() { /* ... */ }
// 避免混合风格
// function Ext_Activate() { /* 不推荐 */ }
- 避免缩写
// 正确示例
function processTextDocument(doc: vscode.TextDocument) { /* ... */ }
// 错误示例
function procDoc(d: any) { /* 类型模糊 + 缩写 */ }
- 命名空间隔离
// 在大型扩展中使用命名空间
namespace MyExtension {
export class DocumentProcessor {
process(doc: vscode.TextDocument) { /* ... */ }
}
}
// 使用时
const processor = new MyExtension.DocumentProcessor();
通过遵循这些命名约定,可以显著提升代码的可读性和可维护性,同时与VS Code内置API保持风格一致,降低学习成本。实际开发中建议结合TypeScript的类型系统,通过接口和类型别名进一步强化命名规范。