Exemple #1
0
        public async Task ExpressionBodyMethodExtended()
        {
            var workspace = WorkspaceFixture.GetWorkspace(ExportProvider);

            workspace.TryApplyChanges(workspace.CurrentSolution.WithOptions(workspace.Options.WithChangedOption(
                                                                                CSharpCodeStyleOptions.PreferExpressionBodiedMethods,
                                                                                new CodeStyleOption2 <ExpressionBodyPreference>(ExpressionBodyPreference.WhenPossible, NotificationOption2.Silent))));

            var text = @"using System;
partial class Bar
{
    public partial void Foo();
    partial $$
}
"
            ;

            var expected = @"using System;
partial class Bar
{
    public partial void Foo();
    public partial void Foo() => throw new NotImplementedException();$$
}
"
            ;

            await VerifyCustomCommitProviderAsync(text, "Foo()", expected);
        }
Exemple #2
0
        public async Task CustomNamingStyleInsideMethod()
        {
            var workspace       = WorkspaceFixture.GetWorkspace();
            var originalOptions = workspace.Options;

            try
            {
                workspace.Options = workspace.Options.WithChangedOption(
                    new OptionKey(SimplificationOptions.NamingPreferences, LanguageNames.CSharp),
                    NamesEndWithSuffixPreferences());

                var markup = @"
class Configuration
{
    void M()
    {
        Configuration $$
    }
}
";
                await VerifyItemExistsAsync(markup, "ConfigurationLocal", glyph : (int)Glyph.Local,
                                            expectedDescriptionOrNull : CSharpFeaturesResources.Suggested_name);
                await VerifyItemExistsAsync(markup, "ConfigurationLocalFunction", glyph : (int)Glyph.MethodPublic,
                                            expectedDescriptionOrNull : CSharpFeaturesResources.Suggested_name);
                await VerifyItemIsAbsentAsync(markup, "ConfigurationField");
                await VerifyItemIsAbsentAsync(markup, "ConfigurationMethod");
                await VerifyItemIsAbsentAsync(markup, "ConfigurationProperty");
            }
            finally
            {
                workspace.Options = originalOptions;
            }
        }
Exemple #3
0
        private async Task <IEnumerable <CompletionItem> > GetCompletionList(string code, int cursorPosition, ScriptState scriptState)
        {
            var metadataReferences = ImmutableArray <MetadataReference> .Empty;

            var forcedState = false;

            if (scriptState == null)
            {
                scriptState = await CSharpScript.RunAsync(string.Empty, ScriptOptions);

                forcedState = true;
            }

            var compilation = scriptState.Script.GetCompilation();

            metadataReferences = metadataReferences.AddRange(compilation.References);
            var originalCode = forcedState ? string.Empty : scriptState.Script.Code ?? string.Empty;

            var buffer = new StringBuilder(originalCode);

            if (!string.IsNullOrWhiteSpace(originalCode) && !originalCode.EndsWith(Environment.NewLine))
            {
                buffer.AppendLine();
            }

            buffer.AppendLine(code);
            var fullScriptCode   = buffer.ToString();
            var offset           = fullScriptCode.LastIndexOf(code, StringComparison.InvariantCulture);
            var absolutePosition = Math.Max(offset, 0) + cursorPosition;

            if (_fixture == null || _metadataReferences != metadataReferences)
            {
                _fixture            = new WorkspaceFixture(compilation.Options, metadataReferences);
                _metadataReferences = metadataReferences;
            }

            var document = _fixture.ForkDocument(fullScriptCode);
            var service  = CompletionService.GetService(document);

            var completionList = await service.GetCompletionsAsync(document, absolutePosition);

            var semanticModel = await document.GetSemanticModelAsync();

            var symbols = await Recommender.GetRecommendedSymbolsAtPositionAsync(semanticModel, absolutePosition, document.Project.Solution.Workspace);

            var symbolToSymbolKey = new Dictionary <(string, int), ISymbol>();

            foreach (var symbol in symbols)
            {
                var key = (symbol.Name, (int)symbol.Kind);
                if (!symbolToSymbolKey.ContainsKey(key))
                {
                    symbolToSymbolKey[key] = symbol;
                }
            }
            var items = completionList.Items.Select(item => item.ToModel(symbolToSymbolKey, document).ToDomainObject()).ToArray();

            return(items);
        }
        public async Task DisabledByOption()
        {
            var workspace = WorkspaceFixture.GetWorkspace();
            var originalOptions = WorkspaceFixture.GetWorkspace().Options;
            try
            {
                workspace.Options = originalOptions.
                    WithChangedOption(CompletionOptions.ShowNameSuggestions, LanguageNames.CSharp, false);

                var markup = @"
class Test
{
    Test $$
}
";
                await VerifyNoItemsExistAsync(markup);
            }
            finally
            {
                workspace.Options = originalOptions;
            }
        }
        public async Task ExpressionBodyMethod()
        {
            var workspace       = WorkspaceFixture.GetWorkspace();
            var originalOptions = workspace.Options;

            try
            {
                workspace.Options = originalOptions.WithChangedOption(
                    CSharpCodeStyleOptions.PreferExpressionBodiedMethods,
                    new CodeStyleOption <ExpressionBodyPreference>(ExpressionBodyPreference.WhenPossible, NotificationOption.Silent));

                var text = @"using System;
partial class Bar
{
    partial void Foo();
    partial $$
}
"
                ;

                var expected = @"using System;
partial class Bar
{
    partial void Foo();
    partial void Foo() => throw new NotImplementedException();$$
}
"
                ;

                await VerifyCustomCommitProviderAsync(text, "Foo()", expected);
            }
            finally
            {
                workspace.Options = originalOptions;
            }
        }