Is it possible to create new C# scripts with proper namespace? - c#

I'm using the C# Mono version for Godot and would like to know if it is possible to configure Godot to make use of namespaces. When creating a new script at
./Folder/MyClass.cs
I would expect the namespace to be
MyProject.Folder
So the created file should be similiar to
using Godot;
namespace MyProject.Folder
{
public class MyClass : Node2D { }
}
Is that possible?

I don't think it is possible to configure Godot to do this. To be clear, it is possible to create custom templates for scripts, see Creating script templates. However, as far as I can tell Godot does not offer a way to use the folder path (or its parts) in a template.

I also don't think its possible to do this full automatically.
But if you are using vs code you can just click on the class definition and press ctrl + point or press the lamp icon to show some quick options and than you can select the change namespace to MyProject.Folder option.

Related

Getting Custom ContentType to Register with Auto-Completion in VSIX Extension

I've written an extension to provide IntelliSense auto-completion for an VS Isolated Shell. The issue I'm having is making this auto-completion apply only to files of a particular file type. I've followed the guide here to do so.
Relevant class file:
using Microsoft.VisualStudio.Utilities;
using System.ComponentModel.Composition;
namespace VSIXProject1
{
internal static class FileAndContentTypeDefinitions
{
[Export]
[Name("assembly")]
[BaseDefinition("text")]
internal static ContentTypeDefinition hidingContentTypeDefinition;
[Export]
[FileExtension(".asm")]
[ContentType("assembly")]
internal static FileExtensionToContentTypeDefinition hiddenAVRASMFileExtensionDefinition;
}
}
Despite this, when I put the [ContentType("assembly")] export on the ICompletionSourceProvider and the IVsTextViewCreationListener that are used to handle the IntelliSense integration, the auto-completion fails to show up at all. However when I use [ContentType("text")] or [ContentType("plaintext")] the auto-completion will show up on either all files or just .txt files, respectively.
This is the first VSIX plugin I've tried to make so maybe I've missed something. Currently, I'm at a loss for what to try next. A lot of googling hasn't yielded much information, and the code resources I have found seem to implement this behavior in the same way I'm attempting to.
So after a few unsuccessful attempts, and a stroke of luck, I realized what my issue was. The specific Isolated Shell (Atmel Studio) I was trying to write the extension for already had a ContentType for assembly files. I'm guessing that when I added my own content type it was overwritten by the shell itself.
In order to fix the issue, I used the ContentType name that the shell uses which happened to be "assembler". This happened to be a lucky guess based on the languages that appear under Tools > Options > Text Editor. Hopefully, this helps someone who runs into the same issue in the future.

How to access a class which is in a folder c# (wpf)?

I'm building a WPF application. I'm almost finished with one part of it, and now I want to clean up the code a bit. English is not my native language (so sorry if this gets marked as a duplicate) and I couldn't find anything about this, so I hope someone can help me out.
If I have public voids in a class called 'class.cs' and this class is in a folder called 'classes', how do I access the class so I can use its functions?
At the top of the file where you want to use the class include a using statement.
using [namespace];
Then you will be able to instantiate the class
Class testClass = new Class();
testClass.Method();
If you aren't sure what the namespace for your Class class is, then you can write the line to instantiate the class and then use the intellisense (assuming you are using a development ide like Visual Studio) to automatically include the reference. Alternatively, you can look at the top of your class.cs file to see what namespace has been declared.
check what namespace it's in , then what the class name is and call it as
"Namespace.class"
in case that you can't see it still , check if the class is public and the class.cs properties have the build property set to compile
I'm not sure from where You are trying to access this class. But I think the easiest way of doing it probably will be just typing your class name in place where You are trying to use it and press 'ctrl+.' which should show You context menu with options to add using statement for this class.
If You are trying to access this class from other project in solution then above way will only work with VS2017(because it will also add missing project reference) otherwise You need to add reference for this project and then add missing using statement for your class(in your case 'using ProjectName.classes').
The folder you are talking about is called a namespace. You first need to import the namespace and then you can use your class.
*Normally the IDE will prompt you.

How to refactor a file namespace?

I have a N-Layer project and when im defining the ContextBounderies i use to change it during the needs of the development. For example i have this class:
namespace Domain.Entities.Item
public class ItemForSale
{
}
This class is now inside the Entites/Item folder in the solution. But during the development I realize that this class will make more sense inside the Sell folder, so the namespace should be namespace Domain.Entities.Sell. Well, the problem is that i have lots of references of this class in a number of layers projects, so its always a pain to refactor it by hand.
Is there a way to configure it, or some free extension (resharper do that but its not for free) wich do this job?
Im using Visual Studio 2015
Thanks!
Yes, this is just a feature of VS2015. You can right click on the namespace you want to change, and then select the "Rename" options under the drop-down. It will update all of the references inside of the solution. So, if you have multiple projects inside of your solution, they will all get updated with the new name.
(Also, there is a keyboard shortcut as well. You can click the namespace you want to change, and then press F2 on your keyboard to bring up the Rename menu.)
If the problem is not frequent then you can use Replace in Files option to replace all old namespace occurences to new ones. Just use Crtl+Shift+H, fill appropriate fields and select Look in option to look in Entire solution.

