在我构建的Visual Studio扩展中,我需要在Visual Studio编辑器中突出显示方法调用.例如:

我想使用HSV颜色根据唯一调用的数量来划分色谱.
如果我将每种颜色导出为自己的EditorFormatDefinition,我可以实现突出显示:
[Export(typeof(EditorFormatDefinition))]
[ClassificationType(ClassificationTypeNames = "red-background")]
[Name("red-background")]
[UserVisible(true)]
[Order(After = Priority.High)]
public sealed class RedBackground : ClassificationFormatDefinition
{
public RedBackground()
{
DisplayName = "red-background";
BackgroundColor = Colors.Red;
}
}
Run Code Online (Sandbox Code Playgroud)
但是,这需要我手动设置我想提前使用的所有颜色.有没有办法EditorFormatDefinitions在运行时导出?
IContentTypeRegistryService中的某些注册表,IClassificationTypeRegistryService允许在运行时创建新的内容类型和分类.是否存在类似的API EditorFormatDefinitions.
或者,是否可以EditorFormatDefinition在Visual Studio中动态MEF导出?
c# vspackage visual-studio visual-studio-extensions visual-studio-package
我正在一个项目中,我们正在使用Roslyn编译,发出和运行代码。我遇到了一个问题,其中Roslyn没有将资源文件嵌入使用发出的DLL中Compilation.Emit()。
我看到IEnumerable<ResourceDescription>我认为需要使用类型的参数。
不幸的是,我在中找不到有关资源的任何信息Project,因此我不确定如何获取所需的信息。
Roslyn是否允许用户发现有关资源文件的任何信息?还是我需要退回EnvDTE或手动使用MSBuild?(如果需要使用MSBuild,如何支持DNX项目?)
我正在学习F#而且我已经开始使用序列和match表达式.
我正在编写一个Web浏览器,它正在通过类似于以下内容的HTML进行查看,<span>并在paging类的父级中获取最后一个URL .
<html>
<body>
<span class="paging">
<a href="http://google.com">Link to Google</a>
<a href="http://TheLinkIWant.com">The Link I want</a>
</span>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
我尝试获取最后一个URL如下:
type AnHtmlPage = FSharp.Data.HtmlProvider<"http://somesite.com">
let findMaxPageNumber (page:AnHtmlPage)=
page.Html.Descendants()
|> Seq.filter(fun n -> n.HasClass("paging"))
|> Seq.collect(fun n -> n.Descendants() |> Seq.filter(fun m -> m.HasName("a")))
|> Seq.last
|> fun n -> n.AttributeValue("href")
Run Code Online (Sandbox Code Playgroud)
但是,当我正在搜索的课程缺少页面时,我遇到了问题.特别是我得到带有消息的ArgumentExceptions:Additional information: The input sequence was empty.
我的第一个想法是构建另一个匹配空序列的函数,并paging在页面上找不到类时返回一个空字符串.
let findUrlOrReturnEmptyString (span:seq<HtmlNode>) =
match span with
| Seq.empty -> String.Empty // <----- …Run Code Online (Sandbox Code Playgroud) 我似乎无法使用Roslyn的Compilation.GetTypeByMetaDataName()方法检索嵌套类.
例如:
var tree = CSharpSyntaxTree.ParseText(@"
using System;
namespace MyNamespace
{
public class MyClass
{
public class MyInnerClass
{
}
}
}
");
var Mscorlib = new MetadataFileReference(typeof(object).Assembly.Location);
var compilation = CSharpCompilation.Create("MyCompilation",
syntaxTrees: new[] { tree }, references: new[] { Mscorlib });
//Correctly retrieves outer type.
var outerClass = compilation.GetTypeByMetadataName("MyNamespace.MyClass");
//Cannot correctly retrieve inner type (returns null)
var innerClass = compilation.GetTypeByMetadataName("MyNamespace.MyClass.MyInnerClass");
Run Code Online (Sandbox Code Playgroud)
是否可以使用其完全限定名称检索嵌套类型?
我意识到一个解决方法是首先检查包含类型是否包含任何类型INamespaceorTypeSymbol.GetTypeMembers(),但我宁愿不去那条路.我假设该GetTypeByMetaDataName()方法适用于任何类型,嵌套或其他类型.
我想将更改应用于解决方案的多个文档,但只反映第一个更改,其余部分被拒绝.此链接显示了只有一次可以将更改应用于解决方案.这将是一个什么样的解决方案.我希望链接指向解决方案或代码片段.
这是我的功能:
public static async Task<bool> AddMethod(string solutionPath)
{
var workspace = MSBuildWorkspace.Create();
var solution = await workspace.OpenSolutionAsync(solutionPath);
ClassDeclarationSyntax cls = SyntaxFactory.ClassDeclaration("someclass");
foreach (var project in solution.Projects)
{
foreach(var document in project.Documents)
{
Document doc = project.GetDocument(document.Id);
var root = await doc.GetSyntaxRootAsync();
var classes = root.DescendantNodes().Where(n => n.IsKind(SyntaxKind.ClassDeclaration));
if (classes.Count() != 0)
{
SyntaxNode FirstClass = classes.First() as ClassDeclarationSyntax;
if (FirstClass != null)
{
var newRoot = root.ReplaceNode(FirstClass, cls);
doc = doc.WithText(newRoot.GetText());
Project proj = doc.Project;
var abc …Run Code Online (Sandbox Code Playgroud) 我在使用PredicateBuilder动态地将"Or Where"子句添加到LINQ语句时遇到了麻烦.我会先解释一下我想要完成的事情.
我有一个倒排索引,用于存储来自一堆链接标题的关键字.我正在使用一个,所以我可以根据这些关键字快速搜索这些链接.倒排索引是类型
Dictionary<string, List<VerifiedUrl>>
Run Code Online (Sandbox Code Playgroud)
所以基本上每个单词都与包含该单词的URL列表相关联.
我正在进行的下一个阶段是使倒排索引可搜索.因此,当我搜索"蓝色"时,我返回了与键"the"或"blue"相关联的所有链接.在几次Google搜索之后,似乎是通过PredicateBuilder类动态地将"Or Where"子句添加到LINQ语句的最佳方法.我在使用我构建的谓词的最后一步遇到了麻烦.
var invertedIndex = new Dictionary<string, List<VerifiedUrl>>();
//the invertedIndex is built and filled here. Now I am trying to search through it.
Console.WriteLine("\nEnter words to see if there are matches");
string query = Console.ReadLine();
Console.WriteLine();
string[] words = query.Split(',', ' ');
//the predicate I'll be building. I'm not positive it is of the correct type. I've assumed that it should be of the same type as the Dictionary type in the …Run Code Online (Sandbox Code Playgroud) 我正在使用Roslyn来分析C#代码,并且在使用明确实现的接口时遇到了一个问题.给定一个实现接口的类型,我无法按名称检索显式实现的成员.例如:
var tree = CSharpSyntaxTree.ParseText(@"
using System;
namespace ConsoleApplication1
{
class MyClass : IDisposable
{
void IDisposable.Dispose()
{
}
public void Dispose()
{
}
}
}");
var Mscorlib = new MetadataFileReference(typeof(object).Assembly.Location);
var compilation = CSharpCompilation.Create("MyCompilation",
syntaxTrees: new[] { tree }, references: new[] { Mscorlib });
var model = compilation.GetSemanticModel(tree);
var myType = compilation.GetTypeByMetadataName("ConsoleApplication1.MyClass");
var dispose = myType.GetMembers("Dispose").SingleOrDefault();
//explicitDispose is null.
var explicitDispose = myType.GetMembers("IDisposable.Dispose").SingleOrDefault();
Run Code Online (Sandbox Code Playgroud)
仅当类型存在于命名空间内时才会出现这种情况,以下代码可以正常工作.
var tree = CSharpSyntaxTree.ParseText(@"
class MyClass : IDisposable
{
void IDisposable.Dispose()
{
}
public void …Run Code Online (Sandbox Code Playgroud) C#6引入了在没有setter的情况下初始化属性的功能,因此现在可以使用这种语法
public class MyClass
{
public int Answer { get; } = 42;
}
Run Code Online (Sandbox Code Playgroud)
甚至这个
public class MyClass
{
public int Answer { get; }
public MyClass()
{
Answer = 42;
}
}
Run Code Online (Sandbox Code Playgroud)
我知道(或者更确切地说,强烈假设)这将被转换为readonly带有CIL中存取方法的生成字段,所以我理解这是怎么回事
public class MyClass
{
public int Answer { get; }
public MyClass CreateMyClassInstance()
{
return new MyClass()
{
Answer = 42
};
}
}
Run Code Online (Sandbox Code Playgroud)
不编译(因为赋值技术上发生在构造函数之外,这与支持readonly字段施加的限制冲突).
我的问题是为什么首先禁止这种行为?从语法和/或编译的角度来看,为什么属性赋值是属于对象初始值设定项的一部分的属性赋值,而不仅仅被视为在其后执行的额外内联逻辑,但仍然在对象的构造函数中?是设计,技术限制或向后兼容的结果,还是仅仅是一个不够重要的变化?
我是F#的新手,所以如果我使用不正确的名字,我会道歉.
我正在尝试使用F#来解析看起来像这样的网页:
<!--This is simplified, in reality there are more divs, anchors etc. -->
<html>
<body>
<div class="pr-single"><a href="http://google.ca">Google</a></div>
<div class="pr-single"><a href="http://apple.com">Apple</a></div>
<div class="pr-single"><a href="http://microsoft.com">Microsoft</a></div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
我宣布了一种类型
type PromoterPage = FSharp.Data.HtmlProvider<"http://somewebpage.com">
Run Code Online (Sandbox Code Playgroud)
现在我正在尝试获取页面上所有链接的列表.我的思考过程是:
<a>标记我的尝试如下:
let GetFirst (page:PromoterPage) =
page.Html.Descendants()
|> Seq.filter(fun n -> n.HasClass("pr-single")) //Find the divs
|> Seq.map(fun n -> n.Descendants()) //Get the descendants
|> Seq.collect(fun n -> n |> Seq.where(fun m -> m.HasName("a")) //Filter and collect the anchors
Run Code Online (Sandbox Code Playgroud)
问题似乎是你无法嵌套Seq函数或者我不能正确地执行它.我收到错误:
Incomplete …
我正在编写一个使用FSharp.Collections.ParallelSeq和重试计算的刮刀.我想从多个页面并行检索HTML,我想在失败时重试请求.
例如:
open System
open FSharp.Collections.ParallelSeq
type RetryBuilder(max) =
member x.Return(a) = a // Enable 'return'
member x.Delay(f) = f // Gets wrapped body and returns it (as it is)
// so that the body is passed to 'Run'
member x.Zero() = failwith "Zero" // Support if .. then
member x.Run(f) = // Gets function created by 'Delay'
let rec loop(n) =
if n = 0 then failwith "Failed" // Number of retries exceeded
else try …Run Code Online (Sandbox Code Playgroud) 我有点复杂的情况.我必须创建分析器/代码修复提供程序,例如仅分配参数但从未使用过,或者从不使用本地变量.
对于参数情况,我要进行方法声明并查看参数列表以获取所有分析器.我正在遍历方法中的赋值表达式,并且我过滤了使用辅助方法分配的参数.
它变得模糊的地方我不知道何时使用局部变量/参数.我已经完成了符号,但他们无法告诉我使用/未使用的变量.我可以尝试通过在字符串中转换方法声明语法上下文来查找方法中提到变量名称的次数,并查找已分配的参数,但这只是一个很糟糕的想法.
我真的被困住了,我会从那些曾经遇到过这种情况的人那里得到一些帮助.
对于可能会问的人,我主要是在寻找分析仪缺失的逻辑.我不知道代码修复提供程序将如何工作.如果您对我能做的事情有所了解,请随意将其包含在您的答案中!截至目前,我认为可以从方法中删除未使用的局部变量,同样可以使用未使用的参数.我现在不确定.
UPDATE
我现在正在尝试使用DataFlow API,但目前它并不适合我.这个帖子最古老的答案给了我一个起点,但实际上并没有起作用.
我想出了自己的方式:
private static bool IsLocalVariableBeingUsed(VariableDeclaratorSyntax variableDeclarator, SyntaxNodeAnalysisContext syntaxNode)
{
var model = syntaxNode.SemanticModel.Compilation.GetSemanticModel(variableDeclarator.SyntaxTree);
var methodBody = variableDeclarator.AncestorsAndSelf(false).OfType<MethodDeclarationSyntax>().First();
var lastMethodNode = methodBody?.ChildNodes().LastOrDefault();
if (lastMethodNode == null)
return false;
var readWrite = syntaxNode.SemanticModel.AnalyzeDataFlow(variableDeclarator, lastMethodNode);
}
Run Code Online (Sandbox Code Playgroud)
但这也行不通.使用NUnit测试时:
var input = @"
class TestClass {
void TestMethod ()
{
int i;
}
}";
Run Code Online (Sandbox Code Playgroud)
当运行时到达readWrite或result(从最旧的答案)时,我收到以下消息:
System.ArgumentOutRangeException Index was out of range Must be non negative and lesser than the size of the collection"
Run Code Online (Sandbox Code Playgroud)
但在此之前,在我的分析器中,当我尝试验证我的节点以确保它不是null并为数据流API创建适当的元素时,没有代码中断(不确定这是否是合适的术语)但是此刻我无法进步.