SetSelection() public method

public SetSelection ( Mono.TextEditor.DocumentLocation anchor, Mono.TextEditor.DocumentLocation lead ) : void
anchor Mono.TextEditor.DocumentLocation
lead Mono.TextEditor.DocumentLocation
return void
        public static void EndLineSelection(TextEditorData data)
        {
            int fromLine  = data.MainSelection.Anchor.Line;
            int toLine    = data.Caret.Line;
            var toSegment = data.Document.GetLine(toLine);

            //flip the anchor if pivoting around the origin line
            if (fromLine == toLine + 1)
            {
                if ((fromLine - data.MainSelection.Lead.Line) != 2)
                {
                    var fromSegment = data.Document.GetLine(fromLine);
                    data.SetSelection(fromSegment.EndOffsetIncludingDelimiter, toSegment.Offset);
                }
                else
                {
                    data.SetSelection(toSegment.Offset, toSegment.EndOffsetIncludingDelimiter);
                }
            }
            //else just extend the selection else
            {
                int toOffset = (toLine < fromLine) ? toSegment.Offset : toSegment.EndOffsetIncludingDelimiter;
                data.ExtendSelectionTo(toOffset);
            }
            data.Caret.PreserveSelection = false;
        }
示例#2
0
        public static void IndentSelection(TextEditorData data)
        {
            if (!data.IsSomethingSelected)
            {
                return;
            }
            int startLineNr, endLineNr;

            GetSelectedLines(data, out startLineNr, out endLineNr);
            var anchor            = data.MainSelection.Anchor;
            var lead              = data.MainSelection.Lead;
            var indentationString = data.Options.IndentationString;

            using (var undo = data.OpenUndoGroup(OperationType.Format)) {
                foreach (DocumentLine line in data.SelectedLines)
                {
                    if (data.Options.IndentStyle == IndentStyle.Virtual && line.Length == 0)
                    {
                        continue;
                    }
                    data.Insert(line.Offset, indentationString);
                }
            }
            int chars     = indentationString.Length;
            var leadCol   = lead.Column > 1 || lead < anchor ? lead.Column + chars : 1;
            var anchorCol = anchor.Column > 1 || anchor < lead ? anchor.Column + chars : 1;

            data.SetSelection(anchor.Line, anchorCol, lead.Line, leadCol);
            data.Document.RequestUpdate(new MultipleLineUpdate(startLineNr, endLineNr));
            data.Document.CommitDocumentUpdate();
        }
        void EditableTextImplementor.CutText(int start_pos, int end_pos)
        {
            var oldSelection = TextEditorData.MainSelection;

            TextEditorData.SetSelection(start_pos, end_pos);
            ClipboardActions.Cut(TextEditorData);
            TextEditorData.MainSelection = oldSelection;
        }
        // TODO: move this somewhere else? extend TextEditor?
        public static void SetSelectLines(TextEditorData editor, int start, int end)
        {
            start = Math.Min(start, editor.LineCount);
            end = Math.Min(end, editor.LineCount);

            var startLine = start > end ? editor.GetLine(end) : editor.GetLine(start);
            var endLine = start > end ? editor.GetLine(start) : editor.GetLine(end);

            editor.SetSelection(startLine.Offset, endLine.EndOffsetIncludingDelimiter);
        }
示例#5
0
        public static void MoveBlockDown(TextEditorData data)
        {
            int              lineStart = data.Caret.Line;
            int              lineEnd = data.Caret.Line;
            bool             setSelection = lineStart != lineEnd;
            DocumentLocation anchor = DocumentLocation.Empty, lead = DocumentLocation.Empty;

            if (data.IsSomethingSelected)
            {
                setSelection = true;
                lineStart    = data.MainSelection.MinLine;
                lineEnd      = data.MainSelection.MaxLine;
                anchor       = data.MainSelection.Anchor;
                lead         = data.MainSelection.Lead;
            }

            if (lineStart <= 0)
            {
                return;
            }
            using (var undo = data.OpenUndoGroup()) {
                //Mono.TextEditor.LineSegment startLine = data.Document.GetLine (lineStart);
                //int relCaretOffset = data.Caret.Offset - startLine.Offset;

                Mono.TextEditor.DocumentLine nextLine = data.Document.GetLine(lineEnd + 1);
                if (nextLine == null)
                {
                    return;
                }
                string text = data.Document.GetTextAt(nextLine.Offset, nextLine.Length);

                List <TextLineMarker> prevLineMarkers = new List <TextLineMarker> (data.Document.GetMarkers(nextLine));
                data.Document.ClearMarkers(nextLine);
                var loc = data.Caret.Location;
                for (int i = lineEnd + 1; i >= lineStart; i--)
                {
                    DocumentLine cur  = data.Document.GetLine(i);
                    DocumentLine prev = data.Document.GetLine(i - 1);
                    data.Replace(cur.Offset, cur.Length, i != lineStart ? data.Document.GetTextAt(prev.Offset, prev.Length) : text);
                    data.Document.ClearMarkers(data.Document.GetLine(i));
                    foreach (TextLineMarker marker in (i != lineStart ? data.Document.GetMarkers(data.Document.GetLine(i - 1)) : prevLineMarkers))
                    {
                        data.Document.AddMarker(data.Document.GetLine(i), marker);
                    }
                }

                data.Caret.Location = new DocumentLocation(loc.Line + 1, loc.Column);
                if (setSelection)
                {
                    data.SetSelection(anchor.Line + 1, anchor.Column, lead.Line + 1, lead.Column);
                }
            }
        }
示例#6
0
        public static void MoveBlockDown(TextEditorData data)
        {
            int  lineStart    = data.Caret.Line;
            int  lineEnd      = data.Caret.Line;
            bool setSelection = lineStart != lineEnd;

            if (data.IsSomethingSelected)
            {
                setSelection = true;
                lineStart    = data.MainSelection.MinLine;
                lineEnd      = data.MainSelection.MaxLine;
            }

            if (lineStart <= 0)
            {
                return;
            }
            data.Document.BeginAtomicUndo();

            //Mono.TextEditor.LineSegment startLine = data.Document.GetLine (lineStart);
            //int relCaretOffset = data.Caret.Offset - startLine.Offset;

            Mono.TextEditor.LineSegment nextLine = data.Document.GetLine(lineEnd + 1);
            if (nextLine == null)
            {
                return;
            }
            string            text            = data.Document.GetTextAt(nextLine.Offset, nextLine.EditableLength);
            List <TextMarker> prevLineMarkers = new List <TextMarker> (nextLine.Markers);

            nextLine.ClearMarker();

            for (int i = lineEnd + 1; i >= lineStart; i--)
            {
                LineSegment cur  = data.Document.GetLine(i);
                LineSegment prev = data.Document.GetLine(i - 1);
                data.Replace(cur.Offset, cur.EditableLength, i != lineStart ? data.Document.GetTextAt(prev.Offset, prev.EditableLength) : text);
                data.Document.GetLine(i).ClearMarker();
                foreach (TextMarker marker in (i != lineStart ? data.Document.GetLine(i - 1).Markers : prevLineMarkers))
                {
                    data.Document.GetLine(i).AddMarker(marker);
                }
            }

            data.Caret.Line++;
            if (setSelection)
            {
                data.SetSelection(data.Document.GetLine(lineStart + 1).Offset, data.Document.GetLine(lineEnd + 1).Offset + data.Document.GetLine(lineEnd + 1).EditableLength);
            }
            data.Document.EndAtomicUndo();
        }
        public virtual void HandleSpecialSelectionKey(TextEditorData textEditorData, int unicodeKey)
        {
            string start, end;

            GetSelectionSurroundings(textEditorData, unicodeKey, out start, out end);

            if (textEditorData.MainSelection.SelectionMode == SelectionMode.Block)
            {
                var selection = textEditorData.MainSelection;
                int startCol  = System.Math.Min(selection.Anchor.Column, selection.Lead.Column) - 1;
                int endCol    = System.Math.Max(selection.Anchor.Column, selection.Lead.Column);
                for (int lineNumber = selection.MinLine; lineNumber <= selection.MaxLine; lineNumber++)
                {
                    DocumentLine lineSegment = textEditorData.GetLine(lineNumber);

                    if (lineSegment.Offset + startCol < lineSegment.EndOffset)
                    {
                        textEditorData.Insert(lineSegment.Offset + startCol, start);
                    }
                    if (lineSegment.Offset + endCol < lineSegment.EndOffset)
                    {
                        textEditorData.Insert(lineSegment.Offset + endCol, end);
                    }
                }

                textEditorData.MainSelection = new Selection(
                    new DocumentLocation(selection.Anchor.Line, endCol == selection.Anchor.Column ? endCol + start.Length : startCol + 1 + start.Length),
                    new DocumentLocation(selection.Lead.Line, endCol == selection.Anchor.Column ? startCol + 1 + start.Length : endCol + start.Length),
                    Mono.TextEditor.SelectionMode.Block);
                textEditorData.Document.CommitMultipleLineUpdate(textEditorData.MainSelection.MinLine, textEditorData.MainSelection.MaxLine);
            }
            else
            {
                int anchorOffset = textEditorData.MainSelection.GetAnchorOffset(textEditorData);
                int leadOffset   = textEditorData.MainSelection.GetLeadOffset(textEditorData);
                if (leadOffset < anchorOffset)
                {
                    int tmp = anchorOffset;
                    anchorOffset = leadOffset;
                    leadOffset   = tmp;
                }
                textEditorData.Insert(anchorOffset, start);
                textEditorData.Insert(leadOffset >= anchorOffset ? leadOffset + start.Length : leadOffset, end);
                textEditorData.SetSelection(anchorOffset + start.Length, leadOffset + start.Length);
            }
        }
示例#8
0
        public static void MoveBlockUp(TextEditorData data)
        {
            int  lineStart    = data.Caret.Line;
            int  lineEnd      = data.Caret.Line;
            bool setSelection = lineStart != lineEnd;

            if (data.IsSomethingSelected)
            {
                setSelection = true;
                lineStart    = data.MainSelection.MinLine;
                lineEnd      = data.MainSelection.MaxLine;
            }

            if (lineStart <= 0)
            {
                return;
            }

            using (var undo = data.OpenUndoGroup()) {
                //Mono.TextEditor.LineSegment startLine = data.Document.GetLine (lineStart);
                //int relCaretOffset = data.Caret.Offset - startLine.Offset;

                Mono.TextEditor.LineSegment prevLine = data.Document.GetLine(lineStart - 1);
                string            text            = data.Document.GetTextAt(prevLine.Offset, prevLine.EditableLength);
                List <TextMarker> prevLineMarkers = new List <TextMarker> (prevLine.Markers);
                prevLine.ClearMarker();

                for (int i = lineStart - 1; i <= lineEnd; i++)
                {
                    LineSegment cur  = data.Document.GetLine(i);
                    LineSegment next = data.Document.GetLine(i + 1);
                    data.Replace(cur.Offset, cur.EditableLength, i != lineEnd ? data.Document.GetTextAt(next.Offset, next.EditableLength) : text);
                    data.Document.GetLine(i).ClearMarker();
                    foreach (TextMarker marker in (i != lineEnd ? data.Document.GetLine(i + 1).Markers : prevLineMarkers))
                    {
                        data.Document.GetLine(i).AddMarker(marker);
                    }
                }

                data.Caret.Line--;
                if (setSelection)
                {
                    data.SetSelection(data.Document.GetLine(lineStart - 1).Offset, data.Document.GetLine(lineEnd - 1).Offset + data.Document.GetLine(lineEnd - 1).EditableLength);
                }
            }
        }
