根据Visual Studio中的行和列来获取当前打开的文件的角度位置

发布于 2025-02-12 13:35:22 字数 888 浏览 0 评论 0原文

我正在在Visual Studio中创建一个扩展名,该扩展名应该在当前打开的文件中给出Caret的当前行和当前列。

using System.Linq;
using Microsoft.VisualStudio.Text;

namespace TestProject
{
    [Command(PackageIds.MyCommand)]
    internal sealed class MyCommand : BaseCommand<MyCommand>
    {
        protected override async Task ExecuteAsync(OleMenuCmdEventArgs e)
        {
            var docView = await VS.Documents.GetActiveDocumentViewAsync();
            SnapshotPoint position = docView.TextView.Caret.Position.BufferPosition;
            var selection = docView?.TextView.Selection.SelectedSpans.FirstOrDefault();
            if (selection.HasValue)
            {
                var guid = Guid.NewGuid().ToString();   
                docView.TextBuffer.Replace(selection.Value, position.ToString());
            }
        }
    }
}

该程序以字符打印位置(例如Caret当前在POS 15),但在行和列方面却没有。
有帮助吗?

I am creating an extension in Visual Studio that should give the current row and current column of the caret in the currently opened file.

using System.Linq;
using Microsoft.VisualStudio.Text;

namespace TestProject
{
    [Command(PackageIds.MyCommand)]
    internal sealed class MyCommand : BaseCommand<MyCommand>
    {
        protected override async Task ExecuteAsync(OleMenuCmdEventArgs e)
        {
            var docView = await VS.Documents.GetActiveDocumentViewAsync();
            SnapshotPoint position = docView.TextView.Caret.Position.BufferPosition;
            var selection = docView?.TextView.Selection.SelectedSpans.FirstOrDefault();
            if (selection.HasValue)
            {
                var guid = Guid.NewGuid().ToString();   
                docView.TextBuffer.Replace(selection.Value, position.ToString());
            }
        }
    }
}

This program prints the position in terms of character ( Like the caret is currently at pos 15) but not in terms of row and columns.
Any help?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文