For now we are able to create only new file or folder. And it's very annoying to write namespaces each time you create class declaration.
But is it possible to create new C# class file with auto generated appropriate namespaces inside? Or maybe some snippet there?
This extension provides a context menu button to add a new class, which will auto populate the namespace.
Visual Studio Code has changed a bit since the last answer. It now provides the variable TM_DIRECTORY in snippets, but this is an absolute path. I've submitted an enhancement request to provide a relative path that could be transformed to a namespace. But honestly, I think the above extension satisfies my needs (and the context menu is a plus)
That's currently not possible. You have no possibility to retrieve the current filename, directory or other information in a snippet declaration file for Visual Studio Code.
You could create a snippet that lets you enter a namespace and a class name. But I guess this wouldn't help you so much. Nevertheless it'd look like this:
"Namespace and class": {
"prefix": "namespaceAndClass",
"body": [
"namespace $1",
"{",
" class $2",
" {",
"",
" }",
"}"
],
"description": "Create a namespace block with a class"
}
In case you really want a snippet that fills in the correct namespace and class name based on the file path you could have a look at the OmniSharp project. This can give you an idea on how to improve the csharp-o extension in order to provide the correct data as a suggestion from within the plugin.
But I think this is a much bigger task then typing namespace and class on your own.
I have found these extensions that provide create C# class context menu option:
VS Sharper for C#
C# helper
C# Template Extensions
HelloVS - C# new file
A moderately dirty solution with the current variable and regex system of vscode is the following:
Assuming that your have all your projects in /your/projects/directory/
So project #1 is in /your/projects/directory/Project1/
And project #2 is in /your/projects/directory/Project2/
etc.
The following snippet will create a namespace implementation for all sub-directories:
Linux/ MacOS
"Namespace declaration":
{
"prefix": "name",
"description": "Creates a new namespace declaration.",
"body":
[
"namespace ${TM_DIRECTORY/^\\/your\\/projects\\/directory(\\/([^\\/]+))(\\/([^\\/]+))?(\\/([^\\/]+))?(\\/([^\\/]+))?(\\/([^\\/]+))?(\\/([^\\/]+))?(\\/([^\\/]+))?(\\/([^\\/]+))?(\\/([^\\/]+))?(\\/([^\\/]+))?/$2${3:+.}$4${5:+.}$6${7:+.}$8${9:+.}$10${11:+.}$12${13:+.}$14${15:+.}$16${17:+.}$18${19:+.}$20/gi}",
"{",
"}"
]
}
Windows
"Namespace declaration":
{
"prefix": "name",
"description": "Creates a new namespace declaration.",
"body":
[
"namespace ${TM_DIRECTORY/^c:\\\\your\\\\projects\\\\directory(\\\\([^\\\\]+))(\\\\([^\\\\]+))?(\\\\([^\\\\]+))?(\\\\([^\\\\]+))?(\\\\([^\\\\]+))?(\\\\([^\\\\]+))?(\\\\([^\\\\]+))?(\\\\([^\\\\]+))?(\\\\([^\\\\]+))?(\\\\([^\\\\]+))?/$2${3:+.}$4${5:+.}$6${7:+.}$8${9:+.}$10${11:+.}$12${13:+.}$14${15:+.}$16${17:+.}$18${19:+.}$20/gi}",
"{",
"}"
]
}
Explanation
The snippet matches your base directory and up to ten sub-directories (the first directory is mandatory (\\/([^\\/]+)), while all additional nine ones are optional (\\/([^\\/]+))?)
The namespace directive is then created with the first matched directory
For every successful additional sub-directory match, a dot . is inserted (${3:+.}) with the sub-match of that group ($4); for unsuccessful groups, no dot inserted and the sub-match is empty
Enjoy :)
Found this thread looking for the same answer.
I did not find any of the existing snippet suggestions to generate proper namespaces.
After some trial and error the closest I have gotten, using "variables" and "placeholder transformation", is:
"Namespace and Class": {
"prefix": "nclass",
"body": [
"namespace ${RELATIVE_FILEPATH/[\\\\]|\\w+\\.cs$/./g}$1",
"{",
"\tpublic class $TM_FILENAME_BASE\n\t{\n\t\t$0\n\t}",
"}"
],
"description": "Generates namespace and class name"
}
This will generate a namespace from the folder structure in the workspace, and class name using the file name.
There is however an issue with this as it will leave two trailing .. at end of namespace.
But cursor will stop there, so they can quickly be removed with a Ctrl + Backspace and then tab again to jump inside body of the class.
Use this one, first it focuses at the end of the namespace to add any additional section. Then it focuses inside the class.
"Namespace and Class": {
"prefix": "nc",
"body": [
"namespace $WORKSPACE_NAME$1\n{\n\tpublic class $TM_FILENAME_BASE\n\t{\n\t\t$2\n\t}\n}"
],
"description": "Generates namespace and class name"
}
Related
I'm trying to load some basic json-ld content as a string, but I'm not able to see the namespace prefixes that should be included.
Given the following json-ld:
{
"#context": {
"name": "http://schema.org/name",
"image": {
"#id": "http://schema.org/image",
"#type": "#id"
},
"foaf": "http://xmlns.com/foaf/0.1/"
},
"name": "Manu Sporny",
"foaf:homepage": "http://manu.sporny.org/",
"image": "http://manu.sporny.org/images/manu.png"
}
I run this against the dotnetrdf library:
void Main()
{
var targetPath = #"C:\Users\me\MinContext.json";
var jsonStr = File.ReadAllText(targetPath);
var parser = new JsonLdParser();
var store = new TripleStore();
parser.Load(store, new StringReader(jsonStr));
var g = store.Graphs.FirstOrDefault();
IUriNode rdfType = g.CreateUriNode("rdf:type");
IUriNode home = g.CreateUriNode("foaf:homepage");
}
On the last line I get this RdfException message:
The Namespace URI for the given Prefix 'foaf' is not known by the
in-scope NamespaceMapper. Did you forget to define a namespace for
this prefix?
...and if you inspect the graph namespaces (g.NamespaceMap.Prefixes) you can see that it only contains three: rdf, rdfs and xsd.
So question: how do I get the foaf prefix and namespace to load correctly?
This is based on using NuGet package version 2.6.1
Prefixes are not an inherent part of any RDF graph, they are just conventions and shortcuts so that you don't have to type the full IRI. A specific database software/implementation can have options for configuring namespaces/prefixes, but they are just for presentation.
In this case, JsonLdParser simply does not import any prefix from the source data into the graph. This is a perfectly valid behaviour, and I don't know if it can be changed. Load can also take IRdfHandlerwhich seems to be able to do something with prefixes, but creating an implementation will most likely be more difficult than simply defining the namespace yourself:
g.NamespaceMap.AddNamespace("foaf", new Uri("http://xmlns.com/foaf/0.1/"));
I'd argue this is actually the more correct option. The source document can specify foaf: to be absolutely anything, but you want this foaf: (the full meaning of a resource name comes from the IRI of its prefix, not from the prefix name itself).
The alternative to that is g.CreateUriNode(new Uri("http://xmlns.com/foaf/0.1/homepage")) which creates a completely equivalent node. Of course it is simpler to add the namespace instead of typing the full IRI every time – that's what namespaces are for.
I want to build a generic cmake library importer for Unreal-Engine 4 plugins. I would like to create another class to handle it and used over multiple plugins. How would I include this new class in the plugins Build/Target.cs file? (https://github.com/caseymcc/UE4CMake)
I did figure out that you can put the needed code into an empty plugin:
public class BuildSource : ModuleRules
{
public BuildSource(ReadOnlyTargetRules Target) : base(Target)
{
}
...
//what ever code you want to include in your builds
...
}
Then add the plugin using your uproject/uplugin file with:
{
"FileVersion": 3,
"EngineAssociation": "4.25",
...
"Plugins": [
{
"Name": "BuildSource",
"Enabled": true
}
]
}
This will force the code in BuildSource to be compiled into an Assembly and include into your project (no imports needed). If you are using the Editor as well you might need to build an empty class that inherits from IModuleInterface to get it to link properly. Make sure to call IMPLEMENT_MODULE(FBuildSourceEditorModule, BuildSource) to generate the module.
I am currently using that for the code mentioned above (https://github.com/caseymcc/UE4CMake)
we have started getting the following error in one of our function apps:
Invalid script file name configuration. The 'scriptFile' property is set to a file that does not exist.
This is a recent development and we have been refactoring the code and configuration so we have obviously done something wrong. I have searched for the error and I keep finding fixes for typescript or javascript. We have written our functions in C# but not sure if either of those two are involved in producing the file.
function.json:
{
"generatedBy": "Microsoft.NET.Sdk.Functions-1.0.29",
"configurationSource": "attributes",
"bindings": [
{
"type": "queueTrigger",
"queueName": "%CampaignQueueName%",
"name": "message"
}
],
"disabled": false,
"scriptFile": "../bin/CampaignIngestion.Functions.dll",
"entryPoint": "CampaignIngestion.Functions.CampaignSplitterFunction.Run"
}
I have checked the path in one of the function.json files and it seems to be correct as I can find the file through Kudo's powershell console.
I suspect we have broken this with some of the configuration changes but we need help to identify where and how to fix this. Any suggestions as to what is causing this please?
Is it possible to write the output of snippet to the line above the current line?
I'm trying to create a snippet where you select an element name from csharp file and it inserts a Summary section above the element.
Snippet:
"CSharpSummarySelected": {
"prefix": "c#-summary-selected",
"body": [
"/// <summary>",
"/// ${1:$TM_SELECTED_TEXT}",
"/// </summary>",
"/// <returns>${2:returns}</returns>"
],
"description": "Inserts Summary for component with selected text as name in C# files"
}
Microsoft Visual Studio product includes a code snippet manager.
But with Visual Studio Code, you will need to use an extension. You will one in the Visual Studio Marketplace C# Snippets for Visual Studio Code by Jorge Serrano.
The author has also released the source code on Github https://github.com/J0rgeSerran0/vscode-csharp-snippets. This provide a great learning opportunity for other developer to wrote their extensions.
I don't think you can insert a snippet above text without using a macro or an extension - except in the trivial case where the selection was the only thing on the line, which I assume is not the case for you.
Using some macro extension, here multi-command, put this into your settings.json:
"multiCommand.commands": [
{
"command": "multiCommand.cSharpSummary",
"sequence": [
"editor.action.clipboardCopyAction",
"editor.action.insertLineBefore",
{
"command": "editor.action.insertSnippet",
"args": {
"name": "CSharpSummarySelected"
}
}
]
}
]
and your snippet into some snippets file:
"CSharpSummarySelected": {
"prefix": "c#-summary-selected",
"body": [
"/// <summary>",
// "/// ${1:$TM_SELECTED_TEXT}",
"/// ${1:$CLIPBOARD}", // this works witheditor.action.clipboardCopyAction, select only
"/// </summary>",
"/// <returns>${2:returns}</returns>"
],
"description": "Inserts Summary for component with selected text as name in C# files"
}
NOTE: my snippet uses $CLIPBOARD instead of $TM_SELECTED_TEXT. This is because the act of inserting a line above your selected text de-selects your selection (i.e., you lose your selection by inserting the line above).
You only have to select your variable, not actually copy it - the macro will take care of the copy action for you. But the clipBoard will now have your variable on it instead of whatever it had before - this may or may not be acceptable to you - in a macro I think it is unavoidable.
Now the macro will be triggered not with a prefix but with a keybinding. In keybindings.json:
{
"key": "ctrl+alt+b", // whatever keybinding you choose
"command": "extension.multiCommand.execute",
"args": { "command": "multiCommand.cSharpSummary" }
},
Now when you select some text and trigger the macro with the keybinding your snippet will be inserted above that line with your desired tabstops. See demo:
In ConfUserEX Obfuscation (https://yck1509.github.io/ConfuserEx/)
How to EXCLUDE just one Namespace from rename module?
For example :
[assembly: Obfuscation(Exclude = true, Feature = "namespace 'ABC.XYZ':-rename")]
Looks it does not work. I have a objectmodels for Json parsing in a namespace and this throwing error. Also I am using .crproj file for obfuscating using CLI.
Please download latest version v0.6.0 by this link. Exclude field from true to false. And that's it.
For example:
[assembly: Obfuscation(Exclude = false, Feature = "namespace 'Your.Namespace':-rename")]
According to the documentation , if you want to exclude a namespace, the correct way to do it is to write a line like:
[assembly: Obfuscation(Exclude = false, Feature = "namespace('namespaceToExclude'):-rename")]
This should be written in the Assembly.info file of the project.