示例#9
0
		public static void IndentSelection (TextEditorData data)
		{
			if (!data.IsSomethingSelected)
				return;
			int startLineNr, endLineNr;
			GetSelectedLines (data, out startLineNr, out endLineNr);
			var anchor = data.MainSelection.Anchor;
			var lead = data.MainSelection.Lead;
			using (var undo = data.OpenUndoGroup ()) {
				foreach (DocumentLine line in data.SelectedLines) {
					data.Insert (line.Offset, data.Options.IndentationString);
				}
			}
			var leadCol = lead.Column > 1 || lead < anchor ? lead.Column + 1 : 1;
			var anchorCol = anchor.Column > 1 || anchor < lead ? anchor.Column + 1 : 1;
			data.SetSelection (anchor.Line, anchorCol, lead.Line, leadCol);
			data.Document.RequestUpdate (new MultipleLineUpdate (startLineNr, endLineNr));
			data.Document.CommitDocumentUpdate ();
		}
示例#10
0
		public static void RemoveIndentSelection (TextEditorData data)
		{
			if (!data.IsSomethingSelected)
				return;
			int startLineNr, endLineNr;
			GetSelectedLines (data, out startLineNr, out endLineNr);
			
			using (var undo = data.OpenUndoGroup ()) {
				var anchor = data.MainSelection.Anchor;
				var lead = data.MainSelection.Lead;
				bool first = true;
				bool removedFromLast = false;
				bool removedFromFirst = false;
				foreach (var line in data.SelectedLines) {
					int remove = RemoveTabInLine (data, line);
					removedFromLast |= remove > 0;
					if (first) {
						removedFromFirst = remove > 0;
						first = false;
					}
				}

				var ac = System.Math.Max (DocumentLocation.MinColumn, anchor.Column - 1);
				var lc = System.Math.Max (DocumentLocation.MinColumn, lead.Column - 1);
				
				if (anchor < lead) {
					if (!removedFromFirst)
						ac = anchor.Column;
					if (!removedFromLast)
						lc = lead.Column;
				} else {
					if (!removedFromFirst)
						lc = lead.Column;
					if (!removedFromLast)
						ac = anchor.Column;
				}
				data.SetSelection (anchor.Line, ac, lead.Line, lc);
			}
			data.Document.RequestUpdate (new MultipleLineUpdate (startLineNr, endLineNr));
			data.Document.CommitDocumentUpdate ();
		}
        public static TextEditorData Create(string content, ITextEditorOptions options = null)
        {
            var data = new TextEditorData ();
            data.Options.DefaultEolMarker = eolMarker;
            data.Options.IndentStyle = IndentStyle.Smart;
            if (options != null)
                data.Options = options;
            var sb = new StringBuilder ();
            int caretIndex = -1, selectionStart = -1, selectionEnd = -1;
            var foldSegments = new List<FoldSegment> ();
            var foldStack = new Stack<FoldSegment> ();

            for (int i = 0; i < content.Length; i++) {
                var ch = content [i];
                switch (ch) {
                    case '$':
                    caretIndex = sb.Length;
                    break;
                    case '<':
                    if (i + 1 < content.Length) {
                        if (content [i + 1] == '-') {
                            selectionStart = sb.Length;
                            i++;
                            break;
                        }
                    }
                    goto default;
                    case '-':
                    if (i + 1 < content.Length) {
                        var next = content [i + 1];
                        if (next == '>') {
                            selectionEnd = sb.Length;
                            i++;
                            break;
                        }
                        if (next == '[') {
                            var segment = new FoldSegment (data.Document, "...", sb.Length, 0, FoldingType.None);
                            segment.IsFolded = false;
                            foldStack.Push (segment);
                            i++;
                            break;
                        }
                    }
                    goto default;
                    case '+':
                    if (i + 1 < content.Length) {
                        var next = content [i + 1];
                        if (next == '[') {
                            var segment = new FoldSegment (data.Document, "...", sb.Length, 0, FoldingType.None);
                            segment.IsFolded = true;
                            foldStack.Push (segment);
                            i++;
                            break;
                        }
                    }
                    goto default;
                    case ']':
                    if (foldStack.Count > 0) {
                        FoldSegment segment = foldStack.Pop ();
                        segment.Length = sb.Length - segment.Offset;
                        foldSegments.Add (segment);
                        break;
                    }
                    goto default;
                    default:
                    sb.Append (ch);
                    break;
                }
            }

            data.Text = sb.ToString ();

            if (caretIndex >= 0)
                data.Caret.Offset = caretIndex;
            if (selectionStart >= 0) {
                if (caretIndex == selectionStart) {
                    data.SetSelection (selectionEnd, selectionStart);
                } else {
                    data.SetSelection (selectionStart, selectionEnd);
                    if (caretIndex < 0)
                        data.Caret.Offset = selectionEnd;
                }
            }
            if (foldSegments.Count > 0)
                data.Document.UpdateFoldSegments (foldSegments);
            return data;
        }
示例#12
0
        public static void SortSelectedLines(TextEditorData data)
        {
            var start = data.MainSelection.Start;
            var end   = data.MainSelection.End;
            var caret = data.Caret.Location;

            int startLine = start.Line;
            int endLine   = end.Line;

            if (startLine == endLine)
            {
                return;
            }

            int length = 0;
            var lines  = new string[endLine - startLine + 1];

            for (int i = startLine; i <= endLine; i++)
            {
                //get lines *with* line endings
                var lineText = data.GetLineText(i, true);
                lines[i - startLine] = lineText;
                length += lineText.Length;
            }

            var linesUnsorted = new string[lines.Length];

            Array.Sort(lines, StringComparer.Ordinal);

            bool changed = false;

            for (int i = 0; i <= lines.Length; i++)
            {
                //can't simply use reference comparison as Array.Sort is not stable
                if (string.Equals(lines[i], linesUnsorted[i], StringComparison.Ordinal))
                {
                    continue;
                }
                changed = true;
                break;
            }
            if (!changed)
            {
                return;
            }


            var sb = new StringBuilder();

            for (int i = 0; i < lines.Length; i++)
            {
                sb.Append(lines[i]);
            }

            var startOffset = data.Document.LocationToOffset(new TextLocation(startLine, 0));

            data.Replace(startOffset, length, sb.ToString());

            data.Caret.Location = LimitColumn(data, caret);
            data.SetSelection(LimitColumn(data, start), LimitColumn(data, end));
        }
		public static void MoveBlockUp (TextEditorData data)
		{
			int lineStart = data.Caret.Line;
			int lineEnd = data.Caret.Line;
			bool setSelection = lineStart != lineEnd;
			if (data.IsSomethingSelected) {
				setSelection = true;
				lineStart = data.MainSelection.MinLine;
				lineEnd = data.MainSelection.MaxLine;
			}
			
			if (lineStart <= 0)
				return;
			
			Mono.TextEditor.LineSegment startLine = data.Document.GetLine (lineStart);
			Mono.TextEditor.LineSegment endLine = data.Document.GetLine (lineEnd);
			Mono.TextEditor.LineSegment prevLine = data.Document.GetLine (lineStart - 1);
			int relCaretOffset = data.Caret.Offset - startLine.Offset;
			data.Document.BeginAtomicUndo ();
			
			string text = data.Document.GetTextBetween (startLine.Offset, endLine.EndOffset);
			
			int delta = endLine.EndOffset - startLine.Offset;
			int newStartOffset = prevLine.Offset;
			bool hasEmptyDelimiter = endLine.DelimiterLength == 0;
			
			data.Remove (startLine.Offset, delta);
			
			// handle the last line case
			if (hasEmptyDelimiter) {
				text = text + data.EolMarker;
				data.Remove (prevLine.Offset + prevLine.EditableLength, prevLine.DelimiterLength);
			}
			
			// move markers
			List<TextMarker> markers = new List<TextMarker> (prevLine.Markers);
			prevLine.ClearMarker ();
			for (int i = lineStart; i <= lineEnd; i++) {
				foreach (TextMarker marker in data.Document.GetLine (i).Markers) {
					data.Document.GetLine (i - 1).AddMarker (marker);
				}
				data.Document.GetLine (i).ClearMarker ();
			}
			markers.ForEach (m => endLine.AddMarker (m));
			
			data.Insert (newStartOffset, text);
			data.Caret.Offset = newStartOffset + relCaretOffset;
			if (setSelection)
				data.SetSelection (newStartOffset, newStartOffset + delta - endLine.DelimiterLength);
			data.Document.EndAtomicUndo ();
		}
        static TextEditorData Create(string content, ITextEditorOptions options = null)
        {
            var sb = new StringBuilder();
            int caretIndex = -1, selectionStart = -1, selectionEnd = -1;

            for (int i = 0; i < content.Length; i++)
            {
                var ch = content[i];
                switch (ch)
                {
                case '$':
                    caretIndex = sb.Length - 1;
                    break;
                case '<':
                    if (i + 1 < content.Length)
                    {
                        if (content[i + 1] == '-')
                        {
                            selectionStart = sb.Length;
                            i++;
                            break;
                        }
                    }
                    goto default;
                case '-':
                    if (i + 1 < content.Length)
                    {
                        var next = content[i + 1];
                        if (next == '>')
                        {
                            selectionEnd = sb.Length;
                            i++;
                            break;
                        }
                    }
                    goto default;
                default:
                    sb.Append(ch);
                    break;
                }
            }
            var editor = new TextEditorData();
            editor.Text = sb.ToString();
            if (options != null)
                editor.Options = options;

            if (caretIndex >= 0)
                editor.Caret.Offset = caretIndex;
            if (selectionStart >= 0)
            {
                if (caretIndex == selectionStart)
                {
                    editor.SetSelection(selectionEnd, selectionStart);
                }
                else
                {
                    editor.SetSelection(selectionStart, selectionEnd);
                    if (caretIndex < 0)
                        editor.Caret.Offset = selectionEnd;
                }
            }
            return editor;
        }
		public static void MoveBlockDown (TextEditorData data)
		{
			int lineStart = data.Caret.Line;
			int lineEnd = data.Caret.Line;
			bool setSelection = lineStart != lineEnd;
			if (data.IsSomethingSelected) {
				setSelection = true;
				lineStart = data.MainSelection.MinLine;
				lineEnd = data.MainSelection.MaxLine;
			}
			
			if (lineEnd + 1 >= data.Document.LineCount)
				return;
			
			Mono.TextEditor.LineSegment startLine = data.Document.GetLine (lineStart);
			Mono.TextEditor.LineSegment endLine = data.Document.GetLine (lineEnd);
			Mono.TextEditor.LineSegment nextLine = data.Document.GetLine (lineEnd + 1);
			int relCaretOffset = data.Caret.Offset - startLine.Offset;
			data.Document.BeginAtomicUndo ();
			string text = data.Document.GetTextBetween (startLine.Offset, endLine.EndOffset);
			
			int nextLineOffset = nextLine.EndOffset;
			int delta = endLine.EndOffset - startLine.Offset;
			int newStartOffset = nextLineOffset - delta;
			
			// handle the last line case
			if (nextLine.DelimiterLength == 0) {
				text = data.EolMarker + text.Substring (0, text.Length - endLine.DelimiterLength);
				newStartOffset += data.EolMarker.Length;
			}
			data.Insert (nextLineOffset, text);
			data.Remove (startLine.Offset, delta);
			
			// move markers
			List<TextMarker> markers = new List<TextMarker> (nextLine.Markers);
			nextLine.ClearMarker ();
			for (int i = lineEnd; i <= lineStart; i++) {
				foreach (TextMarker marker in data.Document.GetLine (i).Markers) {
					data.Document.GetLine (i + 1).AddMarker (marker);
				}
				data.Document.GetLine (i).ClearMarker ();
			}
			markers.ForEach (m => startLine.AddMarker (m));

			
			data.Caret.Offset = newStartOffset + relCaretOffset;
			if (setSelection)
				data.SetSelection (newStartOffset, newStartOffset + text.Length - endLine.DelimiterLength);
			data.Document.EndAtomicUndo ();
		}
