Xcode source automatic formatting - xcode

As a C# developer, I have become highly dependent on the automatic formatting in Visual Studio 2008. Specifically, I will use the CTRL + K , D keyboard shortcut to force things back into shape after my sloppy implementation.
I am now trying to learn Objective-C and am missing certain features in Xcode, but probably none are quite as painful as the formatting shortcut. My Google searches have yielded nothing built in, though it seems there are some hacks. Am I missing something or does this feature not exist natively in Xcode?

That's Ctrl + i.
Or for low-tech, cut and then paste. It'll reformat on paste.

Unfortunately, Xcode doesn't have anything nearly as extensive as VS or Jalopy for Eclipse available. There are SOME disparate features, such as Structure > Re-Indent as well as the auto-formatting used when you paste code into your source file. I am totally with you, though; there definitely should be something in there to help with formatting issues.

I'd like to recommend two options worth considering. Both quite new and evolving.
ClangFormat-Xcode (free) - on each cmd+s file is reformatted to specific style and saved, easy to deploy within team
An Xcode plug-in to format your code using Clang's format tools, by
#travisjeffery.
With clang-format you can use Clang to format your code to styles such
as LLVM, Google, Chromium, Mozilla, WebKit, or your own configuration.
Objective-Clean (paid, didn't try it yet) - app raising build errors if predefined style rules are violated - possibly quite hard to use within the team, so I didn't try it out.
With very minimal setup, you can get Xcode to use our App to enforce
your rules. If you are ever caught violating one of your rules, Xcode
will throw a build error and take you right to the offending line.

In xcode, you can use this shortcut to Re-indent your source code
Go to file, which has indent issues, and follow this :
Cmd + A to select all source codes
Ctrl + I to re-indent
Hope this helps.

My personal fav PrettyC wantabe is uncrustify: http://uncrustify.sourceforge.net/. It's got a few billion options however so I also suggest you download UniversalIndentGUI_macx, (also on sourceforge) a GUI someone wrote to help set the options the way you like them.
You can then add this custom user script to uncrustify the selected text:
#! /bin/sh
#
# uncrustify!
echo -n "%%%{PBXSelection}%%%"
/usr/local/bin/uncrustify -q -c /usr/local/share/uncrustify/geo_uncrustify.cfg -l oc+ <&0
echo -n "%%%{PBXSelection}%%%"

You can use Command + A to select all content and next Ctrl + I to format the selected content.

I also feel xcode should have this function.
So I made an extension to do it: Swimat
Simple install by brew cask install swimat
You can give it a try, see https://github.com/Jintin/Swimat for more information.

Cmd A + Ctrl I
Or Cmd A And then Right Click. Goto Structure -> Re-Indent

Consider buying yourself a license for AppCode, an intelligent Objective-C IDE that helps iOS/OS X developers. AppCode is fully compatible with Xcode, but goes beyond Xcode in adding powerful features.
AppCode an Objective-C variant of the Intellij IDEA IDE from JetBrains. They are also authors of popular ReSharper extension to Visual Studio, which main purpose from here seems like a desperate attempt to bring a touch of IDEA experience to a Microsoft product.
AppCode is using its own code analyser which gives close-to-perfect refactoring and code navigation support. There is an ability to re-indent and completely reformat code also (although I still keep missing a couple of formatting settings in hard cases, but mostly it works well).
You might try the trial version, of course.

Swift - https://github.com/nicklockwood/SwiftFormat
It provides Xcode Extension as well as CLI option.

CTRL + i
that's it.
(no COMMAND + i)

You can also have a look at https://github.com/octo-online/Xcode-formatter which is a formatter based on Uncrustify and integrated into Xcode. Works like a charm.

You could try that XCode plugin https://github.com/benoitsan/BBUncrustifyPlugin-Xcode
Just clone github repository, open plugin project in XCode and run it. It will be installed automatically. Restart Xode before using formatter plugin.
Don't forget to install uncrustify util before. Homebrew, for exmaple
brew install uncrustify
P.S. You can turn on "after save formatting" feature at Edit > Format Code > BBUncrustifyPlugin Preferences > Format On Save
Hope this will be useful for u ;-)

