Hello,
I would like to implement Go To Definition functionality.
I tried to implement it by referring to this forum, but it doesn't work.
Go To Definition functionality
My code is below
private void OnKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.F12)
{
this.MoveCaretToDefinition();
}
}
private void MoveCaretToDefinition()
{
// CSharp editor is SyntaxEditor class
var snapshotOffset = this.CSharpEditor.ActiveView.Selection.EndSnapshotOffset;
var context = new CSharpContextFactory().CreateContext(snapshotOffset);
var results = context?.Resolve();
if (results == null || results.Results.Count == 0) return;
var result = results.Results[0];
var type = result?.Type;
var def = type as ITypeDefinition;
var locationCollection = def?.SourceFileLocations;
if (locationCollection == null || locationCollection.Count == 0) return;
var offset = locationCollection[0].NavigationOffset;
if (offset.HasValue)
{
this.CSharpEditor.ActiveView.Selection.CaretOffset = offset.Value;
}
}
By this code, when selecting `this` identifier and press F12 in the tareget C# code, it was possible to move caret to the class declaration.
Otherwise, it does not work.
For example, if select a member defined in the class, the type cannot cast to ITypeDefinition and def is null.
Also, if argument in the method, cast is ok but the locationCollection is empty collection.
Thanks.