如何添加空格和/或格式代码? [英] How to add white space and/or format code?

查看:52
本文介绍了如何添加空格和/或格式代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

鉴于我已经使用 SymbolFactory.CreateProperty 创建了一个符号,我将如何添加空格.目前,我在写出文档时将可访问性、修饰符、名称、类型等全部串在一起.也许我写错了,或者我需要添加额外的步骤来添加空格?我使用 document.GetText() 将其写出到控制台.

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.

推荐答案

不,这正是您所期望的.生成的节点没有空格,目的是在完成后处理它.

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

有两种选择:

  1. 在节点上调用 .NormalizeWhitespace().这是一个积极的格式化程序,只有在您生成不打算被人类使用的代码时才真正有用——它使输出有效",但会消除任何现有的格式.
  2. Microsoft.CodeAnalysis.Formatting 调用 Formatter.Format().这是花哨的格式化程序,它将尝试保留现有的空白等,并且只会更新需要修复的节点.如果您正在更新用户代码并且不想踩到它,这是最好的选择.
  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.

这篇关于如何添加空格和/或格式代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