Skip to content

Commit

Permalink
refactor(main): slight improvements to query handling code
Browse files Browse the repository at this point in the history
  • Loading branch information
nathancartlidge committed May 3, 2024
1 parent 26d76a6 commit 6412637
Showing 1 changed file with 20 additions and 25 deletions.
45 changes: 20 additions & 25 deletions src/Community.PowerToys.Run.Plugin.UnicodeInput/Main.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
using ManagedCommon;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Wox.Plugin;

namespace Community.PowerToys.Run.Plugin.UnicodeInput
{
public class Main : IPlugin
{
private string IconPath { get; set; }
private AgdaLookup _lookup = new AgdaLookup();
private readonly AgdaLookup _lookup = new AgdaLookup();

private PluginInitContext Context { get; set; }
public string Name => "Unicode Input";

public string Description => "Agda-style Unicode Input";
public int MaxResults => 8;
private static int MaxResults => 8;

public static string PluginID => "778f24fc48714097b30303f83d5bed6a";

Expand All @@ -32,8 +29,9 @@ private Result MakeResult(string prefix, List<string> choices, List<char> nextCh
Title = prefix,
SubTitle = "No match found yet - keep typing! " + _arrayToString(nextChar),
IcoPath = IconPath,
Score = nextChar.Count == 0 ? score - 5 : score - 1,
Action = e => false,
// if there is only one possible letter to be typed, this could easily get in the way
Score = nextChar.Count <= 1 ? score - 2 : score - 1,
Action = _ => false,
};
}

Expand Down Expand Up @@ -62,7 +60,7 @@ private Result MakeResult(string prefix, List<string> choices, List<char> nextCh
SubTitle = subtitleStringBuilder.ToString(),
IcoPath = IconPath,
Score = score,
Action = e =>
Action = _ =>
{
Clipboard.SetText(choices[0]);
return true;
Expand Down Expand Up @@ -97,25 +95,22 @@ private static string _subscriptNumber(int i)
}
return output.ToString();
}

public List<Result> Query(Query query)
{
// Fetch the non-keyword part of the query
string q;
if (string.IsNullOrEmpty(query.ActionKeyword))
{
q = query.RawQuery.Trim();
}
else
{
q = query.RawQuery[query.ActionKeyword.Length..].Trim();
}

// Clean up the raw query by discarding the keyword and trimming
return Query(string.IsNullOrEmpty(query.ActionKeyword)
? query.RawQuery.Trim() // no keyword - just trim
: query.RawQuery[query.ActionKeyword.Length..].Trim()); // keyword - remove it, then trim
}

private List<Result> Query(string query)
{
List<Result> results = [];

// Exact matching - agda has a key, we provide that key
var exactMatches = AgdaLookup.ExactMatches(q);
(List<char> validChars, List<string> partialMatches) = _lookup.PartialMatch(q);
var exactMatches = AgdaLookup.ExactMatches(query);
var (validChars, partialMatches) = _lookup.PartialMatch(query);

// In the case where we have nothing useful to add (e == 0 and p == 0), we should avoid polluting the list
// of results (e == 0 and p == 0)
Expand All @@ -127,7 +122,7 @@ public List<Result> Query(Query query)
{
results.Add(
item: MakeResult(
prefix: q,
prefix: query,
choices: exactMatches,
nextChar: validChars,
score: 0
Expand All @@ -136,7 +131,7 @@ public List<Result> Query(Query query)
}

// Number-indexed matching support
var (k, i, numberMatch) = _lookup.NumberMatch(q);
var (k, i, numberMatch) = _lookup.NumberMatch(query);
if (numberMatch != null)
{
results.Add(
Expand Down Expand Up @@ -187,7 +182,7 @@ public List<Result> Query(Query query)
else if (exactMatches.Count > 1)
{
options = exactMatches[1..];
searchKey = q;
searchKey = query;
jStart = 1;
}
else return results;
Expand Down

0 comments on commit 6412637

Please sign in to comment.