示例#16
0
		public static void EndLineSelection (TextEditorData data)
		{
			int fromLine = data.MainSelection.Anchor.Line;
			int toLine = data.Caret.Line;
			var toSegment = data.Document.GetLine (toLine);
			
			//flip the anchor if pivoting around the origin line
			if (fromLine == toLine + 1) {
				if ((fromLine - data.MainSelection.Lead.Line) != 2) {
					var fromSegment = data.Document.GetLine (fromLine);
					data.SetSelection (fromSegment.EndOffset, toSegment.Offset);
				} else {
					data.SetSelection (toSegment.Offset, toSegment.EndOffset);
				}
			}
			//else just extend the selection
			else {
				int toOffset = (toLine < fromLine)? toSegment.Offset: toSegment.EndOffset;
				data.ExtendSelectionTo (toOffset);
			}
			data.Caret.PreserveSelection = false;
		}
示例#17
0
        public static void RemoveIndentSelection(TextEditorData data)
        {
            if (!data.IsSomethingSelected)
            {
                return;
            }
            int startLineNr, endLineNr;

            GetSelectedLines(data, out startLineNr, out endLineNr);

            using (var undo = data.OpenUndoGroup(OperationType.Format)) {
                var          anchor           = data.MainSelection.Anchor;
                var          lead             = data.MainSelection.Lead;
                bool         first            = true;
                bool         removedFromLast  = false;
                int          removeLast       = 0;
                bool         removedFromFirst = false;
                int          removeFirst      = 0;
                var          changes          = new List <Microsoft.CodeAnalysis.Text.TextChange> ();
                DocumentLine last             = null;
                foreach (var line in data.SelectedLines)
                {
                    var remove = RemoveTabInLine(data, line);
                    removedFromLast |= remove.Span.Length > 0;
                    removeLast       = remove.Span.Length;
                    if (first)
                    {
                        removedFromFirst = remove.Span.Length > 0;
                        removeFirst      = remove.Span.Length;
                        first            = false;
                    }
                    if (remove.Span.Length > 0)
                    {
                        changes.Add(remove);
                    }
                    last = line;
                }
                data.Document.ApplyTextChanges(changes);
                if (last != null)
                {
                    data.Document.CommitLineUpdate(last);
                }
                var ac = System.Math.Max(DocumentLocation.MinColumn, anchor.Column - (anchor < lead ? removeFirst : removeLast));
                var lc = System.Math.Max(DocumentLocation.MinColumn, lead.Column - (anchor < lead ? removeLast : removeFirst));

                if (anchor < lead)
                {
                    if (!removedFromFirst)
                    {
                        ac = anchor.Column;
                    }
                    if (!removedFromLast)
                    {
                        lc = lead.Column;
                    }
                }
                else
                {
                    if (!removedFromFirst)
                    {
                        lc = lead.Column;
                    }
                    if (!removedFromLast)
                    {
                        ac = anchor.Column;
                    }
                }
                data.SetSelection(anchor.Line, ac, lead.Line, lc);
            }
            data.Document.RequestUpdate(new MultipleLineUpdate(startLineNr, endLineNr));
            data.Document.CommitDocumentUpdate();
        }
 bool TextImplementor.SetSelection(int selection_num, int start_offset, int end_offset)
 {
     TextEditorData.SetSelection(start_offset, end_offset);
     return(true);
 }
		public override void Run (RefactoringOptions options)
		{
			fileName = declaringType.CompilationUnit.FileName;
			
			var openDocument = IdeApp.Workbench.OpenDocument (fileName);
			if (openDocument == null) {
				MessageService.ShowError (string.Format (GettextCatalog.GetString ("Can't open file {0}."), fileName));
				return;
			}
			data = openDocument.Editor;
			if (data == null)
				return;
			openDocument.RunWhenLoaded (delegate {
				try {
					indent = data.Document.GetLine (declaringType.Location.Line).GetIndentation (data.Document) ?? "";
				} catch (Exception) {
					indent = "";
				}
				indent += "\t";
				
				InsertionCursorEditMode mode = new InsertionCursorEditMode (data.Parent, CodeGenerationService.GetInsertionPoints (openDocument, declaringType));
				if (fileName == options.Document.FileName) {
					for (int i = 0; i < mode.InsertionPoints.Count; i++) {
						var point = mode.InsertionPoints [i];
						if (point.Location < data.Caret.Location) {
							mode.CurIndex = i;
						} else {
							break;
						}
					}
				}
				
				ModeHelpWindow helpWindow = new ModeHelpWindow ();
				helpWindow.TransientFor = IdeApp.Workbench.RootWindow;
				helpWindow.TitleText = GettextCatalog.GetString ("<b>Create Method -- Targeting</b>");
				helpWindow.Items.Add (new KeyValuePair<string, string> (GettextCatalog.GetString ("<b>Key</b>"), GettextCatalog.GetString ("<b>Behavior</b>")));
				helpWindow.Items.Add (new KeyValuePair<string, string> (GettextCatalog.GetString ("<b>Up</b>"), GettextCatalog.GetString ("Move to <b>previous</b> target point.")));
				helpWindow.Items.Add (new KeyValuePair<string, string> (GettextCatalog.GetString ("<b>Down</b>"), GettextCatalog.GetString ("Move to <b>next</b> target point.")));
				helpWindow.Items.Add (new KeyValuePair<string, string> (GettextCatalog.GetString ("<b>Enter</b>"), GettextCatalog.GetString ("<b>Declare new method</b> at target point.")));
				helpWindow.Items.Add (new KeyValuePair<string, string> (GettextCatalog.GetString ("<b>Esc</b>"), GettextCatalog.GetString ("<b>Cancel</b> this refactoring.")));
				mode.HelpWindow = helpWindow;
				mode.StartMode ();
				mode.Exited += delegate(object s, InsertionCursorEventArgs args) {
					if (args.Success) {
						SetInsertionPoint (args.InsertionPoint);
						BaseRun (options);
						if (string.IsNullOrEmpty (fileName))
							return;
						data.ClearSelection ();
						data.Caret.Offset = selectionEnd;
						data.SetSelection (selectionStart, selectionEnd);
					}
				};
			});
		}
示例#20
0
		public override void OnTheFlyFormat (PolicyContainer policyParent, IEnumerable<string> mimeTypeChain, 
			TextEditorData data, int startOffset, int endOffset)
		{
			var parser = new CSharpParser ();
			var compilationUnit = parser.ParseSnippet (data);
			if (compilationUnit == null) {
				Console.WriteLine ("couldn't parse : " + data.Text);
				return;
			}
			if (parser.HasErrors)
				return;
			
			var policy = policyParent.Get<CSharpFormattingPolicy> (mimeTypeChain);
			var adapter = new TextEditorDataAdapter (data);
			var factory = new FormattingActionFactory (data);
			
			var formattingVisitor = new ICSharpCode.NRefactory.CSharp.AstFormattingVisitor (policy.CreateOptions (), adapter, factory) {
				HadErrors =  parser.HasErrors
			};
			
			compilationUnit.AcceptVisitor (formattingVisitor, null);
			
			var changes = new List<ICSharpCode.NRefactory.CSharp.Refactoring.Action> ();
			changes.AddRange (formattingVisitor.Changes.
				Where (c => (startOffset <= c.Offset && c.Offset < endOffset)));
			
			var startPositionChange = factory.CreateTextReplaceAction (startOffset, 0, null);
			changes.Add (startPositionChange);
			
			var endPositionChange = factory.CreateTextReplaceAction (endOffset, 0, null);
			changes.Add (endPositionChange);
			
			using (var undo = data.OpenUndoGroup ()) {
				MDRefactoringContext.MdScript.RunActions (changes, null);
			}
			
			if (data.IsSomethingSelected) {
				var range = data.SelectionRange;
				if (range.Offset == startOffset && range.EndOffset == endOffset) {
					data.SetSelection (startPositionChange.Offset, endPositionChange.Offset);
				}
			}
		}
示例#21
0
		public static void MoveBlockDown (TextEditorData data)
		{
			int lineStart = data.Caret.Line;
			int lineEnd = data.Caret.Line;
			bool setSelection = lineStart != lineEnd;
			DocumentLocation anchor = DocumentLocation.Empty, lead = DocumentLocation.Empty;
			if (data.IsSomethingSelected) {
				setSelection = true;
				lineStart = data.MainSelection.MinLine;
				lineEnd = data.MainSelection.MaxLine;
				anchor = data.MainSelection.Anchor;
				lead = data.MainSelection.Lead;
			}
			
			if (lineStart <= 0)
				return;
			using (var undo = data.OpenUndoGroup ()) {
				
				//Mono.TextEditor.LineSegment startLine = data.Document.GetLine (lineStart);
				//int relCaretOffset = data.Caret.Offset - startLine.Offset;
				
				Mono.TextEditor.DocumentLine nextLine = data.Document.GetLine (lineEnd + 1);
				if (nextLine == null)
					return;
				string text = data.Document.GetTextAt (nextLine.Offset, nextLine.Length);
				List<TextLineMarker> prevLineMarkers = new List<TextLineMarker> (nextLine.Markers);
				nextLine.ClearMarker ();
				var loc = data.Caret.Location;
				for (int i = lineEnd + 1; i >= lineStart; i--) {
					DocumentLine cur = data.Document.GetLine (i);
					DocumentLine prev = data.Document.GetLine (i - 1);
					data.Replace (cur.Offset, cur.Length, i != lineStart ? data.Document.GetTextAt (prev.Offset, prev.Length) : text);
					data.Document.GetLine (i).ClearMarker ();
					foreach (TextLineMarker marker in (i != lineStart ? data.Document.GetLine (i - 1).Markers : prevLineMarkers)) {
						data.Document.GetLine (i).AddMarker (marker);
					}
				}
				
				data.Caret.Location = new DocumentLocation (loc.Line + 1, loc.Column);
				if (setSelection)
					data.SetSelection (anchor.Line + 1, anchor.Column, lead.Line + 1, lead.Column);
			}
		}
