Skip to content

Commit ac5038c

Browse files
authored
Merge pull request #3244 from github/koesie10/suggest-box-highlight
Add highlighting to items in suggest box
2 parents ec6a725 + 070f0ca commit ac5038c

File tree

4 files changed

+55
-4
lines changed

4 files changed

+55
-4
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { styled } from "styled-components";
2+
import type { Snippet } from "./highlight";
3+
4+
const Normal = styled.span``;
5+
const Highlighted = styled.span`
6+
font-weight: 700;
7+
color: var(--vscode-editorSuggestWidget-focusHighlightForeground);
8+
`;
9+
10+
type Props = {
11+
snippets: Snippet[];
12+
};
13+
14+
export const HighlightedText = ({ snippets }: Props) => {
15+
return (
16+
<>
17+
{snippets.map((snippet, index) =>
18+
snippet.highlight ? (
19+
<Highlighted key={index}>{snippet.text}</Highlighted>
20+
) : (
21+
<Normal key={index}>{snippet.text}</Normal>
22+
),
23+
)}
24+
</>
25+
);
26+
};
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { useMemo } from "react";
2+
import { createHighlights } from "./highlight";
3+
import { HighlightedText } from "./HighlightedText";
4+
import type { Option } from "./options";
5+
6+
type Props<T extends Option<T>> = {
7+
item: T;
8+
9+
tokens: string[];
10+
};
11+
12+
export const LabelText = <T extends Option<T>>({ item, tokens }: Props<T>) => {
13+
const highlights = useMemo(() => {
14+
const highlightedToken = tokens[tokens.length - 1] ?? "";
15+
16+
return createHighlights(item.label, highlightedToken);
17+
}, [item, tokens]);
18+
19+
return <HighlightedText snippets={highlights} />;
20+
};

extensions/ql-vscode/src/view/common/SuggestBox/SuggestBox.tsx

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { VSCodeTextField } from "@vscode/webview-ui-toolkit/react";
1818
import type { Option } from "./options";
1919
import { findMatchingOptions } from "./options";
2020
import { SuggestBoxItem } from "./SuggestBoxItem";
21+
import { LabelText } from "./LabelText";
2122

2223
const Input = styled(VSCodeTextField)`
2324
width: 430px;
@@ -147,9 +148,13 @@ export const SuggestBox = <T extends Option<T>>({
147148
[onChange],
148149
);
149150

151+
const tokens = useMemo(() => {
152+
return parseValueToTokens(value);
153+
}, [value, parseValueToTokens]);
154+
150155
const suggestionItems = useMemo(() => {
151-
return findMatchingOptions(options, parseValueToTokens(value));
152-
}, [options, value, parseValueToTokens]);
156+
return findMatchingOptions(options, tokens);
157+
}, [options, tokens]);
153158

154159
useEffect(() => {
155160
if (disabled) {
@@ -222,7 +227,7 @@ export const SuggestBox = <T extends Option<T>>({
222227
})}
223228
active={activeIndex === index}
224229
icon={getIcon?.(item)}
225-
labelText={item.label}
230+
labelText={<LabelText tokens={tokens} item={item} />}
226231
details={getDetails?.(item)}
227232
/>
228233
))}

extensions/ql-vscode/src/view/common/SuggestBox/highlight.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
type Snippet = {
1+
export type Snippet = {
22
text: string;
33
highlight: boolean;
44
};

0 commit comments

Comments
 (0)