Get func placeholder values from a snippet to appear after hitting tab at top level code level - swift

New to swift and xcode, version 11.3.1.
If I hit func<tab> in a class, the placeholder values pop in nicely and I can tab through them to complete them.
However, when I have a simple playground file open and hit func<tab> I don't see any placeholder values. Just the keyword func appears.
How do I turn on these placeholder values for top-level functions?

What you are talking about are "code snippets". You can insert them not only by typing the "magic word", but also by clicking on the plus sign on the top right, then choosing from the list:
If you search for "Function", you can find the one you're looking for.
But who wants to click a button when writing code, right? It seems like some of the snippets somehow can't be used in playgrounds, while others can. This seems like an Xcode bug. For me, things like var, varget, vargetset all worked.
As an workaround, you can create your own code snippet for a function. First, paste this code into Xcode:
func <#name#>(<#parameters#>) -> <#return type#> {
<#function body#>
}
Select all of it, then go to Editor -> Create Code Snippet. Give it a name, and most importantly, a "Completion".
Apparently, you can still put func as the "Completion" and it will work.

I'm not sure if there is a better answer but here is what worked to get the func snippet to show up while editing a "playground" file:
1) Edit snippets file with: sudo vim /Applications/Xcode.app/Contents/PlugIns/IDESourceEditor.framework/Versions/A/Resources/SystemCodeSnippets.codesnippets
2) Search for snippet and find the related scope key and modify the array of the scope key to <string>All</string>:
<key>IDECodeSnippetCompletionScopes</key>
<array>
<string>All</string>
</array>
Yes, I could create my own snippet which is the same as the built-in snippet but then I have two different snippets that do the same thing, which I found annoying.

Related

Xcode 13.0 does not automatically complete the function / code

I'm new to Swift programming. When I look at my Udemy course, the functions are always completed automatically.
Example:
"switch" becomes
switch <#value#> {
case <#pattern#>:
<#code#>
default:
<#code#>
}
the word automatically becomes a whole code snippet with a placeholder. When I enter "switch", there is only the dropdown menu where I can select "switch" but the code is not automatically output. It just shows the word "switch".
Does anyone know how to activate the whole thing? My teacher on Udemy couldn't help me either.
I'm using the latest Xcode from the App Store (13.0) on a Macbook M1 2021 with macOs Big Sur 11.6.
Depending on your case.
If you type func viewD, you will get the full template. Otherwise Xcode interprets that you want to call the func, not to override it.
OR
Use Editor > Refactor > Add Missing Switch Cases
Convenient way:
Open Preferences ( [command] + [,] ) in Xcode.
Go to Key Bindings tab and set a shortcut for the command Refactor > Add Missing Switch Cases.
Input the shortcut in editor. Be sure the cursor is on switch letters.

deleting an element in debug mode in eclipse

I'm running my code in debug mode in eclipse and in the middle of it, I want to change the size of a List,say from 9 to 6, by deleting 3 elements.
But I'm not seeing any option to do that, in fact what I'm seeing is the option to change the values present in the elements.
So how can I delete the elements itself from the List ?
Make sure you have the "Variables" tabs on eclipse "Debug" view focused on your current evaluated code.
In "Debug" view, right-click on "Value" cell inside the "Variables" table, and select "Change value".
You will have an option to write a Java expression so you can add something like:
yourList.add("newItem"); or: yourList.remove(0);
Make sure to reload the variable ("F5") once you are done and you will see the updated state.
Note that not every List implementation supports add() or remove() methods.
See this for more details if you encounter an exception.
See also:
Eclipse docs - Variables view
Eclipse docs - Change variable value
I was looking to do something similar. I had an ArrayList containing 1 element, and I wanted to remove it.
I tried #Leet-Falcon's answer, e.g. yourList.remove(0), and Eclipse replied "Unsupported operation exception".
What ended up working was : return new java.util.ArrayList<>();
Looks like the "Debug Shell" view allows this, or additionally if it's a simple enough list, the following simple Change Variable can also work:
new ArrayList<String>(java.util.Arrays.asList("1","2")) // or any other simple list
Modifying Java Collection (List) Variable in Eclipse Debugger

Surround selection with method call

Often when I'm writing code I forget to surround a section of code with a method. For example, when printing an array, I realize that I forgot to pass the array into Arrays.toString().
String[] foo(){
return new String[3];
}
main() {
System.out.println(foo());
}
Is there a way in Eclipse that I can select foo() and then use auto complete or something to surround it with Arrays.toString()? So I want to end up with this:
main() {
System.out.println(Arrays.toString(foo()));
}
I know I could use templates, but I would have to make a template for each method I want to use. I'm looking for something like Eclipse's auto complete feature, which knows about every class and method in the build path.
Yes, you could use templates for that:
First, experiment with existing templates:
Go to the source editor and select "foo()".
Open the view General > Templates.
Select some template, for example, Java > toArray and see how it works.
Then, add your own template:
Windows > Preferences > Java > Editor > Templates > New.
I think the right context should be "Java".
Another way of accesing templates is through the content assist: In the source code, in a new line, start typing the first letters of your template, then press [CTRL][SPACE]. A selector will appear with the matching templates. You may find it useful to check the checkbox "Automatically inserted" in the template definition window.
And yet another way to access them is to select a line of code and then Context Menu > Surround With.
A quick way:
Double click or use select enclosing element and its cousins to select the expression you wish to wrap. ctrl-x to temporarily cut it. Type a few characters and ctrl-space to insert your method name and parentheses. Finally, ctrl-v to paste what you just cut.
with templates - under Java Statements: ${method}(${word_selection})${cursor}
You can make a template like the one described by #LittleSanti. If you use a fake template variable for the method name (like ${method} or ${name}) instead of a constant like foo, Eclipse will highlight it and let you paste or type or complete over it. Then when you hit return or tab, it will jump the cursor to the end (the position indicated by ${cursor}
Unfortunately I don't think Eclipse provides a "real" template variable for selecting methods in scope. It would be nice if it would let did completion for you on methods.

Eclipse shortcut - generate some a method from some code

is there a shortcut to generate a method, when selecting a piece of code in my program? Sth like encapsule code or sth like that. Does that exist in eclipse? I cannot find anything in the help section.
I really appreciate your answer!
In order to move a code part to a method you have to do the following:
select the code which should be extracted
Right click and select: Refactor -> Extract Method
in the option window: enter a method name and click OK

Eclipse auto-formatter, disable auto line-wrapping of comment lines

I love eclipse auto formatting, but there's one feature that's driving me raging mad:
Since I use wrap-lines in my auto-formmatter, code like this:
private static Location _location = null; // this is a comment
turns into horrible, horrible code like this:
private static Location _location = null; // this
// is
// a
// comment
this is not only painful to watch, but not at all convenient to change back...
Is there any way to remove line-wrapping for comments, or at least fix so it doesn't look like an absolute mess?
Thanks
I think that the thing you are specifically asking about you can achieve by editing your formatter:
Window -> Preferences -> Java -> Code Style -> Formatter. There edit you click on Edit...
If you are using the default Eclipse formatter you will need to edit the profile name (you cna not edit built in profile).
Go to comments
Deselect line comment formatting.
That way no formatting of comments of the type // will be done.
This annoys me also. To fix the wrapped comments you can use the keyboard shortcut CTL+ALT+J to join the comments onto one line. The comment-indicator characters (// or *) will still be there but at least it will save you some steps. Then you can just run a find and replace for the comment-indicators.