public async Task <DocumentHighlightContainer> Handle(DocumentHighlightParams request, CancellationToken token)
        {
            var(document, position) = _workspace.GetLogicalDocument(request);

            var documentHighlightsService = _workspace.Services.GetService <IDocumentHighlightsService>();

            var documentHighlightsList = await documentHighlightsService.GetDocumentHighlightsAsync(
                document, position,
                ImmutableHashSet <Document> .Empty, // TODO
                token);

            var result = new List <DocumentHighlight>();

            foreach (var documentHighlights in documentHighlightsList)
            {
                if (documentHighlights.Document != document)
                {
                    continue;
                }

                foreach (var highlightSpan in documentHighlights.HighlightSpans)
                {
                    result.Add(new DocumentHighlight
                    {
                        Kind = highlightSpan.Kind == HighlightSpanKind.Definition
                            ? DocumentHighlightKind.Write
                            : DocumentHighlightKind.Read,
                        Range = Helpers.ToRange(document.SourceText, highlightSpan.TextSpan)
                    });
                }
            }

            return(result);
        }
        public override Task <DocumentHighlightContainer> Handle(
            DocumentHighlightParams request,
            CancellationToken cancellationToken)
        {
            ScriptFile scriptFile = _workspaceService.GetFile(request.TextDocument.Uri);

            IReadOnlyList <SymbolReference> symbolOccurrences = _symbolsService.FindOccurrencesInFile(
                scriptFile,
                request.Position.Line + 1,
                request.Position.Character + 1);

            if (symbolOccurrences == null)
            {
                return(Task.FromResult(s_emptyHighlightContainer));
            }

            var highlights = new DocumentHighlight[symbolOccurrences.Count];

            for (int i = 0; i < symbolOccurrences.Count; i++)
            {
                highlights[i] = new DocumentHighlight
                {
                    Kind  = DocumentHighlightKind.Write, // TODO: Which symbol types are writable?
                    Range = symbolOccurrences[i].ScriptRegion.ToRange()
                };
            }

            return(Task.FromResult(new DocumentHighlightContainer(highlights)));
        }
        public async Task HandleRequestAsync_ProjectionNotFound_ReturnsNull()
        {
            // Arrange
            var requestInvoker     = Mock.Of <LSPRequestInvoker>(MockBehavior.Strict);
            var projectionProvider = new Mock <LSPProjectionProvider>(MockBehavior.Strict).Object;

            Mock.Get(projectionProvider).Setup(projectionProvider => projectionProvider.GetProjectionAsync(It.IsAny <LSPDocumentSnapshot>(), It.IsAny <Position>(), CancellationToken.None))
            .Returns(Task.FromResult <ProjectionResult>(null));
            var documentMappingProvider = Mock.Of <LSPDocumentMappingProvider>(MockBehavior.Strict);
            var highlightHandler        = new DocumentHighlightHandler(requestInvoker, DocumentManager, projectionProvider, documentMappingProvider, LoggerProvider);
            var highlightRequest        = new DocumentHighlightParams()
            {
                TextDocument = new TextDocumentIdentifier()
                {
                    Uri = Uri
                },
                Position = new Position(0, 1)
            };

            // Act
            var result = await highlightHandler.HandleRequestAsync(highlightRequest, new ClientCapabilities(), CancellationToken.None).ConfigureAwait(false);

            // Assert
            Assert.Null(result);
        }
예제 #4
0
        public async Task HandleRequestAsync_ProjectionNotFound_ReturnsNull()
        {
            // Arrange
            var documentManager = new TestDocumentManager();

            documentManager.AddDocument(Uri, Mock.Of <LSPDocumentSnapshot>());
            var requestInvoker          = Mock.Of <LSPRequestInvoker>();
            var projectionProvider      = Mock.Of <LSPProjectionProvider>();
            var documentMappingProvider = Mock.Of <LSPDocumentMappingProvider>();
            var highlightHandler        = new DocumentHighlightHandler(requestInvoker, documentManager, projectionProvider, documentMappingProvider);
            var highlightRequest        = new DocumentHighlightParams()
            {
                TextDocument = new TextDocumentIdentifier()
                {
                    Uri = Uri
                },
                Position = new Position(0, 1)
            };

            // Act
            var result = await highlightHandler.HandleRequestAsync(highlightRequest, new ClientCapabilities(), CancellationToken.None).ConfigureAwait(false);

            // Assert
            Assert.Null(result);
        }
