Reloading keyboard shortcut definitions in Pharo - smalltalk

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:
Is this the proper way to apply keybinding changes to a running editor?
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!

Related

PhpStorm - class names as strings suggestions

I have to write the following code (using Zend\Filter\Inflector):
$inflector = new Inflector(':string');
$inflector->setRules([
':string' => [
new StringToLower(),
new UnderscoreToSeparator(),
new DashToCamelCase(),
new UpperCaseWords(),
]
]);
As you see, it uses 4 times the new keyword, immediately instantiating classes (following Zend Filter Interface). In this case autocomplete works fine, PhpStorm easily found what I wanted typing after new.
But better notation, using factories, is using strings, instead of direct instantiation using new:
$inflector = new Inflector(':string');
$inflector->setRules([
':string' => [
'StringToLower',
'UnderscoreToSeparator',
'DashToCamelCase',
'UpperCaseWords',
]
]);
Is there a way to have autocomplete for those strings? Maybe some annotation hint or something?
Why don't you use UpperCaseWords::class? The resulting value (that will be available during runtime) will be FQN.
I'm not familiar with Zend Framework so I'm just not sure if Zend\Filter\Inflector accepts FQN or it limited to/requires class names only (it should accept FQN ... so user-made classes would also be accepted/it's expected behaviour).
The benefit: refactoring / find usages will also be supported (since this is a piece of code and not just a string).
In any case: class name completion in strings should work since 2017.1.4 (works fine here in current stable 2017.2.4).
You just invoke code completion one more time (e.g. Ctrl + Space twice (or whatever else shortcut you have there on your computer/OS for Code | Completion | Basic)) .. or just use Ctrl + Alt + Space straight away (class name completion).
Obviously, it will work if completion is invoked on the beginning of the string. If it's in the middle/end of it (e.g. "use [CLASS_NAME_EXPECTED_HERE]") -- type whole thing manually or try other completion methods (e.g. Cyclic Expand Word if such class name was already mentioned in current file).

JetBrains: How to watch the return value of a function?

I have a question about debugging mode in JetBrains IDEs (PyCharm, WebStorm, IntelliJ ..). Let's say I have a line in the code that looks like this:
....func1()...func2()...func3()...
Several functinos are called in the same line, and none of them is assigned to a variable. Now, I want to know what is the return value of each of these functions. I know the feature Evaluate Expression, but I don't want to use it, since it may invoke these functions again.
Do you know any way to find the return values of a function without assigning its value to a variable and checking its value in debugger?
As of PyCharm 2016.2, you can show function return values; to do so, you need to:
Click on the Settings gear icon in the left-hand toolbar of the Debug panel
Ensure that Show Return Values is checked
Then when a Return Value is present, you will see it listed under Return Values at the top of the Variables section of the Debug panel (and that information is retained while still in the calling function)
I don't think that this is possible right now but you could set breakpoints inside the functions itself.
Additionally you could add a "Disable until selected breakpoint is hit" + "Disable again" and join them with a breakpoint above the line you posted to make sure they are only called from this line.
Or simply refactor your code:
foobar.huey()
.dewey()
.louie();
and set line breakpoints as usual.
I was looking also for this, and I can link you an answer I found, extending the answer of David Fraser: in IntelliJ, someone replied with screenshots to a similar question in this same site:
java - Can I find out the return value before returning while debugging in Intellij
Remember to put a breakpoint inside the function and step out :)
As said there (although it includes screenshots, much better than this) by the user Birchlabs:
On IntelliJ IDEA 2016.3: it's hidden inside the cog button of the
debug panel. Ensure Show Method Return Values is checked.
IntelliJ IDEA 2016.3 "Show Method Return Values"
Use the debugger to break somewhere inside the function whose return
value you'd like to see.
step into function
Step out of the function (or step over until you escape):
step out
Observe that the return value appears in your variables:
observe the return value

where can I read the source code?

