forked from tidbcloud/tisqleditor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtooltip-hint.ts
169 lines (147 loc) · 4.17 KB
/
tooltip-hint.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import {
EditorState,
Extension,
Prec,
StateEffect,
StateField
} from '@codemirror/state'
import { EditorView, Tooltip, keymap, showTooltip } from '@codemirror/view'
import { getFirstNonUseTypeStatement } from '@tidbcloud/codemirror-extension-cur-sql'
import { getAiWidgetOptions, isPromptInputActive } from './prompt-input'
import { isAppleOs } from './utils'
//------------------------------------------
const cursorTooltipBaseTheme = EditorView.baseTheme({
'.cm-tooltip.cm-ai-tooltip-cursor': {
fontSize: '12px',
fontWeight: 500,
borderRadius: '8px',
padding: '16px',
'& code': {
borderRadius: '4px',
padding: '4px 6px',
'& b': {
fontWeight: 'bold'
}
}
},
'&light .cm-tooltip.cm-ai-tooltip-cursor': {
backgroundColor: '#fff',
color: '#555',
border: '1px solid #EDEDED',
boxShadow: '0px 8px 32px 0px rgba(0, 0, 0, 0.08)'
},
'&light .cm-tooltip.cm-ai-tooltip-cursor code': {
border: '1px solid #E6E6E6',
backgroundColor: '#F9F9F9'
},
'&dark .cm-tooltip.cm-ai-tooltip-cursor': {
backgroundColor: '#222',
color: '#c6c6c6',
border: '1px solid #393939',
boxShadow: '0px 8px 32px 0px rgba(0, 0, 0, 0.08)'
},
'&dark .cm-tooltip.cm-ai-tooltip-cursor code': {
border: '1px solid #333',
backgroundColor: '#1a1a1a'
}
})
//------------------------------------------
function getCursorTooltips(state: EditorState): readonly Tooltip[] {
const cmd = isAppleOs() ? 'Command' : 'Ctrl'
return state.selection.ranges
.filter((range) => !range.empty)
.map((range) => {
const pos = range.head
const line = state.doc.lineAt(pos)
let delta = pos - line.from
if (delta > 5) {
delta = 5
}
return {
pos: range.head,
above: true,
create: () => {
let dom = document.createElement('div')
dom.className = 'cm-ai-tooltip-cursor'
dom.innerHTML =
getAiWidgetOptions().tooltipHintElement ||
`Press <code><b>${cmd}</b> + <b>I</b></code> to rewrite SQL by AI`
return { dom, offset: { x: -16 * delta, y: 4 } }
}
}
})
}
const cursorTooltipField = StateField.define<readonly Tooltip[]>({
create: getCursorTooltips,
update(tooltips, tr) {
for (let effect of tr.effects) {
if (effect.is(enableTooltipEffect)) {
if (effect.value) {
return getCursorTooltips(tr.state)
} else {
return []
}
}
}
return tooltips
},
provide: (f) => showTooltip.computeN([f], (state) => state.field(f))
})
//------------------------------------------
const enableTooltipEffect = StateEffect.define<boolean>()
export function hideTooltip(view: EditorView) {
view.dispatch({
effects: enableTooltipEffect.of(false)
})
}
// dismiss tooltip when press ai-widget hotkey (default is 'Mod-i')
const hideTooltipKeymap = (hotkey?: string) =>
Prec.highest(
keymap.of([
{
key: hotkey || 'Mod-i',
run: (view) => {
if (view.state.field(cursorTooltipField).length !== 0) {
hideTooltip(view)
}
return false
}
}
])
)
const selectionChangeListener = () => {
let timer: number | undefined
let preFrom = -1
let preTo = -1
return EditorView.updateListener.of((update) => {
if (!update.selectionSet) return
const { from, to } = update.view.state.selection.main
if (from === preFrom && to === preTo) return
preFrom = from
preTo = to
// dismiss the previous tooltip immediately
if (from === to) {
hideTooltip(update.view)
return
}
// debounce
timer && clearTimeout(timer)
// delay to show the tooltip
timer = window.setTimeout(() => {
if (isPromptInputActive(update.view.state)) return
if (!getFirstNonUseTypeStatement(update.view.state)) return
update.view.dispatch({
effects: enableTooltipEffect.of(true)
})
}, 500)
})
}
//------------------------------------------
export function aiCursorTooltip(hotkey?: string): Extension {
return [
cursorTooltipBaseTheme,
hideTooltipKeymap(hotkey),
cursorTooltipField,
selectionChangeListener()
]
}