protected Result <TextEdit[], ResponseError> AddSourceFileToProject(JToken token)
        {
            if (m_addSourceFileConfiguration == null || m_addSourceFileConfiguration.Configurations.Length == 0)
            {
                return(Result <TextEdit[], ResponseError> .Error(new ResponseError
                {
                    code = ErrorCodes.InvalidRequest,
                    message = BuildXL.Ide.LanguageServer.Strings.AddSourceFileConfigurationNotReceived,
                }));
            }

            var appState = m_getAppState();

            if (appState == null)
            {
                return(Result <TextEdit[], ResponseError> .Error(new ResponseError
                {
                    code = ErrorCodes.InternalError,
                    message = BuildXL.Ide.LanguageServer.Strings.WorkspaceParsingFailedCannotPerformAction,
                }));
            }

            var addSourceFileParams = token.ToObject <AddSourceFileToProjectParams>();

            var workspace = appState.IncrementalWorkspaceProvider.WaitForRecomputationToFinish();
            var uri       = new Uri(addSourceFileParams.ProjectSpecFileName);

            if (uri.TryGetSourceFile(workspace, appState.PathTable, out var projectSourceFile))
            {
                var checker = workspace.GetSemanticModel().TypeChecker;
                if (TryAddSourceFileToSourceFile(
                        checker,
                        projectSourceFile,
                        addSourceFileParams.RelativeSourceFilePath,
                        workspace,
                        appState.PathTable,
                        m_addSourceFileConfiguration.Configurations))
                {
                    var formattedText = PrettyPrint.GetFormattedText(projectSourceFile);

                    var textEdit = new TextEdit
                    {
                        NewText = formattedText,
                        Range   = projectSourceFile.ToRange(),
                    };

                    return(Result <TextEdit[], ResponseError> .Success(new[] { textEdit }));
                }
            }

            return(Result <TextEdit[], ResponseError> .Success(null));
        }
예제 #2
0
        /// <nodoc />
        public Result <TextEdit[], ResponseError> FormatDocument(DocumentFormattingParams @params, CancellationToken token)
        {
            // TODO: support cancellation
            string uri = @params.TextDocument.Uri;

            if (!TryGetSourceFile(uri, out var spec, out var error))
            {
                string errorMessage = $"Could not open the file '{uri}'.{Environment.NewLine}{error}";
                return(Result.InternalError <TextEdit[]>(errorMessage));
            }

            var formattedText = PrettyPrint.GetFormattedText(spec);

            var textEdit = new TextEdit
            {
                NewText = formattedText,
                Range   = spec.ToRange(),
            };

            return(Result <TextEdit[], ResponseError> .Success(new[] { textEdit }));
        }