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?