Does Xcode support regions? - objective-c

Does Xcode support anything akin to Visual Studio style #region directives for arbitrary code folding?

No, you can only fold code on various defined scoping levels in Xcode.
You can use little tricks to make navigating via the function menu easier, though.
#pragma mark
Allows you to create a grouping where the label following mark will show up in the function menu. If the label is a hyphen, a separator is inserted into the function menu.
Also, the following labels in comments will show up in the function menu:
// MARK:
// TODO:
// FIXME:
// !!!:
// ???:
Obviously since #pragma mark is not really portable, if you're building a portable application and need it to work with a compiler that doesn't just ignore #pragma directives that it doesn't understand, the comment-style mark is a decent alternative.

I am going to hell for this but here goes:
At the top of a given file, put
#define FOLD 1
Wherever you want to fold something, wrap it in an if block like so:
if(FOLD) {
// your code to hide
// more code
}
That will let you fold it away out of sight.

That won't work in the place you want it most, that is, around groups of functions or methods.
It may be useful inside a long, linear method with no internal conditionals or loops, but such methods aren't common in general Mac OS X UI code, though if you're writing some big numeric or graphics-crunching code it could help group things.
And the if(fold) is entirely superfluous. Just use the braces inside a method or function and Xcode will fold them.

Try this way :
//region title1
{
//region Subtitl1
{
}
//region Subtitl2
{
}
}
It can do like that :

Without support for .Net style regions, being able to collapse all your functions at the same time is the next best thing.
command-option-shift-left arrow
to collapse all.
command-option-shift-right arrow
to expand all.
Xcode will remember the last state of collapsed functions.

A useful option in XCode 12 (maybe before), is an option in preferences "Code Folding Ribbon"
When you check it, the source code looks like this
When you hover the mouse over this ribbon, you get foldable regions based on brackets, like this
When you click the Ribbon, it folds the bracket region, like this
Its not as the regions in Visual Studio, where you can place them wherever you want, but they're good enough to tidy up your code files.

To answer your question...No. And It drives me nuts.
If you have the opportunity/ability you can use AppCode for this. I've been using it for a few years and it usually beats Xcode in many areas.
Also I specifically use AppCode because of these features:
Ability to use regions
Searching classes, text and usages is MUCH faster.
Refactoring is also faster.
Cleaner and more customizable UI.
Tabs are handled (in my opinion) much better than in Xcode.
FOLDING. You can actually change what levels of folding you want. Why Apple thought there should be no quick-key to fold extensions is beyond me. And fold ribbons? Really Apple? Yes they're pretty and all but most professionals use hotkeys for everything.
Better GIT integration.
Support for live updates in SwiftUI
If you use other Jetbrains IDE's like PyCharm or Android Studio the UI is exactly the same.
Some downsides of AppCode:
Some things that work in Xcode aren't supported
Visual #colorLiteral(). When using them they don't show a color picker.
No Storyboard support. Annoying to have to open up Xcode. If you write your UI in code this is a moot point.
Editing .plist files isn't as nice. Doable, but not nice.
Initial indexing can take a while.
Cost. But I would argue the time savings in just navigation will compensate for this.
Kind of a lot for a simple question but I think it's nice having alternatives.

Put your desired code inside brackets { }, and it will become a folding zone.
But you have to keep in mind that brackets also define variables scope, so this code should not have variables declarations which will be used outside these brackets.

One nice solution I just found:
Put your project into one big namespace.
Close and reopen this namespace for the individual sections of your source file:
namespace myproj { // members of class MyClassA
void MyClassA::dosomething()
{
}
void MyClassA::dosomethingelse()
{
}
} // members of class MyClassA
namespace myproj { // members of MyClassB
void MyClassB::dosomething()
{
}
void MyClassB::dosomethingelse()
{
}
} // members of MyClassB

Related

Clang format for banner style

