VS Code扩展开发中的命名约定

一、核心命名模式

命名类型
事件命名
方法命名
参数/变量
配置项

二、事件命名规范

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
});

七、最佳实践建议

  1. 保持一致性
// 统一使用动词开头
function activateExtension() { /* ... */ }
function deactivateExtension() { /* ... */ }

// 避免混合风格
// function Ext_Activate() { /* 不推荐 */ }
  1. 避免缩写
// 正确示例
function processTextDocument(doc: vscode.TextDocument) { /* ... */ }

// 错误示例
function procDoc(d: any) { /* 类型模糊 + 缩写 */ }
  1. 命名空间隔离
// 在大型扩展中使用命名空间
namespace MyExtension {
  export class DocumentProcessor {
    process(doc: vscode.TextDocument) { /* ... */ }
  }
}

// 使用时
const processor = new MyExtension.DocumentProcessor();

通过遵循这些命名约定,可以显著提升代码的可读性和可维护性,同时与VS Code内置API保持风格一致,降低学习成本。实际开发中建议结合TypeScript的类型系统,通过接口和类型别名进一步强化命名规范。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值