Skip to content

Commit 5b64d2d

Browse files
feat: add language switch
1 parent 1f10b4f commit 5b64d2d

File tree

3 files changed

+89
-3
lines changed

3 files changed

+89
-3
lines changed

src/webview/SearchSidebar/SearchResultList/Icon.tsx

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const styles = stylex.create({
99
},
1010
})
1111

12-
const icons = `
12+
export const icons = `
1313
bash,
1414
c,
1515
cpp,
@@ -37,9 +37,14 @@ const icons = `
3737
.map(icon => icon.trim())
3838
.filter(Boolean)
3939

40-
export function Icon({ name }: { name: string }) {
40+
interface IconProps {
41+
name: string
42+
style?: stylex.StyleXStyles
43+
}
44+
45+
export function Icon({ name, style }: IconProps) {
4146
const iconName = icons.includes(name) ? name : 'file'
4247
// @ts-expect-error
4348
const src = window.ICON_SRC + `/${iconName}.svg`
44-
return <img src={src} {...stylex.props(styles.icon)} alt={iconName} />
49+
return <img src={src} {...stylex.props(styles.icon, style)} alt={iconName} />
4550
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import * as stylex from '@stylexjs/stylex'
2+
import { Icon, icons } from '../SearchResultList/Icon'
3+
import { ChangeEvent, useCallback, useState } from 'react'
4+
import { VscListFlat } from 'react-icons/vsc'
5+
6+
const styles = stylex.create({
7+
langButton: {
8+
position: 'absolute',
9+
height: '20px',
10+
width: '20px',
11+
border: '1px solid transparent',
12+
right: 2,
13+
top: 3,
14+
borderRadius: '3px',
15+
':hover': {
16+
backgroundColor: 'var(--vscode-inputOption-hoverBackground)',
17+
},
18+
},
19+
langActive: {
20+
borderColor: 'var(--vscode-inputOption-activeBorder)',
21+
':hover': {
22+
background: 'none',
23+
filter: 'drop-shadow(1px 1px 3px rgba(0,0,0,0.2))',
24+
},
25+
},
26+
langDropdown: {
27+
height: '100%',
28+
width: '100%',
29+
border: 'none',
30+
background: 'transparent',
31+
position: 'absolute',
32+
appearance: 'none',
33+
outline: 'none',
34+
color: 'transparent',
35+
cursor: 'pointer',
36+
':focus': {
37+
outline: 'none',
38+
},
39+
},
40+
langIcon: {
41+
marginTop: 1,
42+
marginLeft: 1,
43+
},
44+
})
45+
46+
function capitalize(word) {
47+
return word.charAt(0).toUpperCase() + word.slice(1)
48+
}
49+
50+
export function LangSelect() {
51+
const [lang, setLang] = useState('')
52+
const title = lang
53+
? `Search pattern in ${lang}`
54+
: 'Search pattern in specific language'
55+
const onChange = useCallback(
56+
(e: ChangeEvent<HTMLSelectElement>) => {
57+
setLang(e.target.value)
58+
},
59+
[setLang],
60+
)
61+
return (
62+
<label
63+
{...stylex.props(styles.langButton, lang ? styles.langActive : null)}
64+
title={title}
65+
>
66+
<select
67+
{...stylex.props(styles.langDropdown)}
68+
style={{ outline: 'none' }}
69+
onChange={onChange}
70+
>
71+
<option value="">Auto Detect</option>
72+
{icons.map(icon => (
73+
<option value={icon}>{capitalize(icon)}</option>
74+
))}
75+
</select>
76+
{lang ? <Icon name={lang} style={styles.langIcon} /> : <VscListFlat />}
77+
</label>
78+
)
79+
}

src/webview/SearchSidebar/SearchWidgetContainer/SearchWidget.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
import { childPort } from '../../postMessage'
88
import { useSearchResult, acceptAllChanges } from '../../hooks/useSearch'
99
import { SearchInput } from './SearchInput'
10+
import { LangSelect } from './LangSelect'
1011
import { useBoolean, useEffectOnce } from 'react-use'
1112
import { VscChevronRight, VscChevronDown, VscReplaceAll } from 'react-icons/vsc'
1213
import * as stylex from '@stylexjs/stylex'
@@ -99,6 +100,7 @@ function SearchWidgetContainer() {
99100
onChange={setPattern}
100101
onKeyEnterUp={refreshResult}
101102
/>
103+
<LangSelect />
102104
{isExpanded ? <ReplaceBar /> : null}
103105
</div>
104106
</div>

0 commit comments

Comments
 (0)