Software To Help Me Reuse Code - visual-studio

I often find myself writing small (5-20 lines) files for things like input validation, reading a URL and so on. In fact, I probably write nearly the same files over and over because I either forget that I've already implemented such functionality in another project or I don't want to have to go searching for that file.
I want to know if there is a way to simply add code to a global repository from an IDE (in my case Visual Studio) and simply extract that code into whatever project I'm working on. It should be able to:
Extract code to a new file or to the currently open file in my IDE.
w.r.t. #1 I want smart defaults based on how I added the code to the repository.
Easy to use UI.
I don't need replacement/substitution of variables.
For instance, if I right click on a file I want to add to the repository then the default extraction operation is to create a new file. If, instead, I highlight some text in the currently open file and commit that, then the default is just to paste the snippet into the currently open file.
I've tried VS's snippet manager a few times, always with disappointment and quick abandonment. My biggest complaint is its interface and complexity required to add new code to it, plus the inability to extract the code to a file versus just inside whatever I have open.

You might want to check out ReSharper's Live Templates.
I don't use them to the extent that you're obviously looking for, but you can definitely create new file-based templates. I use that all the time when I create a new unit testing fixture class - just Add New Item -> From Template -> NUnit Test Fixture, where "NUnit Test Fixture" is one of my defined file templates.
Additionally, all the templates can be assigned a shortcut sequence, so you can auto-replace a given combination of text with a code snippet.
All the template configuration is file based, so you can save them to solutions or global workspaces as well.

Related

$(shell some-command) equivalent for Visual Studio project macros?

