1

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

Mark
  • 143,421
  • 24
  • 428
  • 436
Andre.L
  • 36
  • 1
  • 7

2 Answers2

0

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

General Grievance
  • 4,555
  • 31
  • 31
  • 45
Eskandar Abedini
  • 2,090
  • 2
  • 13
0

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 = $"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

MalteKl
  • 1
  • 1