You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在VS Code扩展中为SOSSE语言实现命令触发的第二种代码格式化

Implementing Single-Line Formatting Command for Your Sosse VS Code Extension

Got it, let's break down how to add that single-line formatting command to your extension—this fits right in with your existing document-wide formatter setup. Here's a step-by-step implementation:

1. Register the Custom Command

First, you'll need to register a dedicated command that triggers your single-line formatting logic. Use VS Code's vscode.commands.registerCommand API for this. This command will handle grabbing the current line (or selected lines), running your formatting, and applying the changes.

// 注册单行格式化命令
vscode.commands.registerCommand('extension.formatSosseSingleLine', async () => {
  const editor = vscode.window.activeTextEditor;
  // 确保当前编辑器是Sosse语言文件且存在
  if (!editor || editor.document.languageId !== 'sosse') {
    vscode.window.showInformationMessage('Single-line formatting only works in Sosse files!');
    return;
  }

  const document = editor.document;
  const selections = editor.selections;
  const edits: vscode.TextEdit[] = [];

  // 支持多行选中:对每个选中的行单独格式化
  for (const selection of selections) {
    const targetLine = document.lineAt(selection.active.line);
    const originalLineText = targetLine.text;
    
    // 执行你的单行格式化逻辑
    const formattedLine = formatSingleLineAction(originalLineText);
    
    // 创建替换整行的编辑操作
    edits.push(vscode.TextEdit.replace(targetLine.range, formattedLine));
  }

  // 应用所有编辑
  const workspaceEdit = new vscode.WorkspaceEdit();
  workspaceEdit.set(document.uri, edits);
  await vscode.workspace.applyEdit(workspaceEdit);
});

2. Implement Your Single-Line Formatting Logic

Next, write the formatSingleLineAction function that contains your custom single-line formatting rules. This is where you'll define how the line should be transformed—adjust indentation, clean up syntax, or whatever your use case requires.

function formatSingleLineAction(lineText: string): string {
  // 示例逻辑:根据你的需求替换成实际格式化规则
  // 比如:去除多余空格,调整缩进,格式化变量声明等
  const trimmedLine = lineText.trim();
  // 假设Sosse语言要求单行代码缩进4个空格
  return '    ' + trimmedLine;
}

3. Declare the Command in package.json

To make the command visible in the command palette (and optional keyboard shortcuts), add it to your extension's package.json under contributes:

{
  "contributes": {
    "commands": [
      {
        "command": "extension.formatSosseSingleLine",
        "title": "Format Sosse Single Line"
      }
    ],
    // 可选:添加快捷键绑定(自定义为你喜欢的组合)
    "keybindings": [
      {
        "command": "extension.formatSosseSingleLine",
        "key": "ctrl+shift+l",
        "when": "editorLangId == sosse && editorTextFocus"
      }
    ]
  }
}

4. Test It Out

Once everything is set up:

  • Reload your extension in VS Code (Ctrl+R in the extension development host window)
  • Open a Sosse file
  • Place your cursor on a line (or select multiple lines)
  • Either open the command palette (Ctrl+Shift+P) and search for "Format Sosse Single Line", or use your custom keyboard shortcut to trigger the formatting.

This approach keeps your single-line formatter separate from the document-wide one, so users can choose which to use based on their needs.

内容的提问来源于stack exchange,提问作者peni4142

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 06:46:15