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(CodeCompletionResultUtility.FromICompletionDataToFileCodeCompletionResult)
                                             .ToArray();
                for (int i = 0, len = response.CompletionOptions.Length; i < len; i++)
                {
                    response.CompletionOptions[i].Id = i;
                }
                response.CompletionWord = analysisResult.CompletionWord;
                if (analysisResult.BestMatchToCompletionWord != null)
                {
                    response.BestMatchToCompletionWord = response.CompletionOptions.FirstOrDefault(x => x.CompletionText == analysisResult.BestMatchToCompletionWord.CompletionText);
                }
            }
            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.MsElapsed = analysisResult.TimeElapsed.TotalMilliseconds; // string.Format("{0}", analysisResult.TimeElapsed.TotalMilliseconds);

            return(response);
        }
Ejemplo n.º 2
0
        public static FileOperationResponse DoAutoComplete(FileOperationRequest request)
        {
            FileOperationResponse response     = new FileOperationResponse();
            ProjectModel          projectModel = null;
            Stopwatch             sw           = Stopwatch.StartNew();

            try
            {
                var projectModelRepo = EndpointHost.AppHost.TryResolve <IProjectModelRepository>();

                projectModel = projectModelRepo.GetProject(request.UserId, request.ProjectId);

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


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


                // Convert analysis result model to file operation response DTO
                if (analysisResult.CompletionOptions != null)
                {
                    var codeCompletion = response.CodeCompletion = new FileCodeCompletionResponse();
                    codeCompletion.CompletionOptions = analysisResult.CompletionOptions
                                                       .Select(CodeCompletionResultUtility.FromICompletionDataToFileCodeCompletionResult).ToArray();
                    for (int i = 0, len = codeCompletion.CompletionOptions.Length; i < len; i++)
                    {
                        codeCompletion.CompletionOptions[i].Id = i;
                    }
                    codeCompletion.CompletionWord = analysisResult.CompletionWord;
                    if (analysisResult.BestMatchToCompletionWord != null)
                    {
                        codeCompletion.BestMatchToCompletionWord = codeCompletion.CompletionOptions.FirstOrDefault(x => x.CompletionText == analysisResult.BestMatchToCompletionWord.CompletionText);
                    }
                    // Record text around cursor
                    codeCompletion.TextBeforeCursor = analysisResult.CompletionContextBefore;
                    codeCompletion.TextAfterCursor  = analysisResult.CompletionContextAfter;
                    // Record inputs
                    codeCompletion.Line   = analysisResult.Line;
                    codeCompletion.Column = analysisResult.Column;
                    codeCompletion.Offset = analysisResult.Offset;
                }
                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.ParseResults = allErrors.ToArray();


                /*
                 * 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;
                 *  if (analysisResult.BestMatchToCompletionWord != null)
                 *      response.BestMatchToCompletionWord = response.CompletionOptions.FirstOrDefault(x => x.CompletionText == analysisResult.BestMatchToCompletionWord.CompletionText);
                 * }
                 * 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.MsElapsed = analysisResult.TimeElapsed.TotalMilliseconds; // string.Format("{0} ms", analysisResult.TimeElapsed.TotalMilliseconds);
                 */
                response.Status.Success = true;
            }
            catch (Exception ex)
            {
                log.Error(ex.ToString(), ex);
                response.Status.SetError(ex);
            }
            finally
            {
                //response.CodeCompletion.MsElapsed = analysisResult.TimeElapsed.TotalMilliseconds; // string.Format("{0} ms", analysisResult.TimeElapsed.TotalMilliseconds);
                response.Status.MsElapsed = sw.ElapsedMilliseconds;
            }

            return(response);
        }