An open-source project I contribute to uses banner style (also called Ratliff style). It looks like that:
// In C
for (i = 0; i < 10; i++) {
if (i % 2 == 0) {
doSomething(i);
}
else {
doSomethingElse(i);
}
}
Some IDE's like QtCreator have their own configurations for formatting, but others, like Visual Studio Code, require a .clang-format file.
I looked online for existing configurations, and couldn't find any. Then I tried to make one from scratch using this clang-format generator, but I couldn't manage to indent the braces right.
So, is it possible to create a clang-format file for Ratliff/Banner style, or is there some missing configuration that will force us to use some other generator?
This answer is not going to be the "answer" that you were looking for, but it is what I've discovered over the last two days of digging through the code for clang-format. I, too, have been looking for a way to auto-format a variant of banner style with clang-format.
clang-format seems to be missing some capabilities that would enable it to auto-format banner style.
For instance, you need to indent ending (right) braces for code blocks. clang-format does not even appear to track ending braces, but it does track starting (left) braces within its internals.
I started out my studying streak thinking I could understand the way clang-format implements its formatter system, so I could add the features needed to do it. Sadly it was way too complicated for me.
Perhaps someday we will be able to auto-format banner style with clang-format, but not today.
If someone out there knows this answer is inaccurate, please don't hesitate to correct me.
edit: Instead of giving you just a verification of what can't be done with clang-format, perhaps you might find http://astyle.sourceforge.net useful.

Intellisense - deduce right Class by its methods

I'm used to work with NetBeans and now I'm trying IntelliJ. So my question is: Does IntelliJ has a way to get right class by its methods?
For example, in NetBeans if I write:
glGenBu // Intellisense will kick in and will suggest me
to use GL15.glGenBuffers() method from GL15 class
This will automatically import the right library.
This is very handy because I'm working with LWJGL and it has a bad way to manage OpenGL methods ('GLXX' where XX is the version of OpenGL and all methods that appeared in that version are stored in that class) and I never remember the right class where my desired method is.
Thank you.
Pressing Ctrl+Space when you already see completion list will show more suggestions, with static methods from the entire project among them. See https://www.jetbrains.com/help/idea/auto-completing-code.html for more details.

Why doesn't Xcode suggest #synchronized?

I only rarely use #synchronized, but as far as I can remember (meaning around Xcode 3.2 or something), it never suggested #synchronized when using the auto-completion, and still never does.
I do get suggestions when typing '#', like #autorelease, #encode, #selector and so forth.
Is there any known reason for this ? I wasn't able to find any related topic. It's been bugging me, because it gives me the feeling that this is not a valid method to handle concurrency in iOS.
#synchronized does not appear when using auto-completion because Apple has not mapped that keyword to any code sense implementation.
Per Apple's suggestion, I have filed a bug report.
However, I can offer an auto-complete solution.
You may create an #synchronized snippet that can be auto-completed or drag and dropped into your code.
Copy/paste the code below into one of your Xcode documents.
Select the new code and drag it to your snippets library.
Double-click the new snippet and click edit.
Enter #synchronized for the completion shortcut.
For more info: http://nshipster.com/xcode-snippets/
#synchronized(anObj) {
// Everything between the braces is protected by the #synchronized directive.
}

Eclipse custom text editor update syntax highlighting

