public async Task <CompletionList> Handle(CompletionParams request, CancellationToken cancellationToken)
        {
            int cursorLine   = (int)request.Position.Line + 1;
            int cursorColumn = (int)request.Position.Character + 1;

            ScriptFile scriptFile =
                _workspaceService.GetFile(
                    request.TextDocument.Uri.ToString());

            CompletionResults completionResults =
                await GetCompletionsInFileAsync(
                    scriptFile,
                    cursorLine,
                    cursorColumn);

            CompletionItem[] completionItems = s_emptyCompletionResult;

            if (completionResults != null)
            {
                completionItems = new CompletionItem[completionResults.Completions.Length];
                for (int i = 0; i < completionItems.Length; i++)
                {
                    completionItems[i] = CreateCompletionItem(completionResults.Completions[i], completionResults.ReplacedRange, i + 1);
                }
            }

            return(new CompletionList(completionItems));
        }
        /// <summary>
        /// Gets completions for a statement contained in the given
        /// script file at the specified line and column position.
        /// </summary>
        /// <param name="scriptFile">
        /// The script file in which completions will be gathered.
        /// </param>
        /// <param name="lineNumber">
        /// The 1-based line number at which completions will be gathered.
        /// </param>
        /// <param name="columnNumber">
        /// The 1-based column number at which completions will be gathered.
        /// </param>
        /// <returns>
        /// A CommandCompletion instance completions for the identified statement.
        /// </returns>
        public async Task <CompletionResults> GetCompletionsInFileAsync(
            ScriptFile scriptFile,
            int lineNumber,
            int columnNumber)
        {
            Validate.IsNotNull(nameof(scriptFile), scriptFile);

            // Get the offset at the specified position.  This method
            // will also validate the given position.
            int fileOffset =
                scriptFile.GetOffsetAtPosition(
                    lineNumber,
                    columnNumber);

            CommandCompletion commandCompletion = null;

            using (var cts = new CancellationTokenSource(DefaultWaitTimeoutMilliseconds))
            {
                commandCompletion =
                    await AstOperations.GetCompletionsAsync(
                        scriptFile.ScriptAst,
                        scriptFile.ScriptTokens,
                        fileOffset,
                        _powerShellContextService,
                        _logger,
                        cts.Token).ConfigureAwait(false);
            }

            if (commandCompletion == null)
            {
                return(new CompletionResults());
            }

            try
            {
                CompletionResults completionResults =
                    CompletionResults.Create(
                        scriptFile,
                        commandCompletion);

                // save state of most recent completion
                _mostRecentCompletions   = completionResults;
                _mostRecentRequestFile   = scriptFile.Id;
                _mostRecentRequestLine   = lineNumber;
                _mostRecentRequestOffest = columnNumber;

                return(completionResults);
            }
            catch (ArgumentException e)
            {
                // Bad completion results could return an invalid
                // replacement range, catch that here
                _logger.LogError(
                    $"Caught exception while trying to create CompletionResults:\n\n{e.ToString()}");

                return(new CompletionResults());
            }
        }
Exemplo n.º 3
0
        public async Task LanguageServiceCompletesFilePath()
        {
            CompletionResults completionResults =
                await this.GetCompletionResults(
                    CompleteFilePath.SourceDetails);

            Assert.NotEqual(0, completionResults.Completions.Length);
            Assert.Equal(
                CompleteFilePath.ExpectedRange,
                completionResults.ReplacedRange);
        }
Exemplo n.º 4
0
        public async Task LanguageServiceCompletesVariableInFile()
        {
            CompletionResults completionResults =
                await this.GetCompletionResults(
                    CompleteVariableInFile.SourceDetails);

            Assert.Equal(1, completionResults.Completions.Length);
            Assert.Equal(
                CompleteVariableInFile.ExpectedCompletion,
                completionResults.Completions[0]);
        }
Exemplo n.º 5
0
        public async Task LanguageServiceCompletesCommandFromModule()
        {
            CompletionResults completionResults =
                await this.GetCompletionResults(
                    CompleteCommandFromModule.SourceDetails);

            Assert.NotEqual(0, completionResults.Completions.Length);
            Assert.Equal(
                CompleteCommandFromModule.ExpectedCompletion,
                completionResults.Completions[0]);
        }
        public async Task LanguageServiceCompletesCommandInFile()
        {
            CompletionResults completionResults =
                await this.GetCompletionResults(
                    CompleteCommandInFile.SourceDetails).ConfigureAwait(false);

            Assert.NotEmpty(completionResults.Completions);
            Assert.Equal(
                CompleteCommandInFile.ExpectedCompletion,
                completionResults.Completions[0]);
        }
        public async Task LanguageServiceCompletesAttributeValue()
        {
            CompletionResults completionResults =
                await this.GetCompletionResults(
                    CompleteAttributeValue.SourceDetails).ConfigureAwait(false);

            Assert.NotEmpty(completionResults.Completions);
            Assert.Equal(
                CompleteAttributeValue.ExpectedRange,
                completionResults.ReplacedRange);
        }
        public async Task <CompletionList> Handle(CompletionParams request, CancellationToken cancellationToken)
        {
            int cursorLine   = request.Position.Line + 1;
            int cursorColumn = request.Position.Character + 1;

            ScriptFile scriptFile = _workspaceService.GetFile(request.TextDocument.Uri);

            try
            {
                await _completionLock.WaitAsync(cancellationToken).ConfigureAwait(false);
            }
            catch (OperationCanceledException)
            {
                _logger.LogDebug("Completion request canceled for file: {0}", request.TextDocument.Uri);
                return(Array.Empty <CompletionItem>());
            }

            try
            {
                if (cancellationToken.IsCancellationRequested)
                {
                    _logger.LogDebug("Completion request canceled for file: {0}", request.TextDocument.Uri);
                    return(Array.Empty <CompletionItem>());
                }

                CompletionResults completionResults =
                    await GetCompletionsInFileAsync(
                        scriptFile,
                        cursorLine,
                        cursorColumn).ConfigureAwait(false);

                if (completionResults == null)
                {
                    return(Array.Empty <CompletionItem>());
                }

                CompletionItem[] completionItems = new CompletionItem[completionResults.Completions.Length];
                for (int i = 0; i < completionItems.Length; i++)
                {
                    completionItems[i] = CreateCompletionItem(completionResults.Completions[i], completionResults.ReplacedRange, i + 1);
                }

                return(completionItems);
            }
            finally
            {
                _completionLock.Release();
            }
        }
        public async Task LanguageServiceCompletesFilePath()
        {
            CompletionResults completionResults =
                await this.GetCompletionResults(
                    CompleteFilePath.SourceDetails).ConfigureAwait(false);

            Assert.NotEmpty(completionResults.Completions);
            // TODO: Since this is a path completion, this test will need to be
            //       platform specific. Probably something like:
            //         - Windows: C:\Program
            //         - macOS:   /User
            //         - Linux:   /hom
            //Assert.Equal(
            //    CompleteFilePath.ExpectedRange,
            //    completionResults.ReplacedRange);
        }
        public async Task LanguageServiceCompletesCommandFromModule()
        {
            CompletionResults completionResults =
                await this.GetCompletionResults(
                    CompleteCommandFromModule.SourceDetails).ConfigureAwait(false);

            Assert.NotEmpty(completionResults.Completions);

            Assert.Equal(
                CompleteCommandFromModule.ExpectedCompletion.CompletionText,
                completionResults.Completions[0].CompletionText
                );

            Assert.Equal(
                CompleteCommandFromModule.ExpectedCompletion.CompletionType,
                completionResults.Completions[0].CompletionType
                );

            Assert.NotNull(completionResults.Completions[0].ToolTipText);
        }
        protected async Task HandleCompletionRequest(
            TextDocumentPosition textDocumentPosition,
            RequestContext <CompletionItem[]> requestContext)
        {
            int cursorLine   = textDocumentPosition.Position.Line + 1;
            int cursorColumn = textDocumentPosition.Position.Character + 1;

            ScriptFile scriptFile =
                editorSession.Workspace.GetFile(
                    textDocumentPosition.Uri);

            CompletionResults completionResults =
                await editorSession.LanguageService.GetCompletionsInFile(
                    scriptFile,
                    cursorLine,
                    cursorColumn);

            CompletionItem[] completionItems = null;

            if (completionResults != null)
            {
                int sortIndex = 1;
                completionItems =
                    completionResults
                    .Completions
                    .Select(
                        c => CreateCompletionItem(
                            c,
                            completionResults.ReplacedRange,
                            sortIndex++))
                    .ToArray();
            }
            else
            {
                completionItems = new CompletionItem[0];
            }

            await requestContext.SendResult(completionItems);
        }
        public async Task LanguageServiceCompletesTypeName()
        {
            Skip.If(
                !VersionUtils.IsNetCore,
                "Windows PowerShell return no results from CommandCompletion in the test harness. Since it works in PS7 and works manually when I run the extension, I'm skipping this  test");

            CompletionResults completionResults =
                await this.GetCompletionResults(
                    CompleteTypeName.SourceDetails).ConfigureAwait(false);

            Assert.NotEmpty(completionResults.Completions);

            Assert.Equal(
                CompleteTypeName.ExpectedCompletion.CompletionText,
                completionResults.Completions[0].CompletionText
                );

            Assert.Equal(
                CompleteTypeName.ExpectedCompletion.CompletionType,
                completionResults.Completions[0].CompletionType
                );

            Assert.NotNull(completionResults.Completions[0].ToolTipText);
        }
