Repository navigation
feat(runtime): rank exact matches above higher priority suggestions #511
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
Open
1ilsang (1ilsang)
wants to merge
2
commits into
microsoft:main
Choose a base branch
from
1ilsang:feat/rank-suggestions-by-match-relevance
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| import { getArgDrivenRecommendation, getSubcommandDrivenRecommendation } from "../../runtime/suggestion.js"; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe instead of adding this suggestion.test.ts, we can just add some cases to testData in runtime.test.ts to cover it? |
||
| import { CommandToken } from "../../runtime/parser.js"; | ||
| import { Shell } from "../../utils/shell.js"; | ||
|
|
||
| // The runtime flags any token containing a path separator as a path (see resolveCwd), so branch names | ||
| // like feat/foo reach the suggestion ranking as path tokens. | ||
| const token = (value: string): CommandToken => ({ | ||
| token: value, | ||
| tokenLength: value.length, | ||
| complete: false, | ||
| isOption: false, | ||
| ...(value.includes("/") ? { isPath: true, isPathComplete: value.endsWith("/") } : {}), | ||
| }); | ||
|
|
||
| type Rank = (suggestions: Fig.Suggestion[], typed: string, filterStrategy?: Fig.Arg["filterStrategy"]) => Promise<string[]>; | ||
|
|
||
| // Completing an argument right after a subcommand, e.g. `git checkout feat/foo`. | ||
| const rankFromSubcommand: Rank = async (suggestions, typed, filterStrategy) => { | ||
| const partialToken = token(typed); | ||
| const blob = await getSubcommandDrivenRecommendation( | ||
| { name: "checkout", args: { name: "branch", suggestions, filterStrategy } }, | ||
| [], | ||
| partialToken, | ||
| false, | ||
| false, | ||
| [], | ||
| [partialToken], | ||
| process.cwd(), | ||
| Shell.Bash, | ||
| ); | ||
| return blob?.suggestions.map((s) => s.name) ?? []; | ||
| }; | ||
|
|
||
| // Completing an argument slot reached after other tokens, e.g. an option's value or a variadic argument. | ||
| const rankFromArg: Rank = async (suggestions, typed, filterStrategy) => { | ||
| const partialToken = token(typed); | ||
| const blob = await getArgDrivenRecommendation( | ||
| [{ name: "branch", suggestions, filterStrategy }], | ||
| { name: "checkout" }, | ||
| [], | ||
| partialToken, | ||
| [], | ||
| [partialToken], | ||
| false, | ||
| process.cwd(), | ||
| Shell.Bash, | ||
| ); | ||
| return blob?.suggestions.map((s) => s.name) ?? []; | ||
| }; | ||
|
|
||
| // Mirrors the git spec, which gives the current branch priority 100 and other branches priority 75, and | ||
| // filters branch arguments fuzzily (path tokens are filtered by their basename, e.g. search-). | ||
| const branches: Fig.Suggestion[] = [ | ||
| { name: "feat/search-1234", description: "Current branch", priority: 100 }, | ||
| { name: "feat/search-", description: "Branch", priority: 75 }, | ||
| { name: "fix/search-5678", description: "Branch", priority: 75 }, | ||
| ]; | ||
|
|
||
| describe.each([ | ||
| ["getSubcommandDrivenRecommendation", rankFromSubcommand], | ||
| ["getArgDrivenRecommendation", rankFromArg], | ||
| ])("%s ranking", (_, rank) => { | ||
| const rankBranches = (typed: string) => rank(branches, typed, "fuzzy"); | ||
|
|
||
| test("ranks an exact branch name above the higher priority current branch", async () => { | ||
| expect(await rankBranches("feat/search-")).toEqual(["feat/search-", "feat/search-1234", "fix/search-5678"]); | ||
| }); | ||
|
|
||
| test("matches exactly case-sensitively", async () => { | ||
| const options: Fig.Suggestion[] = [ | ||
| { name: "-L", priority: 100 }, | ||
| { name: "-l", priority: 50 }, | ||
| ]; | ||
| expect(await rank(options, "-l")).toEqual(["-l", "-L"]); | ||
| }); | ||
|
|
||
| test("matches exactly on any of a suggestion's names", async () => { | ||
| const suggestions: Fig.Suggestion[] = [ | ||
| { name: "statistics", priority: 100 }, | ||
| { name: ["stat", "status"], priority: 50 }, | ||
| ]; | ||
| expect((await rank(suggestions, "stat"))[0]).toBe("stat"); | ||
| }); | ||
|
|
||
| test("keeps priority order when nothing matches exactly", async () => { | ||
| expect(await rankBranches("feat/s")).toEqual(["feat/search-1234", "feat/search-", "fix/search-5678"]); | ||
| }); | ||
|
|
||
| test("does not match a branch that only shares the typed basename", async () => { | ||
| const withBasenameBranch: Fig.Suggestion[] = [{ name: "search-", priority: 75 }, ...branches]; | ||
| expect(await rank(withBasenameBranch, "feat/search-", "fuzzy")).toEqual(["feat/search-", "feat/search-1234", "search-", "fix/search-5678"]); | ||
| }); | ||
|
|
||
| test("matches files by basename and keeps an exact file above a higher priority file", async () => { | ||
| const files: Fig.Suggestion[] = [ | ||
| { name: "index.ts.bak", type: "file", priority: 80 }, | ||
| { name: "index.ts", type: "file", priority: 50 }, | ||
| ]; | ||
| expect(await rank(files, "src/index.ts")).toEqual(["index.ts", "index.ts.bak"]); | ||
| }); | ||
| }); | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: trying to keep comments to a minimum in the repo, this function looks pretty self explanatory, no need to add it