Why grammar does not highlight keyword (Visual Studio Language Server) - c#

ITNOA
I want to write Kamailio Language Server for Visual Studio 2022, I Create a project in GitHub with below structure
And I use kamailio.tmLanguage.json from https://github.com/miconda/vscode-kamailio-syntax/blob/master/syntaxes/kamailio.tmLanguage.json that I sure works for Visual Studio Code, So this grammar is correct
But I do not know why my code is not working correctly and does not highlight keyword?
My test with .kcfg file extension like below
using Microsoft.VisualStudio.LanguageServer.Client;
using Microsoft.VisualStudio.Utilities;
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Kamailio.VisualStudio
{
#pragma warning disable 649
public class KamailioContentDefinition
{
[Export]
[Name("kamailio")]
[BaseDefinition(CodeRemoteContentDefinition.CodeRemoteContentTypeName)]
internal static ContentTypeDefinition KamailioContentTypeDefinition;
[Export]
[FileExtension(".kcfg")]
[ContentType("kamailio")]
internal static FileExtensionToContentTypeDefinition KamailioFileExtensionDefinition;
}
#pragma warning restore 649
}
Any body can find my mistake?
All code to reproduce my problem is in GitHub

The good news:
It seems that all you need to do is add fileTypes to your kamailio.tmLanguage.json file, e.g.
"fileTypes": [
".cfg"
],
VS matches TextMate grammars to files based on these properties.
The bad news:
VS is apparently not smart enough to support other ways to filter when you are or aren't applicable. Specifically, it does not seem to support the firstLineMatch property, so it will apply your grammar to all *.cfg files. If there is another way to filter which files are or are not applicable, I wasn't able to find it.

Related

Visual Studio Sort usings in Wrong Order from SA1210

This is a scenario I identified when we turned on Stylecop rule SA1210 in our project.
The below is the set of using in one file I tried this sorted by visual studio's Remove and Sort usings.
I have configured to use System Directives first. Also, I have configured to automatically sort usings on file save.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using Bss.Cloud.Api.Caches.Redis;
using BSS.Angel.BusinessEntities.Cache;
using BSS.Angel.BusinessEntities.Lists;
using BSS.Angel.BusinessEntities.Repositories;
using BSS.Common.DataAccess.Claims;
But the above raised Stylecop SA1210.
When I use VS's intellisence's auto fix, I get
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using BSS.Angel.BusinessEntities.Cache;
using BSS.Angel.BusinessEntities.Lists;
using BSS.Angel.BusinessEntities.Repositories;
using Bss.Cloud.Api.Caches.Redis;
using BSS.Common.DataAccess.Claims;
The reason being Cloud > Angel.
This makes it difficult for me as when I save, It gets sorted back to the old VS' Sort Using way.
Their reason being BSS > Bss according to Visual studio.
Now I cannot save the SA passing way(I have automatic sort enabled). VS does not support style cop way.
We cannot update the BSS to Bss as this is a huge change in a different library project for us.
Is there something I can do for Stylecop to be compatible with Visual Studio and not disable Automatic Sort using on save.

Roslyn - namespaces

Can anyone tell what namespace the Syntax class is in?
I have installed all the roslyn packages through nuget but I don't know what namespace the static factory methods for creating expression syntax objects is in.
Also a lot of the examples of Roslyn on the web are using these namespaces
using Roslyn.Compilers;
using Roslyn.Compilers.CSharp;
Are these obsolete now? I am using the following
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
Can somebody explain the difference?
From what I can gather the namespaces are obsolete now.
SyntaxFactory replaces Syntax if anyone is interested.

Regarding adding namespace in common area

we always add namespace at the top of the every form in win apps
like
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using Business;
using System.Data.SqlClient;
using Business;
and most of namespace are common in most form but in asp.net there is a provision to add namespace only once in web.config file. so we dont have to add it in all web form and it save time. so i just need to know is there any same sort of provision for our win apps. how to achieve it. thanks
No, there isn't any sort of provision for your WinForm applications as there is for ASP.NET applications using the <namespaces> section in web.config. Also note that this applies only to .aspx pages and not code behind C#. In C# you have to add proper using statements in order to bring the types you want to use into scope.
One method is to add the standard using directives into the common templates.
You don't mention which version of visual studio you are using but in VS2010 you can find the templates in:
C:\Program Files\Microsoft Visual Studio 10.0\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class.zip
Change the Class.cs file in the zip to include the usings you want by default.
The only downside to this is that it is per machine - so you will need to do it on each developers computer - although I daresay you could roll it out with Active Directory (that's a question for serverfault.com).
I believe it also possible to set up team templates - but it's not something I have tried. More information available here:
http://msdn.microsoft.com/en-us/magazine/cc188697.aspx

Properly remove StyleCop R# plugin

I installed StyleCop and the associated plugin for ReSharper 5. After getting annoyed with it I removed both the plugin and StyleCop, but ReSharper is still using some of the StyleCop behaviour - most notably moving using statements to within the namespace declaration, rather than keeping them outside the declaration.
For instance say you have the following source:
using System;
using System.Web;
namespace Foo.Bar
{
////
}
And the file sits within The Foo/Bar/Widget directory, using ReSharper's fix namespace tool I would expect the file to stay the same, but the namespace to have changed to Foo.Bar.Widgets (this is the behaviour it exhibited before StyleCop came along).
Now however it rearranges the file:
namespace Foo.Bar.Widget
{
using System;
using System.Web;
////
}
Now putting aside people's personal preferences about which one is better, I don't like it, and it is inconsistent with our existing code. Having to manually move using statements after renaming the namespace takes long than renaming the namespace manually.
Does anyone know how to correct this (I'm assuming there is a file or something still lingering around from the install, or a config that hasn't been reverted).
You can change it here:
ReSharper -> Options -> Languages -> C# -> Namespace Imports -> Add using directive to the deepest scope
UPDATE - Resharper 9 This option is now moved to:
ReSharper -> Options -> Code Editing -> C# -> Code Style -> Reference qualification
I just had the same issue. It turns out that the StyleCop settings are stored in the "This computer" layer of ReSharper settings. See ReSharper > Manage Options for a list of layers.
I just had to reset the "This computer" layer; this was possible since I had never intentionally modified it.

Referencing namespaces globally?

Is there a way to reference a namespace globally across the whole solution?
So instead of having these lines in every code file:
using System;
using MyNamespace;
having to declare them only once, and every code file would use them.
Btw I am using Visual Studio.
No, C# doesn't have this concept. Each source file is independent in this respect. (And if the using directives are in a namespace declaration, those are independent from other using directives in peer namespace declarations, too. That's a pretty rare case though in my experience.)
You don't need ReSharper to change what gets included in a new class though. You can use the Visual Studio templates.
EDIT: Just to clarify the point about using directives within namespaces, suppose we had (all in one file):
using Foo;
namespace X
{
using Bar;
// Foo and Bar are searched for code in here, but not Baz
}
namespace Y
{
using Baz;
// Foo and Baz are searched for code in here, but not Bar
}
Usually I only have one namespace declaration in a file, and put all the using directives before it.
No, this is not possible.
If you're using ReSharper, you can set an option to include specific using directives in every new file you create though.
From this SO question and follow-up blog post. You can edit the Visual Studio default templates.
To do this, look at the file in this zip : [Program Files][Visual Studio]\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class.zip
and modify the Class.cs file as needed. Additionally, Visual Studio may have cached this file here :
[Program Files][Visual Studio]\Common7\IDE\ItemTemplatesCache\CSharp\Code\1033\Class.zip
In C# 10.0 you can use Global Usings.
global using System;
global using MyNamespace;
No, you can not reference a namespace globally across the whole solution in .NET or .NET CORE.
But you can use project wise namespace globally in solution. this feature will be available from c#10/.NET 6. currently it's in preview but it will be released in NOV 2021
=========Project level .NET 6 global using namespace=========
Create a class file at root of the project e.g GlobalNamespace.cs
global using System;
global using System.Linq;
global using System.Text.RegularExpressions;
global using System.Threading.Tasks;
Then you don't need to declare using namespace in other .cs files of the project which are already declared globally.
As others have mentioned Visual Studio Templates are the way to go.
Note that simply adding a using statement to your template will not ensure that the compiler can resolve your types. So, if you are adding a using statement for MyNamespace in every class you may need to add an assembly reference to your project as well. See the C# FAQ for more information.
One trick I miss as a newb to CSharp is to look at the "refences" (in VS), to right click and "Add New Reference". This is especially handy when combining mulitple projects where I have made some generic class for reuse elsewhere.

Categories

Resources