From f9e67d27c03d9959269b5a35914ab7db5fe51146 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonatan=20K=C5=82osko?= Date: Fri, 20 Aug 2021 23:25:18 +0200 Subject: [PATCH] Add a keyboard shortcut for triggering on-hover docs (#508) * Add a keyboard shortcut for triggering on-hover docs * Update changelog --- CHANGELOG.md | 1 + assets/js/cell/live_editor.js | 14 ++++++++++++- assets/js/cell/live_editor/monaco.js | 21 +++++++++++++++++++ .../live/session_live/shortcuts_component.ex | 7 +++++++ 4 files changed, 42 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 07d4c33f7..5b7c37ad7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). - Introduced file system abstraction and an S3 support ([#492](https://github.com/livebook-dev/livebook/pull/492)) - Added support for configuring file systems using env variables ([#498](https://github.com/livebook-dev/livebook/pull/498)) +- Added a keyboard shortcut for triggering on-hover docs ([#508](https://github.com/livebook-dev/livebook/pull/508)) ### Fixed diff --git a/assets/js/cell/live_editor.js b/assets/js/cell/live_editor.js index b45a2a115..f6b8db42e 100644 --- a/assets/js/cell/live_editor.js +++ b/assets/js/cell/live_editor.js @@ -1,4 +1,4 @@ -import monaco from "./live_editor/monaco"; +import monaco, { addKeybinding } from "./live_editor/monaco"; import EditorClient from "./live_editor/editor_client"; import MonacoEditorAdapter from "./live_editor/monaco_editor_adapter"; import HookServerAdapter from "./live_editor/hook_server_adapter"; @@ -193,6 +193,18 @@ class LiveEditor { const contentHeight = this.editor.getContentHeight(); this.container.style.height = `${contentHeight}px`; }); + + // Replace built-in keybindings + // Note that generally we want to stick to defaults, so that we match + // VS Code, but some keybindings are overly awkward, in which case we + // add our own + + // By default this is a sequence of: Ctrl + K Ctrl + I + addKeybinding( + this.editor, + "editor.action.showHover", + monaco.KeyMod.CtrlCmd | monaco.KeyCode.KEY_I + ); } /** diff --git a/assets/js/cell/live_editor/monaco.js b/assets/js/cell/live_editor/monaco.js index d4bb54163..ab69d65cc 100644 --- a/assets/js/cell/live_editor/monaco.js +++ b/assets/js/cell/live_editor/monaco.js @@ -1,4 +1,5 @@ import * as monaco from "monaco-editor/esm/vs/editor/editor.api"; +import { CommandsRegistry } from "monaco-editor/esm/vs/platform/commands/common/commands"; import ElixirOnTypeFormattingEditProvider from "./elixir/on_type_formatting_edit_provider"; import theme from "./theme"; @@ -80,3 +81,23 @@ export function highlight(code, language) { return result.replace(/$/, ""); }); } + +/** + * Updates keybinding for the given editor command. + * + * This uses an internal API, since there is no clean support + * for customizing keybindings. + * See https://github.com/microsoft/monaco-editor/issues/102#issuecomment-822981429 + */ +export function addKeybinding(editor, id, newKeybinding) { + const { handler, when } = CommandsRegistry.getCommand(id) ?? {}; + + if (handler) { + editor._standaloneKeybindingService.addDynamicKeybinding( + id, + newKeybinding, + handler, + when + ); + } +} diff --git a/lib/livebook_web/live/session_live/shortcuts_component.ex b/lib/livebook_web/live/session_live/shortcuts_component.ex index deb0ef68e..b6cb2561a 100644 --- a/lib/livebook_web/live/session_live/shortcuts_component.ex +++ b/lib/livebook_web/live/session_live/shortcuts_component.ex @@ -11,6 +11,13 @@ defmodule LivebookWeb.SessionLive.ShortcutsComponent do desc: "Show completion list, use twice for details", basic: true }, + %{ + seq: ["ctrl", "i"], + seq_mac: ["⌘", "i"], + press_all: true, + desc: "Show identifier documentation", + basic: true + }, %{ seq: ["ctrl", "shift", "i"], seq_mac: ["⇧", "⌥", "f"],