Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
nhwCoder opened this issue Nov 22, 2024 · 4 comments
Open

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

nhwCoder opened this issue Nov 22, 2024 · 4 comments
Labels
feature-request New feature or request

Comments

@nhwCoder
Copy link

nhwCoder commented Nov 22, 2024

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 #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
Copy link
Owner

Thank you for the code sample

@stevencohn stevencohn added the feature-request New feature or request label Nov 22, 2024
@stevencohn
Copy link
Owner

stevencohn commented Nov 22, 2024

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
Copy link
Author

nhwCoder commented Nov 22, 2024

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
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature-request New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants