Beispiel #1
0
        private void ReplaceStepBindingAttribute(CodeFunction codeFunction, IStepDefinitionBinding binding, string newRegex)
        {
            if (!codeFunction.ProjectItem.IsOpen)
            {
                codeFunction.ProjectItem.Open();
            }

            var formattedOldRegex = FormatRegexForDisplay(binding.Regex);

            var navigatePoint = codeFunction.GetStartPoint(vsCMPart.vsCMPartHeader);

            navigatePoint.TryToShow();
            navigatePoint.Parent.Selection.MoveToPoint(navigatePoint);

            var stepBindingEditorContext = GherkinEditorContext.FromDocument(codeFunction.DTE.ActiveDocument, _gherkinLanguageServiceFactory);
            var attributeLinesToUpdate   = stepBindingEditorContext.TextView.TextViewLines.Where(x => x.Start.GetContainingLine().GetText().Contains("\"" + formattedOldRegex + "\""));

            foreach (var attributeLineToUpdate in attributeLinesToUpdate)
            {
                using (var textEdit = attributeLineToUpdate.Snapshot.TextBuffer.CreateEdit())
                {
                    var regexStart = attributeLineToUpdate.Start.GetContainingLine().GetText().IndexOf(formattedOldRegex);
                    textEdit.Replace(attributeLineToUpdate.Start.Position + regexStart, formattedOldRegex.Length, newRegex);
                    textEdit.Apply();
                }
            }
        }
Beispiel #2
0
        private void RenameStep(StepInstanceWithProjectScope stepInstance, string newStepRegex, IStepDefinitionBinding binding)
        {
            var featureFileDocument = JumpToStep(stepInstance);

            if (featureFileDocument == null)
            {
                return;
            }

            var stepEditorContext = GherkinEditorContext.FromDocument(featureFileDocument, _gherkinLanguageServiceFactory);

            var stepToRename = GetCurrentStep(stepEditorContext);

            if (stepToRename == null)
            {
                return;
            }

            if (!binding.Regex.IsMatch(stepToRename.Text))
            {
                return;
            }

            var stepLineStart = stepEditorContext.TextView.Selection.Start.Position.GetContainingLine();

            using (var stepNameTextEdit = stepLineStart.Snapshot.TextBuffer.CreateEdit())
            {
                var line                  = stepLineStart.Snapshot.GetLineFromLineNumber(stepLineStart.LineNumber);
                var lineText              = line.GetText();
                var trimmedText           = lineText.Trim();
                var numLeadingWhiteSpaces = lineText.Length - trimmedText.Length;

                var actualStepName = trimmedText.Substring(stepToRename.Keyword.Length);
                var newStepName    = BuildStepNameWithNewRegex(actualStepName, newStepRegex, binding);

                var stepNamePosition = line.Start.Position + numLeadingWhiteSpaces + stepToRename.Keyword.Length;
                stepNameTextEdit.Replace(stepNamePosition, actualStepName.Length, newStepName);

                stepNameTextEdit.Apply();
            }
        }