Example #1
0
        public void TestUndoRedoCommand()
        {
            // Arrange
            var project = new Project();
            var context = new BlockCommandContext(project);
            ProjectBlockCollection blocks = project.Blocks;
            Block block = blocks[0];

            using (block.AcquireBlockLock(RequestLock.Write))
            {
                block.SetText("abcd");
            }
            int      blockVersion = block.Version;
            BlockKey blockKey     = block.BlockKey;

            var command = new ReplaceTextCommand(
                new BlockPosition(blockKey, 2), 1, "YES");

            project.Commands.Do(command, context);
            project.Commands.Undo(context);

            // Act
            project.Commands.Redo(context);

            // Assert
            Assert.AreEqual(1, blocks.Count);
            Assert.AreEqual(
                new BlockPosition(blocks[0], 5), project.Commands.LastPosition);

            const int index = 0;

            Assert.AreEqual("abYESd", blocks[index].Text);
            Assert.AreEqual(blockVersion + 6, blocks[index].Version);
        }
Example #2
0
        public void CanUndo_WithIndexGreaterThanZero_ShouldReturnTrue()
        {
            var history = new History();
            var text    = new Text();
            var command = new ReplaceTextCommand(text, "text");

            history.AddAndExecuteCommand(command);
            Assert.IsTrue(history.CanUndo());
        }
        public void CanCreateCommandAndReplaceItemText()
        {
            var i = new TestReplacable();

            i.Text = "test";
            string str = "sth";
            var    c   = new ReplaceTextCommand(i, str);

            c.Execute();
            Assert.AreEqual(i.Text, str);
        }
Example #4
0
        public void Redo_WithValidIndex_ShouldRedoCommand()
        {
            var history = new History();
            var text    = new Text();
            var command = new ReplaceTextCommand(text, "text");

            history.AddAndExecuteCommand(command);
            history.Undo();
            Assert.IsTrue(history.CanRedo());
            history.Redo();
        }
Example #5
0
        public void Execute_ReplaceTextCommand_WithNewText_ShouldReplacePrevTextFromParagraph()
        {
            var paragraph = new Text()
            {
                Value = "Old text"
            };
            var newText = "New text";
            var command = new ReplaceTextCommand(paragraph, newText);

            command.Execute();
            Assert.AreEqual(newText, paragraph.Value);
        }
        public void CanReturnTextBackToPreviousState()
        {
            var    i       = new TestReplacable();
            string oldText = "test";

            i.Text = oldText;
            string str = "sth";
            var    c   = new ReplaceTextCommand(i, str);

            c.Execute();
            Assert.AreEqual(i.Text, str);
            c.Unexecute();
            Assert.AreEqual(i.Text, oldText);
        }
Example #7
0
        public void Execute_GetItemAndReplaceText()
        {
            // Arrange
            var paragrpahDocumentItem = new DocumentItem(new Paragraph.Paragraph(""));

            _documentMock.Setup(d => d.GetItem(It.IsAny <int>())).Returns(paragrpahDocumentItem);
            ICommand command = new ReplaceTextCommand(1, "Hey", _documentMock.Object);

            // Act
            command.Execute();

            // Assert
            Assert.Equal("Hey", paragrpahDocumentItem.Paragraph.Text);
        }
Example #8
0
        public void Unexecute_ReplaceTextCommand_ShouldReturnPrevTextToParagraph()
        {
            var prevText  = "Old text";
            var paragraph = new Text()
            {
                Value = prevText
            };
            var newText = "New text";
            var command = new ReplaceTextCommand(paragraph, newText);

            command.Execute();
            Assert.AreEqual(newText, paragraph.Value);

            command.Unexecute();
            Assert.AreEqual(prevText, paragraph.Value);
        }
Example #9
0
        /// <summary>
        /// Gets the editor actions associated with the given TextSpan.
        /// </summary>
        /// <param name="block">The block.</param>
        /// <param name="textSpan">The text span.</param>
        /// <returns>
        /// A list of editor actions associated with this span.
        /// </returns>
        /// <remarks>
        /// This will be called within a read-only lock.
        /// </remarks>
        public IList <IEditorAction> GetEditorActions(
            Block block,
            TextSpan textSpan)
        {
            // We only get to this point if we have a misspelled word.
            string word = textSpan.GetText(block.Text);

            // Get the suggestions for the word.
            IList <SpellingSuggestion> suggestions = GetSuggestions(word);

            // Go through the suggestions and create an editor action for each one.
            // These will already be ordered coming out of the GetSuggestions()
            // method.
            BlockCommandSupervisor commands = block.Project.Commands;
            var actions = new List <IEditorAction>(suggestions.Count);

            foreach (SpellingSuggestion suggestion in suggestions)
            {
                // Figure out the operation we'll be using to implement the change.
                var command =
                    new ReplaceTextCommand(
                        new BlockPosition(block.BlockKey, textSpan.StartTextIndex),
                        textSpan.Length,
                        suggestion.Suggestion);

                // Create the suggestion action, along with the replacement command.
                var action =
                    new EditorAction(
                        string.Format("Change to \"{0}\"", suggestion.Suggestion),
                        new HierarchicalPath("/Plugins/Spelling/Change"),
                        context => commands.Do(command, context));

                actions.Add(action);
            }

            // Add the additional editor actions from the plugins.
            foreach (ISpellingProjectPlugin controller in SpellingControllers)
            {
                IEnumerable <IEditorAction> additionalActions =
                    controller.GetAdditionalEditorActions(word);
                actions.AddRange(additionalActions);
            }

            // Return all the change actions.
            return(actions);
        }
Example #10
0
        public void AddAndExecuteCommand_WithElevenCommands_ShouldRemoveFirstCommand()
        {
            var history = new History();
            var text    = new Text();
            var command = new ReplaceTextCommand(text, "text");

            history.AddAndExecuteCommand(command);
            history.AddAndExecuteCommand(command);
            history.AddAndExecuteCommand(command);
            history.AddAndExecuteCommand(command);
            history.AddAndExecuteCommand(command);
            history.AddAndExecuteCommand(command);
            history.AddAndExecuteCommand(command);
            history.AddAndExecuteCommand(command);
            history.AddAndExecuteCommand(command);
            history.AddAndExecuteCommand(command);
            history.AddAndExecuteCommand(command);
        }
        public void ProcessImmediateEdits(
            BlockCommandContext context,
            Block block,
            int textIndex)
        {
            // Get the plugin settings from the project.
            ImmediateBlockTypesSettings settings = Settings;

            // Grab the substring from the beginning to the index and compare that
            // in the dictionary.
            string text = block.Text.Substring(0, textIndex);

            if (!settings.Replacements.ContainsKey(text))
            {
                // We want to fail as fast as possible.
                return;
            }

            // If the block type is already set to the same name, skip it.
            string    blockTypeName = settings.Replacements[text];
            BlockType blockType     = Project.BlockTypes[blockTypeName];

            if (block.BlockType == blockType)
            {
                return;
            }

            // Perform the substitution with a replace operation and a block change
            // operation.
            var replaceCommand =
                new ReplaceTextCommand(
                    new BlockPosition(block.BlockKey, 0), textIndex, string.Empty);
            var changeCommand = new ChangeBlockTypeCommand(block.BlockKey, blockType);

            // Create a composite command that binds everything together.
            var compositeCommand = new CompositeCommand <BlockCommandContext>(true, false);

            compositeCommand.Commands.Add(replaceCommand);
            compositeCommand.Commands.Add(changeCommand);

            // Add the command to the deferred execution so the command could
            // be properly handled via the undo/redo management.
            block.Project.Commands.DeferDo(compositeCommand);
        }
Example #12
0
        private void ReplaceTextCommandExecutor(string commandParams)
        {
            var argumentsParser = new ArgumentsParser(commandParams);

            if (argumentsParser.NextArgumentsCount < 2)
            {
                throw new MenuException();
            }

            int?position = argumentsParser.GetNextAsInt();

            if (position == null)
            {
                throw new MenuException();
            }
            string text = argumentsParser.GetNextsAsString(' ');

            ICommand command = new ReplaceTextCommand(position.Value, text, _document);

            command.Execute();
        }
