I need to go through hundreds of c++ files and remove a function call and I'm looking for a more automated way of doing this. Regex is incapable of doing this because, well, c++ is not a regular language. Here's an example:
int x = DoSomething(Foo(), Bar(Baz() + Thing()))
I want to remove the call to Bar, so that I have this:
int x = DoSomething(Foo(), Baz() + Thing())
This is just one example, there could be any number of possible variations of the use of the Bar function. Regex could remove Bar and just leave the ( ), but then I've got a mess of extra parenthesis everywhere which I don't want. I'd also prefer to disturb the rest of the code as little as possible, so I'd prefer not to do mass removal of all extra parenthesis, unless it's the only option.
I'm hoping there's some kind of sublime text or visual studio code plugin that can do this.
Related
Is there any way to get data with in braces, before a certain latex command (\body) and assign that content (long text) to a variable.
eg:
\text{just a text before body} \body{contains lot of paragraphs etc
etc etc, and that paragraphs also contains lot of latex command like
\textbf{my name} and \textit{text} etc. but i want all the content
with in the brace} \text{just a text after body}
i need
\body{contains lot of paragraphs etc etc etc, and that paragraphs also
contains lot of latex command like \textbf{my name} and \textit{text}
etc. but i want all the content with in the brace}
in a variable
I want some search and replace in it. thats why
i did a macro to yank text with in braces by the help of % (to search with in braces).
is there any easy way to do this?
thanks in advance
You can assign everything inside the { to register a using
"ayi{
Breakdown is
"a - select register a
y - yank
i{ - everything inside {}
It does the right thing for matching braces
If you later need to access the contents of a you can do this in multiple different ways depending on the context - I think the ways are
"ap : paste register a in normal mode
<C-r>a : paste register a into command line
#a : access register in script
It's worth noting that regex is not powerful enough to do matching braces. (I think there are extensions that can make it powerful enough, but I'm not sure if vim supports any of those),
I'm trying to find using visual studio regular expressions (https://msdn.microsoft.com/en-us/library/2k3te2cs.aspx) all calls to Assert.IsTrue that only pass the Boolean argument, for example Assert.IsTrue(parameter) would be one and Assert.IsTrue(parameter, "message") wouldn'
t.
For simple things, Assert.IsTrue\(([a-zA-Z ]+)\) does the trick, this works for the example provided above but not for things when there are evaluations done for example Assert.IsTrue(2 > 3). For this I tried using Assert.IsTrue\((.+[^,])\) so it matches everything that doesn't have "," but this only filters when the , is at the end, I'm not sure how to filter commas inside.
Finally, what I really want to do (which I'm not sure if it's possible with regular expressions alone) is to find Assert.IsTrue that have only one parameter but this parameter could be a method call, so it could have commas or not, something like Assert.IsTrue(isTrue(p1,p2))
I don't know why you want the solution to be programmatically, but if it's ok for you to have a Visual Studio based solution, you could just look for an example of Assert.IsTrue(parameter);, right click the method and select "Find all references`.
Remove the .+ from your second example and add a * also you should escape the period directly after Assert.
Assert\.IsTrue\(([^,])*\)
As for your expanded expression, something like this might work.
Assert\.IsTrue\(([a-zA-Z\.])*\(.*\)\)
This should let you find what you're looking for.
Background
I use JScript (Microsoft's ECMAScript implementation) for a lot of Windows system administration needs. This means I use a lot of ActiveX (Automated COM) objects. The methods of these objects often expect Number or Boolean arguments. For example:
var fso = new ActiveXObject("Scripting.FileSystemObject");
var a = fso.CreateTextFile("c:\\testfile.txt", true);
a.WriteLine("This is a test.");
a.Close();
(CreateTextFile Method on MSDN)
On the second line you see that the second argument is one that I'm talking about. A Boolean of "true" doesn't really describe how the method's behavior will change. This isn't a problem for me, but my automation-shy coworkers are easily spooked. Not knowing what an argument does spooks them. Unfortunately a long list of constants (not real constants, of course, since current JScript versions don't support them) will also spook them. So I've taken to documenting some of these basic function calls with inline block comments. The second line in the above example would be written as such:
var a = fso.CreateTextFile("c:\\testfile.txt", /*overwrite*/ true, /*unicode*/ false);
That ends up with a small syntax highlighting dilemma for me, though. I like my comments highlighted vibrantly; both block and line comments. These tiny inline block comments mean little to me, personally, however. I'd like to highlight those particular comments in a more muted fashion (light gray on white, for example). Which brings me to my dilemma.
Dilemma
I'd like to override the default syntax highlighting for block comments when both the beginning and end marks are on the same line. Ideally this is done solely in my vimrc file, and not in a superseding personal copy of the javascript.vim syntax. My initial attempt is pathetic:
hi inlineComment guifg=#bbbbbb
match inlineComment "\/\*.*\*\/"
Straight away you can see the first problem with this regular expression pattern is that it's a greedy search. It's going to match from the first "/*" to the last "*/" on the line, meaning everything between two inline block comments will get this highlight style as well. I can fix that, but I'm really not sure how to deal with my second concern.
Comments can't be defined inside of String literals in ECMAScript. So this syntax highlighting will override String highlighting as well. I've never had a problem with this in system administration scripts, but it does often bite me when I'm examining the source of many javascript libraries intended for browsers (less.js for example).
What regex pattern, syntax definition, or other solution would the amazing StackOverflow community recommend to restore my vimrc zen?
I'm not sure, but from your description it sounds like you don't need a new syntax definition. Vim syntax files usually let you override a particular syntax item with your own choice of highlighting. In this case, the item you want is called javaScriptComment, so a command like this will set its highlighting:-
hi javaScriptComment guifg=#bbbbbb
but you have to do this in your .vimrc file (or somewhere that's sourced from there), so it's evaluated before the syntax file. The syntax file uses the highlight default command, so the syntax file's choice of highlighting only affects syntax items with no highlighting set. See :help :hi-default for more details on that. BTW, it only works on Vim 5.8 and later.
The above command will change all inline /* */ comments, and leave // line comments with their default setting, because line comments are a different syntax item (javaScriptLineComment). You can find the names of all these groups by looking at the javascript.vim file. (The easiest way to do this is :e $VIMRUNTIME/syntax/javascript.vim .)
If you only want to change some inline comments, it's a little more complicated, but still easy to see what to do by looking at javascript.vim . If you do that, you can see that block comments are defined like this:-
syn region javaScriptComment start="/\*" end="\*/" contains=#Spell,javaScriptCommentTodo
See that you can use separate regexes for begin and end markers: you don't need to worry about matching the stuff in between with non-greedy quantifiers, or anything like that. To have a syntax item that works similarly but only on one line, try adding the oneline option (:h :syn-oneline for more details):-
syn region myOnelineComment start="/\*" end="\*/" oneline
I've removed the two contains groups because (1) if you're only using it for parameter names, you probably don't want spell-checking turned on inside these comments, and (2) contained sections that aren't oneline override the oneline in the container region, so you would still match all TODO comments with this region.
You can define this new kind of comment region in your .vimrc, and set the highlighting how you like: it looks like you already know how to do that, so I won't go into more details on that. I haven't tried out this particular example, so you may still need a bit of fiddling to make it work. Give it a try and let me know how it goes.
Why don't you simply add a comment line above the call?
I think that
// fso.CreateTextFile(filename:String, overwrite:Boolean, unicode:Boolean)
var a = fso.CreateTextFile("c:\\testfile.txt", true, false);
is a lot more readable and informative than
var a = fso.CreateTextFile("c:\\testfile.txt", /*overwrite*/ true, /*unicode*/ false);
I am using vim 7.x
I am using alternate file.
I have a mapping of *.hpp <--> *.cpp
Suppose I'm in
class Foo {
void some_me#mber_func(); // # = my cursor
}
in Foo.hpp
is there a way to tell vim to do the following:
Grab word under # (easy, expand("")
Look up the class I'm inside of ("Foo") <-- I have no idea how to do this
Append `1 & 2 (easy: using ".") --> "Foo::some_member_func"
4: Switch files (easy, :A)
Do a / on 4
So basically, I can script all of this together, except the "find the name of the enclosing class I'm in part (especially if classes are nested).
I know about ctags. I know about cscope. I'm choosing to not use them -- I prefer solutions where I understand where they break.
This is relatively easy to do crudely and very difficult to do well. C and C++ are rather complex languages to parse reliably. At the risk of being downvoted, I'd personally recommend parsing the tags file generated by ctags, but if you really want to do it in Vim, there are a few of options for the "crude" method.
Make some assumptions. The assumptions you make depend on how complicated you want it to be. At the simplest level: assume you're in a class definition and there are no other nearby braces. Based on your coding style, assume that the opening brace of the class definition is on the same line as "class".
let classlineRE = '^class\s\+\(\k\+\)\s\+{.*'
let match = search(classlineRE, 'bnW')
if match != 0
let classline = getline(match)
let classname = substitute(classline, classlineRE, '\1', '')
" Now do something with classname
endif
The assumptions model can obviously be extended/generalised as much as you see fit. You can just search back for the brace and then search back for class and take what's in between (to handle braces on a separate line to "class"). You can filter out comments. If you want to be really clever, you can start looking at what level of braces you're in and make sure it's a top level one (go to the start of the file, add 1 every time you see '{' and subtract one every time you see '}' etc). Your vim script will get very very very complicated.
Another one risking the downvote, you could use one of the various C parsers written in python and use the vim-python interface to make it act like a vim script. To be honest, if you're thinking of doing this, I'd stick with ctags/cscope.
Use rainbow.vim. This does highlighting based on depth of indentation, so you could be a little clever and search back (using search('{', 'bW') or similar) for opening braces, then interrogate the syntax highlighting of those braces (using synIDattr(synID(line("."), col("."),1), "name")) and if it's hlLevel0, you know it's a top-level brace. You can then search back for class and parse as per item 1.
I hope that all of the above gives you some food for thought...
A colleague was working on a Perl script to consume a C++ source file and add text above all of the methods in the file. He was looking to develop code using regular expressions from the ground up to detect the top line of the method:
void MyClass::MyMethod(int somethingOrOther)
Trying to do this from scratch is fraught with landmines, like discriminating the method headers from macros, comments, conditionals, etc.
This may be the really, really hard way to do things, as VS 2005 seems to be able to figure out exactly where all of the methods start and end (so that I can click on the box to collapse the method source).
Is there an easy way within the VS 2005 IDE to add some text above each method, solution-wide?
You can do a regular expression search and replace. Since you can place new lines into replace box, you can go nuts and do anything you want(except for extracting parameters). Example forthcoming.
Search string: ^:b*{:i}:b{:i}\:\:{:i}:b*{\(.*\)}
Replace string: ///Regex Example\n///Class: \2\n///Method: \3 returning \1\n\1 \2::\3\4
Code:
///Regex Example
///Class: Class
///Method: Foo returning void
void Class::Foo(int oneParam)
///Regex Example
///Class: Class
///Method: Bar returning void
void Class::Bar(int noParam)
I am not aware of a method of hooking into Visual Studio parser, unless you write a plugin, which might be a bit of an overkill.