InitializeHandler.cs 3.8 KB
Newer Older
J
Jonathon Marolf 已提交
1 2 3
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
4

5 6
#nullable enable

D
David Barbet 已提交
7 8 9
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
10
using System.Composition;
11
using System.Linq;
12 13
using System.Threading;
using System.Threading.Tasks;
14
using Microsoft.CodeAnalysis;
D
David Barbet 已提交
15
using Microsoft.CodeAnalysis.Completion;
16
using Microsoft.CodeAnalysis.Host.Mef;
17
using Microsoft.VisualStudio.LanguageServer.Protocol;
D
David Barbet 已提交
18
using LSP = Microsoft.VisualStudio.LanguageServer.Protocol;
19 20 21 22

namespace Microsoft.CodeAnalysis.LanguageServer.Handler
{
    [Shared]
D
David Barbet 已提交
23 24
    [ExportLspMethod(LSP.Methods.InitializeName)]
    internal class InitializeHandler : IRequestHandler<LSP.InitializeParams, LSP.InitializeResult>
25
    {
D
David Barbet 已提交
26 27
        private readonly ImmutableArray<Lazy<CompletionProvider, Completion.Providers.CompletionProviderMetadata>> _completionProviders;

28
        [ImportingConstructor]
29
        [Obsolete(MefConstruction.ImportingConstructorMessage, error: true)]
D
David Barbet 已提交
30
        public InitializeHandler([ImportMany] IEnumerable<Lazy<CompletionProvider, Completion.Providers.CompletionProviderMetadata>> completionProviders)
31
        {
D
David Barbet 已提交
32 33 34
            _completionProviders = completionProviders
                .Where(lz => lz.Metadata.Language == LanguageNames.CSharp || lz.Metadata.Language == LanguageNames.VisualBasic)
                .ToImmutableArray();
35 36
        }

37
        public Task<LSP.InitializeResult> HandleRequestAsync(LSP.InitializeParams request, RequestContext context, CancellationToken cancellationToken)
38
        {
A
Allison Chou 已提交
39
            var triggerCharacters = _completionProviders.SelectMany(lz => GetTriggerCharacters(lz.Value)).Distinct().Select(c => c.ToString()).ToArray();
40

D
David Barbet 已提交
41
            return Task.FromResult(new LSP.InitializeResult
42
            {
43
                Capabilities = new LSP.VSServerCapabilities
44 45
                {
                    DefinitionProvider = true,
46
                    RenameProvider = true,
47
                    ImplementationProvider = true,
48 49
                    CodeActionProvider = new LSP.CodeActionOptions { CodeActionKinds = new[] { CodeActionKind.QuickFix, CodeActionKind.Refactor } },
                    CodeActionsResolveProvider = true,
A
Allison Chou 已提交
50
                    CompletionProvider = new LSP.CompletionOptions { ResolveProvider = true, TriggerCharacters = triggerCharacters },
D
David Barbet 已提交
51
                    SignatureHelpProvider = new LSP.SignatureHelpOptions { TriggerCharacters = new[] { "(", "," } },
52 53 54 55
                    DocumentSymbolProvider = true,
                    WorkspaceSymbolProvider = true,
                    DocumentFormattingProvider = true,
                    DocumentRangeFormattingProvider = true,
D
David Barbet 已提交
56
                    DocumentOnTypeFormattingProvider = new LSP.DocumentOnTypeFormattingOptions { FirstTriggerCharacter = "}", MoreTriggerCharacter = new[] { ";", "\n" } },
57
                    OnAutoInsertProvider = new LSP.DocumentOnAutoInsertOptions { TriggerCharacters = new[] { "'", "/", "\n" } },
58
                    DocumentHighlightProvider = true,
59
                    ReferencesProvider = true,
D
David 已提交
60
                    ProjectContextProvider = true,
61
                    ExecuteCommandProvider = new LSP.ExecuteCommandOptions(),
62 63 64 65
                    TextDocumentSync = new LSP.TextDocumentSyncOptions
                    {
                        Change = LSP.TextDocumentSyncKind.None
                    }
66 67 68
                }
            });
        }
A
Allison Chou 已提交
69 70 71 72 73 74 75 76 77 78

        private static ImmutableHashSet<char> GetTriggerCharacters(CompletionProvider provider)
        {
            if (provider is LSPCompletionProvider lspProvider)
            {
                return lspProvider.TriggerCharacters;
            }

            return ImmutableHashSet<char>.Empty;
        }
79 80
    }
}