I suggest using ClangFormat. In order to install, please follow these steps:
Install Alcatraz package manager for XCode
Supports Xcode 5+ & OS X 10.9+
After installation restart XCode.
Open XCode -> Window Menu -> Package Manager
Search (find) ClangFormat and install it. After installation again restart XCode.
Now at XCode menu you can use Edit -> Clang Format submenu for formatting.
You can choose different types of formatting. Also by enabling Format On Save you can gain auto-format capability.

If your Xcode version 3.x , you should use "User Script" With Uncrustify , here this a Example:
#!/bin/sh
echo -n "%%%{PBXSelection}%%%"
$YOURPATH_TO_UNCRUSTIFY/uncrustify -q -c $YOURPATH_TO_UNCRUSTIFY_CONFIG/CodeFormatConfig.cfg -l OC+
echo -n "%%%{PBXSelection}%%%"
add above to your Xcode "User Script".
if Xcode version 4.x , I think you should read this blog : Code Formatting in Xcode 4,
In this way , used the "Apple Services" , but it's not good enough , cause too slow experience, does anyone has the same thing ?
why apple drop "user script" .... xD

First, Examine XCode Preferences "Indentation" section. You can customize things quite a bit there...
For more fine grained control, refer to the XCode User Defaults document from apple. (May require a developer login to view). For example, I was able to disable the "indent on paste" by entering the following in terminal:
defaults write com.apple.XCODE PBXIndentOnPaste No
to read back your setting:
defaults read com.apple.XCODE PBXIndentOnPaste

This only works for languages with are not whitespace delineated, but my solution is to remove all whitespace except for spaces, then add a newline after characters that usually delineate EOL (e.g. replace ';' with ';\n') then do the ubiquitous ^+i solution.
I use Python.
Example code, just replace the filenames:
python -c "import re; open(outfile,'w').write(re.sub('[\t\n\r]','',open(infile).read()).replace(';',';\n').replace('{','{\n').replace('}','}\n'))"
It 's not perfect (Example: for loops), but I like it.

We can use Xcode Formatter which uses uncrustify to easily format your source code as your team exactly wants to be!.
Installation
The recommended way is to clone GitHub project or download it from https://github.com/octo-online/Xcode-formatter and add the CodeFormatter directory in your Xcode project to get :
Xcode shortcut-based code formatting: a shortcut to format modified sources in the current workspace
automatic code formatting: add a build phase to your project to format current sources when application builds
all sources formatting: format all your code with one command line
your formatting rules shared by project: edit and use a same configuration file with your project dev team
1) How to setup the code formatter for your project
Install uncrustify
The simplest way is to use brew:
$ brew install uncrustify
To install brew:
$ ruby –e “$(curl –fsSkl raw.github.com/mxcl/homebrew/go)”
Check that uncrustify is located in /usr/local/bin
$ which uncrustify
If your uncrustify version is lower than 0.60, you might have to install it manually since modern Objective-C syntax has been added recently.
Add CodeFormatter directory beside your .xcodeproj file
Check that your Xcode application is named "Xcode" (default name)
You can see this name in the Applications/ directory (or your custom Xcode installation directory). Be carefull if you have multiple instances of Xcode on your mac: ensure that project's one is actually named "Xcode"!
(Why this ? This name is used to find currently opened Xcode files. See CodeFormatter/Uncrustify_opened_Xcode_sources.workflow appleScript).
Install the automator service Uncrustify_opened_Xcode_sources.workflow
Copy this file to your ~/Library/Services/ folder (create this folder if needed).Be careful : by double-clicking the .workflow file, you will install it but the file will be removed! Be sure to leave a copy of it for other users.
How to format opened files when building the project
Add a build phase "run script" containing the following line:
sh CodeFormatter/scripts/formatOpendSources.sh
How to format files in command line
To format currently opened files, use formatOpenedSources.sh:
$sh CodeFormatter/scripts/formatOpendSources.sh
To format all files, use formatAllSources.sh:
$sh CodeFormatter/scripts/formatAllSources.sh PATH
PATH must be replaced by your sources path.
E:g; if project name is TestApp then the command will be
$sh CodeFormatter/scripts/formatAllSources.sh TestApp
it will look for all files in the project and will format all the files as configured in uncrustify_objective_c.cfg file.
How to change formatter’s rules
Edit CodeFormatter/uncrustify_objective_c.cfg open with TextEdit

