public async Task <Unit> Handle(DidChangeTextDocumentParams notification, CancellationToken token)
        {
            _threadManager.AssertBackgroundThread();
            _router.Window.LogMessage(new LogMessageParams()
            {
                Type    = MessageType.Log,
                Message = "Proto file changed: " + notification.TextDocument.Uri.AbsolutePath,
            });

            var document = await Task.Factory.StartNew(() =>
            {
                _snapshotManager.TryResolveDocument(notification.TextDocument.Uri.AbsolutePath, out var documentSnapshot);
                return(documentSnapshot);
            },
                                                       CancellationToken.None,
                                                       TaskCreationOptions.None,
                                                       _threadManager.ForegroundScheduler);

            var sourceText = await document.GetTextAsync();

            sourceText = ApplyContentChanges(notification.ContentChanges, sourceText);

            await Task.Factory.StartNew(
                () => _snapshotManager.DocumentChanged(notification.TextDocument.Uri.AbsolutePath, sourceText, notification.TextDocument.Version),
                CancellationToken.None,
                TaskCreationOptions.None,
                _threadManager.ForegroundScheduler);

            return(Unit.Value);
        }
示例#2
0
        public async Task <CompletionList> Handle(CompletionParams request, CancellationToken cancellationToken)
        {
            _router.Window.LogMessage(new LogMessageParams()
            {
                Type    = MessageType.Log,
                Message = "Proto file completion list request at line: " + (request.Position.Line + 1),
            });

            _threadManager.AssertBackgroundThread();

            var document = await Task.Factory.StartNew(
                () => {
                _snapshotManager.TryResolveDocument(request.TextDocument.Uri.AbsolutePath, out var doc);
                return(doc);
            },
                CancellationToken.None,
                TaskCreationOptions.None,
                _threadManager.ForegroundScheduler);

            var syntaxTree = await document.GetSyntaxTreeAsync();

            var completionItems = new List <CompletionItem>();

            foreach (var completionItemProvider in _completionItemProviders)
            {
                var owner = syntaxTree.Root.GetNodeAt((int)request.Position.Line, (int)request.Position.Character);
                var resolvedCompletions = completionItemProvider.GetCompletionItems(owner, request.Position, syntaxTree);
                completionItems.AddRange(resolvedCompletions);
            }

            var completionList = new CompletionList(completionItems);

            return(completionList);
        }
        public async Task <LocationOrLocationLinks> Handle(DefinitionParams request, CancellationToken cancellationToken)
        {
            _router.Window.LogMessage(new LogMessageParams()
            {
                Type    = MessageType.Log,
                Message = "Go to definition request at line: " + (request.Position.Line + 1),
            });

            _threadManager.AssertBackgroundThread();

            var document = await Task.Factory.StartNew(
                () =>
            {
                _snapshotManager.TryResolveDocument(request.TextDocument.Uri.AbsolutePath, out var doc);
                return(doc);
            },
                CancellationToken.None,
                TaskCreationOptions.None,
                _threadManager.ForegroundScheduler);

            var syntaxTree = await document.GetSyntaxTreeAsync();

            var child = syntaxTree.Root.GetNodeAt((int)request.Position.Line, (int)request.Position.Character);

            if (child.Parent is MethodNode && (child is InputNode || child is OutputNode))
            {
                //var declaringTypeNode = FindDeclaringTypeNode(syntaxTree.Root, child.Content);
                var declaringTypeNode = syntaxTree.Root.Messages.SingleOrDefault(c => c.Name == child.Content).NameNode;
                if (declaringTypeNode != null)
                {
                    var declaringTypeNodeLocation = new LocationOrLocationLink(
                        new Location()
                    {
                        Range = new Range(
                            new Position(declaringTypeNode.StartLine, declaringTypeNode.StartCol),
                            new Position(declaringTypeNode.EndLine, declaringTypeNode.EndCol)),
                        Uri = request.TextDocument.Uri,
                    });
                    var locations = new LocationOrLocationLinks(declaringTypeNodeLocation);
                    return(locations);
                }
            }

            var emptyLocations = new LocationOrLocationLinks();

            return(emptyLocations);
        }
        public async Task <CompletionList> Handle(CompletionParams request, CancellationToken cancellationToken)
        {
            _router.Window.LogMessage(new LogMessageParams()
            {
                Type    = MessageType.Log,
                Message = "Proto file completion list request at line: " + (request.Position.Line + 1),
            });

            _threadManager.AssertBackgroundThread();

            var document = await Task.Factory.StartNew(
                () => {
                _snapshotManager.TryResolveDocument(request.TextDocument.Uri.AbsolutePath, out var doc);
                return(doc);
            },
                CancellationToken.None,
                TaskCreationOptions.None,
                _threadManager.ForegroundScheduler);

            var syntaxTree = await document.GetSyntaxTreeAsync();

            // TODO: Do something useful with this syntax tree.

            // Provide your completions here
            var item1 = new CompletionItem()
            {
                Label      = "Sample completion item 1",
                InsertText = "SampleInsertText1"
            };
            var item2 = new CompletionItem()
            {
                Label      = "Sample completion item 2",
                InsertText = "SampleInsertText2"
            };
            var completionList = new CompletionList(item1, item2);

            return(completionList);
        }
示例#5
0
        public async Task <WorkspaceEdit> Handle(RenameParams request, CancellationToken cancellationToken)
        {
            _router.Window.LogMessage(new LogMessageParams()
            {
                Type    = MessageType.Log,
                Message = "Proto file completion list request at line: " + (request.Position.Line + 1),
            });

            _threadManager.AssertBackgroundThread();

            var document = await Task.Factory.StartNew(
                () =>
            {
                _snapshotManager.TryResolveDocument(request.TextDocument.Uri.AbsolutePath, out var doc);
                return(doc);
            },
                CancellationToken.None,
                TaskCreationOptions.None,
                _threadManager.ForegroundScheduler);

            var syntaxTree = await document.GetSyntaxTreeAsync();

            var documentChanges = new List <TextEdit>();

            var owner = syntaxTree.Root.GetNodeAt((int)request.Position.Line, (int)request.Position.Character);


            if (owner is InputNode || owner is OutputNode || (owner is NameNode && owner.Parent is MessageNode))
            {
                var nameQuery = owner.Content;

                // We need to find all other Input, Output and Message nodes to rename them.
                var inputNodes = syntaxTree.Root.GetDescendentNodes <InputNode>();
                for (var i = 0; i < inputNodes.Count; i++)
                {
                    var inputNode = inputNodes[i];
                    if (inputNode.Content == nameQuery)
                    {
                        var textEdit = new TextEdit()
                        {
                            Range = new Range(
                                new Position(inputNode.StartLine, inputNode.StartCol),
                                new Position(inputNode.EndLine, inputNode.EndCol)),
                            NewText = request.NewName,
                        };
                        documentChanges.Add(textEdit);
                    }
                }

                var outputNodes = syntaxTree.Root.GetDescendentNodes <OutputNode>();
                for (var i = 0; i < outputNodes.Count; i++)
                {
                    var outputNode = outputNodes[i];
                    if (outputNode.Content == nameQuery)
                    {
                        var textEdit = new TextEdit()
                        {
                            Range = new Range(
                                new Position(outputNode.StartLine, outputNode.StartCol),
                                new Position(outputNode.EndLine, outputNode.EndCol)),
                            NewText = request.NewName,
                        };
                        documentChanges.Add(textEdit);
                    }
                }

                for (var i = 0; i < syntaxTree.Root.Messages.Count; i++)
                {
                    var message = syntaxTree.Root.Messages[i];
                    if (message.Name == nameQuery)
                    {
                        var textEdit = new TextEdit()
                        {
                            Range = new Range(
                                new Position(message.NameNode.StartLine, message.NameNode.StartCol),
                                new Position(message.NameNode.EndLine, message.NameNode.EndCol)),
                            NewText = request.NewName,
                        };
                        documentChanges.Add(textEdit);
                    }
                }
            }

            var workspaceEdit = new WorkspaceEdit();

            if (documentChanges.Count > 0)
            {
                workspaceEdit.Changes = new Dictionary <Uri, IEnumerable <TextEdit> >()
                {
                    [request.TextDocument.Uri] = documentChanges,
                };
            }

            return(workspaceEdit);
        }