예제 #5
0
        public DocumentHighlight[] GetDocumentHighlights(DocumentHighlightParams arg, CancellationToken token)
        {
            this.traceSource.TraceEvent(TraceEventType.Information, 0, $"Received: {JToken.FromObject(arg)}");
            var result = this.server.GetDocumentHighlights(arg.PartialResultToken, arg.Position, token);

            this.traceSource.TraceEvent(TraceEventType.Information, 0, $"Sent: {JToken.FromObject(result)}");
            return(result);
        }
        public Task <DocumentHighlight[]> HighlightDocumentAsync(DocumentHighlightParams documentHighlightParams, CancellationToken cancellationToken)
        {
            if (documentHighlightParams is null)
            {
                throw new ArgumentNullException(nameof(documentHighlightParams));
            }

            return(ExecuteRequestAsync <DocumentHighlightParams, DocumentHighlight[]>(Methods.TextDocumentDocumentHighlightName, documentHighlightParams, _clientCapabilities, cancellationToken));
        }
        public async Task <DocumentHighlightContainer> Handle(DocumentHighlightParams request, CancellationToken cancellationToken)
        {
            (var code, var lines, var tree) = bufferManager.Get(request.TextDocument.Uri.ToString());
            int c = request.Position.Character;
            int l = request.Position.Line;

            if (lines == null)
            {
                return(new DocumentHighlight[0]);
            }

            string token = lines[l][c].ToString();

            if (lines[l][c].ToString().IsIdentifier())
            {
                for (int i = c + 1; i < lines[l].Length && lines[l][i].ToString().IsIdentifier(); i++)
                {
                    token += lines[l][i];
                }
                for (int i = c - 1; i >= 0 && lines[l][i].ToString().IsIdentifier(); i--)
                {
                    token = lines[l][i] + token;
                }
            }
            else
            {
                return(new DocumentHighlight[0]);
            }
            if (token.IsKeyword())
            {
                return(new DocumentHighlight[0]);
            }

            List <DocumentHighlight> highlights = new List <DocumentHighlight>();

            for (int lineNumber = 1; lineNumber < lines.Length; lineNumber++)
            {
                var matches = Regex.Matches(lines[lineNumber], token);
                foreach (Match match in matches)
                {
                    highlights.Add(new DocumentHighlight()
                    {
                        Range = new Range(new Position(lineNumber, match.Index), new Position(lineNumber, match.Index + match.Length))
                    });
                }
            }
            return(highlights);
        }
        public async Task HandleRequestAsync_RemapFailure_DiscardsLocation()
        {
            // Arrange
            var called            = false;
            var expectedHighlight = GetHighlight(5, 5, 5, 5);
            var documentManager   = new TestDocumentManager();

            documentManager.AddDocument(Uri, Mock.Of <LSPDocumentSnapshot>(d => d.Version == 0, MockBehavior.Strict));

            var csharpHighlight = GetHighlight(100, 100, 100, 100);
            var requestInvoker  = GetRequestInvoker <DocumentHighlightParams, DocumentHighlight[]>(
                new[] { csharpHighlight },
                (method, serverContentType, highlightParams, ct) =>
            {
                Assert.Equal(Methods.TextDocumentDocumentHighlightName, method);
                Assert.Equal(RazorLSPConstants.CSharpContentTypeName, serverContentType);
                called = true;
            });

            var projectionResult = new ProjectionResult()
            {
                LanguageKind = RazorLanguageKind.CSharp,
            };
            var projectionProvider = GetProjectionProvider(projectionResult);

            var documentMappingProvider = new Mock <LSPDocumentMappingProvider>(MockBehavior.Strict).Object;

            Mock.Get(documentMappingProvider).Setup(p => p.MapToDocumentRangesAsync(RazorLanguageKind.CSharp, Uri, It.IsAny <Range[]>(), CancellationToken.None))
            .Returns(Task.FromResult <RazorMapToDocumentRangesResponse>(null));

            var highlightHandler = new DocumentHighlightHandler(requestInvoker, documentManager, projectionProvider, documentMappingProvider);
            var highlightRequest = new DocumentHighlightParams()
            {
                TextDocument = new TextDocumentIdentifier()
                {
                    Uri = Uri
                },
                Position = new Position(10, 5)
            };

            // Act
            var result = await highlightHandler.HandleRequestAsync(highlightRequest, new ClientCapabilities(), CancellationToken.None).ConfigureAwait(false);

            // Assert
            Assert.True(called);
            Assert.Empty(result);
        }