I am using squeak4.1 for development, when I am looking up add method in method chain below: Kernel-Numbers -> Integer -> arithmetic -> + ,the method for adding is +, in + method I find
sample code like this :
ifTrue: [^ (self digitAdd: t1) normalize].
Can I know how I can trace into digitAdd and look the implementation of add method in smalltalk? thanks first!
As the message is send to self, you can query the Integer class itself for its definition. For this, right click Integer in the System Browser, select "find method" and enter `digitAdd' in the search window that appears. Click the "Accept" button. This will show you the message definition.
You can also use the search facility in Squeak. (the search box on the main menu bar).
Select string 'digitAdd:' in text editor, and press Alt-m shortcut or right-click and in opened menu find 'implementors of it'.
This will open a window with all methods in all classes in system which implement given message.

Can IntelliJ auto-complete constructor parameters on "new" expression?

If my class has a non-empty constructor, is it possible to auto-complete parameters in the new expression?
With Eclipse, if you press ctrl+space when the cursor is between the parenthesis:
MyClass myObject = new MyClass();
it will find the appropriate parameters.
--> MyClass myObject = new MyClass(name, value);
When I use ctrl+shift+spacebar after the new, Intellij shows me the constructors, but I can't choose one for auto-completion. Am I missing an option?
I usually start with CtrlP (Parameter Info action) to see what arguments are accepted (auto guess complete is way to error prone in my opinion). And if as in your case you want to fill in name type n a dropdown menu appears with all available variables/fields (etc) starting with n Arrow Up/Down and Tab to select name, or CtrlSpace to select a method (or even CtrlAltSpace to be killed by suggestions;-), followed by , and v Tab for value.
Well I used the eclipse key map where Parameter Info is unassigned.
Here is how to change that:
Well there's the Ctrl+Shift+Space combination, which tries to come up with a set of possible arguments. And if you press the Ctrl+Shift+Space a second time, Idea tries find arguments which fit across multiple calls & conversions.
So in your example Ctrl+Shift+Space would almost certainly bring up the 'name' as suggestion. And the next Ctrl+Shift+Space would bring up 'value' as suggestion.
In Intellij Idea 2016.3 you can use option + return. It will ask you if you want to introduce the named argument for the argument you are on and all the followers.
There's no such possibility yet. As IDEA doesn't fill the arguments automatically, distinguishing the constructors in the lookup makes no sense. There's a request for that (http://youtrack.jetbrains.net/issue/IDEABKL-5496) although I sincerely believe such a behavior is too dangerous and error-prone.

Is there an Xcode version of "Override/Implement Method"?

This is one of my favorite eclipse features. Does it exist in Xcode? I'm getting tired of cutting and pasting from my header files in to my implementations.
Just type "dash" then "space" and start typing the method name that you want to override. Now push Esc.
Example:
- tab
will prompt your to pick a method that overrides any of the TableViewDatasource / Delegate methods. Hit Return and it will automatically provide the return type too...
Here's a pic of what it looks like and notice that I did not provide the return type myself:
Cheers...
This is the kind of task that a user script is useful for. I use this one I banged out in ruby.
#! /usr/bin/env ruby -w
dash="------------------------------------"
r=/(^.+);/ # find entire function definition
pr=/(\w+(:|;))/ #find named parameters to make selector style string
s=STDIN.read
s.each_line() do |l|
m=l.match(r)
if m
n=l.match(/:/)
if n #if the function as one or more parameters
params=l.scan(/(\w+:)/)
puts m.captures[0] + "{\n\n}//"+dash + params.to_s + dash +"\n\n"
else #method has no parameters
puts m.captures[0]+ "{\n\n}//"+dash + m.captures[0] + dash +"\n\n"
end
end
end
To use, select a header method definition, run the script, switch to implementation and paste. This one adds in my preferred method comments boiler plate so you can customized that as you wish.
Check out Accessorizer, it may not be exactly what you're looking for, but it could help in other things that you may like. I haven't used it extensively yet, but I got it as part of one of MobileOrchard's bundle.
Take a look at the ODCompletionDictionary plug-in for Xcode. It allows you to define expandable macros that are configurable with many options. It is an enormous time saver.
With Swift, pressing CTRL+SPACE in the class body will bring up auto-complete for methods. Just start typing the method name.
If you're extending a class, XCode 10 doesn't seem to automatically insert override when necessary.