示例#22
0
		public static void MoveBlockDown (TextEditorData data)
		{
			int lineStart = data.Caret.Line;
			int lineEnd = data.Caret.Line;
			bool setSelection = lineStart != lineEnd;
			if (data.IsSomethingSelected) {
				setSelection = true;
				lineStart = data.MainSelection.MinLine;
				lineEnd = data.MainSelection.MaxLine;
			}
			
			if (lineStart <= 0)
				return;
			data.Document.BeginAtomicUndo ();
			
			Mono.TextEditor.LineSegment startLine = data.Document.GetLine (lineStart);
			int relCaretOffset = data.Caret.Offset - startLine.Offset;
			
			Mono.TextEditor.LineSegment nextLine = data.Document.GetLine (lineEnd + 1);
			string text = data.Document.GetTextAt (nextLine.Offset, nextLine.EditableLength);
			List<TextMarker> prevLineMarkers = new List<TextMarker> (nextLine.Markers);
			nextLine.ClearMarker ();
			
			for (int i = lineEnd + 1; i >= lineStart; i--) {
				LineSegment cur = data.Document.GetLine (i);
				LineSegment prev = data.Document.GetLine (i - 1);
				data.Replace (cur.Offset, cur.EditableLength, i != lineStart ? data.Document.GetTextAt (prev.Offset, prev.EditableLength) : text);
				data.Document.GetLine (i).ClearMarker ();
				foreach (TextMarker marker in (i != lineStart ? data.Document.GetLine (i - 1).Markers : prevLineMarkers)) {
					data.Document.GetLine (i).AddMarker (marker);
				}
			}
			
			data.Caret.Offset = nextLine.Offset + relCaretOffset;
			if (setSelection)
				data.SetSelection (data.Document.GetLine (lineStart + 1).Offset, data.Document.GetLine (lineEnd + 1).Offset + data.Document.GetLine (lineEnd + 1).EditableLength);
			data.Document.EndAtomicUndo ();
		}
示例#23
0
		public static void SortSelectedLines (TextEditorData data)
		{
			var start = data.MainSelection.Start;
			var end = data.MainSelection.End;
			var caret = data.Caret.Location;

			int startLine = start.Line;
			int endLine = end.Line;
			if (startLine == endLine)
				return;

			int length = 0;
			var lines = new string[endLine - startLine + 1];
			for (int i = startLine; i <= endLine; i++) {
				//get lines *with* line endings
				var lineText = data.GetLineText (i, true);
				lines [i - startLine] = lineText;
				length += lineText.Length;
			}

			var linesUnsorted = new string[lines.Length];

			Array.Sort (lines, StringComparer.Ordinal);

			bool changed = false;
			for (int i = 0; i <= lines.Length; i++) {
				//can't simply use reference comparison as Array.Sort is not stable
				if (string.Equals (lines [i], linesUnsorted [i], StringComparison.Ordinal)) {
					continue;
				}
				changed = true;
				break;
			}
			if (!changed) {
				return;
			}


			var sb = new StringBuilder ();
			for (int i = 0; i < lines.Length; i++) {
				sb.Append (lines [i]);
			}

			var startOffset = data.Document.LocationToOffset (new TextLocation (startLine, 0));
			data.Replace (startOffset, length, sb.ToString ());

			data.Caret.Location = LimitColumn (data, caret);
			data.SetSelection (LimitColumn (data, start), LimitColumn (data, end));
		}
