You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hello guys! I am new with Roslyn and doing some experiments. I am trying to write extensions for VS (2022) with custom autocomplete functionality, but i have some problems. I wanna do 2 things:
If we have comment that started with @ //@ -> @fruits: (and trigger to step 2)
If we have comment //@fruits: -> @fruits:apple/cherry/banana
I tried different variants, but it doesnt work. Nothing is happened
I tried to debug it, but breakpoint does not hit and nothing happened. I tried different variants and i didn`t find any literature about intellisense...Can you help me and give some advices/guides and how to fix it? I found that CompletionProvider can help, but nothing :(
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Options;
using Microsoft.CodeAnalysis.Text;
using Microsoft.CodeAnalysis.Completion;
using System.Composition;
using Microsoft.IO;
namespace Fruits_VS
{
[ExportCompletionProvider(nameof(ColorCompletionProvider), LanguageNames.CSharp)]
[Shared]
public sealed class ColorCompletionProvider : CompletionProvider
{
public override async Task ProvideCompletionsAsync(CompletionContext context)
{
string currentLine = await GetCurrentLineTextAsync(context).ConfigureAwait(false);
int indexOfAt = currentLine.LastIndexOf('@');
int indexOfColon = currentLine.LastIndexOf(':');
switch (indexOfAt >= 0)
{
case true when indexOfColon < 0:
context.AddItem(CompletionItem.Create("@fruits:"));
break;
case true when indexOfColon > indexOfAt:
{
if (currentLine.Contains("@fruits:"))
{
var fruits= new[] { "apple", "cherry", "banana" };
foreach (var fruit in fruits)
{
context.AddItem(CompletionItem.Create(fruit ));
}
}
break;
}
}
}
private async Task<string> GetCurrentLineTextAsync(CompletionContext context)
{
var text = await context.Document.GetTextAsync().ConfigureAwait(false);
var position = context.Position;
var line = text.Lines.GetLineFromPosition(position);
return text.ToString(line.Span);
}
public override bool ShouldTriggerCompletion(SourceText text, int caretPosition, CompletionTrigger trigger, OptionSet options)
{
switch (trigger.Kind)
{
case CompletionTriggerKind.Invoke:
return true;
case CompletionTriggerKind.Insertion when caretPosition > 0:
{
var currentCharacter = text[caretPosition - 1];
var lineStart = text.Lines.GetLineFromPosition(caretPosition).Start;
var lineTextUpToCaret = text.GetSubText(TextSpan.FromBounds(lineStart, caretPosition)).ToString();
return (currentCharacter == '@' && (lineTextUpToCaret.StartsWith("//") || lineTextUpToCaret.StartsWith("/*")))
|| (currentCharacter == ':' && lineTextUpToCaret.Contains('@') && (lineTextUpToCaret.StartsWith("//") || lineTextUpToCaret.StartsWith("/*")));
}
default:
return false;
}
}
}
}
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Hello guys! I am new with Roslyn and doing some experiments. I am trying to write extensions for VS (2022) with custom autocomplete functionality, but i have some problems. I wanna do 2 things:
If we have comment that started with @ //@ -> @fruits: (and trigger to step 2)
If we have comment //@fruits: -> @fruits:apple/cherry/banana
I tried different variants, but it doesnt work. Nothing is happened
I tried to debug it, but breakpoint does not hit and nothing happened. I tried different variants and i didn`t find any literature about intellisense...Can you help me and give some advices/guides and how to fix it? I found that CompletionProvider can help, but nothing :(
Beta Was this translation helpful? Give feedback.
All reactions