Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
362 views
in Technique[技术] by (71.8m points)

roslyn - How to add white space and/or format code?

Given that I have created a symbol using SymbolFactory.CreateProperty, how would I add white space. Currently I get accessibility, modifiers, name, type etc. all stringed together when writing out document. Maybe I am writing it out wrong, or I need to add extra step to add white space? I use document.GetText() to write it out to console.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Nope, that's what you expect. Generated nodes don't have whitespace, with the intent you'll process it once you're done.

There are two options:

  1. Call .NormalizeWhitespace() on the nodes. This is an aggressive formatter that is really only useful if you're generating code you don't intend to be consumed by a human -- it makes the output "valid" but blows away any existing formatting.
  2. Call Formatter.Format(), from Microsoft.CodeAnalysis.Formatting. This is the fancy formatter which will attempt to preserve existing whitespace and such, and will only update the nodes that need fixing. This is best if you're updating user code and you don't want to stomp on it.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...