I believe it is called "cuddle" style:
function foo() {
// blah
}
function foo()
{
// blah
}
Does anyone know of any scripts that will go through a file and toggle in one direction or the other? My goal is TextMate integration through a bundle, but any script in any language should be fine. I believe I can hook most any language into textmate through a bundle.
You might get away with using astyle, though it doesn't specifically support JavaScript, it's loose enough that astyle -b does format your example correctly.
http://www.bierkandt.org/beautify/ is how I solved this. I made a new bundle in Textmate of
/path/to/php ~/Library/Application\ Support/TextMate/beautifyphp/beautify_php -i 5 -v 0 -l -b 0
Now all I do, is select my chunk of php, run the bundle command, and it will reformat it to my liking. It does other nice things, like align equals as well. Granted, align equals is something you can do with the "source" bundle in TextMate already.
The only downside is that it shoves in a "made pretty with phpBeautify" or something like that at the bottom, but it is a quick delete. I am sure I could strip it inside the bundle, but this work is not something I do that often.
Switching indentation styles is a form of pretty-printing or code beautifying.
For Javascript, the online tool at http://jsbeautifier.org/ also has the code available for download.
For other languages, there's a comprehensive list of code beautifiers on Wikipedia http://en.wikipedia.org/wiki/Prettyprint
You can script TextMate with Javascript.
Look at the CSS bundle in TextMate as it does a very similar thing when you press C-q
Related
I've been hacking away at the 4clojure.com problem set and having a lot of fun. I've done a few dozen of the Elementary and Easy level problems.
However, my "development" environment for this work leaves something to be desired. Basically, I'm writing one-liners, either in the Leiningen REPL or Sublime Text's "SublimeREPL" for Clojure.
I'd like to be able to unpack those one-liners as I work (few solutions need to be longer than a few lines that I've seen so far, but one line is pretty condensed at least for my newbie eyes).
My desired workflow is, as I implied in the title, is to have a single file for each solution, and to be able to iteratively hack on it, save it, run it and see the output.
I'm on OS X and Ubuntu; my primary editor is Sublime Text, but I do use Emacs as well. CIDER perhaps?
Emacs with, as Charles suggests, either the Emacs Live config or Emacs Prelude (or mine).
Open a file, hack away, and run
Ctrl-cAlt-j to start up a clojure repl through Cider (nrepl's replacement)
Ctrl-cC-k to load the file
Ctrl-cAlt-n to put the repl in that namespace,
and hack away.
And if you're installing Emacs Live you might as well play with Overtone and make some "music"
I have a research project with several files (~100).
The code has been written over the years without any specific style. Each developer (mostly master students that come, code and leave) used their own "style", if any.
Now, I'm trying to maintain the code in a way to make new people that join us follow certain rules. I found that Google published some style-guide. Luckily enough they published also a python script, that is easy to use.
The problem is, the script gives me for each file a tone of silly errors like
Missing space after , [whitespace/comma] [3]
or
Missing space before { [whitespace/braces] [5]
My question is: Is it somehow possible to automatize the correction of such "errors"? That mean running a script over a file that eliminates automatically all those errors.
clang-format might be useful, as it can be run with an option to use Google style rules:
clang-format -style=Google ...
See e.g. http://clang.llvm.org/docs/ClangFormatStyleOptions.html
To update this answer for those of you using vscode, there's an extension there called clang-format and in the settings there is the parameter
Clang-format › Language › Cpp: Style
clang-format fallback style for C++, left empty to use clang-format.style
Typing google in the text box should enable application of the Google formatting rules.
So, I've looked everywhere for a good rainbow parentheses plugin that will give different level parentheses different colors. I really like the couple that I've found, because they both do a good job of customizability while highlighting the right thing. It supports more than parentheses; chevrons, braces and brackets all get highlighted, which I really like.
It seems like there are quite a few plugins for this!; I'm currently using oblitum's because his is optimized for dark backgrounds (I often work straight from the shell).
So, following the tip at the github for that plugin, I have the "always on" snippet in my .vimrc. But when the always on block is above "syntax enable" it doesn't show {} as being highlighted for cpp files. When the always on block is below syntax enable, folding doesn't work. I think its the nature of the plugin that makes it do this; it goes though the file and adds coloration information. I notice that if I use the command :syntax enable after I've loaded the file when its not recognizing folds, then it does recognize the folds. But at this point, it removes the coloration that rainbow parentheses put on it.
In my .vimrc, I have the follow pertinent lines:
syntax enable
set foldmethod=syntax
set foldenable
set foldlevel=100
let g:rainbow_operators=2
au FileType c,cpp,objc,objcpp call rainbow#activate()
I think that, from looking at syntax files that come with vim, such as c.vim, you can see that certain blocks are annotated as folding. I bet that if you could just write a regex based upon it, you could identify characters as syntactic groups. Then you could just define a colorscheme for it. In fact, the rainbow plugin is actually calling "syn region" commands, so I think that this route is very doable, I'm just not that knowledgable with vim scripting.
Can anybody help me modify possibly the plugin or come up with a script or something that achieves both?
Sorry the lateness, I've tried to solve it at Fix disabled folding (issue #2)
I dimly remember having had the same or a similar problem.
Also XML code highlighting was broken IIRC.
I put 'always on' off, and activated the colored parentheses only when I needed it.
So my working solution was just a shortcut to toggle the plugin on and off.
For example, I'd like to not indent namespaces in C++ code, but the prefpane doesn't seem to have any place to make a decision of this granularity. Is there some hidden config file or something? Or am I just out of luck?
Apple's XCode documentation contains a full list of user preferences, many of them that don't have a corresponding UI. I'm not seeing anything that is namespace-specific however, so I think you might be out of luck.
However, I thought I'd pass along the preferences list in case it's useful.
I've also attempted to do this.
The answer is that whoever did the code formatting in XCode appears to be completely unaware that there are languages other than Objective C, or coding styles other than Apple's.
Here's a list of things that one would want to do that can't be done in XCode.
Indent public: or private: just one space.
Indent namespaces zero spaces.
Alternate indentation for arguments NOT relative to the opening parenthesis.
The last one needs a little discussion. Sometimes, a function or method name can be quite long, as well as its first argument, so you want also to be able to indent like this:
someExcitingClass->AVeryLongMethodNameTraLaLaLaLa(
someLongExpressionOrVariableNameGoesHere,
anotherNameHere);
Of course, you might want to be extracting subexpressions to make the line shorter, but in real-world code this comes up all the time, and creating subexpressions just to fit everything into a reasonable line length is annoying.
It's a terrible shame and I really have no idea what to do. I personally write in emacs and only dip into XCode as a build system but :-D that's not for everyone.
As the Xcode indenter uses just the lexer, and not the AST, you can 'fool' the formatting by defining away the curly braces.
I have:
#define NAMESPACE_OPEN(_name) namespace _name {
#define NAMESPACE_CLOSE(_name) }
#define dsmsg_namespace_open NAMESPACE_OPEN(dsmsg)
#define dsmsg_namespace_close NAMESPACE_CLOSE(dsmsg)
i.e., a generic 'NAMESPACE_OPEN/CLOSE' define, and a define specific to my most-used namespace 'dsmsg'. Then, whenever I want to open the namespace, I use
dsmsg_namespace_open
... code ...
dsmsg_namespace_close
Ugly hack, but I quite like having a specific, named 'close'
I bypass Xcode's indenting altogether, and have a user script that calls uncrustify on the currently displayed document.
#!/bin/sh
#echo -n "%%%{PBXSelection}%%%"
uncrustify -q -c ~/.uncrustify/sample.cfg -l oc+
#echo -n "%%%{PBXSelection}%%%"
Notes:
uncrustify must be in your PATH
you may need to adjust the location of your config file
if you want to have the new code selected in Xcode, uncomment the two echo statements (this can also be used to make a "Format Selection" script, rather than "Format All"
Script Settings:
Input:Entire Document
Directory: Home directory
Output: Replace Document Contents
Errors: display in alert
As of Xcode 4.3.1 no custom namespace indent options are available, however I overcame this irritation by navigating to Preferences->Text Editing->Indentation and disabling "Syntax-aware indenting".
Another possibility is to use Articstic Style (astyle). A tutorial how to integrate astyle into XCode using automator and services can be found here: http://eatmyrandom.blogspot.com/2011/03/xcode-astyle-part-2-for-xcode-4x.html and http://youtu.be/d8bbE6_OHGc
I'm working on +1M LOC C/C++ project on Solaris (remote, via VNC or SSH). I have a daily updated copy of source code on my local machine too (Windows, just for browsing code).
I use VIM and ctags combo (on both Solaris and Windows) but I'm not happy with results / speed. What settings for ctags would you recommend? There are a lot of options what should be tagged and how. Should I use single tag file per project, per dir or perhaps just one for everything?
Using anything less than one for everything doesn't really make sense to me. Being able to quickly jump around your project is what tags are for in the first place. For instance, our code is divided into 3 main sections, Include/, Processes/, Libraries/. Without being able to jump between these I would be incredibly unproductive.
Personally I use cscope (its C++ parsing isn't great, but its ok, and its VIM integration is better than just ctags), but when I do use ctags I usually just add --c++-kinds=+p.
I use etags:
find src1 src2 src3 | grep -v "\\.svn" | xargs etags --append
In emacs, position cursor on identifier and press M-. ([alt] + [period], or [esc] followed by [period]).
I don't know how it compares to your setup as far as speed goes, or if you're willing to use emacs. I'm just posting in case you want to try some alternatives.