With Makefiles I'm used to being able to write things like:
includedir=$(shell pg_config --includedir)/server
to run an external program, pg_config, with argument(s) --includedir, and include the result in a variable or as part of a variable. So if pg_config --includedir output /usr/include to stdout, the value of includedir would become:
includedir=/usr/include/server
Is there any way to do the equivalent with a Visual Studio project? Run a command, get the result, and substitute it into a property?
I find myself having to edit properties pages all over the place - changing the include directories and library directories for both the x86 and x64 configurations of a project whenever I want to build an extension against a different PostgreSQL version. It is intensely frustrating.
I want to be able to put something like this into Configuration Properties -> C/C++ -> General -> Additional Include Directories:
%(shell pg_config --includedir)
or even better:
%(shell %(PG_CONFIG) --includedir)
where %(PG_CONFIG)'s location is defined in a single place for each platform in the project.
So I'm looking for at least user-defined macros, and preferably the ability to invoke a command line tool and replace the macro with the resulting standard output.
(Preferably in a way that doesn't involve delving into semi-documented UI elements that move and get renamed in every VS version, and that appear and disappear from the various Express editions).
This has been possible in Makefiles for 20 years, there must be a way to do it in VS, right? Or do "Real Windows Developers" generate their VS projects with scripts and build them using MSBuild?
I've looked at some similar questions without finding much of use, e.g.:
Visual Studio - Where to define custom path macros?
In particular, I'm aware of property sheets (View -> Other Windows -> Property Manager), but they don't seem to provide a way to set a value in just one place, they're still per-configuration and per-architecture, so if you have four configurations and two architectures it gets awkward. Unlike with the normal project property editor you can't even apply a change across a group of architectures/configurations, either.
I could use a VS extension, but they require installation into the user's VS, can be version-specific, and seem like a very big hammer for a small problem.
I find myself having to edit properties pages all over the place
That bugged me to no end as well. Property sheets to the rescue! When setting up a major solution in VS10, for example, I had every project pull in a settings.props that contained the common settings, made in only one place. Then go through all the generated or imported projects and kill any explicit value (even if blank) for everything possible. That way things will inherit from property sheets. Select "all configurations" and on each properly use the drop-down to "inherit from...".
I have property sheets for each special library too, just defining the proper #define, include paths, lib paths, etc. Projects that use that particular external lib simply use that property sheet. Users are told, in the worst case, to “edit the XML to change the path to where you have Boost”.
As for setting such a properly to a dynamic determined value, you can do that too. There are property functions you can use.
It sounds like you're going down the same path as I did.
More notes: “prop sheets are per configuration/platform”: If you include a prop sheet at the top-level node for the project itself (not the Debug|Win32, etc. child nodes) it will include it into all current configurations at once. If you edit the properly page, you can choose Multiple or All configurations on the Property dialog box, just as with the usual project use of the Property dialog.
“Custom user macros are well hidden” A property page shows up for that when in a property sheet you created, but not when opening property dialog on a proj file as in the normal File View. The macro will be set in one place (the prop page) and usable as a $(name) in all projects that include it, and even in other property pages that come later in the evaluation sequence.
Let me know how it goes. You should be able to do everything you asked.
—John
In addition to #jdlugosz's answer:
It looks like the traditional way to do this with Visual Studio, before the advent of property functions, was to write a new MSBuild Task. The task can potentially do things like modify a property sheet.
MSBuild supports "inline tasks" where the task code is in the MSBuild project file, rather than a separate assembly, so it might not be neccessary to create a new subproject just for the task.
There are a bunch of built-in tasks, like Exec and CreateProperty that may be useful.
The docs say that:
[The Exec task] is useful when a specific MSBuild task for the job that you want to perform is not available. However, the Exec task, unlike a more specific task, cannot gather output from the tool or command that it runs.
... but that seems to be outdated/wrong so you don't need horrible workarounds.
So, prior to .NET 4.5 I'd probably have to write a custom task for this simple job, because there's no way to feed the command stdout/stderr into the CreateProperty task or have Exec create a property directly. But in 4.5 it looks like I can do it directly. At least in VS Express support for tasks etc is very limited so you'll probably land up editing the XML.

Purpose of linked file vs class project?

What is the purpose of "Linked" Files in visual studio, and when I would use them versus simply including the class project that contains the file to my current project.
Project + Add Existing Item, arrow on the Add button, select "Add as Link".
Visual Studio strongly favors copying the item you select in this dialog into your project directory. This will keep you out of trouble, like accidentally modifying a source code file or resource that's also used in another project. Or forgetting to check-in the file into source control, by far the most dangerous mishap. Something you'll discover only a year or more from now, far too late to do anything about it.
The Add as Link option overrides that behavior, the file stays in its original location and the project file records the relative path of the file.
Good reasons to use this feature are very hard to come by. Trying to come up with some:
You may want to maintain a single source code file that's included in multiple projects, using Add as Link ensures you'll only ever edit that one single source file.
You may have a project that uses the same source code files as another project but with very different build settings, in which case you'd make all of the project items a link.
You may have a humongous resource that you don't want to have copied repeatedly in multiple projects
You may have a deployable item, like an unmanaged DLL, that you keep in a single directory but is used in multiple unrelated projects.
Digging yourself a deep source control hole is a high risk, be sure to think this through before you commit to using the feature.
Linked files are for when you want the code to be included in that project assembly.
"Wow Jimmy, thanks for pointing out the obvious. But why would I want to do this?"
Well, there can be several reasons, but for now I'll just list one example. Consider the scenario that your company|team requires every assembly to have certain constants or assembly attributes defined. Instead of creating a duplicate class in every single project, you could just have one copy of the file and link it into each project. Update the file once, and every assembly gets the changes.

Is there a way to link two comments together in an IDE?

Case: One source file has a comment in it that is directly linked to a comment in another source file (it says see line 315 in xxx.cs for more information). The problem with this approach is that the comment on line 315 may not be at that line number in the future. Is there a way to link comments together in an IDE? (currently using Visual Studio 2010, but use other IDEs from time to time)
You can try this addin (I haven't used this):
http://hyperaddin.codeplex.com/
Besides this addin, the only thing I can think of is using a file link to directly go to the linked file; something like:
// ...
// See file://path_to_file
//...
The link will be converted to an actual link that you can click using Ctrl+Left Click but it won't take you to a given line number - it just opens the file.
The path can be a relative file path or a full file path - full paths work best if all team members use the same folder structure in the project. For example:
// file://w:/projects/GUI/frmMain.cs
Referring to a particular source file and line number is never a good idea, because someone might move things around in the other file without being aware that something is pointing at it. It's better to point at the particular type/method, for example See DoThings() in the MyThing class..
In Java, using Javadoc, you can use #link to do this, for instance See {#link MyThing#doThings()}. Eclipse will automatically update these when using its refactoring tools (e.g. renaming the class or the method). If the change is done manually, Eclipse will still warn that the target of the #link is invalid. (There is also #see which is more appropriate in some situations.)
I'm not sure about C# and Visual Studio, but it's likely that its XML-based doc format offers similar functionality.
The only way to handle this is to put the comment in the same file. Duplicating a comment is not the same as duplicating code, although ideally the code wouldn't need too much explanation in comments.
There are many, many reasons why the comment being in another file will cause pain. As you have stated, the line number may change but also it could be deleted (as they won't know another comment references it), updated in a way that changes its meaning and it is annoying to have to open another file in any case.

How to customize generated Source Code Using Altova Mapforce Project

Background: When I generate the code for my mapping project, only one map will run, regardless of which file type I send in to the command.
I would like to modify the project code generation so that when the maps are generated the applicationConsole.cs allows multiple files and file types, and chooses the appropriate map based upon the parameters I send in.
so far, my project contains 4 maps, each one is different and has a parameter indicating whether or not to run the map. Once the code is generated, the applicationConsole.cs does not differentiate when running the maps. the first map listed in the applicationConsole.cs is run and the others are not. if the first map matches the given parameters, the output is correct, but if the next file coming in does not match, the console closes rather than moving on to the next map.
I am trying to avoid direct manipulation of the applicationConsole.cs, due to having to generate the code frequently.
the question: Does anyone know how to modify the output of Mapforce Project code generation to add a switch for map type based on file type?
In altova mapforce Application Install Directory,there is one SPL Directory. this is generate auto generate code as per your selected Language.
Take the backup of that Directory and modify SPL Directory code as per your requirement.
You need to learn SPL (Spy Programming Language) .
http://manual.altova.com/Mapforce/mapforce-enterprise/index.html?cgthewaytospl(spyprogrammingla.htm
If you need anything else then please inform me here. I have Good command on Customize the SPL.
Thank you
Take a look at refactoring under Java and C#
Really when you look at the stub code generated and you need to combine a couple maps the first refactoring task is to rename the namespace, Altova uses it's namespace as a default, so a mapa namespace would be helpful mapb namespace etc.
I'll provide the C# examples
http://msdn.microsoft.com/en-us/library/vstudio/6kxxabwd.aspx
Then you have the console app so you want to copy the applicationConsole.cs and rename it your project name or class name and then extract the method
http://msdn.microsoft.com/en-us/library/vstudio/0s21cwxk.aspx
Finally you would want to extract the interface so as to return the exception Constructor
http://msdn.microsoft.com/en-us/library/vstudio/fb3dyx26.aspx
and:
http://msdn.microsoft.com/en-us/library/tz6bzkbf.aspx
What you would have is a project with your start-up being the original applicationConsole.cs that you copied and renamed in a project or solution folder. When you update the map you should be able to over-write the code in that folder with what Mapforce generated.
This file was generated by MapForce 2013r2sp2.
YOU SHOULD NOT MODIFY THIS FILE, BECAUSE IT WILL BE
OVERWRITTEN WHEN YOU RE-RUN CODE GENERATION.
Refer to the MapForce Documentation for further details
This is what you will see in that file, use the technique outlined above and the tool is very easy to use. If you are a desktop programmer the refactoring might be new but it saves allot of time and effort down the road and makes the product complete.

Getting an IVsTextLines from file path

I've written a basic LanguageService extension for Visual Studio 2008 for my studio's proprietary scripting language. It works perfectly fine, and I've implemented a basic symbol table to keep track of script definitions and calls allowing for goto definition functionality.
The problem I've run into is that I only know how to parse the current active view, and I'd like to scan the entire solution's contents so that the user can goto the definition of a script defined in a file they have yet to open and have parsed. I've figured out how to generate a list of all files in the solution, but now I need to create a new Microsoft.VisualStudio.Package.Source which requires a Microsoft.VisualStudio.TextManager.Interop.IVsTextLines and I have no idea how to create a new one based off of the file I have.
Maybe I'm going about the problem the wrong way and someone can point me towards a better way to cause a file to be parsed by the LanguageService.
Regards,
Colin
Poking around I found that the reason Visual Studio needs a new Source is that it's keeping an internal list of them, and they're like the view into the text file held by the editor.
I came to the conclusion that files that are closed do not need IVsTextLines or to be entered into the VS internal list of Source files because I'm not doing any operations directly on them, all I care about in this case is to build a table of symbols and their corresponding TextSpan. So instead I created a new API for my parser that just took in a string and built my AST instead of grabbing the text from a ParseRequest, and only worried about specific types of symbols I needed to record. I then pushed this into a BackgroundWorker.
So I guess I was going about the problem in the wrong way. Although it does seem weird I can't just trigger a file to be opened into the Source list.
Interestingly I asked this question to Microsoft on their support forums and they advised me I had to purchase some service and support plan for them to answer my question.

Resources