我必须为 VS 2022 开发一个相当简单的扩展。为此,我需要将一些文本插入到所使用文档的某个位置(为简单起见,直接插入到开头)。我在 YouTube 和 GitHub 上观看了 Mads Kristensen 的一些视频教程,例如他的 InsertGuid 示例 ( https://github.com/MadsKristensen/InsertGuid ),我发现它很有启发,但我未能找到有关如何在文档文本中导航的指导,更具体地说,如何将文本插入其开头或结尾。 我的问题是如何做到这一点或者在哪里可以找到一些详细的教程或文档。
最佳答案
在尝试找出自己的情况(未成功)后,我在 GitHub 上询问并得到了以下解决方案( https://github.com/VsixCommunity/Community.VisualStudio.Toolkit/discussions/355 ):
DocumentView docView = await VS.Documents.GetActiveDocumentViewAsync();
docView.TextBuffer.Insert(0, "I'm at the start.");
// Insert at the end of the document.
int end = docView.TextBuffer.CurrentSnapshot.Length;
docView.TextBuffer.Insert(end, "I'm at the end.");
// Inserting at the start of the second line.
ITextSnapshotLine secondLine = docView.TextBuffer.CurrentSnapshot.GetLineFromLineNumber(1);
docView.TextBuffer.Insert(secondLine.Start, "Now I am the second line\n.");
经过测试,有效。
关于visual-studio-extensions - 开发 VS 2022 扩展时如何在 DocumentView 中导航?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72740091/