Ejemplo n.º 1
0
        protected override async Task <IEnumerable <CodeActionOperation> > ComputePreviewOperationsAsync(CancellationToken cancellationToken)
        {
            // If we have a codeaction, then just call the base method which will call ComputeOperationsAsync below.
            // This creates an ApplyChagesOperation so that Roslyn can show a preview of the changes.
            if (_codeActionWorkspaceEdit != null)
            {
                return(await base.ComputePreviewOperationsAsync(cancellationToken).ConfigureAwait(false));
            }

            // We have a command - this will be executed on the host but the host may have a preview for the current document.
            var runCodeActionsCommand = ((JToken)_command.Arguments?.Single()).ToObject <LSP.Command>();
            var runCodeActionParams   = ((JToken)runCodeActionsCommand.Arguments?.Single())?.ToObject <RunCodeActionParams>();

            var request   = new LSP.LspRequest <RunCodeActionParams, LSP.TextEdit[]>(RoslynMethods.CodeActionPreviewName);
            var textEdits = await _lspClient.RequestAsync(request, runCodeActionParams, cancellationToken).ConfigureAwait(false);

            if (textEdits == null || textEdits.Length == 0)
            {
                return(ImmutableArray <CodeActionOperation> .Empty);
            }

            var newDocument = await ApplyEditsAsync(_document, textEdits, cancellationToken).ConfigureAwait(false);

            return(ImmutableArray.Create(new ApplyChangesOperation(newDocument.Project.Solution)));
        }
        public async Task <ImmutableArray <ProjectInfo> > GetRemoteProjectInfosAsync(CancellationToken cancellationToken)
        {
            if (!_remoteLanguageServiceWorkspace.IsRemoteSession)
            {
                return(ImmutableArray <ProjectInfo> .Empty);
            }

            var lspClient = _roslynLspClientServiceFactory.ActiveLanguageServerClient;

            if (lspClient == null)
            {
                return(ImmutableArray <ProjectInfo> .Empty);
            }

            CustomProtocol.Project[] projects;
            try
            {
                var request = new LSP.LspRequest <object, CustomProtocol.Project[]>(CustomProtocol.RoslynMethods.ProjectsName);
                projects = await lspClient.RequestAsync(request, new object(), cancellationToken).ConfigureAwait(false);
            }
            catch (Exception)
            {
                projects = null;
            }

            if (projects == null)
            {
                return(ImmutableArray <ProjectInfo> .Empty);
            }

            var projectInfos = ImmutableArray.CreateBuilder <ProjectInfo>();

            foreach (var project in projects)
            {
                // We don't want to add cshtml files to the workspace since the Roslyn will add the generated secondary buffer of a cshtml
                // file to a different project but with the same path. This used to be ok in Dev15 but in Dev16 this confuses Roslyn and causes downstream
                // issues. There's no need to add the actual cshtml file to the workspace - so filter those out.
                var filesTasks = project.SourceFiles
                                 .Where(f => f.Scheme != SystemUriSchemeExternal)
                                 .Where(f => !f.LocalPath.EndsWith(".cshtml"))
                                 .Select(f => lspClient.ProtocolConverter.FromProtocolUriAsync(f, false, cancellationToken));
                var files = await Task.WhenAll(filesTasks).ConfigureAwait(false);

                string language;
                switch (project.Language)
                {
                case LanguageNames.CSharp:
                    language = StringConstants.CSharpLspLanguageName;
                    break;

                case LanguageNames.VisualBasic:
                    language = StringConstants.VBLspLanguageName;
                    break;

                default:
                    language = project.Language;
                    break;
                }
                var projectInfo = CreateProjectInfo(project.Name, language, files.Select(f => f.LocalPath).ToImmutableArray());
                projectInfos.Add(projectInfo);
            }

            return(projectInfos.ToImmutableArray());
        }