通过Visual Studio扩展名进行自定义Intellicode预测

发布于 2025-02-05 23:40:53 字数 338 浏览 2 评论 0原文

我想在带有扩展的Visual Studio中添加自定义Intellicode预测/内联建议。在VSCODE中,我可以使用vscode.inlinecompletionProvider/inlinecompletionItem来执行此操作。在Visual Studio扩展中可以执行相同事情的类/方法是什么?

I would like to add the custom IntelliCode prediction/inline suggestion in Visual Studio with extension. In VSCode, I can use vscode.InlineCompletionProvider/InlineCompletionItem to do that. What's the class/method that would do the same things in Visual Studio extension?

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

别再吹冷风 2025-02-12 23:40:53

我有同样的要求,但我没有找到任何API。

但是,您可以做的是使用装饰来绘制看起来像代码的文本。

定义装饰:

[Export(typeof(AdornmentLayerDefinition))]
[Name("TextAdornment1")]
[Order(After = PredefinedAdornmentLayers.Text)]
private AdornmentLayerDefinition editorAdornmentLayer;

获取图层并添加一个文本块:

_layer = view.GetAdornmentLayer("TextAdornment1");

// triggeringLine is your current line
var geometry = _view.TextViewLines.GetMarkerGeometry(triggeringLine.Extent);
var textBlock = new TextBlock
{
    Width = 600, 
    Foreground = _textBrush,
    Height = geometry.Bounds.Height,
    FontFamily = new FontFamily(_settings.FontFamily),
    FontSize = _fontSize,
    Text = $"Your completion text"
};
// put the box at the end of your current line
Canvas.SetLeft(textBlock, geometry.Bounds.Right);
Canvas.SetTop(textBlock, geometry.Bounds.Top);
            
_layer.AddAdornment(AdornmentPositioningBehavior.TextRelative, triggeringLine.Extent, "your tag", textBlock, (tag, ui) => { });

您可以按以下方式获取当前的编辑器设置:

// Get TextEditor properties
var propertiesList = dte.get_Properties("FontsAndColors", "TextEditor");

// Get the items that are shown in the dialog in VS
var itemsList = (FontsAndColorsItems)propertiesList.Item("FontsAndColorsItems").Object;

// Get color for comments
var commentItem = itemsList.Cast<ColorableItems>().Single(i => i.Name=="Comment");
var colorBytes = BitConverter.GetBytes(commentItem.Foreground);
var commentColor = Color.FromRgb(colorBytes[2], colorBytes[1], colorBytes[0]);

// Get editor BG
var textItem = itemsList.Cast<ColorableItems>().Single(i => i.Name == "Plain Text");
var bgColorBytes = BitConverter.GetBytes(textItem.Background);
var bbgColor = Color.FromRgb(bgColorBytes[2], bgColorBytes[1], bgColorBytes[0]);

// Get font size in points
var fontSize = (short)propertiesList.Item("FontSize").Value;

// Get current font family
var fontFamily = (string)propertiesList.Item("FontFamily").Value;

此方法的问题是样式和字体大小与编辑器略有不同。即使使用编辑器设置。

但是,我认为Intellicode和Github Copilot使用另一种技术。正如您在这里看到的那样:
github coilot

似乎整个代码已经插入了编辑器,但具有特殊的样式/行为。因此,有可能以某种方式,但我没有如何实现这一目标。

有关装饰的更多信息,例如:
使用Roslyn 的VSIX

I had the same requirement but I did not find any API for that.

However, what you can do is use adornments to draw a text that looks like code.

Define the adornment:

[Export(typeof(AdornmentLayerDefinition))]
[Name("TextAdornment1")]
[Order(After = PredefinedAdornmentLayers.Text)]
private AdornmentLayerDefinition editorAdornmentLayer;

Get the layer and add a TextBlock:

_layer = view.GetAdornmentLayer("TextAdornment1");

// triggeringLine is your current line
var geometry = _view.TextViewLines.GetMarkerGeometry(triggeringLine.Extent);
var textBlock = new TextBlock
{
    Width = 600, 
    Foreground = _textBrush,
    Height = geometry.Bounds.Height,
    FontFamily = new FontFamily(_settings.FontFamily),
    FontSize = _fontSize,
    Text = 
quot;Your completion text"
};
// put the box at the end of your current line
Canvas.SetLeft(textBlock, geometry.Bounds.Right);
Canvas.SetTop(textBlock, geometry.Bounds.Top);
            
_layer.AddAdornment(AdornmentPositioningBehavior.TextRelative, triggeringLine.Extent, "your tag", textBlock, (tag, ui) => { });

You can get the current editor settings as follows:

// Get TextEditor properties
var propertiesList = dte.get_Properties("FontsAndColors", "TextEditor");

// Get the items that are shown in the dialog in VS
var itemsList = (FontsAndColorsItems)propertiesList.Item("FontsAndColorsItems").Object;

// Get color for comments
var commentItem = itemsList.Cast<ColorableItems>().Single(i => i.Name=="Comment");
var colorBytes = BitConverter.GetBytes(commentItem.Foreground);
var commentColor = Color.FromRgb(colorBytes[2], colorBytes[1], colorBytes[0]);

// Get editor BG
var textItem = itemsList.Cast<ColorableItems>().Single(i => i.Name == "Plain Text");
var bgColorBytes = BitConverter.GetBytes(textItem.Background);
var bbgColor = Color.FromRgb(bgColorBytes[2], bgColorBytes[1], bgColorBytes[0]);

// Get font size in points
var fontSize = (short)propertiesList.Item("FontSize").Value;

// Get current font family
var fontFamily = (string)propertiesList.Item("FontFamily").Value;

The issue with this approach is that the styling and font size slightly differs from the editor. Even if you use the editor setting.

However I think that IntelliCode and GitHub Copilot use another technique. As you can see here:
GitHub Coilot

It seems as the whole code is already inserted to the editor but has a special styling/behaviour. So it is possible somehow, but I don't how to achieve that.

For more information on adornments look here for example:
Visual Studio Text Adornment VSIX using Roslyn

小女人ら 2025-02-12 23:40:53

您可以实现基于自定义语言的语句完成。请看一下:

浏览:显示语句完成

实现自定义XAML Intellisense VS2017

扩展>

自定义IntelliSense扩展

另一个不同的选项是使用 github copilot for Visual Studio的扩展使用AI预测代码

You can implement your custom language-based statement completion. Please take a look at:

Walkthrough: Displaying Statement Completion

Implementing Custom XAML Intellisense VS2017 Extension

Visual-studio – Custom Intellisense Extension

Custom Intellisense Extension

Another different option is using GitHub Copilot extension for Visual Studio, it predicts code using AI

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文