How to add help documentation on a method in a C# dll?

I want to add help/documentation text to a library that I am building in C#. I am using /// to create the tags and this works just fine when I use methods of my library in my lirary proyect. For example:
if my library have 2 classes Car and Person, if a I type in Person
Car.StartEngi
the auto complete of VS will suggest to use StartEngine methos and I will see the summary that for the method, fro example "This method will start the engine of the car".
But the issue comes when I compile and try to reference the dll on a separate solution. In the new solution I have already add the corresponding reference and using statement but when I write
Car.StartEngi
VS will indeed recommend to use StartEngine BUT I can not see the documentation. Does anyone know why? Do I need to add some parameter when I build the library?
P.D All the methods in the library that generates the dll are public
On the Build page of your project settings you need to turn on "XML documentation file". If you copy the XML documentation file along with your assembly then you should be able to see the documentation in other projects.

Custom Tabs in MonoMac project

I am using MonoMac to develop an application and so far have been delighted at how easy it is to use coming from the Windows world. However I am stumped on what should be an easy feature to implement: putting a close button in a tab header.
This was already asked here:
Add a close button to NSTabviewitem
And one of the solutions was to use chromium tabs here:
https://github.com/rsms/chromium-tabs
Is it possible to use something like that in a MonoMac project with MonoDevelop? I can add the library in xcode as a linked library but MonoDevelop doesn't seem to hold onto those changes.
Since (from my understanding) MonoDevelop basically generates a dynamic xCode project and discards it after editing I am not sure if it is possible and am hoping someone can shed some light on it for me.
I am not married to the idea of chromium tabs - open to any suggestions.
You are correct that you can't simply add the framework to the XCode project, because it's generated by MonoDevelop - it's not a 'real' project in that sense.
You have a couple of options:
1 - Write your own implementation in C#
2 - Choose an open source implementation (e.g. Chromium Tabs) and port it to C#. This should work but will obviously be time consuming. Also you may find that you need access to some Cocoa APIs that are not present in MonoMac yet - you would need to figure out a workaround or implement them.
3 - It's possible to bind a native framework into your app. Perhaps you can find someone that has already done this for the framework you're interested in; or you can do it yourself.
I've been struggling with #3 myself for a day or two now, but finally got it all figured out so I'll share the process with you and maybe this will be helpful.
I downloaded the chromium tabs source and compiled the framework. You will need to compile it as i386 only; since MonoMac currently only ships a 32 bit runtime
I added the ChromiumTabs.framework to my project, and set up a pre-build script to copy it to ${TargetDir}/${ProjectName}.app/Contents/Frameworks/
I load the framework manually inside Main, before the call to NSApplication.Init():
var baseAppPath = Directory.GetParent(Directory.GetParent(System.AppDomain.CurrentDomain.BaseDirectory).ToString());
var chromiumPath = baseAppPath + "/Frameworks/ChromiumTabs.framework/ChromiumTabs";
var hresult = Dlfcn.dlopen(chromiumPath, 0); // Non-zero result indicates success
Next, use parse.exe (available in MonoMac when you compile from source) and feed it the .h files from the framework. Use the generated output to create the interfaces that will define your binding (there is good documentation on binding types here). For example:
using System;
using System.Drawing;
using MonoMac.Foundation;
using MonoMac.AppKit;
namespace ChromiumTabs
{
[BaseType (typeof (NSWindowController))]
interface CTTabWindowController {
}
[BaseType (typeof (CTTabWindowController))]
interface CTBrowserWindowController {
[Export ("browser")]
CTBrowser Browser { get; }
[Export ("initWithBrowser:")]
IntPtr Constructor (CTBrowser browser);
}
[BaseType (typeof (NSObject))]
interface CTBrowser {
[Export ("addBlankTabInForeground:")]
CTTabContents AddBlankTabInForeground (bool foreground);
[Export ("createBlankTabBasedOn:")]
CTTabContents CreateBlankTabBasedOn (CTTabContents baseContents);
}
[BaseType (typeof (NSDocument))]
interface CTTabContents {
[Export ("initWithBaseTabContents:")]
IntPtr Constructor ([NullAllowed]CTTabContents baseContents);
[Export ("viewFrameDidChange:")]
void ViewFrameDidChange (RectangleF newFrame);
}
}
Now, feed that .cs file into bmac.exe (check the help for the arguments you need to supply). This will output a managed DLL which you can reference in your project.
You should now be able to use your new binding!
I did a very minimal binding on the library, just enough to get it up and running. You can download that here.

Categories

Resources