public static ProjectDto LoadProject(int userId, int projectId)
        {
            var projectModel = ServerProjectRepository.GetProject(userId, projectId);
            var projectDto   = ProjectModelConverters.FromModelToDto(projectModel);

            return(projectDto);
        }
        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);
        }
        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);
        }
        //public static ProjectFileModel GetFile(int userId, int projectId, int fileId)
        //{
        //    var project = GetProject(userId, projectId);
        //    if (project == null)
        //        return null;

        //    var file = project.FindFile(fileId);
        //    return file;
        //}

        //public static ProjectModel GetProject(int userId, int projectId)
        //{
        //    return projects.FirstOrDefault(x => x.OwnerUserId == userId && x.ProjectId == projectId);
        //}

        public ProjectModel GetProject(int userId, int projectId)
        {
#if true
            return(projects.FirstOrDefault(x => x.OwnerUserId == userId && x.ProjectId == projectId));
#else
            // Note: there's only one project in this repo:  project 478 for user 125
            var projectDto = MockWebServiceUtility.LoadProject(userId, projectId);
            if (projectDto == null)
            {
                throw new Exception("Could not find project in internal repository.");
            }

            ProjectModel projectModel = ProjectModelConverters.FromDtoToModel(projectDto);
            return(projectModel);
#endif
        }
        private void OnLoaded(object sender, RoutedEventArgs routedEventArgs)
        {
            Loaded -= OnLoaded;

            AppHost appHost = new AppHost();

            appHost.Init();

            //EndpointHost.AppHost.Register(new InMemoryProjectModelRepo());
            var projectModelRepo = EndpointHost.AppHost.TryResolve <IProjectModelRepository>();
            var projectModel     = projectModelRepo.GetProject(478, 174);
            var projectDto       = ProjectModelConverters.FromModelToDto(projectModel);
            var projectVm        = ProjectMVVMConverters.FromModelToViewModel(projectDto);

            ProjectView.DataContext = projectVm;
        }
