Example #1
0
        private void HandleInitialFixupRequest(string id, string paramsJson)
        {
            if (id == null)
            {
                _sendToClient(JsonRpcHelper.CreateErrorResult(id, JsonRpcHelper.ErrorCode.InvalidRequest));
                return;
            }

            Contracts.AssertValue(id);
            Contracts.AssertValue(paramsJson);

            if (!TryParseParams(paramsJson, out InitialFixupParams initialFixupParams))
            {
                _sendToClient(JsonRpcHelper.CreateErrorResult(id, JsonRpcHelper.ErrorCode.ParseError));
                return;
            }

            var documentUri = initialFixupParams.TextDocument.Uri;
            var scope       = _scopeFactory.GetOrCreateInstance(documentUri);

            var expression       = initialFixupParams.TextDocument.Text;
            var scopeDisplayName = scope as IPowerFxScopeDisplayName;

            if (scopeDisplayName != null)
            {
                expression = scopeDisplayName.TranslateToDisplayName(expression);
            }

            _sendToClient(JsonRpcHelper.CreateSuccessResult(id, new TextDocumentItem()
            {
                Uri  = documentUri,
                Text = expression
            }));
        }
Example #2
0
        private void HandleSignatureHelpRequest(string id, string paramsJson)
        {
            if (id == null)
            {
                _sendToClient(JsonRpcHelper.CreateErrorResult(id, JsonRpcHelper.ErrorCode.InvalidRequest));
                return;
            }

            Contracts.AssertValue(id);
            Contracts.AssertValue(paramsJson);

            if (!TryParseParams(paramsJson, out SignatureHelpParams signatureHelpParams))
            {
                _sendToClient(JsonRpcHelper.CreateErrorResult(id, JsonRpcHelper.ErrorCode.ParseError));
                return;
            }

            var documentUri = signatureHelpParams.TextDocument.Uri;
            var scope       = _scopeFactory.GetOrCreateInstance(documentUri);

            var uri        = new Uri(documentUri);
            var expression = HttpUtility.ParseQueryString(uri.Query).Get("expression");

            if (expression == null)
            {
                _sendToClient(JsonRpcHelper.CreateErrorResult(id, JsonRpcHelper.ErrorCode.InvalidParams));
                return;
            }

            var cursorPosition = GetPosition(expression, signatureHelpParams.Position.Line, signatureHelpParams.Position.Character);
            var result         = scope.Suggest(expression, cursorPosition);

            _sendToClient(JsonRpcHelper.CreateSuccessResult(id, result.SignatureHelp));
        }
Example #3
0
        private void HandleDidChangeNotification(string paramsJson)
        {
            Contracts.AssertValue(paramsJson);

            if (!TryParseParams(paramsJson, out DidChangeTextDocumentParams didChangeParams))
            {
                _sendToClient(JsonRpcHelper.CreateErrorResult(null, JsonRpcHelper.ErrorCode.ParseError));
                return;
            }

            if (didChangeParams.ContentChanges.Length != 1)
            {
                _sendToClient(JsonRpcHelper.CreateErrorResult(null, JsonRpcHelper.ErrorCode.InvalidParams));
                return;
            }

            OnDidChange?.Invoke(didChangeParams);

            var documentUri = didChangeParams.TextDocument.Uri;
            var scope       = _scopeFactory.GetOrCreateInstance(documentUri);

            var expression = didChangeParams.ContentChanges[0].Text;
            var result     = scope.Check(expression);

            PublishDiagnosticsNotification(documentUri, expression, result.Errors);

            PublishTokens(documentUri, result);
        }
Example #4
0
        private void HandleCompletionRequest(string id, string paramsJson)
        {
            if (id == null)
            {
                _sendToClient(JsonRpcHelper.CreateErrorResult(id, JsonRpcHelper.ErrorCode.InvalidRequest));
                return;
            }

            Contracts.AssertValue(id);
            Contracts.AssertValue(paramsJson);

            if (!TryParseParams(paramsJson, out CompletionParams completionParams))
            {
                _sendToClient(JsonRpcHelper.CreateErrorResult(id, JsonRpcHelper.ErrorCode.ParseError));
                return;
            }

            var documentUri = completionParams.TextDocument.Uri;
            var scope       = _scopeFactory.GetOrCreateInstance(documentUri);

            var uri        = new Uri(documentUri);
            var expression = HttpUtility.ParseQueryString(uri.Query).Get("expression");

            if (expression == null)
            {
                _sendToClient(JsonRpcHelper.CreateErrorResult(id, JsonRpcHelper.ErrorCode.InvalidParams));
                return;
            }

            var cursorPosition = GetPosition(expression, completionParams.Position.Line, completionParams.Position.Character);
            var result         = scope.Suggest(expression, cursorPosition);

            var items = new List <CompletionItem>();

            foreach (var item in result.Suggestions)
            {
                items.Add(new CompletionItem()
                {
                    Label         = item.DisplayText.Text,
                    Detail        = item.FunctionParameterDescription,
                    Documentation = item.Definition,
                    Kind          = GetCompletionItemKind(item.Kind)
                });
            }

            _sendToClient(JsonRpcHelper.CreateSuccessResult(id, new
            {
                items,
                isIncomplete = false
            }));
        }
Example #5
0
        /// <summary>
        /// Received request/notification payload from client
        /// </summary>
        public void OnDataReceived(string jsonRpcPayload)
        {
            Contracts.AssertValue(jsonRpcPayload);

            string id = null;

            try
            {
                using (var doc = JsonDocument.Parse(jsonRpcPayload))
                {
                    var element = doc.RootElement;
                    if (element.TryGetProperty("id", out JsonElement idElement))
                    {
                        id = idElement.GetString();
                    }
                    if (!element.TryGetProperty("method", out JsonElement methodElement))
                    {
                        _sendToClient(JsonRpcHelper.CreateErrorResult(id, JsonRpcHelper.ErrorCode.InvalidRequest));
                        return;
                    }
                    if (!element.TryGetProperty("params", out JsonElement paramsElement))
                    {
                        _sendToClient(JsonRpcHelper.CreateErrorResult(id, JsonRpcHelper.ErrorCode.InvalidRequest));
                        return;
                    }
                    var method     = methodElement.GetString();
                    var paramsJson = paramsElement.GetRawText();
                    switch (method)
                    {
                    case TextDocumentNames.DidOpen:
                        HandleDidOpenNotification(paramsJson);
                        break;

                    case TextDocumentNames.DidChange:
                        HandleDidChangeNotification(paramsJson);
                        break;

                    case TextDocumentNames.Completion:
                        HandleCompletionRequest(id, paramsJson);
                        break;

                    case TextDocumentNames.SignatureHelp:
                        HandleSignatureHelpRequest(id, paramsJson);
                        break;

                    case CustomProtocolNames.InitialFixup:
                        HandleInitialFixupRequest(id, paramsJson);
                        break;

                    default:
                        _sendToClient(JsonRpcHelper.CreateErrorResult(id, JsonRpcHelper.ErrorCode.MethodNotFound));
                        break;
                    }
                }
            }
            catch (Exception ex)
            {
                _sendToClient(JsonRpcHelper.CreateErrorResult(id, ex.Message));
                return;
            }
        }