Example #13
0
        public void ReplacesText()
        {
            _engine             = new AutomationEngineInstance(null);
            _replaceTextCommand = new ReplaceTextCommand();

            string inputText    = "Hello john";
            string oldSubstring = "Hello";
            string newSubstring = "Goodbye";

            VariableMethods.CreateTestVariable(inputText, _engine, "input", typeof(string));
            VariableMethods.CreateTestVariable(oldSubstring, _engine, "old", typeof(string));
            VariableMethods.CreateTestVariable(newSubstring, _engine, "new", typeof(string));
            VariableMethods.CreateTestVariable(null, _engine, "output", typeof(string));

            _replaceTextCommand.v_InputText = "{input}";
            _replaceTextCommand.v_OldText   = "{old}";
            _replaceTextCommand.v_NewText   = "{new}";
            _replaceTextCommand.v_OutputUserVariableName = "{output}";

            _replaceTextCommand.RunCommand(_engine);

            Assert.Equal("Goodbye john", "{output}".ConvertUserVariableToString(_engine));
        }
        static void Main(string[] args)
        {
            string command;
            var    path  = "";
            var    param = "";

            if (args.Length == 0)
            {
                Utils.PrintAvailableOperations();
                Utils.Output("Enter operation name:", ConsoleColor.Yellow);
                command = Console.ReadLine();
            }
            else
            {
                command = args[0];
                if (args.Length > 1)
                {
                    path = args[1];
                }
                if (args.Length > 2)
                {
                    param = args[2];
                }
            }

            command = command.Trim().Replace("/", "").Replace("-", "");

            if (!CmdCommands.Exists(command))
            {
                Utils.OutputError("Incorrect operation");
                Utils.PrintAvailableOperations();
                return;
            }

            if (path.Length == 0)
            {
                path = Utils.RequestPath(command.Equals(CmdCommands.BrowseFolder) ? "Enter folder path:" : "Enter file name:", !command.Equals(CmdCommands.BrowseFolder));
                if (path.Length == 0)
                {
                    return;
                }
            }

            CommonCommand cmd;

            switch (command)
            {
            case CmdCommands.ReplaceText:
                if (param.Length == 0)
                {
                    Utils.Output("Enter word to replace:", ConsoleColor.Yellow);
                    param = Console.ReadLine();
                }
                cmd = new ReplaceTextCommand(path, param);
                break;

            case CmdCommands.CountWords:
                cmd = new CountWordsCommand(path);
                break;

            case CmdCommands.ReverseWords:
                cmd = new ReverseWordsCommand(path);
                break;

            case CmdCommands.BrowseFolder:
                cmd = new BrowseFolderCommand(path);
                break;

            default:
                cmd = new IncorrectCommand(path);
                break;
            }

            cmd.DoWork();
        }
        public void ProcessImmediateEdits(
            BlockCommandContext context,
            Block block,
            int textIndex)
        {
            // If we aren't optimized, we have to pull the settings back in from the
            // project settings and optimize them.
            if (!optimizedSubstitions)
            {
                RetrieveSettings();
            }

            // Pull out the edit text and add a leading space to simplify the
            // "whole word" substitutions.
            string editText = block.Text.Substring(0, textIndex);

            if (editText.Length - 1 < 0)
            {
                return;
            }

            // Figure out if we're at a word break.
            char finalCharacter = editText[editText.Length - 1];
            bool isWordBreak    = char.IsPunctuation(finalCharacter) ||
                                  char.IsWhiteSpace(finalCharacter);

            // Go through the substitution elements and look for each one.
            foreach (RegisteredSubstitution substitution in Substitutions)
            {
                // If we are doing whole word searches, then we don't bother if
                // the final character isn't a word break or if it isn't a word
                // break before it.
                ReplaceTextCommand command;
                int searchLength     = substitution.Search.Length;
                int startSearchIndex = editText.Length - searchLength;

                // If we are going to be searching before the string, then this
                // search term will never be valid.
                if (startSearchIndex < 0)
                {
                    continue;
                }

                // Do the search based on the whole word or suffix search.
                if (substitution.IsWholeWord)
                {
                    // Check to see if we have a valid search term.
                    if (!isWordBreak)
                    {
                        continue;
                    }

                    if (startSearchIndex > 0 &&
                        char.IsPunctuation(editText[startSearchIndex - 1]))
                    {
                        continue;
                    }

                    if (startSearchIndex - 1 < 0)
                    {
                        continue;
                    }

                    // Make sure the string we're looking at actually is the same.
                    string editSubstring = editText.Substring(
                        startSearchIndex - 1, substitution.Search.Length);

                    if (editSubstring != substitution.Search)
                    {
                        // The words don't match.
                        continue;
                    }

                    // Perform the substitution with a replace operation.
                    command =
                        new ReplaceTextCommand(
                            new BlockPosition(block.BlockKey, startSearchIndex - 1),
                            searchLength + 1,
                            substitution.Replacement + finalCharacter);
                }
                else
                {
                    // Perform a straight comparison search.
                    if (!editText.EndsWith(substitution.Search))
                    {
                        continue;
                    }

                    // Figure out the replace operation.
                    command =
                        new ReplaceTextCommand(
                            new BlockPosition(block.BlockKey, startSearchIndex),
                            searchLength,
                            substitution.Replacement);
                }

                // Add the command to the deferred execution so the command could
                // be properly handled via the undo/redo management.
                block.Project.Commands.DeferDo(command);
            }
        }