public static StatelessProjectResponse Server_HandleStatelessCodeCompletionRequest(StatelessProjectRequest request)
        {
            // Convert web request model to internal analysis model
            ProjectAnalysisRequest analysisRequest = new ProjectAnalysisRequest();

            analysisRequest.ProjectModel = ProjectModelConverters.FromDtoToModel(request.Project);
            if (request.CodeCompletionParameters != null)
            {
                analysisRequest.CodeCompletionParameters = new ProjectAnalysisCodeCompletionParameters()
                {
                    CtrlSpace = request.CodeCompletionParameters.CtrlSpace,
                    Offset    = request.CodeCompletionParameters.Offset,
                    FileId    = request.CodeCompletionParameters.FileId,
                    Line      = request.CodeCompletionParameters.Line,
                    Column    = request.CodeCompletionParameters.Column
                };
            }

            // Analyze!
            ProjectAnalysisResult analysisResult = NRefactoryUtils.RunFullProjectAnalysis(analysisRequest);

            // Convert analysis result model to web service model
            StatelessProjectResponse response = new StatelessProjectResponse();

            if (analysisResult.CompletionOptions != null)
            {
                response.CompletionOptions = analysisResult.CompletionOptions
                                             .Select(x => new FileCodeCompletionResult()
                {
                    CompletionText = x.CompletionText,
                    DisplayText    = x.DisplayText,
                    Description    = x.Description,
                    OverloadCount  = x.OverloadedData.Count(),
                    CompletionCategoryDisplayText = (x.CompletionCategory == null ? "" : x.CompletionCategory.DisplayText)
                }).ToArray();
                response.CompletionWord = analysisResult.CompletionWord;
            }
            var allErrors = new List <FileParseResult>();

            foreach (var fileModel in analysisRequest.ProjectModel.GetFileDescendants())
            {
                allErrors.AddRange(fileModel.Parser.ErrorsAndWarnings
                                   .Select(x => new FileParseResult()
                {
                    FileId   = fileModel.Id,
                    FileName = fileModel.Name,
                    Line     = x.Region.BeginLine,
                    Column   = x.Region.BeginColumn,
                    Type     = x.ErrorType,
                    Message  = x.Message
                }).ToArray());
            }
            response.Errors      = allErrors.ToArray();
            response.TimeElapsed = string.Format("{0} ms", analysisResult.TimeElapsed.TotalMilliseconds);

            return(response);
        }
        private static FileOperationResponse Server_HandleStatefulFileOperationRequest(FileOperationRequest request)
        {
            FileOperationResponse response = new FileOperationResponse();

            ProjectModel     projectModel = null;
            ProjectFileModel fileModel    = null;

            try
            {
                projectModel = ServerProjectRepository.GetProject(request.UserId, request.ProjectId);
                if (projectModel == null)
                {
                    response.SetError("Specified project could not be located.");
                    return(response);
                }

                fileModel = ServerProjectRepository.GetFile(request.UserId, request.ProjectId, request.FileId);
                if (fileModel == null)
                {
                    response.SetError("Specified file could not be located within the project.");
                    return(response);
                }
            }
            catch (Exception ex)
            {
                response.SyncContent.SetError(ex);
            }


            // Set contextual identifiers on response
            response.UserId    = request.UserId;
            response.ProjectId = request.ProjectId;
            response.FileId    = request.FileId;

            // Now, handle each requested operation...

            if (request.SyncContent != null)
            {
                response.SyncContent = new FileSyncContentResponse();

                fileModel.Content           = request.SyncContent.Content;
                response.SyncContent.Status = ResponseStatus.OK;
            }


            if (request.Parse != null)
            {
                response.Parse = new FileParseResponse();

                try
                {
                    response.Parse.Errors = NRefactoryUtils.ParseFile(fileModel);
                    response.Parse.Status = ResponseStatus.OK;
                }
                catch (Exception ex)
                {
                    response.Parse.SetError(ex);
                }
            }


            if (request.CompleteCode != null)
            {
                response.CompleteCode = new FileCodeCompletionResponse();

                try
                {
                    var analysisRequest = new ProjectAnalysisRequest()
                    {
                        ProjectModel             = projectModel,
                        CodeCompletionParameters = new ProjectAnalysisCodeCompletionParameters()
                        {
                            FileId = request.FileId,
                            Line   = request.CompleteCode.LineNumber,
                            Column = request.CompleteCode.ColumnNumber
                        }
                    };

                    var analysisResult = NRefactoryUtils.RunFullProjectAnalysis(analysisRequest);

                    response.CompleteCode.CompletionOptions = analysisResult.CompletionOptions.Select(x => new FileCodeCompletionResult()
                    {
                        CompletionText = x.CompletionText,
                        DisplayText    = x.DisplayText
                    }).ToArray();
                    response.CompleteCode.Status = ResponseStatus.OK;
                }
                catch (Exception ex)
                {
                    response.CompleteCode.SetError(ex);
                }
            }

            response.Status = ResponseStatus.OK;
            return(response);
        }