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

VS Code自定义扩展与原生格式化工具无法协同运行问题求助

Fixing Custom VS Code Formatter + Native TypeScript Formatter Synergy

Hey there! I’ve run into this exact issue before—when you register your own DocumentFormattingEditProvider, VS Code defaults to using yours exclusively instead of combining it with the native TypeScript formatter. Let’s walk through why this happens and how to get them playing nicely together.

The Root Problem

VS Code’s formatting system doesn’t automatically merge edits from multiple providers for the same language. When you register your custom provider, it replaces (or takes priority over) the native one, which is why you’re not seeing both sets of rules applied.

The Solution: Merge Native + Custom Edits

The fix is to explicitly call the native formatter from within your custom provider, then combine its edits with your own. Here’s a step-by-step implementation:

  1. Fetch the native TypeScript formatter provider
  2. Run the native formatter to get its edits
  3. Generate your custom rule edits
  4. Merge and return both sets of edits

Full Code Example

import * as vscode from 'vscode';

export function activate(context: vscode.ExtensionContext) {
  const customFormatter: vscode.DocumentFormattingEditProvider = {
    async provideDocumentFormattingEdits(
      document: vscode.TextDocument,
      options: vscode.FormattingOptions,
      token: vscode.CancellationToken
    ) {
      // 1. Locate the native TypeScript formatter provider
      const nativeProviders = vscode.languages.getDocumentFormattingEditProviders('typescript');
      // Filter to find the built-in one (adjust the check if needed for your setup)
      const nativeFormatter = nativeProviders.find(provider => {
        const providerId = (provider as any).id;
        return providerId?.includes('typescript') || providerId?.includes('ms-vscode');
      });

      // Fallback: if no native formatter is found, just return our custom edits
      if (!nativeFormatter) {
        return getCustomRuleEdits(document);
      }

      try {
        // 2. Get edits from the native formatter
        const nativeEdits = await nativeFormatter.provideDocumentFormattingEdits(document, options, token);
        // 3. Get our custom rule edits
        const customEdits = getCustomRuleEdits(document);

        // 4. Merge edits: apply native first, then custom to override conflicts
        return [...(nativeEdits || []), ...customEdits];
      } catch (err) {
        // Gracefully handle errors with the native formatter
        console.error('Native formatter failed:', err);
        return getCustomRuleEdits(document);
      }
    }
  };

  context.subscriptions.push(
    vscode.languages.registerDocumentFormattingEditProvider('typescript', customFormatter)
  );
}

// Replace this with your actual custom rule logic
function getCustomRuleEdits(document: vscode.TextDocument): vscode.TextEdit[] {
  const edits: vscode.TextEdit[] = [];

  // Example custom rule: Ensure all import statements end with a semicolon
  for (let lineIdx = 0; lineIdx < document.lineCount; lineIdx++) {
    const line = document.lineAt(lineIdx);
    const lineText = line.text.trim();
    
    if (lineText.startsWith('import') && !lineText.endsWith(';')) {
      // Insert semicolon at the end of the line
      edits.push(vscode.TextEdit.insert(line.range.end, ';'));
    }
  }

  return edits;
}

Key Notes

  • Async/Await: The native formatter’s method may be asynchronous, so mark your provideDocumentFormattingEdits as async to avoid race conditions.
  • Edit Order: By adding native edits first and custom edits second, your rules will take precedence if there’s any overlap (e.g., native formatter doesn’t fix a semicolon, yours does).
  • Error Handling: Wrap the native formatter call in a try/catch to ensure your custom rules still apply if the native one fails.
  • Provider Identification: The filter for the native formatter may need tweaking depending on which TypeScript extension you’re using (built-in vs. a community one). Adjust the providerId check as needed.

This approach ensures both the native VS Code formatting and your custom rules run in sequence, giving you the unified code format you need across your team’s editors.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.20 12:24:38