Skip to content

Commit

Permalink
fix: null refs #136
Browse files Browse the repository at this point in the history
  • Loading branch information
Samir Boulema committed Feb 23, 2022
1 parent 6e0d797 commit 52bc18c
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 215 deletions.
1 change: 0 additions & 1 deletion CodeNav.Shared/CodeNav.Shared.projitems
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@
<Compile Include="$(MSBuildThisFileDirectory)Extensions\CodeItemExtensions.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Helpers\ColorHelper.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Helpers\DocumentHelper.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Helpers\FindHelper.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Helpers\HighlightHelper.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Helpers\HistoryHelper.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Helpers\LanguageHelper.cs" />
Expand Down
39 changes: 0 additions & 39 deletions CodeNav.Shared/Helpers/FindHelper.cs

This file was deleted.

138 changes: 2 additions & 136 deletions CodeNav.Shared/Helpers/HighlightHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,11 @@
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using CodeNav.Extensions;
using CodeNav.Models;
using CodeNav.Models.ViewModels;
using Microsoft.VisualStudio.PlatformUI;
using Microsoft.VisualStudio.Shell;
using Task = System.Threading.Tasks.Task;

namespace CodeNav.Helpers
{
Expand All @@ -37,7 +34,7 @@ public static void HighlightCurrentItem(CodeDocumentViewModel codeDocumentViewMo
try
{
UnHighlight(codeDocumentViewModel);
Highlight(codeDocumentViewModel, lineNumber, backgroundColor).FireAndForget();
Highlight(codeDocumentViewModel, lineNumber, backgroundColor);
}
catch (Exception e)
{
Expand Down Expand Up @@ -79,11 +76,9 @@ public static void UnHighlight(CodeDocumentViewModel codeDocumentViewModel)
/// </remarks>
/// <param name="document">Code document</param>
/// <param name="ids">List of unique code item ids</param>
private static async Task Highlight(CodeDocumentViewModel codeDocumentViewModel,
private static void Highlight(CodeDocumentViewModel codeDocumentViewModel,
int lineNumber, Color backgroundColor)
{
FrameworkElement frameworkElement = null;

var itemsToHighlight = codeDocumentViewModel
.CodeDocument
.Flatten()
Expand All @@ -102,101 +97,7 @@ private static async Task Highlight(CodeDocumentViewModel codeDocumentViewModel,
{
classItem.BorderColor = _borderColor;
}

frameworkElement = await FindItemContainer(item, frameworkElement, codeDocumentViewModel);
}

await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();

frameworkElement?.BringIntoView();

//var smallestCodeItem = itemsToHighlight
// .OrderBy(item => (lineNumber - item?.StartLine) + (item?.EndLine - lineNumber))
// .FirstOrDefault();

//var codeItem = FindCodeItem(codeDocumentViewModel.CodeDocument, smallestCodeItem);

//await BringIntoView(smallestCodeItem);

//await BringIntoView(codeItem);
}

/// <summary>
/// Find frameworkElement belonging to a code item
/// </summary>
/// <remarks>
/// Must be called recursively to gradually move the frameworkElement close to the highlighted code item
/// </remarks>
/// <param name="frameworkElement">FrameworkElement to get the item container from</param>
/// <param name="item">Code item of which whe want to find the container</param>
/// <returns></returns>
private static async Task<FrameworkElement> FindItemContainer(CodeItem item,
FrameworkElement frameworkElement, CodeDocumentViewModel codeDocumentViewModel)
{
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();

if (item.Control == null)
{
return null;
}

if (frameworkElement == null)
{
frameworkElement = GetCodeItemsControl(item.Control);
}

if (frameworkElement == null)
{
return null;
}

var itemContainer = (frameworkElement as ItemsControl).ItemContainerGenerator.ContainerFromItem(item);
var itemContainerSubItemsControl = await FindVisualChild<ItemsControl>(itemContainer);

if (itemContainerSubItemsControl != null)
{
return itemContainerSubItemsControl;
}

if ((itemContainer as ContentPresenter)?.Content == item)
{
return itemContainer as FrameworkElement;
}

return null;
}

/// <summary>
/// Based on a code item find the same code item within the view model
/// </summary>
/// <remarks>
/// We need to find the exact code item in the view model nested list,
/// else we cannot find its WPF UI ItemContainer
/// </remarks>
/// <param name="codeItems">List of code items</param>
/// <param name="codeItem">code item to be found</param>
/// <returns></returns>
private static CodeItem FindCodeItem(IEnumerable<CodeItem> codeItems, CodeItem codeItem)
{
foreach (var item in codeItems)
{
if (item.Id == codeItem.Id)
{
return item;
}

if (item is IMembers hasMembersItem && hasMembersItem.Members.Any())
{
var found = FindCodeItem(hasMembersItem.Members, codeItem);

if (found != null)
{
return found;
}
}
}

return null;
}

/// <summary>
Expand Down Expand Up @@ -236,40 +137,5 @@ public static async Task<Color> GetBackgroundHighlightColor()

return ColorHelper.ToMediaColor(highlightBackgroundColor);
}

public static async Task<T> FindVisualChild<T>(DependencyObject depObj) where T : DependencyObject
{
if (depObj == null)
{
return null;
}

for (var i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
{
var child = VisualTreeHelper.GetChild(depObj, i);
if (child != null && child is T t)
{
return t;
}

var childItem = await FindVisualChild<T>(child);
if (childItem != null)
{
return childItem;
}
}

return null;
}

private static FrameworkElement GetCodeItemsControl(ICodeViewUserControl control)
{
if (control is CodeViewUserControl)
{
return (control as CodeViewUserControl).CodeItemsControl;
}

return (control as CodeViewUserControlTop).CodeItemsControl;
}
}
}
26 changes: 7 additions & 19 deletions CodeNav.Shared/Helpers/HistoryHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,13 @@ public static void AddItemToHistory(CodeDocumentViewModel model, Span span)
{
try
{
var item = FindCodeItem(model.CodeDocument, span);
var item = model
.CodeDocument
.Flatten()
.FilterNull()
.Where(i => !(i is IMembers))
.FirstOrDefault(i => i.Span.Contains(span.Start));

AddItemToHistory(item);
}
catch (Exception e)
Expand Down Expand Up @@ -142,24 +148,6 @@ public static void ClearHistory(CodeItem item)
item.Control.UpdateDocument();
}

private static CodeItem FindCodeItem(IEnumerable<CodeItem> items, Span span)
{
foreach (var item in items)
{
if (item.Span.Contains(span.Start) && !(item is IMembers))
{
return item;
}

if (item is IMembers hasMembersItem)
{
return FindCodeItem(hasMembersItem.Members, span);
}
}

return null;
}

public static async Task<SynchronizedCollection<CodeItem>> LoadHistoryItemsFromStorage(string filePath)
{
var storageItem = await SolutionStorageHelper.GetStorageItem(filePath);
Expand Down
17 changes: 9 additions & 8 deletions CodeNav.Shared/Helpers/LogHelper.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using EnvDTE;
using Community.VisualStudio.Toolkit;
using Microsoft.ApplicationInsights;
using Newtonsoft.Json;
using System;
Expand All @@ -14,7 +14,6 @@ public static class LogHelper
{
private static TelemetryClient _client;
private const string InstrumentationKey = "0913ac4a-1127-4d28-91cf-07673e70200f";
public static _DTE Dte;

public static void GetClient()
{
Expand All @@ -24,11 +23,8 @@ public static void GetClient()
_client.Context.Component.Version = GetExecutingAssemblyVersion().ToString();

var enc = Encoding.UTF8.GetBytes(Environment.UserName + Environment.MachineName);
using (var crypto = new MD5CryptoServiceProvider())
{
var hash = crypto.ComputeHash(enc);
_client.Context.User.Id = Convert.ToBase64String(hash);
}
var hash = new MD5CryptoServiceProvider().ComputeHash(enc);
_client.Context.User.Id = Convert.ToBase64String(hash);
}

public static void Log(string message, Exception exception = null,
Expand All @@ -39,13 +35,18 @@ public static void Log(string message, Exception exception = null,
GetClient();
}

if (_client == null)
{
return;
}

var properties = new Dictionary<string, string>
{
{ "version", GetExecutingAssemblyVersion().ToString() },
{ "message", JsonConvert.SerializeObject(message) },
{ "language", language },
{ "additional", JsonConvert.SerializeObject(additional) },
{ "vsVersion", Dte?.Version ?? string.Empty }
{ "vsVersion", VS.Shell.GetVsVersionAsync().Result?.ToString() ?? string.Empty }
};

if (exception == null)
Expand Down
4 changes: 2 additions & 2 deletions CodeNav.Shared/Helpers/SortHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ private static List<CodeItem> SortByName(List<CodeItem> document)

foreach (var item in document)
{
if (item is CodeClassItem)
if (item is CodeClassItem codeClassItem)
{
(item as CodeClassItem).Members = SortByName((item as CodeClassItem).Members);
codeClassItem.Members = SortByName(codeClassItem.Members);
}
if (item is CodeNamespaceItem)
{
Expand Down
9 changes: 5 additions & 4 deletions CodeNav.Shared/Mappers/NamespaceMapper.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using CodeNav.Helpers;
using CodeNav.Extensions;
using CodeNav.Helpers;
using CodeNav.Models;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;
Expand Down Expand Up @@ -42,7 +43,7 @@ public static CodeNamespaceItem MapNamespace(NamespaceDeclarationSyntax member,
{
foreach (var region in regions)
{
if (FindHelper.FindCodeItem(item.Members, region.Id) == null)
if (item.Members.Flatten().FilterNull().Any(i => i.Id == region.Id) == false)
{
item.Members.Add(region);
}
Expand Down Expand Up @@ -85,7 +86,7 @@ public static CodeNamespaceItem MapNamespace(BaseNamespaceDeclarationSyntax memb
{
foreach (var region in regions)
{
if (FindHelper.FindCodeItem(item.Members, region.Id) == null)
if (item.Members.Flatten().FilterNull().Any(i => i.Id == region.Id) == false)
{
item.Members.Add(region);
}
Expand Down Expand Up @@ -127,7 +128,7 @@ public static CodeNamespaceItem MapNamespace(VisualBasicSyntax.NamespaceBlockSyn
{
foreach (var region in regions)
{
if (FindHelper.FindCodeItem(item.Members, region.Id) == null)
if (item.Members.Flatten().FilterNull().Any(i => i.Id == region.Id) == false)
{
item.Members.Add(region);
}
Expand Down
6 changes: 4 additions & 2 deletions CodeNav.VS2019/CodeNav.VS2019.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
<BootstrapperEnabled>true</BootstrapperEnabled>
<UseCodebase>true</UseCodebase>
<TargetFrameworkProfile />
<LangVersion>8.0</LangVersion>
<Nullable>enable</Nullable>
</PropertyGroup>
<PropertyGroup>
<SignAssembly>false</SignAssembly>
Expand Down Expand Up @@ -143,7 +145,7 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="Community.VisualStudio.Toolkit.15">
<Version>15.0.394</Version>
<Version>15.0.416</Version>
</PackageReference>
<PackageReference Include="Community.VisualStudio.VSCT">
<Version>16.0.29.6</Version>
Expand All @@ -168,7 +170,7 @@
<Version>15.4.27004</Version>
</PackageReference>
<PackageReference Include="Microsoft.VSSDK.BuildTools">
<Version>17.0.5234</Version>
<Version>17.1.4054</Version>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
Expand Down
Loading

0 comments on commit 52bc18c

Please sign in to comment.