Fix indentation for entire project in Xcode 7? - xcode

I've inherited a huge iOS project which is all using two-space indenting. I need that to be four-space indenting. I know that I can hit Ctrl+I per file, but I'm wondering whether Xcode (or AppCode, for that matter) has a means of re-indenting every source file in the entire project in one whack.
Failing that, I’m wondering how I might go about writing some sort of script (using Automator, or perhaps an Xcode plugin, or even something on the command line) to achieve this without going insane.

You can use SwiftFormat to make indentation on all of your project .swift files
$ brew install swiftformat
$ cd <path to your project>
$ swiftformat .

Refactoring and Re-Indenting are quite different operations. I'm making this distinction so that folks newer to coding / coding IDEs aren't confused.
Refactoring: taking an existing variable / block of code and propagating changes through the project where it is used. For example, refactoring (renaming) a variable foobar to foobar2 would automatically find all cases where foobar is used and change it to foobar2
Re-indenting: changing the structure / flow / alignment of the code to match the desired coding style for clarity / correctness, particularly ensuring that nested items like conditionals and loops are neatly aligned to improve code readability.

You can use SourceKit, but it is a bit complicated to use, so instead you can use SourceKitten which is a wrapper on top of SourceKit.
You have to search for all .swift files and run sourcekitten format --file path/to/file
find . -type f -name "*.swift" | while read line; do
sourcekitten format --file "$line";
done

So, I did manage this with AppCode in the end. We ended up doing a wider-scale refactor using AppCode’s various tools. I’m not sure if I would’ve been able to do JUST the spacing, their Twitter support suggested that I could, but didn’t detail exactly how.

Related

Refreshing Bash prompt after invocation of a readline bound command

