Skip to content
This repository was archived by the owner on Jan 11, 2023. It is now read-only.

Commit ff98fe5

Browse files
atwalg2jasonLaster
authored andcommitted
[QuickOpen] style matched fuzzy characters (#5292)
1 parent 92f3177 commit ff98fe5

File tree

4 files changed

+28
-108
lines changed

4 files changed

+28
-108
lines changed

src/components/QuickOpenModal.css

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
.result-item .title .fuzzy-match {
1+
.result-item .title .highlight {
22
font-weight: bold;
33
}
44

5-
.result-item .subtitle .fuzzy-match {
5+
.result-item .subtitle .highlight {
66
color: var(--grey-90);
7+
font-weight: 500;
78
}
89

9-
.theme-dark .result-item .title .fuzzy-match,
10-
.theme-dark .result-item .subtitle .fuzzy-match {
10+
.theme-dark .result-item .title .highlight,
11+
.theme-dark .result-item .subtitle .highlight {
1112
color: white;
1213
}

src/components/QuickOpenModal.js

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,10 @@
33
* file, You can obtain one at <http://mozilla.org/MPL/2.0/>. */
44

55
// @flow
6-
76
import React, { Component } from "react";
87
import { connect } from "react-redux";
98
import fuzzyAldrin from "fuzzaldrin-plus";
10-
9+
import { basename } from "../utils/path";
1110
import actions from "../actions";
1211
import {
1312
getSources,
@@ -24,7 +23,7 @@ import {
2423
formatSources,
2524
parseLineColumn,
2625
formatShortcutResults,
27-
groupFuzzyMatches
26+
MODIFIERS
2827
} from "../utils/quick-open";
2928
import Modal from "./shared/Modal";
3029
import SearchInput from "./shared/SearchInput";
@@ -68,10 +67,6 @@ type GotoLocationType = {
6867
column?: number
6968
};
7069

71-
type FuzzyMatch = { type: "match", value: string };
72-
type FuzzyMiss = { type: "miss", value: string };
73-
type FuzzyResult = FuzzyMatch | FuzzyMiss;
74-
7570
function filter(values, query) {
7671
return fuzzyAldrin.filter(values, query, {
7772
key: "value",
@@ -328,32 +323,34 @@ export class QuickOpenModal extends Component<Props, State> {
328323
isSourcesQuery = () => this.props.searchType === "sources";
329324
isSourceSearch = () => this.isSourcesQuery() || this.isGotoSourceQuery();
330325

331-
renderHighlight = (part: FuzzyResult, i: number) => {
332-
if (part.type === "match") {
333-
return (
334-
<span key={`${part.value}-${i}`} className="fuzzy-match">
335-
{part.value}
336-
</span>
337-
);
338-
}
339-
return part.value;
326+
/* eslint-disable react/no-danger */
327+
renderHighlight = (candidateString: string, query: string, name: string) => {
328+
const html = fuzzyAldrin.wrap(candidateString, query);
329+
330+
return <div dangerouslySetInnerHTML={{ __html: html }} />;
340331
};
341332

342333
highlightMatching = (query: string, results: QuickOpenResult[]) => {
343-
if (query === "") {
334+
let newQuery = query;
335+
if (newQuery === "") {
344336
return results;
345337
}
338+
if (Object.keys(MODIFIERS).includes(query[0])) {
339+
newQuery = query.slice(1, query.length);
340+
}
341+
346342
return results.map(result => {
347-
const title = groupFuzzyMatches(result.title, query);
348-
const subtitle =
349-
result.subtitle != null
350-
? groupFuzzyMatches(result.subtitle, query)
351-
: null;
352343
return {
353344
...result,
354-
title: title.map(this.renderHighlight),
355-
...(subtitle != null && !this.isSymbolSearch()
356-
? { subtitle: subtitle.map(this.renderHighlight) }
345+
title: this.renderHighlight(result.title, basename(newQuery), "title"),
346+
...(result.subtitle != null && !this.isSymbolSearch()
347+
? {
348+
subtitle: this.renderHighlight(
349+
result.subtitle,
350+
newQuery,
351+
"subtitle"
352+
)
353+
}
357354
: null)
358355
};
359356
});

src/utils/quick-open.js

Lines changed: 0 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -127,44 +127,3 @@ export function formatSources(sources: SourcesMap): Array<QuickOpenResult> {
127127
.filter(({ value }) => value != "")
128128
.toJS();
129129
}
130-
131-
export function groupFuzzyMatches(input: string, query: string) {
132-
const parts = input.toLowerCase().split("");
133-
const queryChars = query.toLowerCase().split("");
134-
const shared = parts.filter(char => queryChars.includes(char));
135-
const output = [];
136-
let matched;
137-
let missed;
138-
parts.forEach((char, i) => {
139-
if (shared.includes(char)) {
140-
if (!matched) {
141-
matched = { type: "match", value: input[i] };
142-
output.push(matched);
143-
return;
144-
}
145-
matched = output[output.length - 1];
146-
if (matched.type === "match") {
147-
matched.value = `${matched.value}${input[i]}`;
148-
} else {
149-
matched = { type: "match", value: input[i] };
150-
output.push(matched);
151-
}
152-
return;
153-
}
154-
155-
if (!missed) {
156-
missed = { type: "miss", value: input[i] };
157-
output.push(missed);
158-
return;
159-
}
160-
161-
missed = output[output.length - 1];
162-
if (missed.type === "miss") {
163-
missed.value = `${missed.value}${input[i]}`;
164-
} else {
165-
missed = { type: "miss", value: input[i] };
166-
output.push(missed);
167-
}
168-
});
169-
return output;
170-
}

src/utils/tests/quick-open.spec.js

Lines changed: 1 addition & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
import cases from "jest-in-case";
2-
import {
3-
groupFuzzyMatches,
4-
parseQuickOpenQuery,
5-
parseLineColumn
6-
} from "../quick-open";
2+
import { parseQuickOpenQuery, parseLineColumn } from "../quick-open";
73

84
cases(
95
"parseQuickOpenQuery utility",
@@ -33,36 +29,3 @@ cases(
3329
}
3430
]
3531
);
36-
37-
cases(
38-
"groupFuzzyMatches utility",
39-
({ input, query, value }) =>
40-
expect(groupFuzzyMatches(input, query)).toEqual(value),
41-
[
42-
{
43-
input: "anonymous",
44-
query: "ano",
45-
value: [
46-
{ type: "match", value: "anon" },
47-
{ type: "miss", value: "ym" },
48-
{ type: "match", value: "o" },
49-
{ type: "miss", value: "us" }
50-
]
51-
},
52-
{
53-
input: "previousAttributes",
54-
query: "prva",
55-
value: [
56-
{ type: "match", value: "pr" },
57-
{ type: "miss", value: "e" },
58-
{ type: "match", value: "v" },
59-
{ type: "miss", value: "ious" },
60-
{ type: "match", value: "A" },
61-
{ type: "miss", value: "tt" },
62-
{ type: "match", value: "r" },
63-
{ type: "miss", value: "ibutes" }
64-
]
65-
},
66-
{ input: "super", query: "x", value: [{ type: "miss", value: "super" }] }
67-
]
68-
);

0 commit comments

Comments
 (0)