-
Notifications
You must be signed in to change notification settings - Fork 3
/
Main.cs
196 lines (179 loc) · 7.31 KB
/
Main.cs
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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace Flow.Launcher.Plugin.VisualStudio
{
public class Main : IAsyncPlugin, IContextMenu, ISettingProvider, IAsyncReloadable
{
public PluginInitContext context;
private VisualStudioPlugin plugin;
private static readonly TypeKeyword ProjectsOnly = new(0, "p:");
private static readonly TypeKeyword FilesOnly = new(1, "f:");
private Settings settings;
private IconProvider iconProvider;
private Dictionary<Entry, List<int>> entryHighlightData;
public async Task InitAsync(PluginInitContext context)
{
this.context = context;
settings = context.API.LoadSettingJsonStorage<Settings>();
context.API.VisibilityChanged += OnVisibilityChanged;
iconProvider = new IconProvider(context);
plugin = await VisualStudioPlugin.Create(settings, context, iconProvider);
entryHighlightData = new Dictionary<Entry, List<int>>();
}
private void OnVisibilityChanged(object sender, VisibilityChangedEventArgs args)
{
if (context.CurrentPluginMetadata.Disabled)
return;
if (args.IsVisible)
{
Task.Run(async () => await plugin.GetRecentEntries());
}
}
public async Task ReloadDataAsync()
{
await plugin.GetVisualStudioInstances();
iconProvider.ReloadIcons();
}
public async Task<List<Result>> QueryAsync(Query query, CancellationToken token)
{
if (token.IsCancellationRequested)
{
return null;
}
if (!plugin.IsVSInstalled)
{
return SingleResult("No installed version of Visual Studio was found");
}
if(query.IsReQuery)
{
await plugin.GetRecentEntries(token);
}
if (!plugin.RecentEntries.Any())
{
return SingleResult("No recent items found");
}
entryHighlightData.Clear();
var selectedRecentItems = query.Search switch
{
string search when string.IsNullOrEmpty(search) => plugin.RecentEntries,
string search when search.StartsWith(ProjectsOnly.Keyword) => plugin.RecentEntries.Where(e => TypeSearch(e, query, ProjectsOnly)),
string search when search.StartsWith(FilesOnly.Keyword) => plugin.RecentEntries.Where(e => TypeSearch(e, query, FilesOnly)),
_ => plugin.RecentEntries.Where(e => FuzzySearch(e, query.Search))
};
return selectedRecentItems.OrderBy(e => e.Value.LastAccessed)
.Select(CreateEntryResult)
.ToList();
}
public List<Result> LoadContextMenus(Result selectedResult)
{
if (selectedResult.ContextData is Entry currentEntry)
{
return plugin.VSInstances.Select(vs =>
{
return new Result
{
Title = $"Open in \"{vs.DisplayName}\" [Version: {vs.DisplayVersion}]",
SubTitle = vs.ExePath,
IcoPath = iconProvider.GetIconPath(vs),
Action = c =>
{
context.API.ShellRun($"\"{currentEntry.Path}\"", $"\"{vs.ExePath}\"");
return true;
}
};
}).Append(new Result
{
Title = $"Remove \"{selectedResult.Title}\" from recent items list.",
SubTitle = selectedResult.SubTitle,
IcoPath = IconProvider.Remove,
AsyncAction = async c =>
{
await plugin.RemoveEntry(currentEntry);
await Task.Delay(100);
context.API.ChangeQuery(context.CurrentPluginMetadata.ActionKeyword, false);
return true;
}
}).Append(new Result
{
Title = $"Open in File Explorer",
SubTitle = currentEntry.Path,
IcoPath = IconProvider.Folder,
Action = c =>
{
context.API.OpenDirectory(Path.GetDirectoryName(currentEntry.Path), currentEntry.Path);
return true;
}
})
.ToList();
}
return null;
}
private static List<Result> SingleResult(string title)
{
return new List<Result>
{
new Result
{
Title = title,
IcoPath = IconProvider.DefaultIcon,
}
};
}
private Result CreateEntryResult(Entry e)
{
string iconPath = IconProvider.DefaultIcon;
Action action = () => context.API.ShellRun($"\"{e.Path}\"");
if (!string.IsNullOrWhiteSpace(settings.DefaultVSId))
{
var instance = plugin.VSInstances.FirstOrDefault(i => i.InstanceId == settings.DefaultVSId);
if (instance != null)
{
//iconPath = iconProvider.GetIconPath(instance);
action = () => context.API.ShellRun($"\"{e.Path}\"", $"\"{instance.ExePath}\"");
}
}
entryHighlightData.TryGetValue(e, out var highlightData);
return new Result
{
Title = Path.GetFileNameWithoutExtension(e.Path),
TitleHighlightData = highlightData,
SubTitle = e.Value.IsFavorite ? $"★ {e.Path}" : e.Path,
SubTitleToolTip = $"{e.Path}\n\nLast Accessed:\t{e.Value.LastAccessed:F}",
ContextData = e,
IcoPath = iconPath,
Action = c =>
{
action();
return true;
}
};
}
private bool FuzzySearch(Entry entry, string search)
{
var matchResult = context.API.FuzzySearch(search, Path.GetFileNameWithoutExtension(entry.Path));
entryHighlightData[entry] = matchResult.MatchData;
return matchResult.IsSearchPrecisionScoreMet();
}
private bool TypeSearch(Entry entry, Query query, TypeKeyword typeKeyword)
{
var search = query.Search[typeKeyword.Keyword.Length..];
if (string.IsNullOrWhiteSpace(search))
{
return entry.ItemType == typeKeyword.Type;
}
else
{
return entry.ItemType == typeKeyword.Type && FuzzySearch(entry, search);
}
}
public System.Windows.Controls.Control CreateSettingPanel()
{
return new UI.SettingsView(new UI.SettingsViewModel(settings, plugin, iconProvider, this));
}
public record struct TypeKeyword(int Type, string Keyword);
}
}