Well I was searching for an easy way. And find out on medium.
First to copy the json text and validate it on jsonlint or something similar. Then to copy from jsonlint, already the json is formatted. And paste the code on Xcode with preserving the format, shortcut shift + option + command + v

Related

VScode format as save not work for Golang

I'm using vscode and developing a go project via remote-ssh. However, the format on save does not work. I found it works on the normal project without git. If I remove the .git, it will work as expected. Does anyone have some advice for me?
The version of the environment is as follows:
vscode 1.63.2
go 1.17.6
remote machine Ubuntu 20.04
go extension v0.30.0
I had the same issue recently, auto format would only work on projects without a .git directory. What solved the issue for me was to change Format On Save Mode from modificationsIfAvailable to file. Since I prefer modificationsIfAvailable over file for everything, I changed to have formatOnSaveMode to file only for go source files with:
"[go]": {
"editor.formatOnSaveMode": "file"
}
Try to format your code manually by pressing Ctrl + Shift + P to open Command Palette and select Format Document. If your file is being properly formatted without any issues, it means there is something wrong in formatOnSave settings.
Choose a default formatter instead of null. like this :
"editor.defaultFormatter": "esbenp.prettier-vscode"
Uninstall other formatters one by one to see if anything causes the conflict.
Try to make it Language specific.
For better understanding refer this link : https://github.com/microsoft/vscode/issues/108447
About Git Specific:
It may be the case that you are running Git Atom. Atom is a desktop application, so may be causing problems in accessing the remote installation path.Try installing Git Repo.
Also make sure Go: Install/Update Tools are up-to-date.

Project specific .lldbinit in current working directory not read by Xcode

I want to setup project specific .lldbinit files, so that "everything" would be under version control and easily setup in new computers. However it seems like I can read only ~/.lldbinit file and not any ~/git/project/.lldbinit files.
LLDB manual say:
lldb will read settings/aliases/commands from three files at startup, if
they exist.
First, it will read a ~/.lldbinit-debugger command file. If you are
using the lldb command line interface, this is ~/.lldbinit-lldb. If you
are using lldb inside a GUI debugger like Xcode this will be ~/.lldbinit-
Xcode. This is a useful place to put settings that you want to apply
only when a given lldb command interpreter is used.
Second, ~/.lldbinit is read.
Third, an .lldbinit file in the current working directory (where lldb is
started) will be read.
So my question is: how do I setup Xcode or project to use the "third" option i.e. read .lldbinit file from the current working directory? How would I check or change what's Xcode's "current working directory"?
Xcode now (written with the public Xcode 12) has support for setting a scheme-level LLDB Init File. This can be done as part of the Info tab in the Run phase of the scheme.
Open project in Xcode.
⌘ + Shift + , to open scheme editor.
Select Run then the Info tab.
Edit LLDB Init File value.
Use $(PROJECT_DIR) to reference the directory that contains the project.
Use $(SRCROOT) to reference the target's source root directory.
This is the working directory in which you launch whatever process loads the LLDB framework. Xcode doesn't have a useful working directory (it was / last time I looked), so you can't really use the cwd version.
A solution for Xcode project specific lldb settings that will work in many cases is to put a symbolic breakpoint on main, set it to auto-continue, then put the settings you want in the breakpoint commands of that breakpoint.
It would also be great if Xcode had some UI to specify target-specific lldbinit files.
There is now a "Xcode plugin to load project specific .lldbinit" at https://github.com/alloy/lldb-is-it-not
Xcode now has support for Target specific lldb init files. There's an entry form for the path to the file in the Run scheme for the Xcode target.

Fresh (clean) install of Lion has no developer tools and Xcode 4.3 no longer includes make and friends