예제 #9
0
        public async Task HandleRequestAsync_CSharpProjection_RemapsHighlightRange()
        {
            // Arrange
            var called            = false;
            var expectedHighlight = GetHighlight(5, 5, 5, 5);
            var documentManager   = new TestDocumentManager();

            documentManager.AddDocument(Uri, Mock.Of <LSPDocumentSnapshot>(d => d.Version == 0));

            var csharpHighlight = GetHighlight(100, 100, 100, 100);
            var requestInvoker  = GetRequestInvoker <DocumentHighlightParams, DocumentHighlight[]>(
                new[] { csharpHighlight },
                (method, serverKind, highlightParams, ct) =>
            {
                Assert.Equal(Methods.TextDocumentDocumentHighlightName, method);
                Assert.Equal(LanguageServerKind.CSharp, serverKind);
                called = true;
            });

            var projectionResult = new ProjectionResult()
            {
                LanguageKind = RazorLanguageKind.CSharp,
            };
            var projectionProvider = GetProjectionProvider(projectionResult);

            var documentMappingProvider = GetDocumentMappingProvider(expectedHighlight.Range, 0, RazorLanguageKind.CSharp);

            var highlightHandler = new DocumentHighlightHandler(requestInvoker, documentManager, projectionProvider, documentMappingProvider);
            var highlightRequest = new DocumentHighlightParams()
            {
                TextDocument = new TextDocumentIdentifier()
                {
                    Uri = Uri
                },
                Position = new Position(10, 5)
            };

            // Act
            var result = await highlightHandler.HandleRequestAsync(highlightRequest, new ClientCapabilities(), CancellationToken.None).ConfigureAwait(false);

            // Assert
            Assert.True(called);
            var actualHighlight = Assert.Single(result);

            Assert.Equal(expectedHighlight.Range, actualHighlight.Range);
        }
예제 #10
0
        public async Task HandleRequestAsync_VersionMismatch_DiscardsLocation()
        {
            // Arrange
            var called            = false;
            var expectedHighlight = GetHighlight(5, 5, 5, 5);
            var documentManager   = new TestDocumentManager();

            documentManager.AddDocument(Uri, Mock.Of <LSPDocumentSnapshot>(d => d.Version == 1));

            var csharpHighlight = GetHighlight(100, 100, 100, 100);
            var requestInvoker  = GetRequestInvoker <DocumentHighlightParams, DocumentHighlight[]>(
                new[] { csharpHighlight },
                (method, serverContentType, highlightParams, ct) =>
            {
                Assert.Equal(Methods.TextDocumentDocumentHighlightName, method);
                Assert.Equal(RazorLSPConstants.CSharpContentTypeName, serverContentType);
                called = true;
            });

            var projectionResult = new ProjectionResult()
            {
                LanguageKind = RazorLanguageKind.CSharp,
            };
            var projectionProvider = GetProjectionProvider(projectionResult);

            var documentMappingProvider = GetDocumentMappingProvider(expectedHighlight.Range, 0 /* Different from document version (1) */, RazorLanguageKind.CSharp);

            var highlightHandler = new DocumentHighlightHandler(requestInvoker, documentManager, projectionProvider, documentMappingProvider);
            var highlightRequest = new DocumentHighlightParams()
            {
                TextDocument = new TextDocumentIdentifier()
                {
                    Uri = Uri
                },
                Position = new Position(10, 5)
            };

            // Act
            var result = await highlightHandler.HandleRequestAsync(highlightRequest, new ClientCapabilities(), CancellationToken.None).ConfigureAwait(false);

            // Assert
            Assert.True(called);
            Assert.Empty(result);
        }
        public async Task HandleRequestAsync_HtmlProjection_RemapsHighlightRange()
        {
            // Arrange
            var called            = false;
            var expectedHighlight = GetHighlight(5, 5, 5, 5);

            var htmlHighlight  = GetHighlight(100, 100, 100, 100);
            var requestInvoker = GetRequestInvoker <DocumentHighlightParams, DocumentHighlight[]>(
                new[] { htmlHighlight },
                (textBuffer, method, clientName, highlightParams, ct) =>
            {
                Assert.Equal(Methods.TextDocumentDocumentHighlightName, method);
                Assert.Equal(RazorLSPConstants.HtmlLanguageServerName, clientName);
                called = true;
            });

            var projectionResult = new ProjectionResult()
            {
                LanguageKind = RazorLanguageKind.Html,
            };
            var projectionProvider = GetProjectionProvider(projectionResult);

            var documentMappingProvider = GetDocumentMappingProvider(expectedHighlight.Range, 0, RazorLanguageKind.Html);

            var highlightHandler = new DocumentHighlightHandler(requestInvoker, DocumentManager, projectionProvider, documentMappingProvider, LoggerProvider);
            var highlightRequest = new DocumentHighlightParams()
            {
                TextDocument = new TextDocumentIdentifier()
                {
                    Uri = Uri
                },
                Position = new Position(10, 5)
            };

            // Act
            var result = await highlightHandler.HandleRequestAsync(highlightRequest, new ClientCapabilities(), CancellationToken.None).ConfigureAwait(false);

            // Assert
            Assert.True(called);
            var actualHighlight = Assert.Single(result);

            Assert.Equal(expectedHighlight.Range, actualHighlight.Range);
        }
