简体   繁体   中英

Reloading keyboard shortcut definitions in Pharo

I've been playing around with keyboard shortcuts in Pharo 7.0. I wanted to modify the binding for #jumpToNextKeywordOfIt in the Smalltalk editor and so I got the following change in the definition of buildShortcutsOn method:

(aBuilder shortcut: #jumpToNextKeywordOfIt)
    category: RubSmalltalkEditor name
    default: $y meta shift
    do: [ :target | target editor jumpToNextKeywordOfIt: true ]
    description: 'Jump to next keyword'.    

My first thought was that just saving this definition should immediately take effect, but it was not the case. Then I thought, perhaps since this is part of a method definition, then calling this method on the editor class would do the trick. Now, the method takes an argument aBuilder and I don't really know what that is. So two questions arise:

  1. Is this the proper way to apply keybinding changes to a running editor?
  2. What is aBuilder in this context and how does one get it?

Let me give you some hints on how to find the solution (as this might be more valuable than giving the solution at once)

The problem is what's aBuilder right? Well, from the expression

(aBuilder shortcut: #jumpToNextKeywordOfIt)

we deduce that aBuilder is someone that responds to #shortcut: . Cmd+m and you will get 9 implementors of #shortcut: . One of them, KMBuilder has an interesting name. Moreover, its implementation of shortcut: is

shortcut: aKeymapName
  ^KMKeymapBuilder 
    for: aKeymapName
    platform: platform

meaning that it will answer with an instance of KMKeymapBuilder . Browse this class and verify that it understands the next message from your expression:

category: RubSmalltalkEditor name
default: $y meta shift
do: [ :target | target editor jumpToNextKeywordOfIt: true ]
description: 'Jump to next keyword'.

It does! So this must be it! To check this, we still need an instance of KMBuilder . Browse the class, go to the class side and find the unary message #keymap .

This means that we can obtain aBuilder by evaluating

KMBuilder keymap

I love unary messages. Specially when they are on the class side!

Now go to the implementor of the method you already tweaked #buildShortcutsOn: . It is implemented in the class side and we can now evaluate:

RubTextEditor buildShortcutsOn: KMBuilder keymap

To make sure that it works, go now to the desired handler #jumpToNextKeywordOfIt: and insert a halt in it. This is in the same class, instance side.

Now lets press Cmd+Shift+y and see if we get the halt... Bingo! I mean, Halt!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM