Пример #1
0
        protected static async Task <LSP.VSCompletionItem> CreateCompletionItemAsync(
            string label,
            LSP.CompletionItemKind kind,
            string[] tags,
            LSP.CompletionParams request,
            Document document,
            bool preselect = false,
            ImmutableArray <char>?commitCharacters = null,
            LSP.TextEdit?textEdit = null,
            string?insertText     = null,
            string?sortText       = null,
            string?filterText     = null,
            long resultId         = 0)
        {
            var position = await document.GetPositionFromLinePositionAsync(
                ProtocolConversions.PositionToLinePosition(request.Position), CancellationToken.None).ConfigureAwait(false);

            var completionTrigger = await ProtocolConversions.LSPToRoslynCompletionTriggerAsync(
                request.Context, document, position, CancellationToken.None).ConfigureAwait(false);

            var item = new LSP.VSCompletionItem()
            {
                TextEdit         = textEdit,
                InsertText       = insertText,
                FilterText       = filterText ?? label,
                Label            = label,
                SortText         = sortText ?? label,
                InsertTextFormat = LSP.InsertTextFormat.Plaintext,
                Kind             = kind,
                Data             = JObject.FromObject(new CompletionResolveData()
                {
                    ResultId = resultId,
                }),
                Preselect = preselect
            };

            if (tags != null)
            {
                item.Icon = tags.ToImmutableArray().GetFirstGlyph().GetImageElement();
            }

            if (commitCharacters != null)
            {
                item.CommitCharacters = commitCharacters.Value.Select(c => c.ToString()).ToArray();
            }

            return(item);
        }
Пример #2
0
        public async Task <object> GetCompletions(LSP.Position position, LSP.CompletionContext context, CancellationToken token)
        {
            var completionParams = new LSP.CompletionParams()
            {
                TextDocument = new LSP.TextDocumentIdentifier()
                {
                    Uri = DocumentUri,
                },
                Position = GetDocumentPosition(position),
                Context  = context,
            };

            var res = await _client.InvokeTextDocumentCompletionAsync(completionParams, token);

            return(res);
        }
        protected static LSP.VSCompletionItem CreateCompletionItem(
            string insertText,
            LSP.CompletionItemKind kind,
            string[] tags,
            LSP.CompletionParams requestParameters,
            bool preselect = false,
            ImmutableArray <char>?commitCharacters = null,
            string?sortText = null,
            int resultId    = 0)
        {
            var item = new LSP.VSCompletionItem()
            {
                FilterText       = insertText,
                InsertText       = insertText,
                Label            = insertText,
                SortText         = sortText ?? insertText,
                InsertTextFormat = LSP.InsertTextFormat.Plaintext,
                Kind             = kind,
                Data             = JObject.FromObject(new CompletionResolveData()
                {
                    DisplayText       = insertText,
                    TextDocument      = requestParameters.TextDocument,
                    Position          = requestParameters.Position,
                    CompletionTrigger = ProtocolConversions.LSPToRoslynCompletionTrigger(requestParameters.Context),
                    ResultId          = resultId,
                }),
                Preselect = preselect
            };

            if (tags != null)
            {
                item.Icon = tags.ToImmutableArray().GetFirstGlyph().GetImageElement();
            }

            if (commitCharacters != null)
            {
                item.CommitCharacters = commitCharacters.Value.Select(c => c.ToString()).ToArray();
            }

            return(item);
        }
Пример #4
0
        private static async Task <LSP.CompletionList> RunGetCompletionsAsync(
            TestLspServer testLspServer,
            LSP.CompletionParams completionParams
            )
        {
            var clientCapabilities = new LSP.VSClientCapabilities
            {
                SupportsVisualStudioExtensions = true
            };

            return(await testLspServer.ExecuteRequestAsync <
                       LSP.CompletionParams,
                       LSP.CompletionList
                       >(
                       LSP.Methods.TextDocumentCompletionName,
                       completionParams,
                       clientCapabilities,
                       null,
                       CancellationToken.None
                       ));
        }
Пример #5
0
        private static async Task <LSP.CompletionList> RunGetCompletionsAsync(
            TestLspServer testLspServer,
            LSP.CompletionParams completionParams)
        {
            var completionList = await testLspServer.ExecuteRequestAsync <LSP.CompletionParams, LSP.CompletionList>(LSP.Methods.TextDocumentCompletionName,
                                                                                                                    completionParams, CancellationToken.None);

            // Emulate client behavior of promoting "Data" completion list properties onto completion items.
            if (testLspServer.ClientCapabilities.HasCompletionListDataCapability() &&
                completionList is VSInternalCompletionList vsCompletionList &&
                vsCompletionList.Data != null)
            {
                foreach (var completionItem in completionList.Items)
                {
                    Assert.Null(completionItem.Data);
                    completionItem.Data = vsCompletionList.Data;
                }
            }

            return(completionList);
        }
Пример #6
0
        private static LSP.VSCompletionItem CreateResolvedCompletionItem(string text, LSP.CompletionItemKind kind, string[] tags, LSP.CompletionParams requestParameters,
                                                                         ClassifiedTextElement description, string detail, string documentation, string[] commitCharacters = null)
        {
            var resolvedCompletionItem = CreateCompletionItem(text, kind, tags, requestParameters, commitCharacters: commitCharacters);

            resolvedCompletionItem.Detail = detail;
            if (documentation != null)
            {
                resolvedCompletionItem.Documentation = new LSP.MarkupContent()
                {
                    Kind  = LSP.MarkupKind.PlainText,
                    Value = documentation
                };
            }

            resolvedCompletionItem.Description = description;
            return(resolvedCompletionItem);
        }
Пример #7
0
 /// <summary>
 /// Answers a completion request by returning the valid completions at the location.
 /// https://microsoft.github.io/language-server-protocol/specification#textDocument_completion
 /// </summary>
 /// <param name="solution">the solution containing the document.</param>
 /// <param name="request">the document position and completion context.</param>
 /// <param name="clientCapabilities">the client capabilities for the request.</param>
 /// <param name="cancellationToken">a cancellation token.</param>
 /// <returns>a list of completions.</returns>
 public Task <object> GetCompletionsAsync(Solution solution, LSP.CompletionParams request, LSP.ClientCapabilities clientCapabilities, CancellationToken cancellationToken)
 => ExecuteRequestAsync <LSP.CompletionParams, object>(LSP.Methods.TextDocumentCompletionName, solution, request, clientCapabilities, cancellationToken);
Пример #8
0
        private static async Task <LSP.CompletionList> RunGetCompletionsAsync(Solution solution, LSP.CompletionParams completionParams)
        {
            var clientCapabilities = new LSP.VSClientCapabilities {
                SupportsVisualStudioExtensions = true
            };
            var queue = CreateRequestQueue(solution);

            return(await GetLanguageServer(solution).ExecuteRequestAsync <LSP.CompletionParams, LSP.CompletionList>(queue, LSP.Methods.TextDocumentCompletionName,
                                                                                                                    completionParams, clientCapabilities, null, CancellationToken.None));
        }
Пример #9
0
        public override async Task ProvideCompletionsAsync(CompletionContext context)
        {
            // This provider is exported for all workspaces - so limit it to just our workspace & the debugger's intellisense workspace
            if (context.Document.Project.Solution.Workspace.Kind != WorkspaceKind.AnyCodeRoslynWorkspace &&
                context.Document.Project.Solution.Workspace.Kind != StringConstants.DebuggerIntellisenseWorkspaceKind)
            {
                return;
            }

            var lspClient = _roslynLspClientServiceFactory.ActiveLanguageServerClient;

            if (lspClient == null)
            {
                return;
            }

            var text = await context.Document.GetTextAsync(context.CancellationToken).ConfigureAwait(false);

            var completionParams = new LSP.CompletionParams
            {
                TextDocument = ProtocolConversions.DocumentToTextDocumentIdentifier(context.Document),
                Position     = ProtocolConversions.LinePositionToPosition(text.Lines.GetLinePosition(context.Position)),
                Context      = new LSP.CompletionContext {
                    TriggerCharacter = context.Trigger.Character.ToString(), TriggerKind = GetTriggerKind(context.Trigger)
                }
            };

            var completionObject = await lspClient.RequestAsync(LSP.Methods.TextDocumentCompletion.ToLSRequest(), completionParams, context.CancellationToken).ConfigureAwait(false);

            if (completionObject == null)
            {
                return;
            }

            var completionList = ((JToken)completionObject).ToObject <RoslynCompletionItem[]>();

            foreach (var item in completionList)
            {
                ImmutableArray <string> tags;
                if (item.Tags != null)
                {
                    tags = item.Tags.AsImmutable();
                }
                else
                {
                    var glyph = ProtocolConversions.CompletionItemKindToGlyph(item.Kind);
                    tags = GlyphTags.GetTags(glyph);
                }

                var properties = ImmutableDictionary.CreateBuilder <string, string>();
                if (!string.IsNullOrEmpty(item.Detail))
                {
                    // The display text is encoded as TaggedText | value
                    properties.Add("Description", $"Text|{item.Detail}");
                }

                properties.Add("InsertionText", item.InsertText);
                properties.Add("ResolveData", JToken.FromObject(item).ToString());
                var completionItem = CompletionItem.Create(item.Label, item.FilterText, item.SortText, properties: properties.ToImmutable(), tags: tags);
                context.AddItem(completionItem);
            }
        }
Пример #10
0
        private static Task <LSP.CompletionList> RunGetCompletionsAsync(TestLspServer testLspServer, LSP.CompletionParams completionParams)
        {
            var clientCapabilities = new LSP.VSClientCapabilities {
                SupportsVisualStudioExtensions = true
            };

            return(RunGetCompletionsAsync(testLspServer, completionParams, clientCapabilities));
        }
Пример #11
0
 public Task <object> InvokeTextDocumentCompletionAsync(LSP.CompletionParams request, CancellationToken cancellationToken = default)
 => InvokeWithParametersAsync <object>("textDocument/completion", request, cancellationToken);