-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSearchVisualStudioCommandsWPF.xaml.cs
More file actions
200 lines (170 loc) · 7.46 KB
/
SearchVisualStudioCommandsWPF.xaml.cs
File metadata and controls
200 lines (170 loc) · 7.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Threading;
using NaturalCommands.Helpers;
namespace NaturalCommands
{
public partial class SearchVisualStudioCommandsWPF : Window
{
private List<CommandViewModel> _allCommands = new List<CommandViewModel>();
private ICollectionView? _view;
private DispatcherTimer? _searchDebounceTimer;
private string _pendingFilterText = string.Empty;
public SearchVisualStudioCommandsWPF()
{
InitializeComponent();
// Setup debounce timer so search doesn't run on every keystroke/dictation update
_searchDebounceTimer = new DispatcherTimer {
Interval = TimeSpan.FromMilliseconds(300)
};
_searchDebounceTimer.Tick += SearchDebounceTimer_Tick;
LoadCommands();
}
private void LoadCommands()
{
string commandsPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "vs_commands.json");
if (!File.Exists(commandsPath))
{
// Try looking up three levels (project root during dev)
string devPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "..", "..", "..", "vs_commands.json");
if (File.Exists(devPath))
{
commandsPath = devPath;
}
}
VisualStudioCommandLoader.LoadCommands(commandsPath);
var commands = VisualStudioCommandLoader.GetCommands();
_allCommands = commands.Select(c => new CommandViewModel(c)).ToList();
ResultsList.ItemsSource = _allCommands;
_view = CollectionViewSource.GetDefaultView(ResultsList.ItemsSource);
_view.GroupDescriptions.Add(new PropertyGroupDescription("Category"));
_view.SortDescriptions.Add(new System.ComponentModel.SortDescription("Category", System.ComponentModel.ListSortDirection.Ascending));
_view.SortDescriptions.Add(new System.ComponentModel.SortDescription("Name", System.ComponentModel.ListSortDirection.Ascending));
// Initial filter: show nothing by default until the user types a search
_view.Filter = item => false;
}
private void SearchBox_TextChanged(object sender, TextChangedEventArgs e)
{
// Use debounce: store latest text and restart timer
if (_view == null) return;
_pendingFilterText = SearchBox.Text.Trim();
if (_searchDebounceTimer != null)
{
// restart timer on every input so the actual filtering only occurs
// after the user pauses typing/dictation for the interval
_searchDebounceTimer.Stop();
_searchDebounceTimer.Start();
}
}
private void SearchDebounceTimer_Tick(object? sender, EventArgs e)
{
if (_searchDebounceTimer != null)
{
_searchDebounceTimer.Stop();
}
if (_view == null) return;
string filterText = _pendingFilterText;
if (string.IsNullOrWhiteSpace(filterText))
{
// No search text => show nothing
_view.Filter = item => false;
}
else
{
_view.Filter = item =>
{
if (item is CommandViewModel cmd)
{
return cmd.Name.Contains(filterText, StringComparison.OrdinalIgnoreCase) ||
cmd.NaturalLanguageExample.Contains(filterText, StringComparison.OrdinalIgnoreCase);
}
return false;
};
}
// Refresh view so changes take effect
_view.Refresh();
}
private void CopyCommand_Click(object sender, RoutedEventArgs e)
{
if (ResultsList.SelectedItem is CommandViewModel selected)
{
System.Windows.Clipboard.SetText(selected.NaturalLanguageExample);
System.Windows.MessageBox.Show($"Copied to clipboard: \"{selected.NaturalLanguageExample}\"", "Copied", MessageBoxButton.OK, MessageBoxImage.Information);
}
else
{
System.Windows.MessageBox.Show("Please select a command to copy.", "No Selection", MessageBoxButton.OK, MessageBoxImage.Warning);
}
}
private void OpenFileLocation_Click(object sender, RoutedEventArgs e)
{
string commandsPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "vs_commands.json");
if (File.Exists(commandsPath))
{
Process.Start("explorer.exe", "/select,\"" + commandsPath + "\"");
}
else
{
System.Windows.MessageBox.Show("vs_commands.json not found.", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
}
public class CommandViewModel
{
public VisualStudioCommandInfo OriginalCommand { get; }
public string Name => OriginalCommand.Name;
public string Category { get; }
public string NaturalLanguageExample { get; }
public string BindingsDisplay { get; }
public bool HasBindings => OriginalCommand.Bindings != null && OriginalCommand.Bindings.Count > 0;
public CommandViewModel(VisualStudioCommandInfo command)
{
OriginalCommand = command;
// Extract Category (e.g., "Edit.Copy" -> "Edit")
int dotIndex = command.Name.IndexOf('.');
Category = dotIndex > 0 ? command.Name.Substring(0, dotIndex) : "General";
// Generate Natural Language Example
// "Edit.Copy" -> "Edit Copy"
// "Window.ApplyWindowLayout1" -> "Window Apply Window Layout 1"
// Split by dot and camel case
NaturalLanguageExample = GenerateNaturalLanguage(command.Name);
BindingsDisplay = command.Bindings != null ? string.Join(", ", command.Bindings) : "";
}
private string GenerateNaturalLanguage(string name)
{
// Replace dots with spaces
string text = name.Replace(".", " ");
// Insert spaces before capital letters (simple version)
// This is a basic heuristic.
// "ApplyWindowLayout" -> "Apply Window Layout"
// Better approach:
// 1. Split by dot.
// 2. For each part, split by camel case.
var parts = name.Split('.');
var naturalParts = parts.Select(SplitCamelCase);
return string.Join(" ", naturalParts);
}
private string SplitCamelCase(string input)
{
if (string.IsNullOrEmpty(input)) return input;
var result = new System.Text.StringBuilder();
result.Append(input[0]);
for (int i = 1; i < input.Length; i++)
{
if (char.IsUpper(input[i]) && !char.IsUpper(input[i - 1]))
{
result.Append(' ');
}
result.Append(input[i]);
}
return result.ToString();
}
}
}