My shell is GNU Bash 4.3.11, and I currently have M-h bound to cd .. by calling the builtin
bind -x '"\eh": "cd .."'
This gives me a nifty way to navigate up the directory tree, as I can repeatedly hit M-h instead of the incredibly time-consuming cd ... It has the downside, however, either of not resetting my $PS1 or of not redrawing my prompt, so I lose the context of my current working directory until I enter a new command.
One alternative I'm aware of is to put a macro like
"\eh": "\C-a\C-kcd ..\C-m"
in my .inputrc directly. This, however, has the downside of not only losing the context of any existing command I'm typing in (which I think can probably be worked around) but also of printing out cd .. (which I don't think can be).
My desired behavior is to be able to be in a directory ~/one/two with prompt ~/one/two$; hit M-h; and then be in ~/one and have the prompt be ~/one$, ideally keeping any command I had initially. How can I achieve this?
Figured this out.
# maintain state
bind -x '"\200": TEMP_LINE=$READLINE_LINE; TEMP_POINT=$READLINE_POINT'
bind -x '"\201": READLINE_LINE=$TEMP_LINE; READLINE_POINT=$TEMP_POINT; unset TEMP_POINT; unset TEMP_LINE'
# "cd .." use case.
bind -x '"\206": "cd .."'
bind '"\eh":"\200\C-a\C-k\206\C-m\201"'
I'm quite late to that party - and came here looking for that answer also. First of all: As you were the only one providing information on this: thanks for not letting it come to this: https://xkcd.com/979/ ;) instead you pointed me to the on corner in that fractal that seems to hold a solution.
This approach, in my opinion backed up by hours of trying, is the only one where you can a) replace content on the line, and b) execute bash code. Let me offer up some more suggestions to a specific problem:
If you are trying to have it both ways: insert something on the command line, or executing code, things can get very tricky. for both, there exist bindings, and I let the reader figure out things with help bind. But in the case you e.g. have FZF produce some directory as output, and you'd either cd to it, or have it pasted into your command -- depending on the keystroke done in FZF -- things will get near impossible. you'll face either the not-updated-promt problem, or not be able to execute the cd command in the top shell (where it has effect).
Your solution would be a multiplexing -x binding, inspecting the output for "macros" (get extracted and eval'd) or the default pass-through (manipulating READLINE_LINE/POINT).
Because the solution has some enormity, and the audience may be limited (closed answer...), I'll leave it at a haphazard gist where I pasted my code which works now. To make up for the brevity and uncommented-ness, I welcome any questions in comment or elsewhere. Hope someone may be pointed in the right direction.
- The code related to this question starts in function bindInsertEvalWithMacrosVi
- It is designed for Vi keybindings but the same principles apply for normal readline mode
- It depends on some \C-x\C-... combinations to do redrawing in places that are not related to this post.
https://gist.github.com/simlei/032470cfcd23641987f97a96749128d7

Multi-file search in vim

I am comfortable with vim for normal editing.
I still need to use BBEdit for a few things. Mainly multi-file search. I provide it a folder listing of XCode project and what I want to search for and it produces a listing of files that I can do down through and do whatever.
http://www.mactech.com/articles/mactech/Vol.21/21.02/BBEditDoesntSuck/figure3.jpg
Can I somehow do this in vim? I am using the new maximum-awesome version released by Square: https://github.com/square/maximum-awesome
Given that maximum-awesome includes the ack plugin and the silver searcher package, :Ack string is the easiest way to do this. It also includes unimpaired plugin, so you can navigate the results (in clist) using [q and ]q. :help unimpaired, :help ack.
Don't use that silly distribution, it may feel like a convenient shortcut at first but it will slow you down. Configuring Vim and installing plugins yourself according to your needs is a large part of the learning experience. Skip that part and you'll be hooked to plugins and to someone else's tastes without getting the chance to actually learn to use Vim.
Case in point:
Vim does exactly what you want without plugins.
:vim foo **/*.m | cw
searches for foo in every *.m file under the working directory and opens the quickfix window if matches are found.
See :help file-searching and :h :vim grep.
You have installed a distribution that comes with 35 plugins without even reviewing them and deciding if they are worth installing or not or even actually read up on what they do. One of those plugins, Ack.vim, is there specifically to provide a streamlined (and fast) project-wide search experience. You should read its documentation, :help ack.
And, maybe, try the standard method before you get too used to Ack.vim.
You can use vimg for multi file search.
vimg /search string/ **
** makes it recursive for all file types
**/**.java if you want to search recursively for all java files
* if you want to search current working directory only
copen to open search results
cnext to see next match
cprev to see prev match
cclose to close search results

How to use the "Project Drawer" in TextMate 2 when it doesn't seem to exist?

On TextMate 2 and opening two files in two different locations such as /path/1/file.txt and /path/2/file.txt, I am no longer seeing a way to perform diffs as before since one cannot select files in the project "drawer." We now have a file browser that seems to have taken its place and thus no way to pick the two opposing files. This also precludes any other command that requires multi file selection that are not within the file structure.
Am I missing something that would allow this to work properly when dealing with files in two different paths?
This isn't a new trick. It's one we learned when grep in project would go insane when you had a project with files whose common ancestor was root or some directory far above the files. Instead of opening your files like:
mate /foo/bar/baz /quix/quacks/quux
You do the following, assuming you're in an empty directory or don't care that its files will be included in the project as well
ln /foo/bar/baz /quix/quacks/quux . && mate .
That can obviously be wrapped up into a function to reduce the syntactical difference. In fact, at one point, I actually wrote a wrapper script around mate to do that transparently when needed AND clean up the hard linked files after I closed the project or quit TextMate. That went away with some bad hard drive though.
Anyhow I HTH

Great tools to find and replace in files? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 1 year ago.
Improve this question
I'm switching from a Windows PHP-specific editor to VIM, on the philosophy of "use one editor for everything and learn it really well."
However, one feature I liked in my PHP editor was its "find and replace" capability. I could approach things two ways:
Just find. Search all files in a project for a string, see all the occurrences listed, and click to dive into that file at that line.
Blindly replace all occurrences of "foo" with "bar".
And of course I could use the GUI to say what types of files, whether to look in subfolders, whether it was case sensitive, etc.
I'm trying to approximate this ability now, and trying to piece it together with bash is pretty tedious. Doable, but tedious.
Does anybody know any great tools for things like this, for Linux and/or Windows? (I would really prefer a GUI if possible.) Or failing that, a bash script that does the job well? (If it would list file names and line numbers and show code snippets, that would be great.)
Try sed. For example:
sed -i -e 's/foo/bar/g' myfile.txt
Vim has multi-file search built in using the command :vimgrep (or :grep to use an external grep program - this is the only option prior to Vim 7).
:vimgrep will search through files for a regex and load a list of matches into a buffer - you can then either navigate the list of results visually in the buffer or with the :cnext and :cprev commands. It also supports searching through directory trees with the ** wildcard. e.g.
:vimgrep "^Foo.*Bar" **/*.txt
to search for lines starting with Foo and containing Bar in any .txt file under the current directory.
:vimgrep uses the 'quickfix' buffer to store its results. There is also :lvimgrep which uses a local buffer that is specific to the window you are using.
Vim does not support multi-file replace out of the box, but there are plugins that will do that too on vim.org.
I don't get why you can't do this with VIM.
Just Find
/Foo
Highlights all instances of Foo in the file and you can do what you want.
Blindly Replace
:% s/Foo/Bar/g
Obviously this is just the tip of the iceberg. You have lots of flexibility of the scope of your search and full regex support for your term. It might not work exactly like your former editor, but I think your original 'use one editor' idea is a valid one.
Notepad++ allows me to search and replace in an entire folder (and subfolders), with regex support.
You can use perl in command prompt to replace text in files.
perl -p -i".backup" -e "s/foo/bar/g" test.txt
Since you are looking for a GUI tool, I generally use the following 2 tools. Both of them have great functionality including wildcat matching, regex, filetype filter etc. Both of them displays good useful information about the hit in files like filename/lines.
Visual Studio: fast yet powerful. I uses it if the file number is huge (say, tens of thousands...)
pspad: lightweight. And a good feature about find/replace for pspad is that it will organize hits in different files in a tree hierarchy, which is very clear.
There are a number of tools that you can use to make things easier. Firstly, to search all the files in the project from vim you can use :grep like so:
:grep 'Function1' myproject/
This essentially runs a grep and lets you quickly jump from/to locations where it has been found.
Ctags is a tool that finds declarations in your code and then allows vim to jump to these declarations. To do this, run ctags and then place your cursor over a function call and then use Ctrl-]. Here is a link with some more ctags information:
http://www.davedevelopment.co.uk/2006/03/13/vim-ctags-and-php-5/
I don't know if it is an option for you, but if you load all your files into vim with
vim *.php
than you can
:set hidden
:argdo %s/foo/bar/g => will execute the substitue command in all opened buffers
:wall => will write all opened buffers
Or instead of loading all your files into vim try :help vimgrep and a cominbation of :help argdo and :help argadd
For Windows, I think that grepWin is hard to beat -- a GUI to a powerful and flexible grep tool for Windows. It searches, and replaces, knows about regular expressions, that sort of stuff.
look into sed ... powerful command line tool that should accomplish most of what you're looking for ... its supports regex, so your find/replace is quite easy.
(man sed)
Notepad++ has support for syntax highlighting in many languages and supports find and replace across all open files with regex and basic \n \r \t support.
The command grep -rn "search terms" * will search for the specified terms in all files (including those in sub-directories) and will return matching lines including file name and line number. Armed with this info, it is easy to jump to a particular file/line in VIM.
As was mentioned before, sed is extremely powerful for doing find-and-replace.
You can run both of these tools from inside VIM as well.
Some developers I currently work with swear by Textpad. It has a UI and also supports using regex's -- everything you're looking for and more.
A very useful search tool is ack. (Ubuntu refers to it as "ack-grep" in the repositories and man pages.)
The short version of what it does is a combination of find and grep that's more powerful and intelligent than that pair.

How might I determine the number of lines of code in a C++ project?

Is there a quick way, in Visual Studio, to know how many lines of code exist in a project?
In Visual Studio 2008, right click a project and select "Calculate Code Metrics". It includes a few other metrics like cyclomatic complexity. However, it only counts real lines of code, not empty lines or lines with }'s for example.
Install cygwin, start a bash shell, cd to the top directory and issue something like:
find . -name "*.cpp" -exec cat {} \; | wc -l
Paul.
You could always use the line counter add-in from the sample code.
I've used SourceMonitor. Works well enough.
See also the answers on: Simple script to count NLOC?
For a more general solution which will give you line counts and many more useful metrics, I can strongly recommend Source Monitor which is free and can be integrated with VS.
I regularly use http://cloc.sourceforge.net/ to obtain code metrics on several languages including C++.

Resources