Any ideas on how to obtain make and whatever other cruft came along with earlier versions of Xcode? The '/Developer' directory no longer exists as I stupidly got rid of it with the upgrade to Xcode 4.3.
Additionally, it appears the CORE directory in '/System/Library/Perl/5.12/darwin-thread-multi-2level' is missing header (.h) files. I believe these were also populated with earlier versions of Xcode.
I'm unable to build any Perl modules without these, specifically through CPAN. Any help is greatly appreciated! Thanks in advance.
As I see it you have two options:
Way 1
a) Using the "App Store" install Xcode Version 4.3.
b) Run Xcode tools and complete the installation process.
c) Once Xcode is open go to the menu Xcode -> Preferences...
d) Go to the downloads tab.
e) Click "Install" by the last of the four options (Command Line Tools (171.7 MB).
f) This will require you to make a developer account.
g) Do so here: Apple Developer Registration
h) Either create a new ID or use an old one.
i) After filling in all your information you will get to a page where you must select one of three Developer Programs.
j) Select the bottom (free) Safari Developer Program.
k) Now go back to the Xcode -> Preferences >> Downloads tab.
l) Click install and put in your Apple ID and password.
Way 2
1) Open the termial
2) Type "cd" and press enter.
3) Type "vim .profile" and press enter. This will either create a new file called ".profile" or edit your existing one.
4) Put "export PATH=/Applications/Xcode.app/Contents/Developer/usr/bin:$PATH" on its own line somewhere in the file.
5) Press ESC.
6) Press SHIFT + ':' (You should now see a semi-colon in the bottom of the window).
7) Type "wq". (You should now see ":qw" in the bottom of the window).
8) Press enter.
9) Quit Terminal.
10) Open Terminal (This reloads the changes you made in .profile)
11) Type "which make". The result should be "/Applications/Xcode.app/Contents/Developer/usr/bin/make"
If you have any errors (may or may not be necessary)
Try changing the compiler that you are using. If you provide more information I might be able to suggest an alternative.
You want the "Command Line Tools for Xcode - February 2012", available from https://developer.apple.com/ (free developer login required).
…which also says that this very package can be downloaded using the Downloads preferences pane within Xcode 4.3 and later.
No idea if it has Perl stuff, though.
I had difficulties after upgrading to Lion on my older macbookpro. I had no perlfaqs and other documentation. I actually added these by hand. This convinced me to just install a separate complete version of 5.14 in /usr/local and just not mess with the system version.
To do that install the Xcode tools from the App store, then download the source to /usr/local/src (or anywhere actually) and run:
./configure
make
make test
sudo make install
Then open Terminal and type:
export PATH="/usr/local/bin:/usr/local/sbin:$PATH"
You should be able to run cpan and install whatever modules you want and when you upgrade the operating system again it won't overwrite it.
You can just run in Terminal
xcode-select --install

How to set up Jeash for Haxe?

