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

使用Roslyn DocumentEditor.ReplaceNode后代码格式混乱的解决咨询

Fixing Indentation and Formatting Issues with Roslyn DocumentEditor.ReplaceNode

Ah, I feel your pain—nothing's worse than having working code that's completely unreadable because it's all crammed onto one line! The issue here is that DocumentEditor.ReplaceNode doesn't automatically apply code formatting when it inserts your new nodes. It just plops the raw syntax tree content into place, no indentation or line breaks included.

Luckily, Roslyn has a built-in solution for this: the Formatter class in the Microsoft.CodeAnalysis.Formatting namespace. Here's how to fix your problem step by step:

1. Add the necessary using directive

First, make sure you've got the right namespace imported so you can access the formatter:

using Microsoft.CodeAnalysis.Formatting;

2. Format the entire modified document

After you've made all your ReplaceNode calls with DocumentEditor, you can format the entire updated syntax tree to apply proper indentation and line breaks. Here's a code example:

// Assume 'editor' is your existing DocumentEditor instance
// and 'workspace' is your Roslyn Workspace (e.g., MSBuildWorkspace or AdhocWorkspace)

// Get the modified syntax root after your ReplaceNode operations
var unformattedRoot = editor.GetChangedRoot();

// Format the entire root using Roslyn's default (or custom) code styles
var formattedRoot = Formatter.Format(unformattedRoot, Formatter.Annotation, workspace);

// Update the DocumentEditor with the formatted root
await editor.SetRootAsync(formattedRoot);

3. Format only the modified region (for better performance)

If you only want to format the specific part of the code you modified (instead of the whole document), you can target just that node's span:

// Let's say 'replacedNode' is the SyntaxNode you just replaced
var formattedRoot = Formatter.Format(
    unformattedRoot,
    new[] { replacedNode.Span },
    workspace);

await editor.SetRootAsync(formattedRoot);

4. Customize formatting rules (optional)

If you need to enforce specific code styles (like 4-space indentation instead of tabs, or specific line break rules), you can tweak the formatting options:

var customOptions = workspace.Options
    .WithChangedOption(FormattingOptions.IndentationSize, LanguageNames.CSharp, 4)
    .WithChangedOption(FormattingOptions.TabSize, LanguageNames.CSharp, 4)
    .WithChangedOption(FormattingOptions.NewLine, LanguageNames.CSharp, "\r\n");

var formattedRoot = Formatter.Format(unformattedRoot, customOptions, workspace);

The key thing to remember is that DocumentEditor handles the structural changes to your code, but you need to explicitly ask Roslyn to apply formatting rules to make it readable again. This approach will fix those crammed lines and give you clean, properly indented code that matches standard C# style conventions.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.20 12:18:46