Exemplo n.º 1
0
		public override IEnumerable<SlideBlock> BuildUp(BuildUpContext context, IImmutableSet<string> filesInProgress)
		{
			FillProperties(context);
			RemovedLabels = RemovedLabels ?? new Label[0];
			if (PreludeFile == null)
			{
				PreludeFile = context.CourseSettings.GetPrelude(LangId);
				if (PreludeFile != null)
					PreludeFile = Path.Combine("..", PreludeFile);
			}

			var code = context.FileSystem.GetContent(File);
			var regionRemover = new RegionRemover(LangId);
			var extractor = context.GetExtractor(File, LangId, code);

			var prelude = "";
			if (PreludeFile != null)
				prelude = context.FileSystem.GetContent(PreludeFile);

			var exerciseCode = regionRemover.Prepare(code);
			IEnumerable<Label> notRemoved;
			exerciseCode = regionRemover.Remove(exerciseCode, RemovedLabels, out notRemoved);
			int index;
			exerciseCode = regionRemover.RemoveSolution(exerciseCode, SolutionLabel, out index);
			index += prelude.Length;

			ExerciseInitialCode = ExerciseInitialCode.RemoveCommonNesting();
			ExerciseCode = prelude + exerciseCode;
			IndexToInsertSolution = index;
			EthalonSolution = extractor.GetRegion(SolutionLabel);
			ValidatorName = string.Join(" ", LangId, ValidatorName);

			yield return this;
		}
Exemplo n.º 2
0
        public void CodeText_WhenAllFormattingsRun_OutputsExpectedCodeText(string inputString, string fileName, string expectedOutput)
        {
            TestHelper.InitializeConfig(TestData.TestConfig);
            CSharpSyntaxNode root = TestHelper.CreateCSharpSyntaxRoot(inputString);

            var        regionRemover = new RegionRemover();
            SyntaxNode regionRoot    = regionRemover.Visit(root);

            var        usingsHelper = new UsingsPlacer();
            SyntaxNode usingsRoot   = usingsHelper.ProcessUsings(regionRoot);

            var        fhf     = new FileHeaderFormatter();
            SyntaxNode fhfRoot = fhf.AddHeader(usingsRoot, fileName);

            var        sorter     = new CSharpSorter();
            SyntaxNode sorterRoot = sorter.Visit(fhfRoot);

            var        newLineFormatter = new NewlineFormatter();
            SyntaxNode newLineRoot      = newLineFormatter.Visit(sorterRoot);

            var        ebf     = new ExpressionBodiedFormatter();
            SyntaxNode ebfRoot = ebf.Visit(newLineRoot);

            string actualOutput = ebfRoot.ToFullString();

            Debug.WriteLine(actualOutput);
            Assert.That(actualOutput, Is.EqualTo(expectedOutput));
        }
Exemplo n.º 3
0
        public override IEnumerable <SlideBlock> BuildUp(SlideBuildingContext context, IImmutableSet <string> filesInProgress)
        {
            CodeFile = CodeFile ?? context.Slide.DefaultIncludeCodeFile ?? context.Unit.Settings?.DefaultIncludeCodeFile;
            if (CodeFile == null)
            {
                throw new CourseLoadingException($"У блока <exercise.file> не указан атрибут file.");
            }

            if (ExerciseInitialCode == null)
            {
                throw new CourseLoadingException($"У блока <exercise.file> не указан код, который надо показывать пользователю перед началом работы. Укажите его в тэге <initialCode>");
            }

            if (!Language.HasValue)
            {
                Language = LanguageHelpers.GuessByExtension(new FileInfo(CodeFile));
            }

            RemovedLabels = RemovedLabels ?? new Label[0];
            if (PreludeFile == null)
            {
                PreludeFile = context.CourseSettings.GetPrelude(Language);
                if (PreludeFile != null)
                {
                    PreludeFile = Path.Combine("..", PreludeFile);
                }
            }

            var code          = context.UnitDirectory.GetContent(CodeFile);
            var regionRemover = new RegionRemover(Language);
            var extractor     = context.GetExtractor(CodeFile, Language, code);

            var prelude = "";

            if (PreludeFile != null)
            {
                prelude = context.UnitDirectory.GetContent(PreludeFile);
            }

            var exerciseCode = regionRemover.Prepare(code);

            exerciseCode = regionRemover.Remove(exerciseCode, RemovedLabels, out var _);
            exerciseCode = regionRemover.RemoveSolution(exerciseCode, SolutionLabel, out var index);
            if (index < 0)
            {
                index = 0;
            }
            index += prelude.Length;

            ExerciseInitialCode     = ExerciseInitialCode.RemoveCommonNesting();
            ExerciseCode            = prelude + exerciseCode;
            IndexToInsertSolution   = index;
            EthalonSolution         = extractor.GetRegion(SolutionLabel);
            Validator.ValidatorName = string.Join(" ", Language.GetName(), Validator.ValidatorName ?? "");

            yield return(this);
        }
        private static async Task <Document> RemoveAllRegionsAsync(
            Document document,
            CancellationToken cancellationToken)
        {
            SyntaxNode oldRoot = await document.GetSyntaxRootAsync(cancellationToken);

            CompilationUnitSyntax newRoot = RegionRemover.RemoveFrom((CompilationUnitSyntax)oldRoot)
                                            .WithAdditionalAnnotations(Formatter.Annotation);

            return(document.WithSyntaxRoot(newRoot));
        }
