Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

feat(sql-editor): add a vim :run command so vimrc keys can run the query (#119036)

posthog[bot]5d0c94d24e20
diff --git a/frontend/src/lib/monaco/vimMode.ts b/frontend/src/lib/monaco/vimMode.ts
index ebcbbf5f0fcf..4347a5eb1d02 100644
--- a/frontend/src/lib/monaco/vimMode.ts
+++ b/frontend/src/lib/monaco/vimMode.ts
@@ -1,8 +1,14 @@
-import { editor as monacoEditor } from 'monaco-editor'
+import { Range, editor as monacoEditor } from 'monaco-editor'
import { VimMode, initVimMode } from 'monaco-vim'
import { VimrcError, parseVimrc } from './vimrc'
-import { VimMappingContext, VimrcMappingSession, createNonrecursiveVimMapping } from './vimrcMappings'
+import {
+ RUN_QUERY_ACTION,
+ VimMappingContext,
+ VimrcMappingSession,
+ createNonrecursiveVimMapping,
+ holdVimrcMappingPrefixes,
+} from './vimrcMappings'
export interface VimModeHandle {
vimMode: VimMode
@@ -24,6 +30,18 @@ const vimrcEditorStates = new WeakMap<object, VimrcEditorState>()
const vimrcMappingSession = new VimrcMappingSession()
let vimrcSupportRegistered = false
+// CodeEditorImpl registers this action for Cmd+Enter. It runs the selection when there is one, and the full query otherwise.
+const RUN_QUERY_EDITOR_ACTION = 'saveAndRunPostHog'
+
+async function runQuery(cm: any): Promise<void> {
+ const action = cm.editor.getAction(RUN_QUERY_EDITOR_ACTION)
+ if (!action) {
+ cm.openNotification("This editor can't run queries")
+ return
+ }
+ await action.run()
+}
+
// Options and ex commands live on monaco-vim's global Vim object, which all editors share, so register them once.
function registerVimrcSupport(): void {
if (vimrcSupportRegistered) {
@@ -46,6 +64,33 @@ function registerVimrcSupport(): void {
})
}
+ holdVimrcMappingPrefixes(vimrcMappingSession)
+
+ Vim.defineAction(RUN_QUERY_ACTION, (cm: any): void => {
+ void runQuery(cm)
+ })
+
+ // An ex range is linewise, so `:'<,'>run` runs the full lines of the last visual selection.
+ Vim.defineEx('run', 'run', (cm: any, params: { line?: number; lineEnd?: number }): void => {
+ if (params.line === undefined) {
+ void runQuery(cm)
+ return
+ }
+ const editor: monacoEditor.IStandaloneCodeEditor = cm.editor
+ const model = editor.getModel()
+ const position = editor.getPosition()
+ if (!model || !position) {
+ return
+ }
+ const lineCount = model.getLineCount()
+ const lines = [params.line, params.lineEnd ?? params.line].map((line) =>
+ Math.min(Math.max(line + 1, 1), lineCount)
+ )
+ const [startLineNumber, endLineNumber] = [Math.min(...lines), Math.max(...lines)]
+ editor.setSelection(new Range(startLineNumber, 1, endLineNumber, model.getLineMaxColumn(endLineNumber)))
+ void runQuery(cm).finally(() => editor.setPosition(position))
+ })
+
// monaco-vim forces a solid cursor each time the editor enters normal mode. patchCursorBlink re-applies this option after that.
Vim.defineOption('cursorblink', false, 'boolean', [], (value: boolean | undefined, cm?: any): boolean | void => {
const state = cm && vimrcEditorStates.get(cm)
diff --git a/frontend/src/lib/monaco/vimrc.test.ts b/frontend/src/lib/monaco/vimrc.test.ts
index 25b3e131f8fa..0c242ebaa4d5 100644
--- a/frontend/src/lib/monaco/vimrc.test.ts
+++ b/frontend/src/lib/monaco/vimrc.test.ts
@@ -24,6 +24,18 @@ describe('parseVimrc', () => {
})
})
+ it.each([
+ ['uses a backslash when no leader is set', '', 'nnoremap \\r :run<CR>'],
+ ['sets a space leader', 'let mapleader = " "', 'nnoremap <Space>r :run<CR>'],
+ ['sets an escaped space leader', 'let mapleader = "\\<Space>"', 'nnoremap <Space>r :run<CR>'],
+ ['sets a comma leader with g:', "let g:mapleader = ','", 'nnoremap ,r :run<CR>'],
+ ])('replaces <Leader> in mappings when the vimrc %s', (_description, leaderLine, expected) => {
+ const { commands, errors } = parseVimrc(`${leaderLine}\nnnoremap <leader>r :run<CR>`)
+
+ expect(errors).toEqual([])
+ expect(commands).toEqual([{ lineNumber: 2, command: expected }])
+ })
+
it.each([
['deletes every line', '%d'],
['deletes matching lines', 'g/select/d'],
@@ -34,6 +46,8 @@ describe('parseVimrc', () => {
['overrides an option command', 'map :set :sort'],
['overrides a mapping command', 'noremap :map :sort'],
['removes an ex-command mapping', 'unmap :set'],
+ ['sets a variable that is not the leader', 'let g:foo = 1'],
+ ['sets a leader of more than one key', 'let mapleader = "ab"'],
])('rejects a command that %s', (_description, command) => {
const { commands, errors } = parseVimrc(`set relativenumber\n${command}`)
diff --git a/frontend/src/lib/monaco/vimrc.ts b/frontend/src/lib/monaco/vimrc.ts
index 925996cabef6..7b9e02a4576c 100644
--- a/frontend/src/lib/monaco/vimrc.ts
+++ b/frontend/src/lib/monaco/vimrc.ts
@@ -2,10 +2,30 @@
// configure Vim are allowed, because a command like `%d` or `g/x/d` would edit the user's query on every load.
const MAPPING_COMMANDS = ['map', 'nmap', 'imap', 'vmap', 'noremap', 'nnoremap', 'inoremap', 'vnoremap', 'unmap']
const OPTION_COMMANDS = ['set', 'setlocal', 'setglobal']
-export const VIMRC_ALLOWED_COMMANDS: readonly string[] = [...MAPPING_COMMANDS, ...OPTION_COMMANDS]
+export const VIMRC_ALLOWED_COMMANDS: readonly string[] = [...MAPPING_COMMANDS, ...OPTION_COMMANDS, 'let']
+
+// monaco-vim has no <Leader> key, so the parser replaces <Leader> with the key that `let mapleader` sets.
+// Vim uses a backslash when no leader is set.
+const DEFAULT_LEADER = '\\'
+const MAPLEADER_PATTERN = /^let\s+(?:g:)?mapleader\s*=\s*(["'])(.*)\1$/
+const LEADER_PATTERN = /<leader>/gi
+
+function parseLeader(value: string): string | null {
+ if (value === ' ' || /^\\<space>$/i.test(value)) {
+ return '<Space>'
+ }
+ if (value === '\\\\') {
+ return '\\'
+ }
+ return value.length === 1 && value.trim() ? value : null
+}
export const VIMRC_MAX_LENGTH = 10000
+export function mapsRunCommand({ command }: VimrcCommand): boolean {
+ return /\s:run<cr>$/i.test(command)
+}
+
export interface VimrcCommand {
lineNumber: number
command: string
@@ -24,21 +44,38 @@ export interface ParsedVimrc {
export function parseVimrc(vimrc: string): ParsedVimrc {
const commands: VimrcCommand[] = []
const errors: VimrcError[] = []
+ let leader = DEFAULT_LEADER
vimrc.split('\n').forEach((rawLine, index) => {
const lineNumber = index + 1
- const command = rawLine.trim().replace(/^:+/, '')
- if (!command || command.startsWith('"')) {
+ const rawCommand = rawLine.trim().replace(/^:+/, '')
+ if (!rawCommand || rawCommand.startsWith('"')) {
return
}
- const commandName = command.match(/^[a-z]+(?=\s|$)/i)?.[0]
+ const commandName = rawCommand.match(/^[a-z]+(?=\s|$)/i)?.[0]
if (!commandName || !VIMRC_ALLOWED_COMMANDS.includes(commandName)) {
errors.push({
lineNumber,
- message: `"${command}" isn't supported. Use one of: ${VIMRC_ALLOWED_COMMANDS.join(', ')}.`,
+ message: `"${rawCommand}" isn't supported. Use one of: ${VIMRC_ALLOWED_COMMANDS.join(', ')}.`,
})
return
}
+ if (commandName === 'let') {
+ const match = rawCommand.match(MAPLEADER_PATTERN)
+ const nextLeader = match && parseLeader(match[2])
+ if (!nextLeader) {
+ errors.push({
+ lineNumber,
+ message: `"${rawCommand}" isn't supported. Use let only to set one leader key, for example let mapleader = " ".`,
+ })
+ return
+ }
+ leader = nextLeader
+ return
+ }
+ const command = MAPPING_COMMANDS.includes(commandName)
+ ? rawCommand.replace(LEADER_PATTERN, () => leader)
+ : rawCommand
const lhs = command.split(/\s+/)[1]
if (MAPPING_COMMANDS.includes(commandName) && lhs?.startsWith(':') && lhs !== ':') {
errors.push({
diff --git a/frontend/src/lib/monaco/vimrcMappings.test.ts b/frontend/src/lib/monaco/vimrcMappings.test.ts
index 13965bfe7484..10d0218aead8 100644
--- a/frontend/src/lib/monaco/vimrcMappings.test.ts
+++ b/frontend/src/lib/monaco/vimrcMappings.test.ts
@@ -1,7 +1,12 @@
import { VimMode } from 'monaco-vim'
import { parseVimrc } from './vimrc'
-import { VimrcMappingSession, createNonrecursiveVimMapping } from './vimrcMappings'
+import {
+ RUN_QUERY_ACTION,
+ VimrcMappingSession,
+ createNonrecursiveVimMapping,
+ holdVimrcMappingPrefixes,
+} from './vimrcMappings'
jest.mock('monaco-editor', () => ({ KeyCode: {}, editor: {}, SelectionDirection: {} }))
@@ -10,6 +15,12 @@ interface Cursor {
ch: number
}
+interface VimState {
+ insertMode: boolean
+ visualMode?: boolean
+ sel?: { anchor: Cursor; head: Cursor }
+}
+
const Vim = (
VimMode as unknown as {
Vim: {
@@ -17,12 +28,13 @@ const Vim = (
handleKey: (cm: unknown, key: string) => void
handleEx: (cm: unknown, input: string) => void
maybeInitVimState_: (cm: unknown) => void
+ defineAction: (name: string, fn: () => void) => void
}
}
).Vim
function createAdapter(): {
- state: { vim: { insertMode: boolean } | undefined }
+ state: { vim: VimState | undefined }
curOp: Record<string, unknown>
operation: (callback: () => unknown) => unknown
dispatch: () => void
@@ -35,6 +47,8 @@ function createAdapter(): {
charCoords: () => { left: number }
listSelections: () => { head: Cursor; anchor: Cursor }[]
replaceRange: () => void
+ setSelections: () => void
+ setBookmark: (position: Cursor) => { find: () => Cursor }
getInputField: () => HTMLInputElement
off: () => void
setOption: () => void
@@ -44,7 +58,7 @@ function createAdapter(): {
let cursor = { line: 1, ch: 2 }
const input = document.createElement('input')
const cm = {
- state: { vim: undefined as { insertMode: boolean } | undefined },
+ state: { vim: undefined as VimState | undefined },
curOp: {},
operation: (callback: () => unknown): unknown => callback(),
dispatch: (): void => {},
@@ -59,6 +73,8 @@ function createAdapter(): {
charCoords: (): { left: number } => ({ left: 0 }),
listSelections: (): { head: Cursor; anchor: Cursor }[] => [{ head: cursor, anchor: cursor }],
replaceRange: (): void => {},
+ setSelections: (): void => {},
+ setBookmark: (position: Cursor): { find: () => Cursor } => ({ find: () => position }),
getInputField: (): HTMLInputElement => input,
off: (): void => {},
setOption: (): void => {},
@@ -70,6 +86,9 @@ function createAdapter(): {
}
describe('createNonrecursiveVimMapping', () => {
+ const prefixSession = new VimrcMappingSession()
+ holdVimrcMappingPrefixes(prefixSession)
+
beforeEach(() => {
jest.useFakeTimers()
Vim.mapclear()
@@ -129,6 +148,39 @@ describe('createNonrecursiveVimMapping', () => {
expect(nextEditor.getCursor().line).toBe(2)
})
+ it.each([
+ ['noremap', undefined, ['normal', 'visual']],
+ ['nnoremap', 'normal' as const, ['normal']],
+ ['vnoremap', 'visual' as const, ['visual']],
+ ])('makes %s <Space>r :run<CR> run the query in the mapped modes only', (name, context, expectedModes) => {
+ const runQuery = jest.fn()
+ Vim.defineAction(RUN_QUERY_ACTION, runQuery)
+ prefixSession.retainEditor()
+ prefixSession.getCommandsToApply(parseVimrc(`${name} <Space>r :run<CR>`).commands)
+ createNonrecursiveVimMapping('<Space>r', ':run<CR>', context)
+
+ const ranModes = (['normal', 'visual', 'insert'] as const).filter((mode) => {
+ const cm = createAdapter()
+ const cursor = cm.getCursor()
+ cm.state.vim = {
+ ...cm.state.vim!,
+ insertMode: mode === 'insert',
+ visualMode: mode === 'visual',
+ sel: { anchor: cursor, head: cursor },
+ }
+ runQuery.mockClear()
+ Vim.handleKey(cm, '<Space>')
+ Vim.handleKey(cm, 'r')
+ return runQuery.mock.calls.length > 0
+ })
+
+ expect(ranModes).toEqual(expectedModes)
+ })
+
+ it('rejects a :run<CR> mapping in insert mode', () => {
+ expect(() => createNonrecursiveVimMapping('<C-r>', ':run<CR>', 'insert')).toThrow('insert mode')
+ })
+
it('rejects unsupported key sequences instead of making them recursive', () => {
const cm = createAdapter()
diff --git a/frontend/src/lib/monaco/vimrcMappings.ts b/frontend/src/lib/monaco/vimrcMappings.ts
index 099c3f886985..33b101071d95 100644
--- a/frontend/src/lib/monaco/vimrcMappings.ts
+++ b/frontend/src/lib/monaco/vimrcMappings.ts
@@ -3,6 +3,10 @@ import { VimMode } from 'monaco-vim'
import { VimrcCommand } from './vimrc'
export type VimMappingContext = 'normal' | 'insert' | 'visual'
+type CommandModeContext = Exclude<VimMappingContext, 'insert'>
+
+export const RUN_QUERY_ACTION = 'runPostHogQuery'
+const RUN_QUERY_RHS = /^:run<cr>$/i
interface VimKeyMapping {
keys: string
@@ -26,14 +30,46 @@ interface VimMappingApi {
args: Record<string, unknown>,
extra: { context: VimMappingContext }
) => void
+ findKey: (this: unknown, cm: any, key: string, origin?: string) => unknown
+ maybeInitVimState_: (cm: any) => {
+ insertMode: boolean
+ visualMode: boolean
+ inputState: { keyBuffer: string; operator: string | null }
+ }
}
+const VIM_KEY_PATTERN = /<\w+-.+?>|<\w+>|./g
+
function isMappingCommand({ command }: VimrcCommand): boolean {
return command.split(/\s+/, 1)[0].endsWith('map')
}
+const MAPPING_CONTEXTS: Record<string, CommandModeContext[]> = {
+ map: ['normal', 'visual'],
+ noremap: ['normal', 'visual'],
+ nmap: ['normal'],
+ nnoremap: ['normal'],
+ vmap: ['visual'],
+ vnoremap: ['visual'],
+}
+
+function getMappingPrefixes(commands: VimrcCommand[]): Record<CommandModeContext, Set<string>> {
+ const prefixes: Record<CommandModeContext, Set<string>> = { normal: new Set(), visual: new Set() }
+ for (const { command } of commands) {
+ const [commandName, lhs = ''] = command.split(/\s+/)
+ const keys = lhs.match(VIM_KEY_PATTERN) ?? []
+ for (const context of MAPPING_CONTEXTS[commandName] ?? []) {
+ for (let length = 1; length < keys.length; length++) {
+ prefixes[context].add(keys.slice(0, length).join(''))
+ }
+ }
+ }
+ return prefixes
+}
+
export class VimrcMappingSession {
private mappingCommands: string | null = null
+ private mappingPrefixes = getMappingPrefixes([])
private editorCount = 0
retainEditor(): () => void {
@@ -63,8 +99,33 @@ export class VimrcMappingSession {
const Vim = (VimMode as unknown as { Vim: VimMappingApi }).Vim
Vim.mapclear()
this.mappingCommands = mappingCommands
+ this.mappingPrefixes = getMappingPrefixes(commands.filter(isMappingCommand))
return commands
}
+
+ isMappingPrefix(keys: string, context: CommandModeContext): boolean {
+ return this.mappingPrefixes[context].has(keys)
+ }
+}
+
+// monaco-vim runs a full key match at once, so a built-in key such as <Space> or "," runs before the user
+// can type the rest of a mapping like <Space>r. This patch keeps a vimrc mapping prefix pending instead.
+// As with Vim's `notimeout`, the built-in command of that key no longer runs on its own.
+export function holdVimrcMappingPrefixes(session: VimrcMappingSession): void {
+ const Vim = (VimMode as unknown as { Vim: VimMappingApi }).Vim
+ const originalFindKey = Vim.findKey
+
+ Vim.findKey = function (cm, key, origin) {
+ const vim = Vim.maybeInitVimState_(cm)
+ if (!vim.insertMode && !vim.inputState.operator) {
+ const keys = vim.inputState.keyBuffer + key
+ if (session.isMappingPrefix(keys, vim.visualMode ? 'visual' : 'normal')) {
+ vim.inputState.keyBuffer = keys
+ return () => true
+ }
+ }
+ return originalFindKey.call(this, cm, key, origin)
+ }
}
export function createNonrecursiveVimMapping(lhs: string, rhs: string, context?: VimMappingContext): void {
@@ -76,6 +137,19 @@ export function createNonrecursiveVimMapping(lhs: string, rhs: string, context?:
return
}
+ // A key-to-ex mapping leaves visual mode before the ex command runs, which loses the selection.
+ // An action runs while the selection is still active, so a visual mapping runs only the selected text.
+ if (RUN_QUERY_RHS.test(rhs)) {
+ if (context === 'insert') {
+ throw new Error(`Mapping "${lhs} ${rhs}" isn't supported in insert mode. Use nnoremap or vnoremap.`)
+ }
+ const contexts: VimMappingContext[] = context ? [context] : ['normal', 'visual']
+ contexts.forEach((mappingContext) =>
+ Vim.mapCommand(lhs, 'action', RUN_QUERY_ACTION, {}, { context: mappingContext })
+ )
+ return
+ }
+
const mappings: VimKeyMapping[] = []
Vim.noremap.call({ _mapCommand: (mapping) => mappings.push(mapping) }, lhs, rhs, context)
if (!mappings.length) {
diff --git a/products/data_warehouse/frontend/shared/components/VimrcModal.tsx b/products/data_warehouse/frontend/shared/components/VimrcModal.tsx
index 07cdff9627b7..5fa4a511f4a0 100644
--- a/products/data_warehouse/frontend/shared/components/VimrcModal.tsx
+++ b/products/data_warehouse/frontend/shared/components/VimrcModal.tsx
@@ -17,7 +17,10 @@ export function VimrcModal({ editorKey }: { editorKey: string }): JSX.Element {
<span>
These commands run each time Vim mode starts. Use map commands such as{' '}
<code>inoremap jj &lt;Esc&gt;</code>, and set commands such as <code>set cursorblink</code> or{' '}
- <code>set relativenumber</code>. Lines that start with <code>"</code> are comments.
+ <code>set relativenumber</code>. To run the query or the selection with a key, map it to{' '}
+ <code>:run&lt;CR&gt;</code>, for example <code>nnoremap &lt;Leader&gt;r :run&lt;CR&gt;</code> and{' '}
+ <code>vnoremap &lt;Leader&gt;r :run&lt;CR&gt;</code>. Set the leader key with{' '}
+ <code>let mapleader = " "</code>. Lines that start with <code>"</code> are comments.
</span>
}
isOpen={vimrcEditorKey === editorKey}
@@ -50,7 +53,7 @@ export function VimrcModal({ editorKey }: { editorKey: string }): JSX.Element {
<LemonTextArea
value={vimrcDraft}
onChange={setVimrcDraft}
- placeholder={'inoremap jj <Esc>\nset cursorblink'}
+ placeholder={'let mapleader = " "\nnnoremap <Leader>r :run<CR>\ninoremap jj <Esc>\nset cursorblink'}
className="font-mono"
minRows={8}
maxRows={20}
diff --git a/products/data_warehouse/frontend/shared/logics/sqlEditorVimLogic.ts b/products/data_warehouse/frontend/shared/logics/sqlEditorVimLogic.ts
index 92feeb42461e..06b144db04c0 100644
--- a/products/data_warehouse/frontend/shared/logics/sqlEditorVimLogic.ts
+++ b/products/data_warehouse/frontend/shared/logics/sqlEditorVimLogic.ts
@@ -2,7 +2,7 @@ import { MakeLogicType, actions, connect, kea, listeners, path, reducers, select
import posthog from 'posthog-js'
import { userPreferencesLogic } from 'lib/logic/userPreferencesLogic'
-import { VIMRC_MAX_LENGTH, VimrcError, parseVimrc } from 'lib/monaco/vimrc'
+import { VIMRC_MAX_LENGTH, VimrcError, mapsRunCommand, parseVimrc } from 'lib/monaco/vimrc'
import { UI_CONFIGURATION_VERSION, uiCustomizationLogic } from '~/layout/uiCustomizationLogic'
import { SQLEditorConfiguration, UserUIConfiguration } from '~/queries/schema/schema-general'
@@ -183,9 +183,11 @@ export const sqlEditorVimLogic = kea<sqlEditorVimLogicType>([
() => {
actions.closeVimrcModal()
// Counts only. The vimrc text is free text the user typed, so it never leaves the browser in an event.
+ const { commands } = parseVimrc(vimrc)
posthog.capture('sql editor vimrc saved', {
line_count: vimrc.split('\n').length,
- command_count: parseVimrc(vimrc).commands.length,
+ command_count: commands.length,
+ maps_run_command: commands.some(mapsRunCommand),
})
},
actions.saveVimrcFailure