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.
Related
I would like to write Intellij plugin that can display values returned by class def() in python. I would like those values to be evaluated as much as possible and done by static analysis. I need this to work only for very simple expressions in one particular use case.
We have class definitions in our python code base that consist of a lot of very simple def()s.
All the defs are just one return statement returning very simple expression.
All of the code follows the same pattern and uses very few python operator.
the code is long and really hard to follow.
After few jumps "to definition" within this class I can't remember where I am anymore.
I am hoping that some intellij plugin can lessen the pain.
So for example. this is short and very simplified code fragment. hopefully it will be enough to demonstrate the problem.
class SomeClass(object):
def __init__(self, param):
self.param = param
def a(self):
return self.param + 1
def b(self):
return self.a + otherfunc()
def c(self):
return self.b + 3
I would like the plugin to display the following:
class SomeClass(object):
def __init__(self, param):
self.param = param
def a(self): # = param + 1
return self.param + 1
def b(self): # = param + 1 + otherfunc()
return self.a + otherfunc()
def c(self): # = param + 1 + otherfunc() + 3
return self.b + 3
This is just an illustration, real code makes more sense. but the expressions themselves are that simple.
Comments represent plugin output. I would like those values to be always visible as code hints, tooltips or whatever. and be updated as I type.
I don't want to evaluate the defs, because some of the values are not available before runtime. I want to get the expression itself from AST.
Obviously this is impossible to do in the general case. But I have a very specific use case in our code base
where very small python subset is used. And all the code follows the same pattern.
I already have a script that does this in python with ast module. I wonder if there is a way to do the same on the fly in Intellij.
Is there some way to achieve this? or something similar?
Is there a plugin that does something like that?
I doubt that there is. at least not exactly. So I want to try to implement it myself. (the use case is common and very annoying).
I skimmed through some of Intellij Platform Plugin SDK documentation. it's still not clear to me where to begin.
So what would be the easiest way to implement it from scratch or using another plugin as an example?
Is there an opensource plugin that does something similar that I can look at to figure out how to implement this myself?
My best guess is that I would need to implement:
create a call back that will be called every time def() is changed. (by implementing various extensions, no? which one?)
find this def in the file.
walk expression with PSI to extract the expression
create some GUI element to represent the def expression. (what are my options? is there some predefined elements that I can use?
ideally I would assign value to some existing GUI element)
assign value to the GUI element
but I don't know how to begin implementing any of the above. (I can probably figure out PSI part)
I searched for existing plugins, but couldn't find anything even remotely close. I skimmed the documentation, I did the tutorial, but I couldn't figure out which of the many extensions I need to use.
I considered using the debugger for that, but I don't see how debugger can help me here.
Any help (plugins, tutorials, extensions, plugins as an example, or details for implementation) would be greatly appreciated. thanks.
What you want to find is some extension point that will change the text user sees. I suggest you to look at the annotator class but maybe this is not the best extension point for you and you will need to find more suitable one (this is the most difficult part of creating plugins for JetBrain's IDEs). Full list of all available extension points you can find here.
After you find right extension point you need to implement it and change plugin.xml to let IDE know that some changes were made.
Some useful links:
Example plugins from developers
Official documentation
Quick course from JetBrain's developer (in Russian)
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).
I can't seem to understand how to make the replacements work?
help
I'd ask for clarification in a comment but I don't have enough reputation yet.
If the problem is that you don't understand what they do:
Replacements are what JAutodoc uses to create generic comments based on things like method and parameter names. That's why it can create the comment "Sets the number of questions" above a method called setNumberOfQuestions().
If the problem is that you're having trouble creating custom replacements:
Go to Eclipse Preferences > Java > JAutodoc. There will be a list of the default replacements at the bottom of the window. You can see from there what format to use when you create your own. For example, the default ability to comment "Adds the customer" for a method called addCustomer() is reflected in the list with Shortcut: add; Replacement: Adds the; Scope: method; Replace: Prefix. So let's say you often write methods with names like initializeCustomerList() or initializeArray() and you want JAutodoc to be able to automatically comment those. You would click the Add button next to the list of current replacements and fill in the fields as follows:
Shortcut: initialize
Replacement: Initializes the
Scope: method
Replace: prefix
Similarly, if you want to teach it about common names you use for fields, you would choose the scope "field" instead of "method". Or if you use the same names for both fields and methods you could choose "both".
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.
In my language I can use a class variable in my method when the definition appears below the method. It can also call methods below my method and etc. There are no 'headers'. Take this C# example.
class A
{
public void callMethods() { print(); B b; b.notYetSeen();
public void print() { Console.Write("v = {0}", v); }
int v=9;
}
class B
{
public void notYetSeen() { Console.Write("notYetSeen()\n"); }
}
How should I compile that? what i was thinking is:
pass1: convert everything to an AST
pass2: go through all classes and build a list of define classes/variable/etc
pass3: go through code and check if there's any errors such as undefined variable, wrong use etc and create my output
But it seems like for this to work I have to do pass 1 and 2 for ALL files before doing pass3. Also it feels like a lot of work to do until I find a syntax error (other than the obvious that can be done at parse time such as forgetting to close a brace or writing 0xLETTERS instead of a hex value). My gut says there is some other way.
Note: I am using bison/flex to generate my compiler.
My understanding of languages that handle forward references is that they typically just use the first pass to build a list of valid names. Something along the lines of just putting an entry in a table (without filling out the definition) so you have something to point to later when you do your real pass to generate the definitions.
If you try to actually build full definitions as you go, you would end up having to rescan repatedly, each time saving any references to undefined things until the next pass. Even that would fail if there are circular references.
I would go through on pass one and collect all of your class/method/field names and types, ignoring the method bodies. Then in pass two check the method bodies only.
I don't know that there can be any other way than traversing all the files in the source.
I think that you can get it down to two passes - on the first pass, build the AST and whenever you find a variable name, add it to a list that contains that blocks' symbols (it would probably be useful to add that list to the corresponding scope in the tree). Step two is to linearly traverse the tree and make sure that each symbol used references a symbol in that scope or a scope above it.
My description is oversimplified but the basic answer is -- lookahead requires at least two passes.
The usual approach is to save B as "unknown". It's probably some kind of type (because of the place where you encountered it). So you can just reserve the memory (a pointer) for it even though you have no idea what it really is.
For the method call, you can't do much. In a dynamic language, you'd just save the name of the method somewhere and check whether it exists at runtime. In a static language, you can save it in under "unknown methods" somewhere in your compiler along with the unknown type B. Since method calls eventually translate to a memory address, you can again reserve the memory.
Then, when you encounter B and the method, you can clear up your unknowns. Since you know a bit about them, you can say whether they behave like they should or if the first usage is now a syntax error.
So you don't have to read all files twice but it surely makes things more simple.
Alternatively, you can generate these header files as you encounter the sources and save them somewhere where you can find them again. This way, you can speed up the compilation (since you won't have to consider unchanged files in the next compilation run).
Lastly, if you write a new language, you shouldn't use bison and flex anymore. There are much better tools by now. ANTLR, for example, can produce a parser that can recover after an error, so you can still parse the whole file. Or check this Wikipedia article for more options.