stevencohn / OneMore

A OneNote add-in with simple, yet powerful and useful features
Mozilla Public License 2.0
2.65k stars 223 forks source link

Command Palette – Add incremental search (with C# source code) #1680

Open nhwCoder opened 1 day ago

nhwCoder commented 1 day ago

Overview

OneMore provides the great feature "command palette" (thanks for implementing it!).

Every product providing such a command palette (e.g. VSCode, products on Web/macOS/Linux/Windows) implements incremental search instead of strict search for the command palette (have never seen anything different, except OneMore).

Below a description and working C# source code.

PS: Improved & corrected feature request for https://github.com/stevencohn/OneMore/issues/1677

Incremental search - DESCRIPTION

Allows you to find commands even if your input only partially matches the command names Sample entry: ‘Open File Word’ • Match: o • Match: f • Match: w • Match: ofw • Match: open wo • Match: open fi wo etc.

Incremental search - SOURCE CODE (C# source code for NETFramework 4.x console app)

[!NOTE] Hope with this C# source code it is worth the climb :-)

Program.cs

using System;
using System.Collections.Generic;

// EVALUATE: Command palette "incremential search" like in Visual Studio Code
// - Enter characters included in commands = result matches

class Program
{
    static void Main()
    {
        var commands = new List<Command>
        {
            new Command("Open File"),
            new Command("Open File Word"),
            new Command("Open File Excel"),
            new Command("Open File Power Point"),
            new Command("Save File"),
            new Command("Close File"),
            new Command("Exit"),
            new Command("Find"),
            new Command("Replace")
        };

        Console.WriteLine("Enter command:");
        string input = Console.ReadLine();

        var matches = CharacterSearch.GetMatches(input, commands);

        Console.WriteLine("Did you mean:");
        foreach (var match in matches)
        {
            Console.WriteLine(match.Name);
        }
    }
}

Command.cs

public class Command
{
    public string Name { get; set; }
    public Command(string name)
    {
        Name = name;
    }
}

CharacterSearch.cs

using System;
using System.Collections.Generic;
using System.Linq;

public class CharacterSearch
{
    public static List<Command> GetMatches(string input, List<Command> commands)
    {
        return commands
            .Where(command => IsMatch(input, command.Name))
            .ToList();
    }

    private static bool IsMatch(string input, string command)
    {
        int inputIndex = 0;
        foreach (var ch in command)
        {
            if (inputIndex < input.Length && char.ToLower(ch) == char.ToLower(input[inputIndex]))
            {
                inputIndex++;
            }
        }
        return inputIndex == input.Length;
    }
}
stevencohn commented 22 hours ago

Thank you for the code sample

stevencohn commented 13 hours ago

Seems counter-intuitive to me, showing more results than I would expect and have to sift through. TBH, I never really liked it in VSCode either. Just because it's been done doesn't mean it's the right thing to do (for the majority of people). I may include it but make it an option to disable/enable.

image
nhwCoder commented 7 hours ago

Hi Steven

To have an option "Search mode" with "incremental match" vs. "exact match" would be best/great.

BUT its not about like/dislike ==> DE-FACTO every other command palette has this implemented, so to have an option to configure would be great.

-- PS: >TBH, I never really liked it in VSCode either. e.g. in your above example you get same as "exact string match" first and below more...

First I hated this search, step by step I had to understand the inner force. e.g. 'the force be with you']© when you type e.g. "a foo" or "atb" and have a lighting-fast broader match

nhwCoder commented 2 hours ago

BTW: Visual Studio 2022 uses also "incremental search" for their command palette [Ctrl+Shift+P]