This class manages the selections in a document.
Inheritance: IDisposable
        public TextArea(TextEditorControl motherTextEditorControl, TextAreaControl motherTextAreaControl)
        {
            this.motherTextAreaControl   = motherTextAreaControl;
            this.motherTextEditorControl = motherTextEditorControl;
            this.caret                    = new ICSharpCode.TextEditor.Caret(this);
            this.selectionManager         = new ICSharpCode.TextEditor.Document.SelectionManager(this.Document, this);
            this.textAreaClipboardHandler = new TextAreaClipboardHandler(this);
            base.ResizeRedraw             = true;
            base.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
            base.SetStyle(ControlStyles.Opaque, false);
            base.SetStyle(ControlStyles.ResizeRedraw, true);
            base.SetStyle(ControlStyles.Selectable, true);
            this.textView      = new ICSharpCode.TextEditor.TextView(this);
            this.gutterMargin  = new ICSharpCode.TextEditor.GutterMargin(this);
            this.foldMargin    = new ICSharpCode.TextEditor.FoldMargin(this);
            this.iconBarMargin = new ICSharpCode.TextEditor.IconBarMargin(this);
            List <AbstractMargin> abstractMargins = this.leftMargins;

            AbstractMargin[] abstractMarginArray = new AbstractMargin[] { this.iconBarMargin, this.gutterMargin, this.foldMargin };
            abstractMargins.AddRange(abstractMarginArray);
            this.OptionsChanged();
            (new TextAreaMouseHandler(this)).Attach();
            (new TextAreaDragDropHandler()).Attach(this);
            this.bracketshemes.Add(new BracketHighlightingSheme('{', '}'));
            this.bracketshemes.Add(new BracketHighlightingSheme('(', ')'));
            this.bracketshemes.Add(new BracketHighlightingSheme('[', ']'));
            this.caret.PositionChanged                   += new EventHandler(this.SearchMatchingBracket);
            this.Document.TextContentChanged             += new EventHandler(this.TextContentChanged);
            this.Document.FoldingManager.FoldingsChanged += new EventHandler(this.DocumentFoldingsChanged);
        }
		/// <summary>
		/// Selects the specified text range.
		/// </summary>
		static void SelectText(SelectionManager selectionManager, IDocument document, int startOffset, int length)
		{
			selectionManager.ClearSelection();
			TextLocation selectionStart = document.OffsetToPosition(startOffset);
			TextLocation selectionEnd = document.OffsetToPosition(startOffset + length);
			selectionManager.SetSelection(selectionStart, selectionEnd);
		}
        public void CharacterSelectStart()
        {
            const string TextMessage = "char[] validChars = new char[] {'\"', '|(', '<', '{', '['};";
            int offset;
            IDocument document = TestHelper.MakeDocument(TextMessage, out offset);

            ISelection selection = new SingleWordSelection(document, offset);
            SelectionManager selectionManager = new SelectionManager(document);
            selectionManager.SetSelection(selection);
            Assert.AreEqual("(", selectionManager.SelectedText);
        }
        public void CursorAtEndOfWord()
        {
            const string TextMessage = "using(WorkflowRuntime| workflowRuntime = new WorkflowRuntime())";
            int offset;
            IDocument document = TestHelper.MakeDocument(TextMessage, out offset);

            ISelection selection = new SingleWordSelection(document, offset);
            SelectionManager selectionManager = new SelectionManager(document);
            selectionManager.SetSelection(selection);

            Assert.AreEqual("WorkflowRuntime", selectionManager.SelectedText);
            TestHelper.CallAllProperties(selection);
        }
        public void CursorAtEndOfWord23423()
        {
            const string TextMessage = "\"|Hello World\"";
            int offset;
            IDocument document = TestHelper.MakeDocument(TextMessage, out offset);

            ISelection selection = new SingleWordSelection(document, offset);
            SelectionManager selectionManager = new SelectionManager(document);
            selectionManager.SetSelection(selection);

            Assert.AreEqual("Hello", selectionManager.SelectedText);
            TestHelper.CallAllProperties(selection);
        }
        void InsertTabs(IDocument document, SelectionManager selmgr, int y1, int y2)
        {
            string indentationString = GetIndentationString(document);
            for (int i = y2; i >= y1; --i)
            {
                LineSegment line = document.GetLineSegment(i);
                if (i == y2 && i == selmgr.EndPosition.Y && selmgr.EndPosition.X  == 0)
                {
                    continue;
                }

                // this bit is optional - but useful if you are using block tabbing to sort out
                // a source file with a mixture of tabs and spaces
				// string newLine = document.GetText(line.Offset,line.Length);
				// document.Replace(line.Offset,line.Length,newLine);

                document.Insert(line.Offset, indentationString);
            }
        }
        public void Decrease_text_select_with_qoutes_to_only_word()
        {
            // Arrange
            const string textMessage = "\"|Hello World!\"";
            int offset;
            IDocument document = TestHelper.MakeDocument(textMessage, out offset);
            ISelection selection = new SingleWordSelection(document, offset);
            SelectionManager selectionManager = new SelectionManager(document);
            selectionManager.SetSelection(selection.StartPosition, selection.EndPosition);

            selection = new ExtendBlockSelection(document, selection);
            selectionManager.SetSelection(selection.StartPosition, selection.EndPosition);
            Assert.AreEqual("\"Hello World!\"", selectionManager.SelectedText);

            // Act
            selection = new DecreaseSelection(document, selection);
            selectionManager.SetSelection(selection);

            // Arrange
            Assert.AreEqual("Hello World!", selectionManager.SelectedText);
        }
