我无法在Visual Studio中显示灯泡。试图仅运行here中的示例。但这出于某种原因对我不起作用。
这是我的代码
[Export(typeof(ISuggestedActionsSourceProvider))]
[Name("Test Suggested Actions")]
[ContentType("text")]
internal class TestSuggestedActionsSourceProvider : ISuggestedActionsSourceProvider
{
[Import]
internal ITextStructureNavigatorSelectorService NavigatorService { get; set; }
public ISuggestedActionsSource CreateSuggestedActionsSource(ITextView textView, ITextBuffer textBuffer)
{
if (textBuffer == null && textView == null)
{
return null;
}
return new TestSuggestedActionsSource(this, textView, textBuffer);
}
}
这是类TestSuggestedActionsSource
internal class TestSuggestedActionsSource : ISuggestedActionsSource
{
public event EventHandler<EventArgs> SuggestedActionsChanged;
private readonly TestSuggestedActionsSourceProvider m_factory;
private readonly ITextBuffer m_textBuffer;
private readonly ITextView m_textView;
public TestSuggestedActionsSource(TestSuggestedActionsSourceProvider testSuggestedActionsSourceProvider,
ITextView textView, ITextBuffer textBuffer)
{
m_factory = testSuggestedActionsSourceProvider;
m_textBuffer = textBuffer;
m_textView = textView;
}
private bool TryGetWordUnderCaret(out TextExtent wordExtent)
{
ITextCaret caret = m_textView.Caret;
SnapshotPoint point;
if (caret.Position.BufferPosition > 0)
{
point = caret.Position.BufferPosition - 1;
}
else
{
wordExtent = default(TextExtent);
return false;
}
ITextStructureNavigator navigator = m_factory.NavigatorService.GetTextStructureNavigator(m_textBuffer);
wordExtent = navigator.GetExtentOfWord(point);
return true;
}
public Task<bool> HasSuggestedActionsAsync(ISuggestedActionCategorySet requestedActionCategories, SnapshotSpan range, CancellationToken cancellationToken)
{
return Task.Factory.StartNew(() =>
{
TextExtent extent;
if (TryGetWordUnderCaret(out extent))
{
return extent.IsSignificant;
}
return false;
});
}
public IEnumerable<SuggestedActionSet> GetSuggestedActions(ISuggestedActionCategorySet requestedActionCategories, SnapshotSpan range, CancellationToken cancellationToken)
{
TextExtent extent;
if (TryGetWordUnderCaret(out extent) && extent.IsSignificant)
{
ITrackingSpan trackingSpan = range.Snapshot.CreateTrackingSpan(extent.Span, SpanTrackingMode.EdgeInclusive);
var upperAction = new UpperCaseSuggestedAction(trackingSpan);
var lowerAction = new LowerCaseSuggestedAction(trackingSpan);
SuggestedActionSet suggestedActionSet = new SuggestedActionSet(
PredefinedSuggestedActionCategoryNames.Any,
new ISuggestedAction[] { upperAction, lowerAction });
return new SuggestedActionSet[] { suggestedActionSet };
}
return Enumerable.Empty<SuggestedActionSet>();
}
public void Dispose()
{
}
public bool TryGetTelemetryId(out Guid telemetryId)
{
LightBulb telemetry
telemetryId = Guid.Empty;
return false;
}
}
这是LowerCaseSuggestedAction类
internal class LowerCaseSuggestedAction : ISuggestedAction
{
private readonly ITrackingSpan m_span;
private readonly string m_lower;
private readonly string m_display;
private readonly ITextSnapshot m_snapshot;
public bool HasActionSets => false;
public string DisplayText => m_display;
public ImageMoniker IconMoniker => default(ImageMoniker);
public string IconAutomationText => null;
public string InputGestureText => null;
public bool HasPreview => true;
public LowerCaseSuggestedAction(ITrackingSpan span)
{
m_span = span;
m_snapshot = span.TextBuffer.CurrentSnapshot;
m_lower = span.GetText(m_snapshot).ToLower();
m_display = $"Convert '{span.GetText(m_snapshot)}' to lower case";
}
public Task<object> GetPreviewAsync(CancellationToken cancellationToken)
{
var textBlock = new TextBlock();
textBlock.Padding = new Thickness(5);
textBlock.Inlines.Add(new Run() { Text = m_lower });
return Task.FromResult<object>(textBlock);
}
public Task<IEnumerable<SuggestedActionSet>> GetActionSetsAsync(CancellationToken cancellationToken)
{
return Task.FromResult<IEnumerable<SuggestedActionSet>>(null);
}
public void Invoke(CancellationToken cancellationToken)
{
m_span.TextBuffer.Replace(m_span.GetSpan(m_snapshot), m_lower);
}
public void Dispose()
{
}
public bool TryGetTelemetryId(out Guid telemetryId)
{
// This is a sample action and doesn't participate in LightBulb telemetry
telemetryId = Guid.Empty;
return false;
}
}
UpperCaseSuggestedAction类以类似的方式实现。就像我在示例中所做的一样,但是灯泡根本没有出现。这是我项目中的链接
在参考Microsoft.VisualStudio.Language.Intellisense中,参数Copy Local设置为false
答案 0 :(得分:1)
我设法找到了解决此问题的方法。我马上说一些动作可能是多余的。在这里,我只告诉您我为使项目正常进行了哪些操作
通过“ VISX”部分中的“属性”窗口(视图>“属性”窗口),设置以下值:
然后下载了以下Nuget软件包:
其他版本的库都可以使用,但是如果放了它们,则应查看依赖关系。它们在那里密切相关
接下来,我在source.extension.vsixmanifest文件中添加了以下几行
<Dependencies>
<Dependency Id="Microsoft.Framework.NDP" DisplayName="Microsoft .NET Framework" d:Source="Manual" Version="[4.5,)" />
<Dependency Id="Microsoft.VisualStudio.MPF.15.0" DisplayName="Visual Studio MPF 15.0" d:Source="Installed" Version="[15.0]" />
</Dependencies>
<Prerequisites>
<Prerequisite Id="Microsoft.VisualStudio.Component.CoreEditor" Version="[15.0,16.0)" DisplayName="Visual Studio core editor" />
</Prerequisites>
<Assets>
<Asset Type="Microsoft.VisualStudio.VsPackage" d:Source="Project" d:ProjectName="%CurrentProject%" Path="|%CurrentProject%;PkgdefProjectOutputGroup|" />
<Asset Type="Microsoft.VisualStudio.MefComponent" d:Source="Project" d:ProjectName="%CurrentProject%" Path="|%CurrentProject%|" />
</Assets>
PS:我希望这对某人有帮助。
答案 1 :(得分:0)
我尝试使用ЖуэльИтуа的解决方案,但无法在Visual Studio(Visual Studio Community 2017,版本15.9.7)中显示灯泡。该死的nuget包地狱。
我找到了文章VSSDK-Extensibility-Samples/LightBulb的存储库,并且可以正常工作。