Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 7 additions & 9 deletions CSharpCodeAnalyst/App.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System.IO;
using System.Windows;
using System.Windows.Controls;
using CodeParser.Parser;
using CSharpCodeAnalyst.Analyzers;
using CSharpCodeAnalyst.Areas.AdvancedSearchArea;
Expand Down Expand Up @@ -36,10 +35,9 @@ protected override async void OnStartup(StartupEventArgs e)

// Run in UI mode
StartUi();

// Faster debugging
await LoadProjectFileFromCommandLineAsync(e);

}

private async Task LoadProjectFileFromCommandLineAsync(StartupEventArgs e)
Expand All @@ -52,7 +50,7 @@ private async Task LoadProjectFileFromCommandLineAsync(StartupEventArgs e)
{
return;
}

// Allow loading a project file (json) via command line for faster debugging
if (MainWindow?.DataContext is MainViewModel dc)
{
Expand All @@ -70,8 +68,8 @@ private void StartUi()
// ToolTipService.BetweenShowDelayProperty.OverrideMetadata(
// typeof(DependencyObject),
// new FrameworkPropertyMetadata(delayMs));


try
{
Initializer.InitializeMsBuildLocator();
Expand Down Expand Up @@ -113,7 +111,7 @@ private void StartUi()
var viewModel = new MainViewModel(messaging, applicationSettings, userSettings, analyzerManager, refactoringService);
var graphViewModel = new GraphViewModel(explorationGraphViewer, explorer, messaging, applicationSettings, refactoringService);
var treeViewModel = new TreeViewModel(messaging, refactoringService);
var searchViewModel = new AdvancedSearchViewModel(messaging);
var searchViewModel = new AdvancedSearchViewModel(messaging, refactoringService);
var infoPanelViewModel = new InfoPanelViewModel();

viewModel.InfoPanelViewModel = infoPanelViewModel;
Expand All @@ -130,13 +128,13 @@ private void StartUi()
messaging.Subscribe<ShowPartitionsRequest>(viewModel.HandleShowPartitionsRequest);
messaging.Subscribe<ShowCycleGroupRequest>(viewModel.HandleShowCycleGroupRequest);


// Refactorings are forwarded to all other view models
messaging.Subscribe<CodeGraphRefactored>(viewModel.HandleCodeGraphRefactored);
// messaging.Subscribe<CodeElementsMoved>(viewModel.HandleCodeGraphRefactored);
// messaging.Subscribe<CodeElementsDeleted>(viewModel.HandleCodeGraphRefactored);
// messaging.Subscribe<CodeElementCreated>(viewModel.HandleCodeGraphRefactored);


mainWindow.DataContext = viewModel;
MainWindow = mainWindow;
Expand Down
307 changes: 160 additions & 147 deletions CSharpCodeAnalyst/Areas/AdvancedSearchArea/AdvancedSearchControl.xaml

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Input;
using CSharpCodeAnalyst.Resources;

namespace CSharpCodeAnalyst.Areas.AdvancedSearchArea;

Expand All @@ -11,7 +12,7 @@ public AdvancedSearchControl()
{
InitializeComponent();
}

private void DropdownButton_Click(object sender, RoutedEventArgs e)
{
if (sender is Button { ContextMenu: not null } button)
Expand All @@ -26,11 +27,21 @@ private void SearchDataGrid_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.A && (Keyboard.Modifiers & ModifierKeys.Control) == ModifierKeys.Control)
{
if (sender is DataGrid {DataContext: AdvancedSearchViewModel viewModel})
if (sender is DataGrid { DataContext: AdvancedSearchViewModel viewModel })
{
e.Handled = true;
viewModel.SelectAllCommand.Execute(null);
}
}
}

private void SearchDataGrid_OnContextMenuOpening(object sender, ContextMenuEventArgs e)
{
// Update the refactoring movement parent
if (SearchDataGrid.DataContext is AdvancedSearchViewModel vm)
{
var parent = vm.GetRefactoringNewMoveParent();
MenuRefactoringMove.Header = string.IsNullOrEmpty(parent) ? Strings.Refactor_MoveSelectedCodeElements : string.Format(Strings.Refactor_MoveSelectedCodeElementTo, parent);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System.Collections;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows;
Expand All @@ -7,22 +6,25 @@
using Contracts.Graph;
using CSharpCodeAnalyst.Common;
using CSharpCodeAnalyst.Messages;
using CSharpCodeAnalyst.Refactoring;
using CSharpCodeAnalyst.Wpf;

namespace CSharpCodeAnalyst.Areas.AdvancedSearchArea;

public sealed class AdvancedSearchViewModel : INotifyPropertyChanged
{
private readonly MessageBus _messaging;
private readonly RefactoringService _refactoringService;
private readonly DispatcherTimer _searchTimer;
private ObservableCollection<SearchItemViewModel> _allItems;
private CodeGraph? _codeGraph;
private ObservableCollection<SearchItemViewModel> _filteredItems;
private string _searchText;

public AdvancedSearchViewModel(MessageBus messaging)
public AdvancedSearchViewModel(MessageBus messaging, RefactoringService refactoringService)
{
_messaging = messaging;
_refactoringService = refactoringService;
_searchText = string.Empty;
_allItems = [];
_filteredItems = [];
Expand All @@ -45,8 +47,12 @@ public AdvancedSearchViewModel(MessageBus messaging)
CopyToClipboardCommand = new WpfCommand<SearchItemViewModel>(OnCopyToClipboard);
SelectAllCommand = new WpfCommand(SelectAll);
DeselectAllCommand = new WpfCommand(DeselectAll);

SetMovementTargetCommand = new WpfCommand<SearchItemViewModel>(RefactoringSetMovementTarget, RefactoringCanSetMovementTarget);
MoveSelectedCommand = new WpfCommand(RefactoringMoveCodeElement, RefactoringCanMoveCodeElement);
}


public ObservableCollection<SearchItemViewModel> AllItems
{
get => _allItems;
Expand Down Expand Up @@ -87,9 +93,43 @@ public string SearchText
public ICommand CopyToClipboardCommand { get; }
public ICommand SelectAllCommand { get; }
public ICommand DeselectAllCommand { get; }
public ICommand SetMovementTargetCommand { get; }
public ICommand MoveSelectedCommand { get; }

public event PropertyChangedEventHandler? PropertyChanged;

private bool RefactoringCanSetMovementTarget(SearchItemViewModel vm)
{
return _refactoringService.CanSetMovementTarget(vm.CodeElement?.Id);
}

private bool RefactoringCanMoveCodeElement()
{
var ids = GetSelectedCodeElements().Select(e => e.Id).ToHashSet();
return _refactoringService.CanMoveCodeElements(ids);
}

private void RefactoringMoveCodeElement()
{
var elementIds = GetSelectedCodeElements().Select(e => e.Id).ToHashSet();
if (elementIds.Any())
{
_refactoringService.MoveCodeElements(elementIds);
}
}

public string GetRefactoringNewMoveParent()
{
var target = _refactoringService.GetMovementTarget();
return target?.Name != null ? target.Name : string.Empty;
}


private void RefactoringSetMovementTarget(SearchItemViewModel vm)
{
_refactoringService.SetMovementTarget(vm.CodeElement?.Id);
}

private void OnCopyToClipboard(SearchItemViewModel item)
{
var text = item.CodeElement?.FullName;
Expand Down Expand Up @@ -214,8 +254,8 @@ private void AddSelectedToGraphInternal(bool addCollapsed)
}

/// <summary>
/// Gets selected code elements from all items, including the
/// currently non-visible.
/// Gets selected code elements from all items, including the
/// currently non-visible.
/// </summary>
private List<CodeElement> GetSelectedCodeElements()
{
Expand Down
20 changes: 13 additions & 7 deletions CSharpCodeAnalyst/Areas/GraphArea/GraphViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ internal sealed class GraphViewModel : INotifyPropertyChanged
private readonly ApplicationSettings _settings;
private readonly LinkedList<GraphSession> _undoStack;
private readonly IGraphViewer _viewer;
private readonly GraphDropHandler _dropHandler;

private HighlightOption _selectedHighlightOption;
private RenderOption _selectedRenderOption;
Expand All @@ -47,7 +46,7 @@ internal GraphViewModel(IGraphViewer viewer, ICodeGraphExplorer explorer, IPubli
_publisher = publisher;
_settings = settings;
_refactoringService = refactoringService;
_dropHandler = new GraphDropHandler(publisher);
DropHandler = new GraphDropHandler(publisher);

// Initialize RenderOptions
RenderOptions =
Expand Down Expand Up @@ -277,7 +276,7 @@ public HighlightOption SelectedHighlightOption

public ICommand AddParentsCommand { get; }

public GraphDropHandler DropHandler => _dropHandler;
public GraphDropHandler DropHandler { get; }

public event PropertyChangedEventHandler? PropertyChanged;

Expand Down Expand Up @@ -474,7 +473,7 @@ private void OnAddParents()
{
elementIds = _viewer.GetGraph().GetRoots().Select(r => r.Id).ToList();
}

if (elementIds.Any())
{
AddParents(elementIds);
Expand Down Expand Up @@ -591,7 +590,7 @@ private void AddToGraph(IEnumerable<CodeElement> originalCodeElements, IEnumerab
// Don't trigger undo
return;
}

PushUndo();

// Apply "Automatically add containing type" setting
Expand Down Expand Up @@ -828,6 +827,7 @@ public void HandleCodeGraphRefactored(CodeGraphRefactored message)
}
else if (message is CodeElementsMoved moved)
{
// TODO May be sufficient to just check the source ids and not their children.
// Add the same node ids with the same relationships. This fixes parent/child hierarchy.
// We may have moved more nodes than in the graph. Or the graph is not affected at all by this movement.

Expand All @@ -836,13 +836,19 @@ public void HandleCodeGraphRefactored(CodeGraphRefactored message)

// Is the canvas graph affected at all?
var originalGraph = moved.Graph;
var movedIds = originalGraph.Nodes[moved.SourceId].GetChildrenIncludingSelf().ToHashSet();
var movedIds = new HashSet<string>();
foreach (var element in moved.SourceIds.Select(movedId => originalGraph.Nodes[movedId]))
{
movedIds.UnionWith(element.GetChildrenIncludingSelf());
}

if (!movedIds.Intersect(ids).Any())
{
// None of the moved ids is in the graph
return;
}

// I don't know where the element was moved to. I add its parent.
// Add the new parent to ensure the moved elements are visible with correct hierarchy
// Since I cant move an assembly parent is never null
ids.Add(moved.NewParentId);

Expand Down
22 changes: 18 additions & 4 deletions CSharpCodeAnalyst/Areas/TreeArea/TreeViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,19 @@ private void OnSelectedItemChanged(TreeItemViewModel item)

private bool RefactoringCanMoveCodeElement(TreeItemViewModel tvm)
{
return _refactoringService.CanMoveCodeElement(tvm?.CodeElement?.Id);
var id = tvm?.CodeElement?.Id;
return id != null && _refactoringService.CanMoveCodeElements([id]);
}

private void RefactoringMoveCodeElement(TreeItemViewModel? tvm)
{
_refactoringService.MoveCodeElement(tvm?.CodeElement?.Id);
var id = tvm?.CodeElement?.Id;
if (id == null)
{
return;
}

_refactoringService.MoveCodeElements([id]);
}

private bool RefactoringCanSetMovementTarget(TreeItemViewModel tvm)
Expand All @@ -119,7 +126,7 @@ private void RefactoringSetMovementTarget(TreeItemViewModel tvm)

private static void OnCopyToClipboard(TreeItemViewModel vm)
{
var text = vm?.CodeElement?.FullName;
var text = vm.CodeElement?.FullName;
if (string.IsNullOrEmpty(text))
{
return;
Expand Down Expand Up @@ -192,7 +199,14 @@ public void HandleCodeGraphRefactored(CodeGraphRefactored message)
{
// This may be slow but easy.
LoadCodeGraph(moved.Graph);
_messaging.Publish(new LocateInTreeRequest(moved.SourceId));
if (moved.SourceIds.Count == 1)
{
_messaging.Publish(new LocateInTreeRequest(moved.SourceIds.Single()));
}
else
{
_messaging.Publish(new LocateInTreeRequest(moved.NewParentId));
}
}
else if (message is RelationshipsDeleted relationshipsDeleted)
{
Expand Down
6 changes: 2 additions & 4 deletions CSharpCodeAnalyst/Messages/CodeGraphRefactored.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,10 @@ internal class CodeElementsDeleted(CodeGraph codeGraph, string deletedElementId,
internal class RelationshipsDeleted(CodeGraph codeGraph, List<Relationship> deleted) : CodeGraphRefactored(codeGraph)
{
public List<Relationship> Deleted { get; } = deleted;

}

internal class CodeElementsMoved(CodeGraph codeGraph, string sourceId, string oldParentId, string newParentId) : CodeGraphRefactored(codeGraph)
internal class CodeElementsMoved(CodeGraph codeGraph, HashSet<string> sourceIds, string newParentId) : CodeGraphRefactored(codeGraph)
{
public string SourceId { get; } = sourceId;
public string OldParentId { get; } = oldParentId;
public HashSet<string> SourceIds { get; } = sourceIds;
public string NewParentId { get; } = newParentId;
}
Loading