private ILspHandlerDescriptor FindDescriptor(string method, JToken @params)
        {
            _logger.LogDebug("Finding descriptor for {Method}", method);
            var descriptor = _collection.FirstOrDefault(x => x.Method == method);

            if (descriptor is null)
            {
                _logger.LogDebug("Unable to find {Method}, methods found include {Methods}", method,
                                 string.Join(", ", _collection.Select(x => x.Method + ":" + x.Handler?.GetType()?.FullName)));
                return(null);
            }

            if (@params == null || descriptor.Params == null)
            {
                return(descriptor);
            }

            var lspHandlerDescriptors = _collection.Where(handler => handler.Method == method).ToList();

            if (lspHandlerDescriptors.Count == 1)
            {
                return(descriptor);
            }

            var paramsValue = @params.ToObject(descriptor.Params, _serializer.JsonSerializer);

            return(_handlerMatchers.SelectMany(strat => strat.FindHandler(paramsValue, lspHandlerDescriptors)).FirstOrDefault() ?? descriptor);
        }
Exemplo n.º 2
0
        private IRequestDescriptor <ILspHandlerDescriptor> FindDescriptor(string method, JToken? @params)
        {
            _logger.LogDebug("Finding descriptors for {Method}", method);
            var descriptor = _collection.FirstOrDefault(x => x.Method == method);

            if (descriptor is null)
            {
                _logger.LogDebug(
                    "Unable to find {Method}, methods found include {Methods}", method,
                    string.Join(", ", _collection.Select(x => x.Method + ":" + x.Handler.GetType().FullName))
                    );
                return(new RequestDescriptor <ILspHandlerDescriptor>());
            }

            if (@params == null || descriptor.Params == null)
            {
                return(new RequestDescriptor <ILspHandlerDescriptor>(descriptor));
            }

            object?paramsValue;

            if (descriptor.IsDelegatingHandler)
            {
                var o = @params.ToObject(descriptor.Params.GetGenericArguments()[0], _serializer.JsonSerializer);
                paramsValue = Activator.CreateInstance(descriptor.Params, o);
            }
            else
            {
                paramsValue = @params.ToObject(descriptor.Params, _serializer.JsonSerializer);
            }

            var lspHandlerDescriptors = _collection.Where(handler => handler.Method == method).ToList();

            var matchDescriptor = _handlerMatchers.SelectMany(strat => strat.FindHandler(paramsValue, lspHandlerDescriptors)).ToArray();

            if (matchDescriptor.Length > 0)
            {
                return(new RequestDescriptor <ILspHandlerDescriptor>(matchDescriptor));
            }
            // execute command is a special case
            // if no command was found to execute this must error
            // this is not great coupling but other options require api changes
            if (paramsValue is IExecuteCommandParams)
            {
                return(new RequestDescriptor <ILspHandlerDescriptor>());
            }
            if (lspHandlerDescriptors.Count > 0)
            {
                return(new RequestDescriptor <ILspHandlerDescriptor>(lspHandlerDescriptors));
            }
            return(new RequestDescriptor <ILspHandlerDescriptor>());
        }
        private ILspHandlerDescriptor FindDescriptor(string method, JToken @params)
        {
            _logger.LogDebug("Finding descriptor for {Method}", method);
            var descriptor = _collection.FirstOrDefault(x => x.Method == method);

            if (descriptor is null)
            {
                _logger.LogDebug("Unable to find {Method}, methods found include {Methods}", method, string.Join(", ", _collection.Select(x => x.Method + ":" + x.Handler.GetType().FullName)));
                return(null);
            }

            if (@params != null && descriptor.Params != null)
            {
                var paramsValue = @params.ToObject(descriptor.Params);
                if (paramsValue is ITextDocumentIdentifierParams textDocumentIdentifierParams)
                {
                    var attributes = GetTextDocumentAttributes(textDocumentIdentifierParams.TextDocument.Uri);

                    _logger.LogTrace("Found attributes {Count}, {Attributes}", attributes.Count, attributes.Select(x => $"{x.LanguageId}:{x.Scheme}:{x.Uri}"));

                    return(GetHandler(method, attributes));
                }
                else if (paramsValue is DidOpenTextDocumentParams openTextDocumentParams)
                {
                    var attributes = new TextDocumentAttributes(openTextDocumentParams.TextDocument.Uri, openTextDocumentParams.TextDocument.LanguageId);

                    _logger.LogTrace("Created attribute {Attribute}", $"{attributes.LanguageId}:{attributes.Scheme}:{attributes.Uri}");

                    return(GetHandler(method, attributes));
                }
                else if (paramsValue is DidChangeTextDocumentParams didChangeDocumentParams)
                {
                    // TODO: Do something with document version here?
                    var attributes = GetTextDocumentAttributes(didChangeDocumentParams.TextDocument.Uri);

                    _logger.LogTrace("Found attributes {Count}, {Attributes}", attributes.Count, attributes.Select(x => $"{x.LanguageId}:{x.Scheme}:{x.Uri}"));

                    return(GetHandler(method, attributes));
                }
            }

            // TODO: How to split these
            // Do they fork and join?
            return(descriptor);
        }