I am writing an Eclipse plugin (Indigo/Juno) that contains a text editor for a custom text format. I am following the tutorial here: http://www.realsolve.co.uk/site/tech/jface-text.php
So far I have everything working. Eclipse will use my editor to edit files. I have partitioning, damaging, repairing, syntax highlighting all working.
I added a preferences page with color pickers to control syntax highlighting. It works mostly correct. If I update the colors, the editor uses them the next time I open or reopen a file.
How do I get an editor tab to update itself without opening a new one? The built-in JDT Java editor does this, but so far I have not been able to decipher how (it is a very large and complex editor).
I gather that I need to create a preferences listener (http://www.vogella.com/articles/EclipsePreferences/article.html). I have done this and can verify that my listener code is being invoked when I set a breakpoint in it.
The missing piece is the wiring between the listener and reinitializing the editor. I have tried reconstructing the partitioning logic, the color logic, the damager/repairer, etc. but nothing seems to work. It either does nothing I can see or at worst will corrupt the display until I scroll the current text out of view to repaint it... with the old colors.
Any ideas?
I think SourceViewer.invalidatePresentation() needs to be called.
It may be already late to you, but if you want you could use LiClipse for that (http://brainwy.github.io/liclipse/) -- one of its targets is easily doing an editor with syntax highlighting, basic code-completion, outline, etc targeting Eclipse.
No java skills are required to add a new language (mostly creating a new .liclipse -- which is a YAML -- file in the proper place and creating some basic rules to say how to partition your language -- i.e.: usually just separating code from comments from strings -- and specifying the keywords you have in the partition would already give you proper syntax highlighting).
If you download it, there are a number of examples at plugins\com.brainwy.liclipse.editor\languages and there's some basic documentation at http://brainwy.github.io/liclipse/supported_languages.html and http://brainwy.github.io/liclipse/scope_definition.html on how to do it.
For anyone coming across this as I did:
My solution involved adding the following lines into the Constructor of my Editor
Activator.getActivator().getPreferenceStore().addPropertyChangeListener(new IPropertyChangeListener() {
#Override
public void propertyChange(PropertyChangeEvent event) {
getSourceViewer().invalidateTextPresentation();
handlePreferenceStoreChanged(event);
}
});
and then creating a custom class that extended IToken. In the constructor I pass the String of the preference field and then in the 'getObject' method I create the TextAttribute: snippets below
public class MyToken extends Token implements IToken {
public MyToken(Object data) {
super(data);
}
#Override
public Object getData() {
String dataString = (String) super.getData();
return getAttributeFromColorName(dataString);
}
private TextAttribute getAttributeFromColorName(String preferenceField) {
Color color = new Color(Display.getCurrent(), StringConverter.asRGB(Activator.getActivator().getPreferenceStore().getString(preferenceField)));
return new TextAttribute(color);
}
}
When I generate my Rules I have all of my tokens as my custom class and this allowed me to change syntax color dynamically.
I also added an example for updating the coloring if the preference changes to https://www.vogella.com/tutorials/EclipseEditors/article.html#exercise-allow-user-to-customize-the-colors
This is using the Generic editor (currently the best approach to implement a customer editor) but it should be possible to adjust this to any Eclipse editor implementation.

How do I remove icons from menu items in an Eclipse RCP-based application?

I am working on an Eclipse RCP-based application, and we have decided that we do not want any of the menu items to display icons next to the text. The problem we are seeing is that the standard actions like Undo, Redo, Cut, Copy, Paste, and so on all display the default icons for the corresponding actions.
Is there any way to tell the action management infrastructure to ignore the icons? My brute force solution to this was to rebuild the SWT so that MenuItem.setImage() was a no-op, and then include our own copy of the SWT in the final product, but it seems like there should be a lighter-weight solution.
This turned out to be easier than I had hoped.
Create a subclass of org.eclipse.ui.application.ActionBarAdvisor. Override the method register like this:
protected void register(IAction action) {
super.register(action);
action.setImageDescriptor(null);
}
Then, create a subclass of org.eclipse.ui.application.WorkbenchWindowAdvisor that overrides createActionBarAdvisor:
public ActionBarAdvisor createActionBarAdvisor(IActionBarConfigurer configurer) {
return new MyActionBarAdvisor(configurer);
}
That's it. All actions will no longer have icons.
I believe you want to further examine going into the manifest and looking into
org.eclipse.ui.views and seeing if there is anything in there for removing icons
What is the reason for not including icons?
A lot of effort went into creating a standard interface, what would be the benefit of deviating from the standard? Do you think their omission increases usability?
Having said all that you could try contributing a fragment with some AspectJ around advice to intercept calls to setImage() and veto them.
You can do this by going to the extension tab in plugin.xml.add the extension org.eclipse.ui.menu (if not present).Right click create a new menu contribution.again right click and create a new menu.here u have the option to change the images with the ones saved in your icon folder in your class path