Exemplo n.º 13
0
        protected async Task HandleCompletionRequest(
            TextDocumentPosition textDocumentPosition,
            EditorSession editorSession,
            RequestContext <CompletionItem[], object> requestContext)
        {
            int cursorLine   = textDocumentPosition.Position.Line + 1;
            int cursorColumn = textDocumentPosition.Position.Character + 1;

            ScriptFile scriptFile =
                editorSession.Workspace.GetFile(
                    textDocumentPosition.Uri);

            CompletionResults completionResults =
                await editorSession.LanguageService.GetCompletionsInFile(
                    scriptFile,
                    cursorLine,
                    cursorColumn);

            CompletionItem[] completionItems = null;

            if (completionResults != null)
            {
                // By default, insert the completion at the current location
                int startEditColumn = textDocumentPosition.Position.Character;
                int endEditColumn   = textDocumentPosition.Position.Character;

                // Find the extents of the token under the cursor
                var completedToken =
                    scriptFile
                    .ScriptAst
                    .FindAll(
                        ast =>
                {
                    return
                    (!(ast is PipelineAst) &&
                     ast.Extent.StartLineNumber == cursorLine &&
                     ast.Extent.StartColumnNumber <= cursorColumn &&
                     ast.Extent.EndColumnNumber >= cursorColumn);
                },
                        true)
                    .LastOrDefault();       // The most relevant AST will be the last

                if (completedToken != null)
                {
                    // The edit should replace the token that was found at the cursor position
                    startEditColumn = completedToken.Extent.StartColumnNumber - 1;
                    endEditColumn   = completedToken.Extent.EndColumnNumber - 1;
                }

                completionItems =
                    completionResults
                    .Completions
                    .Select(
                        c => CreateCompletionItem(
                            c,
                            textDocumentPosition.Position.Line,
                            startEditColumn,
                            endEditColumn))
                    .ToArray();
            }
            else
            {
                completionItems = new CompletionItem[0];
            }

            await requestContext.SendResult(completionItems);
        }