I am trying to setup Jeash for Haxe, so I read this: http://old.haxe.org/com/libs/jeash/firststeps
Now, I found out that I cannot do that just yet:
"This is the first time you are runing haxelib. Please run haxelib setup first"
(and I love the typo in there :P )
So I decided to read this: http://haxe.org/com/haxelib/setup
I tried what is suggested. First command, I get prompted for a password, so I enter what is necessary when I install software on my comp.
Then I ran "sudo chown jansensan -R /usr/lib/haxe" (replacing "myusername" with my actual username, "jansensan") and then I get an error: "chown: -R: No such file or directory"
Is there a way to get a little bit more information? I mean, the explanation on the Haxe website makes it sound as it if will magically happen, and it is not the case.
For anyone interested (if it helps you understand my issue), my computer is a MacBook Pro running Snow Leopard and I intend to code in FDT. BTW, I can already do some Haxe stuff in FDT, why would I need to run the Haxe setup to be able to install Jeash and NME?
haxelib is a lib repositories .It adds libs to your workflow. jeash and NME are one of them.
once you have installed haxe. you just have to run in the terminal haxelib setup.Normally it goes fine.
I'm on macpro snow leopard too.Never had issues with that !.
test haxelib on terminal .. wath does it tell ?
Another alternative is to use the NME installer:
http://www.haxenme.org
It will install Haxe, Neko, HXCPP, NME and Jeash in one go. NME supports Windows, Mac, Linux, Android, iOS, webOS, Flash and HTML5 -- the HTML5 target handled using Jeash.
You're using FDT and no other Haxe installation right? This is important because FDT comes with it's own 'installation' of Haxe.
You have it two ways:
Add it to Haxelib.
Just download the source code and put the jeash library in the
project your using.
Add it to Haxelib.
For command line: you're going to need to make haxelib visible to your command line.
Get started by typing 'which haxelib'. This will tell you
1) if you have haxelib via command line.
2) Where it is.
If there is an error, then it's not visible to your command line. This is ok, you'll just need to add the whole path when calling it. Like this:
Also notice that since FDT 5 is shipped as an OSX bundle you'll have to go 'inside' of it via right click FDT -> 'Show Contents'.
When you have this path (or if you are using your own version of the haXe SDK ), you can use it to call haxelib.
Add it to source path
Download the library source code and physically add it to your source path.
All these suggestions are quite good. An alternative to dealing with all this Terminal writing is to go to http://www.haxenme.org/developers/get-started/ and use the installers provided. Afterwards, go in the prefs of FDT > Haxe SDK and add the path where the haxelib was installed, "/usr/lib/haxe" most likely.

How can I test my vim plugin?

Hi guys : I recently (accidentally) removed all folders/files from my .vim folder in mac os x (home directory).
I am trying to add in the Clojure Vim plugin (VimClojure) - its simply a folder which you are supposed to "drop into .vim/plugins".
I have added it, but I don't see any changes to the syntax highlighting when I launch vim. I'm not sure wether vim "sees" the plugin or not.
I'm on OS X .
Any ideas on how to debug the plugin ? In particular
1) How does VIM look for plugins ?
2) Are there files which need to be in $HOME/.vim/ ?
3) Is it sufficient to simply dump the unzip a new plugin file into $HOME/.vim/plugins when installing a standard vim plugin ?
Thanks
About debugging: in order to see whether vim has loaded your plugin you can use :scriptnames and also breakadd file /path/to/your/plugin (or breakadd file *your_plugin_name.vim: I never used absolute paths so I do not know what breakadd will do in this case). Other questions:
Described in :h initialization, precisely :h load-plugins.
Vim does not need any files at all (except vim executable, used shared libraries, dynamic linker and the kernel of course).
Follow installation instructions. Normally plugins are either extracted to ~/.vim or distributed as a singe file that should go to either ~/.vim/plugin (no s!), ~/.vim/colors, ~/.vim/ftplugin or such. I guess you should try to extract it to ~/.vim/plugin, but if archive contains some special directories like plugin/, ftplugin/, colors/, after/ (see /usr/share/vim/vim73 for a list) it is likely that it should go to ~/.vim. Also consider using vim-addon-manager, if plugin was posted on vim.org VAM is likely to be able to install it.
A few points.
How does vim look for plugins? See :help startup so see where and when vim looks for files to load.
Not really. Anything there is just personal customization. Vim will run fine without a .vim folder.
That all depends on the plugin. It sounds to me like the VimClojure plugin may be a little misleading. Do you have a link to the source you are using?
In any case, the first step I always take when attempting to debug a script is check the output of :scriptnames. This command will show you what scripts vim has loaded for the current session. If you see none of the files shipped with VimClojure, you probably made a mistake during the installation.
Another tip is that you really should look in to using a plugin manager such as vundle or vim-addon-manager, or at least the runtimepath manager pathogen. This seems to be the way of the future for vim configuration these days and it makes installing and managing plugins much easier. They also help to keep your .vim folder clean and organized.
The VimClojure directory should either be extraction on top of your .vim folder, or in a bundle folder if your using something like pathogen (which you should!). If you're starting from scratch, consider starting with vimclojure-easy (not to toot my own horn) which is a basic, full install of VimClojure with instructions.

Resources