Exemplo n.º 5
0
        private async Task FormatAndSortCodeAsync()
        {
            try
            {
                Document activeDocument = await VisualStudioHelper.GetActiveDocumentAsync();

                if (VisualStudioHelper.FileIsExcludedType(activeDocument.FilePath))
                {
                    return;
                }

                SyntaxNode root = await VisualStudioHelper.GetDocumentRootAsync(activeDocument);

                bool isCSharpDocument = root.Language == VisualStudioHelper.CSharpLanguageName;
                if (!isCSharpDocument)
                {
                    return;
                }

                var        regionRemover = new RegionRemover();
                SyntaxNode regionRoot    = regionRemover.Visit(root);

                var        usingsPlacer = new UsingsPlacer();
                SyntaxNode usingsRoot   = usingsPlacer.ProcessUsings(regionRoot);

                string     fileName = Path.GetFileName(activeDocument.FilePath);
                var        fhf      = new FileHeaderFormatter();
                SyntaxNode fhfRoot  = fhf.AddHeader(usingsRoot, fileName);

                var        sorter     = new CSharpSorter();
                SyntaxNode sorterRoot = sorter.Visit(fhfRoot);

                var        accessModifier = new AccessLevelModifierFormatter();
                SyntaxNode accessRoot     = accessModifier.Visit(sorterRoot);

                var        newLineFormatter = new NewlineFormatter();
                SyntaxNode newLineRoot      = newLineFormatter.Visit(accessRoot);

                var        ebf     = new ExpressionBodiedFormatter();
                SyntaxNode ebfRoot = ebf.Visit(newLineRoot);

                Document newDocument = activeDocument.WithSyntaxRoot(ebfRoot);
                bool     success     = await VisualStudioHelper.ApplyDocumentChangesAsync(newDocument);

                await VisualStudioHelper.InvokeCommandAsync(VisualStudioHelper.RunDefaultCodeCleanup);

                await VisualStudioHelper.InvokeCommandAsync(VisualStudioHelper.FormatDocumentCommandName);
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.Message);
            }
        }
Exemplo n.º 6
0
        public void CodeText_WhenRegionFormatterIsRun_OutputsTextWithoutRegions(string inputString, string expectedOutput)
        {
            TestHelper.InitializeConfig(TestData.TestConfig);
            CSharpSyntaxNode root = TestHelper.CreateCSharpSyntaxRoot(inputString);

            var        regionRemover = new RegionRemover();
            SyntaxNode newNode       = regionRemover.Visit(root);

            string actualOutput = newNode.ToFullString();

            Debug.WriteLine(actualOutput);
            Assert.That(actualOutput, Is.EqualTo(expectedOutput));
        }
Exemplo n.º 7
0
        public override IEnumerable <SlideBlock> BuildUp(BuildUpContext context, IImmutableSet <string> filesInProgress)
        {
            FillProperties(context);
            RemovedLabels = RemovedLabels ?? new Label[0];
            if (PreludeFile == null)
            {
                PreludeFile = context.CourseSettings.GetPrelude(LangId);
                if (PreludeFile != null)
                {
                    PreludeFile = Path.Combine("..", PreludeFile);
                }
            }

            var code          = context.Dir.GetContent(CodeFile);
            var regionRemover = new RegionRemover(LangId);
            var extractor     = context.GetExtractor(CodeFile, LangId, code);

            var prelude = "";

            if (PreludeFile != null)
            {
                prelude = context.Dir.GetContent(PreludeFile);
            }

            var exerciseCode = regionRemover.Prepare(code);

            exerciseCode = regionRemover.Remove(exerciseCode, RemovedLabels, out var _);
            exerciseCode = regionRemover.RemoveSolution(exerciseCode, SolutionLabel, out var index);
            if (index < 0)
            {
                index = 0;
            }
            index += prelude.Length;

            ExerciseInitialCode     = ExerciseInitialCode.RemoveCommonNesting();
            ExerciseCode            = prelude + exerciseCode;
            IndexToInsertSolution   = index;
            EthalonSolution         = extractor.GetRegion(SolutionLabel);
            Validator.ValidatorName = string.Join(" ", LangId, Validator.ValidatorName ?? "");

            CheckScoringGroup(context.SlideTitle, context.CourseSettings.Scoring);

            yield return(this);
        }