示例#24
0
		public static void IndentSelection (TextEditorData data)
		{
			if (!data.IsSomethingSelected)
				return;
			int startLineNr, endLineNr;
			GetSelectedLines (data, out startLineNr, out endLineNr);
			var anchor = data.MainSelection.Anchor;
			var lead = data.MainSelection.Lead;
			var indentationString = data.Options.IndentationString;
			using (var undo = data.OpenUndoGroup (OperationType.Format)) {
				foreach (DocumentLine line in data.SelectedLines) {
					if (data.Options.IndentStyle == IndentStyle.Virtual && line.Length == 0)
						continue;
					data.Insert (line.Offset, indentationString);
				}
			}
			int chars = indentationString.Length;
			var leadCol = lead.Column > 1 || lead < anchor ? lead.Column + chars : 1;
			var anchorCol = anchor.Column > 1 || anchor < lead ? anchor.Column + chars : 1;
			data.SetSelection (anchor.Line, anchorCol, lead.Line, leadCol);
			data.Document.RequestUpdate (new MultipleLineUpdate (startLineNr, endLineNr));
			data.Document.CommitDocumentUpdate ();
		}
		public virtual void HandleSpecialSelectionKey (TextEditorData textEditorData,uint unicodeKey)
		{
			string start, end;
			GetSelectionSurroundings (textEditorData, unicodeKey, out start, out end);
			
			if (textEditorData.MainSelection.SelectionMode == SelectionMode.Block) {
				var selection = textEditorData.MainSelection;
				int startCol = System.Math.Min (selection.Anchor.Column, selection.Lead.Column) - 1;
				int endCol = System.Math.Max (selection.Anchor.Column, selection.Lead.Column);
				for (int lineNumber = selection.MinLine; lineNumber <= selection.MaxLine; lineNumber++) {
					DocumentLine lineSegment = textEditorData.GetLine (lineNumber);
					
					if (lineSegment.Offset + startCol < lineSegment.EndOffset)
						textEditorData.Insert (lineSegment.Offset + startCol, start);
					if (lineSegment.Offset + endCol < lineSegment.EndOffset)
						textEditorData.Insert (lineSegment.Offset + endCol, end);
				}
				
				textEditorData.MainSelection = new Selection (
					new DocumentLocation (selection.Anchor.Line, endCol == selection.Anchor.Column ? endCol + start.Length : startCol + 1 + start.Length),
					new DocumentLocation (selection.Lead.Line, endCol == selection.Anchor.Column ? startCol + 1 + start.Length : endCol + start.Length),
					Mono.TextEditor.SelectionMode.Block);
				textEditorData.Document.CommitMultipleLineUpdate (textEditorData.MainSelection.MinLine, textEditorData.MainSelection.MaxLine);
			} else {
				int anchorOffset = textEditorData.MainSelection.GetAnchorOffset (textEditorData);
				int leadOffset = textEditorData.MainSelection.GetLeadOffset (textEditorData);
				if (leadOffset < anchorOffset) {
					int tmp = anchorOffset;
					anchorOffset = leadOffset;
					leadOffset = tmp;
				}
				textEditorData.Insert (anchorOffset, start);
				textEditorData.Insert (leadOffset >= anchorOffset ? leadOffset + start.Length : leadOffset, end);
				textEditorData.SetSelection (anchorOffset + start.Length, leadOffset + start.Length);
			}
		}
示例#26
0
		public static void RemoveIndentSelection (TextEditorData data)
		{
			if (!data.IsSomethingSelected)
				return;
			int startLineNr, endLineNr;
			GetSelectedLines (data, out startLineNr, out endLineNr);
			
			using (var undo = data.OpenUndoGroup (OperationType.Format)) {
				var anchor = data.MainSelection.Anchor;
				var lead = data.MainSelection.Lead;
				bool first = true;
				bool removedFromLast = false;
				int removeLast = 0;
				bool removedFromFirst = false;
				int removeFirst = 0;
				foreach (var line in data.SelectedLines) {
					int remove = RemoveTabInLine (data, line);
					removedFromLast |= remove > 0;
					removeLast = remove;
					if (first) {
						removedFromFirst = remove > 0;
						removeFirst = remove;
						first = false;
					}
				}

				var ac = System.Math.Max (DocumentLocation.MinColumn, anchor.Column - (anchor < lead ? removeFirst : removeLast));
				var lc = System.Math.Max (DocumentLocation.MinColumn, lead.Column - (anchor < lead ? removeLast : removeFirst));
				
				if (anchor < lead) {
					if (!removedFromFirst)
						ac = anchor.Column;
					if (!removedFromLast)
						lc = lead.Column;
				} else {
					if (!removedFromFirst)
						lc = lead.Column;
					if (!removedFromLast)
						ac = anchor.Column;
				}
				data.SetSelection (anchor.Line, ac, lead.Line, lc);
			}
			data.Document.RequestUpdate (new MultipleLineUpdate (startLineNr, endLineNr));
			data.Document.CommitDocumentUpdate ();
		}
示例#27
0
		public static void MoveBlockUp (TextEditorData data)
		{
			int lineStart = data.Caret.Line;
			int lineEnd = data.Caret.Line;
			bool setSelection = lineStart != lineEnd;
			if (data.IsSomethingSelected) {
				setSelection = true;
				lineStart = data.MainSelection.MinLine;
				lineEnd = data.MainSelection.MaxLine;
			}
			
			if (lineStart <= 0)
				return;
			
			using (var undo = data.OpenUndoGroup ()) {
				//Mono.TextEditor.LineSegment startLine = data.Document.GetLine (lineStart);
				//int relCaretOffset = data.Caret.Offset - startLine.Offset;
				
				Mono.TextEditor.LineSegment prevLine = data.Document.GetLine (lineStart - 1);
				string text = data.Document.GetTextAt (prevLine.Offset, prevLine.EditableLength);
				List<TextMarker> prevLineMarkers = new List<TextMarker> (prevLine.Markers);
				prevLine.ClearMarker ();
				
				for (int i = lineStart - 1; i <= lineEnd; i++) {
					LineSegment cur = data.Document.GetLine (i);
					LineSegment next = data.Document.GetLine (i + 1);
					data.Replace (cur.Offset, cur.EditableLength, i != lineEnd ? data.Document.GetTextAt (next.Offset, next.EditableLength) : text);
					data.Document.GetLine (i).ClearMarker ();
					foreach (TextMarker marker in (i != lineEnd ? data.Document.GetLine (i + 1).Markers : prevLineMarkers)) {
						data.Document.GetLine (i).AddMarker (marker);
					}
				}
				
				data.Caret.Line--;
				if (setSelection)
					data.SetSelection (data.Document.GetLine (lineStart - 1).Offset, data.Document.GetLine (lineEnd - 1).Offset + data.Document.GetLine (lineEnd - 1).EditableLength);
			}
		}