예제 #12
0
        public override Task <DocumentHighlightContainer?> Handle(DocumentHighlightParams request, CancellationToken cancellationToken)
        {
            var result = this.symbolResolver.ResolveSymbol(request.TextDocument.Uri, request.Position);

            if (result == null)
            {
                return(Task.FromResult <DocumentHighlightContainer?>(null));
            }

            var highlights = result.Context.Compilation.GetEntrypointSemanticModel()
                             .FindReferences(result.Symbol)
                             .Select(referenceSyntax => new DocumentHighlight
            {
                Range = PositionHelper.GetNameRange(result.Context.LineStarts, referenceSyntax),
                Kind  = referenceSyntax switch {
                    INamedDeclarationSyntax _ => DocumentHighlightKind.Write,
                    ObjectPropertySyntax _ => DocumentHighlightKind.Write,
                    _ => DocumentHighlightKind.Read,
                },
            });
        public override Task <DocumentHighlightContainer> Handle(DocumentHighlightParams request, CancellationToken cancellationToken)
        {
            var result = this.symbolResolver.ResolveSymbol(request.TextDocument.Uri, request.Position);

            if (result == null)
            {
                return(Task.FromResult(new DocumentHighlightContainer()));
            }

            var highlights = result.Context.Compilation.GetSemanticModel()
                             .FindReferences(result.Symbol)
                             .Select(referenceSyntax => new DocumentHighlight
            {
                Range = PositionHelper.GetNameRange(result.Context.LineStarts, referenceSyntax),
                Kind  = referenceSyntax is IDeclarationSyntax
                        ? DocumentHighlightKind.Write
                        : DocumentHighlightKind.Read
            });

            return(Task.FromResult(new DocumentHighlightContainer(highlights)));
        }
 public static Task <DocumentHighlightContainer> DocumentHighlight(this ILanguageClientDocument mediator, DocumentHighlightParams @params, CancellationToken cancellationToken = default)
 {
     return(mediator.SendRequest(@params, cancellationToken));
 }
 public abstract Task <DocumentHighlightContainer> Handle(DocumentHighlightParams request, CancellationToken cancellationToken);
 public override Task <DocumentHighlightContainer> Handle(DocumentHighlightParams request, CancellationToken cancellationToken) => _handler.Invoke(request, cancellationToken);
예제 #17
0
 public override Task <DocumentHighlightContainer?> Handle(DocumentHighlightParams request, CancellationToken cancellationToken)
 {
     throw new NotImplementedException();
 }
예제 #18
0
        public override Task <DocumentHighlightContainer> Handle(DocumentHighlightParams request,
                                                                 CancellationToken cancellationToken)
        {
            var workDone = ProgressManager.WorkDone(request, new WorkDoneProgressBegin
            {
                Title      = "Begin finding highlights",
                Percentage = 0
            });

            var editorData       = DResolverWrapper.CreateEditorData(request, cancellationToken);
            var nodeSymbolToFind = DResolverWrapper
                                   .ResolveHoveredCodeLoosely(editorData, out LooseResolution.NodeResolutionAttempt _,
                                                              out ISyntaxRegion _)
                                   .Select(GetResultMember)
                                   .FirstOrDefault();

            if (nodeSymbolToFind == null)
            {
                workDone.OnNext(new WorkDoneProgressReport()
                {
                    Message    = "No symbol found",
                    Percentage = 100
                });
                workDone.OnCompleted();
                return(Task.FromResult(new DocumentHighlightContainer()));
            }

            var progress = ProgressManager.For(request, cancellationToken);
            List <DocumentHighlight> allFoundReferences;
            var ctxt = ResolutionContext.Create(editorData, true);

            try
            {
                allFoundReferences = cancellationToken.IsCancellationRequested
                    ? new List <DocumentHighlight>()
                    : ReferencesFinder
                                     .SearchModuleForASTNodeReferences(editorData.SyntaxTree, nodeSymbolToFind, ctxt)
                                     .Where(region => region != null)
                                     .OrderBy(region => region.Location)
                                     .Select(ToDocumentHighlight)
                                     .ToList();
            }
            catch (Exception ex)
            {
                workDone.OnError(ex);
                if (progress != null)
                {
                    progress.OnError(ex);
                    return(Task.FromResult(new DocumentHighlightContainer()));
                }

                return(Task.FromException <DocumentHighlightContainer>(ex));
            }

            if (allFoundReferences.Count > 0)
            {
                progress?.OnNext(new DocumentHighlightContainer(allFoundReferences));
            }

            workDone.OnCompleted();
            progress?.OnCompleted();

            return(Task.FromResult(progress != null
                ? new DocumentHighlightContainer()
                : new DocumentHighlightContainer(allFoundReferences)));
        }
 public static Task <DocumentHighlightContainer> DocumentHighlight(this ILanguageClientDocument mediator, DocumentHighlightParams @params)
 {
     return(mediator.SendRequest <DocumentHighlightParams, DocumentHighlightContainer>(DocumentNames.DocumentHighlight, @params));
 }