Ejemplo n.º 6
0
        //private void ParseFile(ProjectFileViewModel fileViewModelToParse)
        //{
        //    var fileDto = ProjectMVVMConverters.FromViewModelToModel(fileViewModelToParse);

        //    MockWebServiceUtility.ParseFile(fileDto, response =>
        //    {
        //        if (response.Parse == null)
        //            return;

        //        var errors = response.Parse.Errors;
        //        //response.Parse.Errors
        //        if (errors != null)
        //        {
        //            Logger.AppendLine("{0} error(s) detected.", errors.Length);
        //            if (errors.Length > 0)
        //                foreach (var err in errors)
        //                    Logger.AppendLine("{0}: In file {1} ({2}, {3}): {4}", err.Type, err.FileName, err.Line, err.Column, err.Message);
        //        }

        //    });
        //}

        //private static FileOperationResponse DoAutoComplete(FileOperationRequest request)
        //{
        //    return AutocompleteServiceUtil.DoAutoComplete(request);
        //}


        private void AnalyzeProject2(bool ctrlSpace)
        {
            //ctrlSpace = true;
            var sw = Stopwatch.StartNew();

            var projectVm = (ProjectViewModel)DataContext;
            var fileVm    = projectVm.SelectedProjectItem as ProjectFileViewModel;

            if (fileVm == null)
            {
                Logger.AppendLine("A file must be selected");
                return;
            }

            fileVm.CaretIndex = SelectedFileContent.CaretIndex;
            // Synchronize the view's content with the viewmodel (since by default this is only done AFTER the textbox loses focus)
            fileVm.Content = SelectedFileContent.Text;

            // update file content in local data store if not already done
            var projectModelRepo = EndpointHost.AppHost.TryResolve <IProjectModelRepository>();

            projectModelRepo.SaveFileContent(478, fileVm.ProjectId, fileVm.Id, fileVm.Content);


            var fileRequest = new FileOperationRequest()
            {
                UserId       = 478, //projectVm..UserId,
                ProjectId    = projectVm.ProjectId,
                FileId       = fileVm.Id,
                CompleteCode = new FileCodeCompletionRequest()
                {
                    AutoComplete = true,
                    Offset       = fileVm.CaretIndex,
                    LineNumber   = fileVm.CaretLine,
                    ColumnNumber = fileVm.CaretColumn,
                    CtrlSpace    = ctrlSpace
                },
                Parse = new FileParseRequest()
            };


            var fileResponse = AutocompleteServiceUtil.DoAutoComplete(fileRequest);

#if false
            var projectDto = ProjectMVVMConverters.FromViewModelToModel(projectVm);

            var projectModel = ProjectModelConverters.FromDtoToModel(projectDto);

            var analysisRequest = new ProjectAnalysisRequest()
            {
                ProjectModel             = projectModel,
                CodeCompletionParameters = new ProjectAnalysisCodeCompletionParameters()
                {
                    CtrlSpace = ctrlSpace,
                    FileId    = fileVm.Id,
                    Offset    = fileVm.CaretIndex
                                //Line = request.CompleteCode.LineNumber,
                                //Column = request.CompleteCode.ColumnNumber,
                                //CtrlSpace = true // always true for now
                }
            };

            ProjectAnalysisResult analysisResult = NRefactoryUtils.RunFullProjectAnalysis(analysisRequest);

            //StatelessProjectResponse response = MockWebServiceUtility.Server_HandleStatelessCodeCompletionRequest(request);

            FileOperationResponse response = new FileOperationResponse();
            // Convert analysis result model to file operation response DTO
            if (analysisResult.CompletionOptions != null)
            {
                response.CodeCompletion = new FileCodeCompletionResponse();
                response.CodeCompletion.CompletionOptions = analysisResult.CompletionOptions
                                                            .Select(ProjectModelConverters.FromICompletionDataToFileCodeCompletionResult).ToArray();
                for (int i = 0, len = response.CodeCompletion.CompletionOptions.Length; i < len; i++)
                {
                    response.CodeCompletion.CompletionOptions[i].Id = i;
                }
                response.CodeCompletion.CompletionWord = analysisResult.CompletionWord;
                if (analysisResult.BestMatchToCompletionWord != null)
                {
                    response.CodeCompletion.BestMatchToCompletionWord = response.CodeCompletion.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.ParseResults = allErrors.ToArray();
#endif

            //var jsonResponse = JsonConvert.SerializeObject(fileResponse, Formatting.Indented);
            //Logger.AppendLine(jsonResponse);

            // Summarize results...
            Logger.AppendLine("=========================================================");
            Logger.AppendLine("Project analysis completed in {0} ms", sw.ElapsedMilliseconds);

            if (fileResponse.CodeCompletion == null)
            {
                Logger.AppendLine("No Completion Results.");
                Logger.SetCodeCompletionOptions(null, null);
            }
            else
            {
                var codeCompletion = fileResponse.CodeCompletion;

                Logger.SetCodeCompletionOptions(codeCompletion.CompletionOptions, codeCompletion.BestMatchToCompletionWord);

                Logger.AppendLine("Completion Results...");
                Logger.AppendLine("  Input:  Line:{0}  Col:{1}  Offset:{2}", codeCompletion.Line, codeCompletion.Column, codeCompletion.Offset);
                Logger.AppendLine("  Context: \"{0}\" <cursor> \"{1}\"", codeCompletion.TextBeforeCursor, codeCompletion.TextAfterCursor);
                Logger.AppendLine("  {0} code completion option(s) generated.", codeCompletion.CompletionOptions.Length);

                // Try to find closest matching completion result
                if (string.IsNullOrWhiteSpace(codeCompletion.CompletionWord))
                {
                    Logger.AppendLine("  No code completion word detected.");
                }
                else
                {
                    if (codeCompletion.BestMatchToCompletionWord != null)
                    {
                        Logger.AppendLine("  Detected code completion word, \"{0}\", most closely matches completion option \"{1}\".",
                                          codeCompletion.CompletionWord, codeCompletion.BestMatchToCompletionWord.CompletionText);
                    }
                    else
                    {
                        Logger.AppendLine("  Detected code completion word: {0}", codeCompletion.CompletionWord);
                    }
                }
            }


            if (fileResponse.ParseResults != null)
            {
                Logger.AppendLine("{0} error(s) detected.", fileResponse.ParseResults.Length);
                if (fileResponse.ParseResults.Length > 0)
                {
                    foreach (var err in fileResponse.ParseResults)
                    {
                        Logger.AppendLine("{0}: In file {1} ({2}, {3}): {4}", err.Type, err.FileName, err.Line, err.Column, err.Message);
                    }
                }
            }
        }