Example #8
0
		public TextArea(TextEditorControl motherTextEditorControl, TextAreaControl motherTextAreaControl)
		{
			this.motherTextAreaControl      = motherTextAreaControl;
			this.motherTextEditorControl    = motherTextEditorControl;
			
			caret            = new Caret(this);
			selectionManager = new SelectionManager(Document);
			
			this.textAreaClipboardHandler = new TextAreaClipboardHandler(this);
			
			ResizeRedraw = true;
			
			SetStyle(ControlStyles.DoubleBuffer, false);
//			SetStyle(ControlStyles.AllPaintingInWmPaint, true);
//			SetStyle(ControlStyles.UserPaint, true);
			SetStyle(ControlStyles.Opaque, false);
			SetStyle(ControlStyles.ResizeRedraw, true);
			SetStyle(ControlStyles.Selectable, true);
			
			textView = new TextView(this);
			
			gutterMargin = new GutterMargin(this);
			foldMargin   = new FoldMargin(this);
			iconBarMargin = new IconBarMargin(this);
			leftMargins.AddRange(new AbstractMargin[] { iconBarMargin, gutterMargin, foldMargin });
			OptionsChanged();
			
			
			new TextAreaMouseHandler(this).Attach();
			new TextAreaDragDropHandler().Attach(this);
			
			bracketshemes.Add(new BracketHighlightingSheme('{', '}'));
			bracketshemes.Add(new BracketHighlightingSheme('(', ')'));
			bracketshemes.Add(new BracketHighlightingSheme('[', ']'));
			
			caret.PositionChanged += new EventHandler(SearchMatchingBracket);
			Document.TextContentChanged += new EventHandler(TextContentChanged);
			Document.FoldingManager.FoldingsChanged += new EventHandler(DocumentFoldingsChanged);
		}
 bool ShouldComment(IDocument document, string comment, SelectionManager selmgr, int startLine, int endLine)
 {
     for (int i = endLine; i >= startLine; --i)
     {
         LineSegment line = document.GetLineSegment(i);
         if (selmgr != null && i == endLine && line.Offset == selmgr.StartOffset + selmgr.Length)
         {
             --lastLine;
             continue;
         }
         string lineText = document.GetText(line.Offset, line.Length);
         if (!lineText.Trim().StartsWith(comment))
         {
             return true;
         }
     }
     return false;
 }
        void SetCommentAt(IDocument document, string comment, SelectionManager selmgr, int y1, int y2)
        {
            firstLine = y1;
            lastLine  = y2;

            for (int i = y2; i >= y1; --i)
            {
                LineSegment line = document.GetLineSegment(i);
                if (selmgr != null && i == y2 && line.Offset == selmgr.StartOffset + selmgr.Length)
                {
                    --lastLine;
                    continue;
                }

                string lineText = document.GetText(line.Offset, line.Length);
                document.Insert(line.Offset, comment);
            }
        }
        void RemoveCommentAt(IDocument document, string comment, SelectionManager selmgr, int y1, int y2)
        {
            firstLine = y1;
            lastLine  = y2;

            for (int i = y2; i >= y1; --i)
            {
                LineSegment line = document.GetLineSegment(i);
                if (selmgr != null && i == y2 && line.Offset == selmgr.StartOffset + selmgr.Length)
                {
                    --lastLine;
                    continue;
                }

                string lineText = document.GetText(line.Offset, line.Length);
                if (lineText.Trim().StartsWith(comment))
                {
                    document.Remove(line.Offset + lineText.IndexOf(comment), comment.Length);
                }
            }
        }
        void RemoveTabs(IDocument document, SelectionManager selmgr, int y1, int y2)
        {
            document.UndoStack.StartUndoGroup();
            for (int i = y2; i >= y1; --i)
            {
                LineSegment line = document.GetLineSegment(i);
                if (i == y2 && line.Offset == selmgr.EndOffset)
                {
                    continue;
                }
                if (line.Length > 0)
                {
                    /**** TextPad Strategy:
                    /// first convert leading whitespace to tabs (controversial! - not all editors work like this)
                    string newLine = TextUtilities.LeadingWhiteSpaceToTabs(document.GetText(line.Offset,line.Length),document.Properties.Get("TabIndent", 4));
                    if(newLine.Length > 0 && newLine[0] == '\t') {
                    	document.Replace(line.Offset,line.Length,newLine.Substring(1));
                    	++redocounter;
                    }
                    else if(newLine.Length > 0 && newLine[0] == ' ') {
                    	/// there were just some leading spaces but less than TabIndent of them
                    	int leadingSpaces = 1;
                    	for(leadingSpaces = 1; leadingSpaces < newLine.Length && newLine[leadingSpaces] == ' '; leadingSpaces++) {
                    		/// deliberately empty
                    	}
                    	document.Replace(line.Offset,line.Length,newLine.Substring(leadingSpaces));
                    	++redocounter;
                    }
                    /// else
                    /// there were no leading tabs or spaces on this line so do nothing
                    /// MS Visual Studio 6 strategy:
                     ****/
//					string temp = document.GetText(line.Offset,line.Length);

                    if (line.Length > 0)
                    {
                        int charactersToRemove = 0;
                        if(document.GetCharAt(line.Offset) == '\t')   // first character is a tab - just remove it
                        {
                            charactersToRemove = 1;
                        }
                        else if(document.GetCharAt(line.Offset) == ' ')
                        {
                            int leadingSpaces = 1;
                            int tabIndent = document.TextEditorProperties.IndentationSize;
                            for (leadingSpaces = 1; leadingSpaces < line.Length && document.GetCharAt(line.Offset + leadingSpaces) == ' '; leadingSpaces++)
                            {
                                // deliberately empty
                            }
                            if(leadingSpaces >= tabIndent)
                            {
                                // just remove tabIndent
                                charactersToRemove = tabIndent;
                            }
                            else if(line.Length > leadingSpaces && document.GetCharAt(line.Offset + leadingSpaces) == '\t')
                            {
                                // remove the leading spaces and the following tab as they add up
                                // to just one tab stop
                                charactersToRemove = leadingSpaces+1;
                            }
                            else
                            {
                                // just remove the leading spaces
                                charactersToRemove = leadingSpaces;
                            }
                        }

                        if (charactersToRemove > 0)
                        {
                            document.Remove(line.Offset,charactersToRemove);
                        }
                    }
                }
            }
            document.UndoStack.EndUndoGroup();
        }
        public void WordCloseToDot()
        {
            const string TextMessage = "char answer = Console|.ReadKey().KeyChar;";
            int offset;
            IDocument document = TestHelper.MakeDocument(TextMessage, out offset);

            ISelection selection = new SingleWordSelection(document, offset);
            SelectionManager selectionManager = new SelectionManager(document);
            selectionManager.SetSelection(selection);
            Assert.AreEqual("Console", selectionManager.SelectedText);

            offset += 1;
            selection = new SingleWordSelection(document, offset);
            selectionManager.SetSelection(selection);
            Assert.AreEqual("ReadKey", selectionManager.SelectedText);
            TestHelper.CallAllProperties(selection);
        }
        public void SelectNerestWord()
        {
            const string TextMessage = "\tIs |the bug fixed?";
            int offset;
            IDocument document = TestHelper.MakeDocument(TextMessage, out offset);

            ISelection selection = new SingleWordSelection(document, offset);
            SelectionManager selectionManager = new SelectionManager(document);
            selectionManager.SetSelection(selection);
            Assert.AreEqual("the", selectionManager.SelectedText);

            offset--;
            selection = new SingleWordSelection(document, offset);
            selectionManager.SetSelection(selection);
            Assert.AreEqual("Is", selectionManager.SelectedText);
            TestHelper.CallAllProperties(selection);
        }
        public void NullDocument()
        {
            const string TextMessage = null;
            IDocument document = TestHelper.MakeDocument(TextMessage);
            int offset = 21;
            ISelection selection = new SingleWordSelection(document, offset);
            SelectionManager selectionManager = new SelectionManager(document);
            selectionManager.SetSelection(selection);
            Assert.IsEmpty(selectionManager.SelectedText);
            TestHelper.CallAllProperties(selection);

            offset = -2;
            selection = new SingleWordSelection(document, offset);
            selectionManager.SetSelection(selection);
            Assert.IsEmpty(selectionManager.SelectedText);
            TestHelper.CallAllProperties(selection);
        }
        public void NoSelectionOffsetEndOfDocument()
        {
            const string TextMessage = "using(WorkflowRuntime workflowRuntime = new WorkflowRuntime())\n\n\n";
            IDocument document = TestHelper.MakeDocument(TextMessage);
            int offset = 240;
            ISelection selection = new SingleWordSelection(document, offset);
            SelectionManager selectionManager = new SelectionManager(document);
            selectionManager.SetSelection(selection);

            Assert.IsTrue(selection.IsEmpty);
            Assert.IsEmpty(selectionManager.SelectedText);
            TestHelper.CallAllProperties(selection);
        }
        public void CursorPositionBeforeText()
        {
            const string TextMessage = "\rusing(WorkflowRuntime workflowRuntime = new WorkflowRuntime())";
            IDocument document = TestHelper.MakeDocument(TextMessage);
            int offset = -1;
            ISelection selection = new SingleWordSelection(document, offset);
            SelectionManager selectionManager = new SelectionManager(document);
            selectionManager.SetSelection(selection);

            Assert.AreEqual("using", selectionManager.SelectedText);
            TestHelper.CallAllProperties(selection);
        }
        public void CursorBeforeQuotationMark()
        {
            const string TextMessage = "\"hello there|\"";
            int offset;
            IDocument document = TestHelper.MakeDocument(TextMessage, out offset);

            ISelection selection = new SingleWordSelection(document, offset);
            SelectionManager selectionManager = new SelectionManager(document);
            selectionManager.SetSelection(selection);

            Assert.AreEqual("there", selectionManager.SelectedText);
            TestHelper.CallAllProperties(selection);
        }