예제 #1
0
		public override Selection StartSelectionOrSetEndpoint(TextViewPosition startPosition, TextViewPosition endPosition)
		{
			var document = textArea.Document;
			if (document == null)
				throw ThrowUtil.NoDocumentAssigned();
			return Create(textArea, startPosition, endPosition);
		}
예제 #2
0
 public EditorPositionState(double verticalOffset, double horizontalOffset, TextViewPosition textViewPosition, double DesiredXPos)
 {
     this.VerticalOffset = verticalOffset;
     this.HorizontalOffset = horizontalOffset;
     this.TextViewPosition = textViewPosition;
     this.DesiredXPos = DesiredXPos;
 }
예제 #3
0
		static double GetXPos(TextArea textArea, TextViewPosition pos)
		{
			DocumentLine documentLine = textArea.Document.GetLineByNumber(pos.Line);
			VisualLine visualLine = textArea.TextView.GetOrConstructVisualLine(documentLine);
			int vc = visualLine.ValidateVisualColumn(pos, true);
			TextLine textLine = visualLine.GetTextLine(vc);
			return visualLine.GetTextLineVisualXPosition(textLine, vc);
		}
예제 #4
0
		/// <summary>
		/// Creates a new SimpleSelection instance.
		/// </summary>
		internal SimpleSelection(TextArea textArea, TextViewPosition start, TextViewPosition end)
			: base(textArea)
		{
			this.start = start;
			this.end = end;
			this.startOffset = textArea.Document.GetOffset(start.Location);
			this.endOffset = textArea.Document.GetOffset(end.Location);
		}
예제 #5
0
		internal static Selection Create(TextArea textArea, TextViewPosition start, TextViewPosition end)
		{
			if (textArea == null)
				throw new ArgumentNullException("textArea");
			if (textArea.Document.GetOffset(start.Location) == textArea.Document.GetOffset(end.Location) && start.VisualColumn == end.VisualColumn)
				return textArea.emptySelection;
			else
				return new SimpleSelection(textArea, start, end);
		}
예제 #6
0
        internal Caret(TextArea textArea)
        {
            this.textArea = textArea;
            this.textView = textArea.TextView;
            position = new TextViewPosition(1, 1, 0);

            caretAdorner = new CaretLayer(textView);
            textView.InsertLayer(caretAdorner, KnownLayer.Caret, LayerInsertionPosition.Replace);
            textView.VisualLinesChanged += TextView_VisualLinesChanged;
            textView.ScrollOffsetChanged += TextView_ScrollOffsetChanged;
        }
        private static IEnumerable<Tuple<int, int>> GetOffsetForLinesInSegmentOnScreen(TextView textView, ISegment segment,
            bool extendToFullWidthAtLineEnd = false)
        {
            var segmentStart = segment.Offset;
            var segmentEnd = segment.Offset + segment.Length;

            if (segmentStart > textView.TextDocument.TextLength)
            {
                segmentStart = textView.TextDocument.TextLength;
            }

            if (segmentEnd > textView.TextDocument.TextLength)
            {
                segmentEnd = textView.TextDocument.TextLength;
            }

            var start = new TextViewPosition(textView.TextDocument.GetLocation(segmentStart));
            var end = new TextViewPosition(textView.TextDocument.GetLocation(segmentEnd));

            foreach (var line in textView.VisualLines)
            {
                if (!line.DocumentLine.IsDeleted)
                {
                    if (line.Offset > segmentEnd)
                    {
                        break;
                    }

                    if (line.EndOffset < segmentStart)
                    {
                        continue;
                    }

                    // find start and begining in current line.
                    var lineStartOffset = line.Offset;

                    if (segment.Offset > line.Offset)
                    {
                        lineStartOffset = line.Offset + (segment.Offset - line.Offset);
                    }

                    var lineEndOffset = line.EndOffset;

                    if (segment.EndOffset < line.EndOffset)
                    {
                        lineEndOffset = line.EndOffset - (line.EndOffset - segment.EndOffset);
                    }

                    // generate rect for section in this line.
                    yield return new Tuple<int, int>(lineStartOffset, lineEndOffset);
                }
            }
        }
 private RectangleSelection(TextArea textArea, TextViewPosition start, int endLine, double endXPos)
     : base(textArea)
 {
     InitDocument();
     this.startLine = start.Line;
     this.endLine = endLine;
     this.startXPos = GetXPos(textArea, start);
     this.endXPos = endXPos;
     CalculateSegments();
     this.topLeftOffset = this.segments.First().StartOffset;
     this.bottomRightOffset = this.segments.Last().EndOffset;
 }
예제 #9
0
 RectangleSelection(TextArea textArea, int startLine, double startXPos, TextViewPosition end)
     : base(textArea)
 {
     InitDocument();
     this.startLine = startLine;
     endLine = end.Line;
     this.startXPos = startXPos;
     endXPos = GetXPos(textArea, end);
     CalculateSegments();
     topLeftOffset = segments.First().StartOffset;
     bottomRightOffset = segments.Last().EndOffset;
 }
예제 #10
0
 /// <summary>
 /// Creates a new rectangular selection.
 /// </summary>
 public RectangleSelection(TextArea textArea, TextViewPosition start, TextViewPosition end)
     : base(textArea)
 {
     InitDocument();
     startLine = start.Line;
     endLine = end.Line;
     startXPos = GetXPos(textArea, start);
     endXPos = GetXPos(textArea, end);
     CalculateSegments();
     topLeftOffset = segments.First().StartOffset;
     bottomRightOffset = segments.Last().EndOffset;
 }
        public static Rect GetViewPortPosition(TextView textView, int offset)
        {
            var position = new TextViewPosition(textView.GetLocation(offset));

            if (position.Line > 0)
            {
                return GetTextPositionInViewPort(textView, position);
            }
            else
            {
                return new Rect();
            }
        }
        public static string GetWordUnderMouse(this TextDocument document, TextViewPosition position)
        {
            string wordHovered = string.Empty;

            var line = position.Line;
            var column = position.Column;

            var offset = document.GetOffset(line, column);
            if (offset >= document.TextLength)
                offset--;

            var textAtOffset = document.GetText(offset, 1);

            // Get text backward of the mouse position, until the first space
            while (!string.IsNullOrWhiteSpace(textAtOffset))
            {
                wordHovered = textAtOffset + wordHovered;

                offset--;

                if (offset < 0)
                    break;

                textAtOffset = document.GetText(offset, 1);
            }

            // Get text forward the mouse position, until the first space
            offset = document.GetOffset(line, column);
            if (offset < document.TextLength - 1)
            {
                offset++;

                textAtOffset = document.GetText(offset, 1);

                while (!string.IsNullOrWhiteSpace(textAtOffset))
                {
                    wordHovered = wordHovered + textAtOffset;

                    offset++;

                    if (offset >= document.TextLength)
                        break;

                    textAtOffset = document.GetText(offset, 1);
                }
            }

            return wordHovered;
        }
        public static Rect GetTextPositionInViewPort(TextView textView, TextViewPosition position)
        {
            if (position.Line - 1 < textView.VisualLines.Count)
            {
                return new Rect(textView.VisualLines[position.Line - 1].RenderedText.HitTestTextPosition(position.Column - 1).X + textView.TextSurfaceBounds.X,
                    textView.CharSize.Height * (position.Line - 1),
                    textView.CharSize.Width,
                    textView.CharSize.Height);
            }

            return new Rect(textView.TextSurfaceBounds.X + textView.CharSize.Width * (position.Column - 1),
                textView.CharSize.Height * (position.Line - 1),
                textView.CharSize.Width,
                textView.CharSize.Height);
        }
예제 #14
0
파일: Selection.cs 프로젝트: Altaxo/Altaxo
		internal string AddSpacesIfRequired(string newText, TextViewPosition start, TextViewPosition end)
		{
			if (EnableVirtualSpace && InsertVirtualSpaces(newText, start, end)) {
				var line = textArea.Document.GetLineByNumber(start.Line);
				string lineText = textArea.Document.GetText(line);
				var vLine = textArea.TextView.GetOrConstructVisualLine(line);
				int colDiff = start.VisualColumn - vLine.VisualLengthWithEndOfLineMarker;
				if (colDiff > 0) {
					string additionalSpaces = "";
					if (!textArea.Options.ConvertTabsToSpaces && lineText.Trim('\t').Length == 0) {
						int tabCount = (int)(colDiff / textArea.Options.IndentationSize);
						additionalSpaces = new string('\t', tabCount);
						colDiff -= tabCount * textArea.Options.IndentationSize;
					}
					additionalSpaces += new string(' ', colDiff);
					return additionalSpaces + newText;
				}
			}
			return newText;
		}
        public static string GetWordUnderMouse(this TextDocument document, TextViewPosition position, bool cancelDot)
        {
            string wordHovered = string.Empty;
            int line = position.Line;
            int column = position.Column;
            int offset = document.GetOffset(line, column);
            if (offset >= document.TextLength)
                offset--;
            string textAtOffset = document.GetText(offset, 1);

            // Get text backward of the mouse position, until the first space
            while (!string.IsNullOrWhiteSpace(textAtOffset) && !StringContainsBreaks(textAtOffset, true))
            {
                wordHovered = textAtOffset + wordHovered;
                offset--;
                if (offset < 0)
                    break;
                textAtOffset = document.GetText(offset, 1);
            }

            // Get text forward the mouse position, until the first space
            offset = document.GetOffset(line, column);
            if (offset < document.TextLength - 1) {
                offset++;

                textAtOffset = document.GetText(offset, 1);
                while (!string.IsNullOrWhiteSpace(textAtOffset) && !StringContainsBreaks(textAtOffset, false)) {
                    wordHovered = wordHovered + textAtOffset;
                    offset++;
                    if (offset >= document.TextLength)
                        break;
                    textAtOffset = document.GetText(offset, 1);
                }
            }
            return wordHovered;
        }
        /// <inheritdoc/>
        public override void ReplaceSelectionWithText(string newText)
        {
            if (newText == null)
                throw new ArgumentNullException("newText");
            using (textArea.Document.RunUpdate())
            {
                TextViewPosition start = new TextViewPosition(document.GetLocation(topLeftOffset), GetVisualColumnFromXPos(startLine, startXPos));
                TextViewPosition end = new TextViewPosition(document.GetLocation(bottomRightOffset), GetVisualColumnFromXPos(endLine, endXPos));
                int insertionLength;
                int totalInsertionLength = 0;
                int firstInsertionLength = 0;
                int editOffset = Math.Min(topLeftOffset, bottomRightOffset);
                TextViewPosition pos;
                if (NewLineFinder.NextNewLine(newText, 0) == SimpleSegment.Invalid)
                {
                    // insert same text into every line
                    foreach (SelectionSegment lineSegment in this.Segments.Reverse())
                    {
                        ReplaceSingleLineText(textArea, lineSegment, newText, out insertionLength);
                        totalInsertionLength += insertionLength;
                        firstInsertionLength = insertionLength;
                    }

                    int newEndOffset = editOffset + totalInsertionLength;
                    pos = new TextViewPosition(document.GetLocation(editOffset + firstInsertionLength));

                    textArea.Selection = new RectangleSelection(textArea, pos, Math.Max(startLine, endLine), GetXPos(textArea, pos));
                }
                else
                {
                    string[] lines = newText.Split(NewLineFinder.NewlineStrings, segments.Count, StringSplitOptions.None);
                    int line = Math.Min(startLine, endLine);
                    for (int i = lines.Length - 1; i >= 0; i--)
                    {
                        ReplaceSingleLineText(textArea, segments[i], lines[i], out insertionLength);
                        firstInsertionLength = insertionLength;
                    }
                    pos = new TextViewPosition(document.GetLocation(editOffset + firstInsertionLength));
                    textArea.ClearSelection();
                }
                textArea.Caret.Position = textArea.TextView.GetPosition(new Point(GetXPos(textArea, pos), textArea.TextView.GetVisualTopByDocumentLine(Math.Max(startLine, endLine)))).GetValueOrDefault();
            }
        }
예제 #17
0
 /// <summary>
 /// Returns a new selection with the changed end point.
 /// </summary>
 /// <exception cref="NotSupportedException">Cannot set endpoint for empty selection</exception>
 public abstract Selection SetEndpoint(TextViewPosition endPosition);
 /// <summary>
 /// Validates the visual column and returns the correct one.
 /// </summary>
 public int ValidateVisualColumn(TextViewPosition position, bool allowVirtualSpace)
 {
     return(ValidateVisualColumn(Document.GetOffset(position.Location), position.VisualColumn, allowVirtualSpace));
 }
예제 #19
0
 /// <inheritdoc/>
 public override Selection SetEndpoint(TextViewPosition endPosition)
 {
     return(new RectangleSelection(textArea, startLine, startXPos, endPosition));
 }
예제 #20
0
        private void RegisterLanguageService(ISourceFile sourceFile)
        {
            UnRegisterLanguageService();

            if (sourceFile.Project?.Solution != null)
            {
                _snippetManager.InitialiseSnippetsForSolution(sourceFile.Project.Solution);
            }

            if (sourceFile.Project != null)
            {
                _snippetManager.InitialiseSnippetsForProject(sourceFile.Project);
            }

            LanguageService = _shell.LanguageServices.FirstOrDefault(o => o.CanHandle(DocumentAccessor));

            if (LanguageService != null)
            {
                SyntaxHighlighting = CustomHighlightingManager.Instance.GetDefinition(LanguageService.LanguageId.ToUpper());

                LanguageServiceName = LanguageService.Title;

                LanguageService.RegisterSourceFile(DocumentAccessor);

                _diagnosticMarkersRenderer   = new TextMarkerService(Document);
                _textColorizer               = new TextColoringTransformer(Document);
                _scopeLineBackgroundRenderer = new ScopeLineBackgroundRenderer(Document);

                TextArea.TextView.BackgroundRenderers.Add(_scopeLineBackgroundRenderer);
                TextArea.TextView.BackgroundRenderers.Add(_diagnosticMarkersRenderer);
                TextArea.TextView.LineTransformers.Insert(0, _textColorizer);

                _intellisenseManager = new IntellisenseManager(DocumentAccessor, _intellisense, _completionAssistant, LanguageService, sourceFile, offset =>
                {
                    var location = new TextViewPosition(Document.GetLocation(offset));

                    var visualLocation    = TextArea.TextView.GetVisualPosition(location, VisualYPosition.LineBottom);
                    var visualLocationTop = TextArea.TextView.GetVisualPosition(location, VisualYPosition.LineTop);

                    var position = visualLocation - TextArea.TextView.ScrollOffset;
                    position     = position.Transform(TextArea.TextView.TransformToVisual(TextArea).Value);

                    _completionAssistantControl.SetLocation(position);
                });

                _disposables.Add(_intellisenseManager);

                TextArea.IndentationStrategy = LanguageService.IndentationStrategy;

                if (TextArea.IndentationStrategy == null)
                {
                    TextArea.IndentationStrategy = new DefaultIndentationStrategy();
                }

                //LanguageService.Diagnostics?.ObserveOn(AvaloniaScheduler.Instance).Subscribe(d =>
                //{
                //    _diagnosticMarkersRenderer?.SetDiagnostics(d);

                //    Diagnostics = d;

                //    _shell.InvalidateErrors();

                //    TextArea.TextView.Redraw();
                //});
            }
            else
            {
                LanguageService     = null;
                LanguageServiceName = "Plain Text";
            }

            StartBackgroundWorkers();

            Document.TextChanged += TextDocument_TextChanged;

            TextArea.TextEntering += TextArea_TextEntering;

            TextArea.TextEntered += TextArea_TextEntered;

            DoCodeAnalysisAsync().GetAwaiter();
        }
예제 #21
0
        public void TypesWithDelay(int KeyChar, ICSharpCode.AvalonEdit.Document.TextDocument textDocument, TextEditor textEditor, string FileName, int offset)
        {
            //_runnings = true;

            //	this.BeginInvoke(new Action(() =>
            {
                //
                //Point p = Selection.GetCursor();
                //
                //int offset = Caret.Offset;
                //
                //Row r = Document[p.Y];
                //

                if (vp == null)
                {
                    return;
                }

                shouldUpdate = false;

                this.textEditor = textEditor;

                TextViewPosition textViewPosition = new TextViewPosition(textEditor.TextArea.Caret.Location);

                string name = GetWordUnderMouse(textDocument, textViewPosition);

                WordUnderCaret = name;

                //if (WordUnderCaret == "pp")
                //    MessageBox.Show("");

                string names = name;

                if (name == "this.")
                {
                    name         = "this";
                    shouldUpdate = true;
                }
                else
                if (name.EndsWith("."))
                {
                    shouldUpdate = true;
                }
                else if (name.Length == 1)
                {
                    shouldUpdate = true;
                    name         = "";
                }
                else
                if (name.Length > 1 && !name.Contains("."))
                {
                    //sns = new List<ISymbol>();
                    return;
                }
                else
                {
                    name = "";
                }

                // offset = RealOffset(offset);

                List <Microsoft.CodeAnalysis.ISymbol> sn = LoadProjectTypes(vp, FileName, textDocument.Text, offset, name, "");

                sns = sn;

                if (sn.Count <= 0)
                {
                    if (name.Contains("."))
                    {
                        //MessageBox.Show("Check for static class members - " + WordUnderCaret);
                    }
                }

                //ArrayList L = new ArrayList();

                foreach (var b in sn)
                {
                    //if (b.Kind == SymbolKind.Local)
                    //  if (b is INamedTypeSymbol)

                    //        if (((INamedTypeSymbol)b).TypeKind == TypeKind.Class)
                    //            sn.Remove(b);
                }

                {
                    //			if (ins != null)
                    //			{
                    //				ins.Show();
                    //				form.Show();
                    //			}

                    //var c = Selection.GetCaretWords();

                    //string cc = "";
                    //foreach (string s in c)
                    //{
                    //    cc += s;
                    //}
                    //cc = cc.Trim();
                    //if (cc.EndsWith("="))
                    //{
                    //    string[] nc = Selection.GetCaretString().Trim().Split("=".ToCharArray());
                    //    if (nc.Length > 1)
                    //    {
                    //        List<Microsoft.CodeAnalysis.ISymbol> symbols = new List<Microsoft.CodeAnalysis.ISymbol>();
                    //        string data = nc[nc.Length - 2].Trim();
                    //        Microsoft.CodeAnalysis.ISymbol symbol = ins.GetSymbol(data);
                    //        if (symbol is IParameterSymbol)
                    //        {
                    //            IParameterSymbol ps = symbol as IParameterSymbol;
                    //            MessageBox.Show("template = detected for " + symbol.Name + " of type " + ps.Type.Name);
                    //            //Selection.AppendToCaretString(" " + ps.Type.Name);
                    //            symbols.Add(symbol);
                    //        }
                    //        else if (symbol is IPropertySymbol)
                    //        {
                    //            IPropertySymbol ps = symbol as IPropertySymbol;
                    //            MessageBox.Show("template = detected for " + symbol.Name + " of type " + ps.Type.Name);
                    //            //Selection.AppendToCaretString(" " + ps.Type.Name);
                    //            symbols.Add(symbol);
                    //        }
                    //        else if (symbol is IFieldSymbol)
                    //        {
                    //            IFieldSymbol ps = symbol as IFieldSymbol;
                    //            MessageBox.Show("template = detected for " + symbol.Name + " of type " + ps.Type.Name);
                    //            //Selection.AppendToCaretString(" " + ps.Type.Name);
                    //            symbols.Add(symbol);
                    //        }
                    //        else if (symbol != null)
                    //            MessageBox.Show("template = detected for " + symbol.Name);

                    //        ins.LoadFast(symbols);

                    //        //				ins.SetVirtualMode();

                    //        //				ins.AutoSize();

                    //        //				ins.Refresh();

                    //        return;
                    //    }
                    //}
                    //       string[] words = cc.Trim().Split(".".ToCharArray()).Select(s => s).Where(t => t != "").ToArray();

                    //       string name = "";

                    //       Parse(Selection.GetCaretString(), offset);

                    //       if (cc.StartsWith(".this"))
                    //           name = "this";
                    //       else
                    //       if (KeyChar == 8 && words.Length > 1)
                    //       {
                    //           //ins.Hide();
                    //           //form.Hide();
                    //           //if (dfs != null)
                    //           //	dfs.Close();
                    //           return;
                    //       }
                    //       else if (cc.StartsWith("."))
                    //       {
                    //           name = words[0];
                    //           if (/*name.Contains(")") && */name.Contains("("))
                    //           {
                    //               string[] dd = name.Split("(".ToCharArray());

                    //               //name = "." + dd[0];
                    //               name = dd[dd.Length - 1];//dd[0]
                    //           }
                    //       }
                    //       else if (cc.StartsWith("("))
                    //       {
                    //           name = words[0];
                    //           if (name.Contains("("))
                    //               if (name.Contains(")"))
                    //               {
                    //                   string[] dd = name.Split(",".ToCharArray());
                    //                   if (Selection.IsCaretInWord())
                    //                       name = "";
                    //               }
                    //       }
                    //       else if (cc.StartsWith(")"))
                    //       {
                    //           name = words[0];
                    //           if (name.Contains("("))
                    //               if (name.Contains(")"))
                    //               {
                    //                   if (Selection.IsCaretInWord())
                    //                       name = "";
                    //               }
                    //       }
                    //       Point pp = Selection.GetCursor();
                    //       pp.X = pp.X * ActiveViewControl.View.CharWidth;
                    //       pp.Y = (ActiveViewControl.Document[p.Y].VisibleIndex - ActiveViewControl.View.FirstVisibleRow + 1) * ActiveViewControl.View.RowHeight + 3;
                    //  //     form.Location = this.PointToScreen(pp);
                    //       List<Microsoft.CodeAnalysis.ISymbol> sn = ins.LoadProjectTypes(vp, FileName, Document.Text, offset, name, cc);
                    //       if (name.StartsWith("("))
                    //       {
                    //           //ins.Hide();
                    //           //form.Hide();
                    ////           LoadDefinition(sn, this.PointToScreen(pp));
                    //           methods = sn;
                    //           return;
                    //       }
                    //       else if (name.StartsWith(")"))
                    //       {
                    //           //ins.Hide();
                    //           //form.Hide();
                    //           //if (dfs != null)
                    //           //	dfs.Hide();
                    //           return;
                    //       }
                    //       //ins.SetVirtualMode();

                    //         ins.FindSimilarWords(w);

                    //ins.AutoSize();

                    //ins.Refresh();
                }

                //			if (form.Visible == true)
                //				form.SetTopMost();

                //		this.Focus();

                //		_runnings = false;
            }
        }
예제 #22
0
        private void ExecuteMoveLinesCommand(MovementDirection movementDirection)
        {
            var textArea  = TextEditor.TextArea;
            var selection = textArea.Selection;
            var caret     = textArea.Caret;

            var selectionStartPosition = selection.StartPosition;
            var selectionEndPosition   = selection.EndPosition;
            var caretPosition          = caret.Position;
            int caretOffset            = caret.Offset;

            bool advancedPositionCalcualtion = selection is RectangleSelection || !selection.IsEmpty;

            int selectionStartOffset, selectionEndOffset;

            if (advancedPositionCalcualtion)
            {
                if (selectionStartPosition.CompareTo(selectionEndPosition) > 0)
                {
                    var temp = selectionStartPosition;
                    selectionStartPosition = selectionEndPosition;
                    selectionEndPosition   = temp;
                }
                selectionStartOffset = Doc.GetOffset(selectionStartPosition.Location);
                selectionEndOffset   = Doc.GetOffset(selectionEndPosition.Location);
            }
            else
            {
                selectionStartOffset = selectionEndOffset = TextEditor.SelectionStart;
            }

            int selectionLength = selection.Length;

            var startLine = Doc.GetLineByOffset(selectionStartOffset);
            var endLine   = Doc.GetLineByOffset(selectionEndOffset);

            if (selection.IsMultiline && selectionEndOffset == endLine.Offset)
            {
                if (movementDirection == MovementDirection.Down && endLine.TotalLength == 0)
                {
                    return;
                }
                endLine = endLine.PreviousLine;
            }

            if (movementDirection == MovementDirection.Up && startLine.LineNumber == 1)
            {
                return;
            }
            if (movementDirection == MovementDirection.Down && endLine.LineNumber == Doc.LineCount)
            {
                return;
            }

            int    startOffset      = startLine.Offset;
            string primaryText      = Doc.GetText(startOffset, endLine.EndOffset - startOffset);
            string primaryDelimiter = Doc.GetText(endLine.EndOffset, endLine.DelimiterLength);

            var    secondaryLine      = movementDirection == MovementDirection.Up ? startLine.PreviousLine : endLine.NextLine;
            string secondaryText      = Doc.GetText(secondaryLine.Offset, secondaryLine.Length);
            string secondaryDelimiter = Doc.GetText(secondaryLine.EndOffset, secondaryLine.DelimiterLength);

            if (string.IsNullOrEmpty(primaryText + primaryDelimiter) || string.IsNullOrEmpty(secondaryText + secondaryDelimiter))
            {
                return;
            }

            if (movementDirection == MovementDirection.Up)
            {
                string replacementText = primaryText + secondaryDelimiter + secondaryText + primaryDelimiter;
                Doc.Replace(secondaryLine.Offset, replacementText.Length, replacementText);
                int correctionLength = secondaryText.Length + secondaryDelimiter.Length;
                selectionStartOffset -= correctionLength;
                caretOffset          -= correctionLength;
            }
            else
            {
                string replacementText = secondaryText + primaryDelimiter + primaryText + secondaryDelimiter;
                Doc.Replace(startLine.Offset, replacementText.Length, replacementText);
                int correctionLength = secondaryText.Length + primaryDelimiter.Length;
                selectionStartOffset += correctionLength;
                caretOffset          += correctionLength;
            }

            if (advancedPositionCalcualtion)
            {
                var newSelectionStartLocation = Doc.GetLocation(selectionStartOffset);
                int selectionLineOffset       = newSelectionStartLocation.Line - Math.Min(selectionStartPosition.Line, selectionEndPosition.Line);
                selectionStartPosition.Line += selectionLineOffset;
                selectionEndPosition.Line   += selectionLineOffset;
                if (selectionEndPosition.Line > Doc.LineCount)
                {
                    var newLocation = Doc.GetLocation(Doc.TextLength);
                    selectionEndPosition.Line          = newLocation.Line;
                    selectionEndPosition.Column        = newLocation.Column;
                    selectionEndPosition.VisualColumn  = newLocation.Column - 1;
                    selectionEndPosition.IsAtEndOfLine = selectionStartPosition.IsAtEndOfLine; // actual value does not matter; needed for comparison
                }

                if (selectionStartPosition == selectionEndPosition)
                {
                    textArea.ClearSelection();
                }
                else
                {
                    if (selection is RectangleSelection)
                    {
                        textArea.Selection = new RectangleSelection(textArea, selectionStartPosition, selectionEndPosition);
                    }
                    else
                    {
                        textArea.Selection = new SimpleSelection(textArea, selectionStartPosition, selectionEndPosition);
                    }
                }
            }
            else
            {
                TextEditor.SelectionStart  = selectionStartOffset;
                TextEditor.SelectionLength = selectionLength;
            }

            var newCaretLocation = Doc.GetLocation(Math.Min(caretOffset, Doc.TextLength));
            var newCaretPosition = new TextViewPosition(newCaretLocation);

            if (caretPosition.VisualColumn > caretPosition.Column)
            {
                newCaretPosition.VisualColumn = caretPosition.VisualColumn;
            }
            caret.Position = newCaretPosition;
        }
예제 #23
0
        internal static TextViewPosition GetNewCaretPosition(TextView textView, TextViewPosition caretPosition, CaretMovementType direction, bool enableVirtualSpace, ref double desiredXPos)
        {
            switch (direction)
            {
            case CaretMovementType.None:
                return(caretPosition);

            case CaretMovementType.DocumentStart:
                desiredXPos = double.NaN;
                return(new TextViewPosition(0, 0));

            case CaretMovementType.DocumentEnd:
                desiredXPos = double.NaN;
                return(new TextViewPosition(textView.Document.GetLocation(textView.Document.TextLength)));
            }
            DocumentLine caretLine  = textView.Document.GetLineByNumber(caretPosition.Line);
            VisualLine   visualLine = textView.GetOrConstructVisualLine(caretLine);
            TextLine     textLine   = visualLine.GetTextLine(caretPosition.VisualColumn, caretPosition.IsAtEndOfLine);

            switch (direction)
            {
            case CaretMovementType.CharLeft:
                desiredXPos = double.NaN;
                // do not move caret to previous line in virtual space
                if (caretPosition.VisualColumn == 0 && enableVirtualSpace)
                {
                    return(caretPosition);
                }
                return(GetPrevCaretPosition(textView, caretPosition, visualLine, CaretPositioningMode.Normal, enableVirtualSpace));

            case CaretMovementType.Backspace:
                desiredXPos = double.NaN;
                return(GetPrevCaretPosition(textView, caretPosition, visualLine, CaretPositioningMode.EveryCodepoint, enableVirtualSpace));

            case CaretMovementType.CharRight:
                desiredXPos = double.NaN;
                return(GetNextCaretPosition(textView, caretPosition, visualLine, CaretPositioningMode.Normal, enableVirtualSpace));

            case CaretMovementType.WordLeft:
                desiredXPos = double.NaN;
                return(GetPrevCaretPosition(textView, caretPosition, visualLine, CaretPositioningMode.WordStart, enableVirtualSpace));

            case CaretMovementType.WordRight:
                desiredXPos = double.NaN;
                return(GetNextCaretPosition(textView, caretPosition, visualLine, CaretPositioningMode.WordStart, enableVirtualSpace));

            case CaretMovementType.LineUp:
            case CaretMovementType.LineDown:
            case CaretMovementType.PageUp:
            case CaretMovementType.PageDown:
                return(GetUpDownCaretPosition(textView, caretPosition, direction, visualLine, textLine, enableVirtualSpace, ref desiredXPos));

            case CaretMovementType.LineStart:
                desiredXPos = double.NaN;
                return(GetStartOfLineCaretPosition(caretPosition.VisualColumn, visualLine, textLine, enableVirtualSpace));

            case CaretMovementType.LineEnd:
                desiredXPos = double.NaN;
                return(GetEndOfLineCaretPosition(visualLine, textLine));

            default:
                throw new NotSupportedException(direction.ToString());
            }
        }
예제 #24
0
        private void RegisterLanguageService(ISourceFile sourceFile)
        {
            UnRegisterLanguageService();

            if (sourceFile.Project?.Solution != null)
            {
                _snippetManager.InitialiseSnippetsForSolution(sourceFile.Project.Solution);
            }

            if (sourceFile.Project != null)
            {
                _snippetManager.InitialiseSnippetsForProject(sourceFile.Project);
            }

            var contentTypeService = ContentTypeServiceInstance.Instance;

            LanguageService = _shell.LanguageServices.FirstOrDefault(
                o => o.Metadata.TargetCapabilities.Any(
                    c => contentTypeService.CapabilityAppliesToContentType(c, sourceFile.ContentType)))?.Value;

            if (LanguageService != null)
            {
                SyntaxHighlighting = CustomHighlightingManager.Instance.GetDefinition(LanguageService.LanguageId.ToUpper());

                LanguageServiceName = LanguageService.Title;

                LanguageService.RegisterSourceFile(DocumentAccessor);

                _diagnosticMarkersRenderer   = new TextMarkerService(Document);
                _textColorizer               = new TextColoringTransformer(Document);
                _scopeLineBackgroundRenderer = new ScopeLineBackgroundRenderer(Document);

                _contextActionsRenderer = new ContextActionsRenderer(this, _diagnosticMarkersRenderer);
                TextArea.LeftMargins.Add(_contextActionsRenderer);

                foreach (var contextActionProvider in LanguageService.GetContextActionProviders(DocumentAccessor))
                {
                    _contextActionsRenderer.Providers.Add(contextActionProvider);
                }

                TextArea.TextView.BackgroundRenderers.Add(_scopeLineBackgroundRenderer);
                TextArea.TextView.BackgroundRenderers.Add(_diagnosticMarkersRenderer);
                TextArea.TextView.LineTransformers.Insert(0, _textColorizer);

                _intellisenseManager = new IntellisenseManager(DocumentAccessor, _intellisense, _completionAssistant, LanguageService, sourceFile, offset =>
                {
                    var location = new TextViewPosition(Document.GetLocation(offset));

                    var visualLocation    = TextArea.TextView.GetVisualPosition(location, VisualYPosition.LineBottom);
                    var visualLocationTop = TextArea.TextView.GetVisualPosition(location, VisualYPosition.LineTop);

                    var position = visualLocation - TextArea.TextView.ScrollOffset;
                    position     = position.Transform(TextArea.TextView.TransformToVisual(TextArea).Value);

                    _completionAssistantControl.SetLocation(position);
                });

                _disposables.Add(_intellisenseManager);

                TextArea.IndentationStrategy = LanguageService.IndentationStrategy;

                if (TextArea.IndentationStrategy == null)
                {
                    TextArea.IndentationStrategy = new DefaultIndentationStrategy();
                }

                _languageServiceDisposables = new CompositeDisposable
                {
                    Observable.FromEventPattern <DiagnosticsUpdatedEventArgs>(LanguageService, nameof(LanguageService.DiagnosticsUpdated)).ObserveOn(AvaloniaScheduler.Instance).Subscribe(args =>
                                                                                                                                                                                           LanguageService_DiagnosticsUpdated(args.Sender, args.EventArgs))
                };
            }
            else
            {
                LanguageService     = null;
                LanguageServiceName = "Plain Text";
            }

            StartBackgroundWorkers();

            Document.TextChanged += TextDocument_TextChanged;

            TextArea.TextEntering += TextArea_TextEntering;

            TextArea.TextEntered += TextArea_TextEntered;

            DoCodeAnalysisAsync().GetAwaiter();
        }
예제 #25
0
 public override Selection SetEndpoint(TextViewPosition endPosition)
 {
     throw new NotSupportedException();
 }
예제 #26
0
파일: Caret.cs 프로젝트: arkanoid1/Yanitta
        /// <summary>
        /// Validates the visual column of the caret using the specified visual line.
        /// The visual line must contain the caret offset.
        /// </summary>
        void RevalidateVisualColumn(VisualLine visualLine)
        {
            if (visualLine == null)
            {
                throw new ArgumentNullException(nameof(visualLine));
            }

            // mark column as validated
            visualColumnValid = true;

            int caretOffset             = textView.Document.GetOffset(position.Location);
            int firstDocumentLineOffset = visualLine.FirstDocumentLine.Offset;

            position.VisualColumn = visualLine.ValidateVisualColumn(position, textArea.Selection.EnableVirtualSpace);

            // search possible caret positions
            int newVisualColumnForwards = visualLine.GetNextCaretPosition(position.VisualColumn - 1, LogicalDirection.Forward, CaretPositioningMode.Normal, textArea.Selection.EnableVirtualSpace);

            // If position.VisualColumn was valid, we're done with validation.
            if (newVisualColumnForwards != position.VisualColumn)
            {
                // also search backwards so that we can pick the better match
                int newVisualColumnBackwards = visualLine.GetNextCaretPosition(position.VisualColumn + 1, LogicalDirection.Backward, CaretPositioningMode.Normal, textArea.Selection.EnableVirtualSpace);

                if (newVisualColumnForwards < 0 && newVisualColumnBackwards < 0)
                {
                    throw ThrowUtil.NoValidCaretPosition();
                }

                // determine offsets for new visual column positions
                int newOffsetForwards;
                if (newVisualColumnForwards >= 0)
                {
                    newOffsetForwards = visualLine.GetRelativeOffset(newVisualColumnForwards) + firstDocumentLineOffset;
                }
                else
                {
                    newOffsetForwards = -1;
                }
                int newOffsetBackwards;
                if (newVisualColumnBackwards >= 0)
                {
                    newOffsetBackwards = visualLine.GetRelativeOffset(newVisualColumnBackwards) + firstDocumentLineOffset;
                }
                else
                {
                    newOffsetBackwards = -1;
                }

                int newVisualColumn, newOffset;
                // if there's only one valid position, use it
                if (newVisualColumnForwards < 0)
                {
                    newVisualColumn = newVisualColumnBackwards;
                    newOffset       = newOffsetBackwards;
                }
                else if (newVisualColumnBackwards < 0)
                {
                    newVisualColumn = newVisualColumnForwards;
                    newOffset       = newOffsetForwards;
                }
                else
                {
                    // two valid positions: find the better match
                    if (Math.Abs(newOffsetBackwards - caretOffset) < Math.Abs(newOffsetForwards - caretOffset))
                    {
                        // backwards is better
                        newVisualColumn = newVisualColumnBackwards;
                        newOffset       = newOffsetBackwards;
                    }
                    else
                    {
                        // forwards is better
                        newVisualColumn = newVisualColumnForwards;
                        newOffset       = newOffsetForwards;
                    }
                }
                Position = new TextViewPosition(textView.Document.GetLocation(newOffset), newVisualColumn);
            }
            isInVirtualSpace = (position.VisualColumn > visualLine.VisualLength);
        }
예제 #27
0
        private static void SurroundRectangleSelectionWithBlockComment(EdiTextEditor editor,
                                                                       BlockDefinition block,
                                                                       RectangleSelection sel)
        {
            if (sel == null)
            {
                return;
            }

            // Backup current view position of rectangular selection
            int selectionStart  = editor.SelectionStart;
            int selectionLength = editor.SelectionLength;
            int caretOffset     = editor.CaretOffset;

            TextViewPosition startPos = new TextViewPosition(sel.StartPosition);
            TextViewPosition endPos   = new TextViewPosition(sel.EndPosition);

            TextReplaceBlockRegion[] region = new TextReplaceBlockRegion[sel.Segments.Count()];
            bool bFoundNoMatch = true;

            for (int i = sel.Segments.Count() - 1; i >= 0; i--)
            {
                var item = sel.Segments.ElementAt(i);

                // Attempt to find the currently set comment before and after the current selection

                switch (block.TypeOfBlock)
                {
                case BlockDefinition.BlockAt.Start:
                    region[i] = FindSelectedStartCommentRegion(block.StartBlock,
                                                               editor.Document,
                                                               editor.Document.GetText(item),
                                                               item.StartOffset,
                                                               item.Length);
                    break;

                case BlockDefinition.BlockAt.End:
                    region[i] = FindSelectedEndCommentRegion(block.EndBlock,
                                                             editor.Document,
                                                             editor.Document.GetText(item),
                                                             item.StartOffset);
                    break;

                case BlockDefinition.BlockAt.StartAndEnd:
                    region[i] = FindSelectedCommentRegion(block.StartBlock,
                                                          block.EndBlock,
                                                          editor.Document,
                                                          editor.Document.GetText(item),
                                                          item.StartOffset,
                                                          item.Length);
                    break;

                default:
                    throw new NotImplementedException(block.TypeOfBlock.ToString());
                }

                if (region[i] == null)
                {
                    bFoundNoMatch = false;
                }
            }

            if (bFoundNoMatch == true)
            {
                for (int i = sel.Segments.Count() - 1; i >= 0; i--)
                {
                    var item = sel.Segments.ElementAt(i);

                    // Remove the block surround (comment) if there is a match available
                    switch (block.TypeOfBlock)
                    {
                    case BlockDefinition.BlockAt.Start:
                        editor.Document.Remove(region[i].StartOffset, region[i].CommentStart.Length);
                        break;

                    case BlockDefinition.BlockAt.End:
                        editor.Document.Remove(region[i].EndOffset, region[i].CommentEnd.Length);
                        break;

                    case BlockDefinition.BlockAt.StartAndEnd:
                        editor.Document.Remove(region[i].EndOffset, region[i].CommentEnd.Length);
                        editor.Document.Remove(region[i].StartOffset, region[i].CommentStart.Length);
                        break;

                    default:
                        throw new NotImplementedException(block.TypeOfBlock.ToString());
                    }
                }
            }
            else
            {
                for (int i = sel.Segments.Count() - 1; i >= 0; i--)
                {
                    var item = sel.Segments.ElementAt(i);

                    switch (block.TypeOfBlock)
                    {
                    case BlockDefinition.BlockAt.Start:
                        editor.Document.Insert(item.StartOffset, block.StartBlock);
                        break;

                    case BlockDefinition.BlockAt.End:
                        editor.Document.Insert(item.EndOffset, block.EndBlock);
                        break;

                    case BlockDefinition.BlockAt.StartAndEnd: // Insert a new comment since we could not find one ...
                        editor.Document.Insert(item.EndOffset, block.EndBlock);
                        editor.Document.Insert(item.StartOffset, block.StartBlock);
                        break;

                    default:
                        throw new NotImplementedException(block.TypeOfBlock.ToString());
                    }
                }

                // Move original selection to the rigth and apply rectangular selection
                editor.CaretOffset = caretOffset;

                editor.SelectionStart  = selectionStart;
                editor.SelectionLength = selectionLength;

                //startPos.Column += block.StartBlock.Length;
                //endPos.Column += block.StartBlock.Length;

                // Reset selection to keep the text that was originally selected
                editor.TextArea.Selection = new RectangleSelection(editor.TextArea, startPos, endPos);
            }
        }
 /// <inheritdoc/>
 public override Selection StartSelectionOrSetEndpoint(TextViewPosition startPosition, TextViewPosition endPosition)
 {
     return SetEndpoint(endPosition);
 }
        private static IEnumerable<Rect> GetRectsForSegmentImpl(TextView textView, ISegment segment, bool extendToFullWidthAtLineEnd)
        {
            Vector scrollOffset = textView.ScrollOffset;
            int segmentStart = segment.Offset;
            int segmentEnd = segment.Offset + segment.Length;

            segmentStart = segmentStart.CoerceValue(0, textView.Document.TextLength);
            segmentEnd = segmentEnd.CoerceValue(0, textView.Document.TextLength);

            TextViewPosition start;
            TextViewPosition end;

            if (segment is SelectionSegment)
            {
                SelectionSegment sel = (SelectionSegment)segment;
                start = new TextViewPosition(textView.Document.GetLocation(sel.StartOffset), sel.StartVisualColumn);
                end = new TextViewPosition(textView.Document.GetLocation(sel.EndOffset), sel.EndVisualColumn);
            }
            else
            {
                start = new TextViewPosition(textView.Document.GetLocation(segmentStart), -1);
                end = new TextViewPosition(textView.Document.GetLocation(segmentEnd), -1);
            }

            foreach (VisualLine vl in textView.VisualLines)
            {
                int vlStartOffset = vl.FirstDocumentLine.Offset;
                if (vlStartOffset > segmentEnd)
                    break;
                int vlEndOffset = vl.LastDocumentLine.Offset + vl.LastDocumentLine.Length;
                if (vlEndOffset < segmentStart)
                    continue;

                int segmentStartVC;
                if (segmentStart < vlStartOffset)
                    segmentStartVC = 0;
                else
                    segmentStartVC = vl.ValidateVisualColumn(start, extendToFullWidthAtLineEnd);

                int segmentEndVC;
                if (segmentEnd > vlEndOffset)
                    segmentEndVC = extendToFullWidthAtLineEnd ? int.MaxValue : vl.VisualLengthWithEndOfLineMarker;
                else
                    segmentEndVC = vl.ValidateVisualColumn(end, extendToFullWidthAtLineEnd);

                TextLine lastTextLine = vl.TextLines.Last();

                for (int i = 0; i < vl.TextLines.Count; i++)
                {
                    TextLine line = vl.TextLines[i];
                    double y = vl.GetTextLineVisualYPosition(line, VisualYPosition.LineTop);
                    int visualStartCol = vl.GetTextLineVisualStartColumn(line);
                    int visualEndCol = visualStartCol + line.Length;
                    if (line != lastTextLine)
                        visualEndCol -= line.TrailingWhitespaceLength;

                    if (segmentEndVC < visualStartCol)
                        break;
                    if (lastTextLine != line && segmentStartVC > visualEndCol)
                        continue;
                    int segmentStartVCInLine = Math.Max(segmentStartVC, visualStartCol);
                    int segmentEndVCInLine = Math.Min(segmentEndVC, visualEndCol);
                    y -= scrollOffset.Y;
                    if (segmentStartVCInLine == segmentEndVCInLine)
                    {
                        // GetTextBounds crashes for length=0, so we'll handle this case with GetDistanceFromCharacterHit
                        // We need to return a rectangle to ensure empty lines are still visible
                        double pos = vl.GetTextLineVisualXPosition(line, segmentStartVCInLine);
                        pos -= scrollOffset.X;
                        // The following special cases are necessary to get rid of empty rectangles at the end of a TextLine if "Show Spaces" is active.
                        // If not excluded once, the same rectangle is calculated (and added) twice (since the offset could be mapped to two visual positions; end/start of line), if there is no trailing whitespace.
                        // Skip this TextLine segment, if it is at the end of this line and this line is not the last line of the VisualLine and the selection continues and there is no trailing whitespace.
                        if (segmentEndVCInLine == visualEndCol && i < vl.TextLines.Count - 1 && segmentEndVC > segmentEndVCInLine && line.TrailingWhitespaceLength == 0)
                            continue;
                        if (segmentStartVCInLine == visualStartCol && i > 0 && segmentStartVC < segmentStartVCInLine && vl.TextLines[i - 1].TrailingWhitespaceLength == 0)
                            continue;
                        yield return new Rect(pos, y, 1, line.Height);
                    }
                    else
                    {
                        Rect lastRect = Rect.Empty;
                        if (segmentStartVCInLine <= visualEndCol)
                        {
                            foreach (TextBounds b in line.GetTextBounds(segmentStartVCInLine, segmentEndVCInLine - segmentStartVCInLine))
                            {
                                double left = b.Rectangle.Left - scrollOffset.X;
                                double right = b.Rectangle.Right - scrollOffset.X;
                                if (!lastRect.IsEmpty)
                                    yield return lastRect;
                                // left>right is possible in RTL languages
                                lastRect = new Rect(Math.Min(left, right), y, Math.Abs(right - left), line.Height);
                            }
                        }
                        if (segmentEndVC >= vl.VisualLengthWithEndOfLineMarker)
                        {
                            double left = (segmentStartVC > vl.VisualLengthWithEndOfLineMarker ? vl.GetTextLineVisualXPosition(lastTextLine, segmentStartVC) : line.Width) - scrollOffset.X;
                            double right = ((segmentEndVC == int.MaxValue || line != lastTextLine) ? Math.Max(((IScrollInfo)textView).ExtentWidth, ((IScrollInfo)textView).ViewportWidth) : vl.GetTextLineVisualXPosition(lastTextLine, segmentEndVC)) - scrollOffset.X;
                            Rect extendSelection = new Rect(Math.Min(left, right), y, Math.Abs(right - left), line.Height);
                            if (!lastRect.IsEmpty)
                            {
                                if (extendSelection.IntersectsWith(lastRect))
                                {
                                    lastRect.Union(extendSelection);
                                    yield return lastRect;
                                }
                                else
                                {
                                    yield return lastRect;
                                    yield return extendSelection;
                                }
                            }
                            else
                                yield return extendSelection;
                        }
                        else
                            yield return lastRect;
                    }
                }
            }
        }
예제 #30
0
        /// <inheritdoc/>
        protected override void OnLoad()
        {
            // Store caret positions before reload.
            var caretPositions = new TextViewPosition[ViewModels.Count];

            for (int i = 0; i < ViewModels.Count; i++)
            {
                caretPositions[i] = ((TextDocumentViewModel)ViewModels[i]).TextEditor.TextArea.Caret.Position;
            }

            string text;

            using (var fileStream = FileReader.OpenFile(Uri.LocalPath, UTF8NoBOM))
            {
                _encoding = fileStream.CurrentEncoding;
                text      = fileStream.ReadToEnd();
            }

            // Check for binary files.
            // See http://stackoverflow.com/questions/910873/how-can-i-determine-if-a-file-is-binary-or-text-in-c
            foreach (char c in text)
            {
                if (char.IsControl(c) && c != '\t' && c != '\n' && c != '\r')
                {
                    // Binary file detected. Cancel?
                    var result = MessageBox.Show(
                        "Loading file: \"" + this.GetName() + "\"\n\n" +
                        "Unsupported file format. Do you want to open the file as a text document?\n\n" +
                        "Warning: Binary files can slow down the text editor.",
                        Editor.ApplicationName,
                        MessageBoxButton.OKCancel,
                        MessageBoxImage.Warning);

                    if (result == MessageBoxResult.Cancel)
                    {
                        throw new OperationCanceledException();
                    }

                    break;
                }
            }

            AvalonEditDocument.Text = text;
            AvalonEditDocument.UndoStack.ClearAll();
            AvalonEditDocument.UndoStack.MarkAsOriginalFile();

            _fileInfo = new FileInfo(Uri.LocalPath);

            // Update syntax-highlighting mode of all views.
            foreach (var view in ViewModels.OfType <TextDocumentViewModel>())
            {
                view.UpdateSyntaxHighlighting();
            }

            // Restore caret position
            for (int i = 0; i < ViewModels.Count; i++)
            {
                ((TextDocumentViewModel)ViewModels[i]).TextEditor.TextArea.Caret.Position = caretPositions[i];
            }

            BeginInvokeUpdateProperties();
        }
        /// <summary>
        /// Positions the completion window at the specified position.
        /// </summary>
        protected void SetPosition(TextViewPosition position)
        {
            TextView textView = this.TextArea.TextView;

            visualLocation = textView.GetVisualPosition(position, VisualYPosition.LineBottom);
            visualLocationTop = textView.GetVisualPosition(position, VisualYPosition.LineTop);
            UpdatePosition();
        }
예제 #32
0
        public CodeEditor()
        {
            _codeAnalysisRunner = new JobRunner(1);

            _shell = IoC.Get <IShell>();

            _snippetManager = IoC.Get <SnippetManager>();

            _lineNumberMargin = new LineNumberMargin(this);

            _breakpointMargin = new BreakPointMargin(this, IoC.Get <IDebugManager2>().Breakpoints);

            _selectedLineBackgroundRenderer = new SelectedLineBackgroundRenderer(this);

            _selectedWordBackgroundRenderer = new SelectedWordBackgroundRenderer();

            _columnLimitBackgroundRenderer = new ColumnLimitBackgroundRenderer();

            _selectedDebugLineBackgroundRenderer = new SelectedDebugLineBackgroundRenderer();

            TextArea.TextView.Margin = new Thickness(10, 0, 0, 0);

            TextArea.TextView.BackgroundRenderers.Add(_selectedDebugLineBackgroundRenderer);
            TextArea.TextView.LineTransformers.Add(_selectedDebugLineBackgroundRenderer);

            TextArea.SelectionBrush        = Brush.Parse("#AA569CD6");
            TextArea.SelectionCornerRadius = 0;

            this.GetObservable(LineNumbersVisibleProperty).Subscribe(s =>
            {
                if (s)
                {
                    TextArea.LeftMargins.Add(_lineNumberMargin);
                }
                else
                {
                    TextArea.LeftMargins.Remove(_lineNumberMargin);
                }
            });

            this.GetObservable(ShowBreakpointsProperty).Subscribe(s =>
            {
                if (s)
                {
                    TextArea.LeftMargins.Insert(0, _breakpointMargin);
                }
                else
                {
                    TextArea.LeftMargins.Remove(_breakpointMargin);
                }
            });

            this.GetObservable(HighlightSelectedWordProperty).Subscribe(s =>
            {
                if (s)
                {
                    TextArea.TextView.BackgroundRenderers.Add(_selectedWordBackgroundRenderer);
                }
                else
                {
                    TextArea.TextView.BackgroundRenderers.Remove(_selectedWordBackgroundRenderer);
                }
            });

            this.GetObservable(HighlightSelectedLineProperty).Subscribe(s =>
            {
                if (s)
                {
                    TextArea.TextView.BackgroundRenderers.Insert(0, _selectedLineBackgroundRenderer);
                }
                else
                {
                    TextArea.TextView.BackgroundRenderers.Remove(_selectedLineBackgroundRenderer);
                }
            });

            this.GetObservable(ShowColumnLimitProperty).Subscribe(s =>
            {
                if (s)
                {
                    TextArea.TextView.BackgroundRenderers.Add(_columnLimitBackgroundRenderer);
                }
                else
                {
                    TextArea.TextView.BackgroundRenderers.Remove(_columnLimitBackgroundRenderer);
                }
            });

            Options = new AvaloniaEdit.TextEditorOptions
            {
                ConvertTabsToSpaces = true,
                IndentationSize     = 4
            };

            BackgroundRenderersProperty.Changed.Subscribe(s =>
            {
                if (s.Sender == this)
                {
                    if (s.OldValue != null)
                    {
                        foreach (var renderer in (ObservableCollection <IBackgroundRenderer>)s.OldValue)
                        {
                            TextArea.TextView.BackgroundRenderers.Remove(renderer);
                        }
                    }

                    if (s.NewValue != null)
                    {
                        foreach (var renderer in (ObservableCollection <IBackgroundRenderer>)s.NewValue)
                        {
                            TextArea.TextView.BackgroundRenderers.Add(renderer);
                        }
                    }
                }
            });

            DocumentLineTransformersProperty.Changed.Subscribe(s =>
            {
                if (s.Sender == this)
                {
                    if (s.OldValue != null)
                    {
                        foreach (var renderer in (ObservableCollection <IVisualLineTransformer>)s.OldValue)
                        {
                            TextArea.TextView.LineTransformers.Remove(renderer);
                        }
                    }

                    if (s.NewValue != null)
                    {
                        foreach (var renderer in (ObservableCollection <IVisualLineTransformer>)s.NewValue)
                        {
                            TextArea.TextView.LineTransformers.Add(renderer);
                        }
                    }
                }
            });

            this.GetObservable(CaretOffsetProperty).Subscribe(s =>
            {
                if (Document?.TextLength > s)
                {
                    CaretOffset = s;
                }
            });

            /*_analysisTriggerEvents.Select(_ => Observable.Timer(TimeSpan.FromMilliseconds(300)).ObserveOn(AvaloniaScheduler.Instance)
             * .SelectMany(o => DoCodeAnalysisAsync())).Switch().Subscribe(_ => { });*/

            _analysisTriggerEvents.Throttle(TimeSpan.FromMilliseconds(300)).ObserveOn(AvaloniaScheduler.Instance).Subscribe(async _ =>
            {
                await DoCodeAnalysisAsync();
            });

            this.GetObservableWithHistory(SourceFileProperty).Subscribe((file) =>
            {
                if (file.Item1 != file.Item2)
                {
                    if (System.IO.File.Exists(file.Item2.Location))
                    {
                        using (var fs = System.IO.File.OpenText(file.Item2.Location))
                        {
                            Document = new TextDocument(fs.ReadToEnd())
                            {
                                FileName = file.Item2.Location
                            };
                        }
                    }

                    _isLoaded = true;

                    RegisterLanguageService(file.Item2);

                    TextArea.TextView.Redraw();
                }
            });

            TextArea.TextEntering += (sender, e) =>
            {
                _textEntering = true;
            };

            TextArea.Caret.PositionChanged += (sender, e) =>
            {
                if (_intellisenseManager != null && !_textEntering)
                {
                    if (TextArea.Selection.IsEmpty)
                    {
                        var location = Document.GetLocation(CaretOffset);
                        _intellisenseManager.SetCursor(CaretOffset, location.Line, location.Column, UnsavedFiles.ToList());
                    }
                    else
                    {
                        var offset = Document.GetOffset(TextArea.Selection.StartPosition.Location);
                        _intellisenseManager.SetCursor(offset, TextArea.Selection.StartPosition.Line, TextArea.Selection.StartPosition.Column, UnsavedFiles.ToList());
                    }
                }

                if (CaretOffset > 0)
                {
                    var prevLocation = new TextViewPosition(Document.GetLocation(CaretOffset - 1));

                    var visualLocation    = TextArea.TextView.GetVisualPosition(prevLocation, VisualYPosition.LineBottom);
                    var visualLocationTop = TextArea.TextView.GetVisualPosition(prevLocation, VisualYPosition.LineTop);

                    var position = visualLocation - TextArea.TextView.ScrollOffset;
                    position = position.Transform(TextArea.TextView.TransformToVisual(TextArea).Value);

                    _intellisenseControl.SetLocation(position);
                    _completionAssistantControl.SetLocation(position);

                    _selectedWordBackgroundRenderer.SelectedWord = GetWordAtOffset(CaretOffset);

                    Line              = TextArea.Caret.Line;
                    Column            = TextArea.Caret.Column;
                    EditorCaretOffset = TextArea.Caret.Offset;
                }
            };

            TextArea.TextEntered += (sender, e) =>
            {
                _intellisenseManager?.OnTextInput(e, CaretOffset, TextArea.Caret.Line, TextArea.Caret.Column);
                _textEntering = false;
            };

            _intellisense = new IntellisenseViewModel();

            _completionAssistant = new CompletionAssistantViewModel(_intellisense);

            EventHandler <KeyEventArgs> tunneledKeyUpHandler = (send, ee) =>
            {
                if (CaretOffset > 0)
                {
                    _intellisenseManager?.OnKeyUp(ee, CaretOffset, TextArea.Caret.Line, TextArea.Caret.Column);
                }
            };

            EventHandler <KeyEventArgs> tunneledKeyDownHandler = (send, ee) =>
            {
                if (CaretOffset > 0)
                {
                    _intellisenseManager?.OnKeyDown(ee, CaretOffset, TextArea.Caret.Line, TextArea.Caret.Column);

                    if (ee.Key == Key.Tab && _currentSnippetContext == null && LanguageService != null)
                    {
                        var wordStart = Document.FindPrevWordStart(CaretOffset);

                        if (wordStart > 0)
                        {
                            string word = Document.GetText(wordStart, CaretOffset - wordStart);

                            var codeSnippet = _snippetManager.GetSnippet(LanguageService, SourceFile.Project?.Solution, SourceFile.Project, word);

                            if (codeSnippet != null)
                            {
                                var snippet = SnippetParser.Parse(LanguageService, CaretOffset, TextArea.Caret.Line, TextArea.Caret.Column, codeSnippet.Snippet);

                                _intellisenseManager.CloseIntellisense();

                                using (Document.RunUpdate())
                                {
                                    Document.Remove(wordStart, CaretOffset - wordStart);

                                    _intellisenseManager.IncludeSnippets = false;
                                    _currentSnippetContext = snippet.Insert(TextArea);
                                }

                                if (_currentSnippetContext.ActiveElements.Count() > 0)
                                {
                                    IDisposable disposable = null;

                                    disposable = Observable.FromEventPattern <SnippetEventArgs>(_currentSnippetContext, nameof(_currentSnippetContext.Deactivated)).Take(1).Subscribe(o =>
                                    {
                                        _currentSnippetContext = null;
                                        _intellisenseManager.IncludeSnippets = true;

                                        disposable.Dispose();
                                    });
                                }
                                else
                                {
                                    _currentSnippetContext = null;
                                    _intellisenseManager.IncludeSnippets = true;
                                }
                            }
                        }
                    }
                }
            };

            EventHandler <TextInputEventArgs> tunneledTextInputHandler = (send, ee) =>
            {
                if (CaretOffset > 0)
                {
                    _intellisenseManager?.OnTextInput(ee, CaretOffset, TextArea.Caret.Line, TextArea.Caret.Column);
                }
            };

            AddHandler(KeyDownEvent, tunneledKeyDownHandler, RoutingStrategies.Tunnel);
            AddHandler(KeyUpEvent, tunneledKeyUpHandler, RoutingStrategies.Tunnel);
        }
예제 #33
0
			public RestoreCaretAndSelectionUndoAction(TextArea textArea)
			{
				this.textAreaReference = new WeakReference(textArea);
				// Just save the old caret position, no need to validate here.
				// If we restore it, we'll validate it anyways.
				this.caretPosition = textArea.Caret.NonValidatedPosition;
				this.selection = textArea.Selection;
			}
예제 #34
0
 public string GetCurrentWord(TextViewPosition pos)
 {
     return(string.Empty);
 }
예제 #35
0
 /// <inheritdoc/>
 public override Selection StartSelectionOrSetEndpoint(TextViewPosition startPosition, TextViewPosition endPosition)
 {
     return(SetEndpoint(endPosition));
 }
예제 #36
0
 /// <inheritdoc/>
 public override Selection SetEndpoint(TextViewPosition endPosition)
 {
     return(Create(textArea, start, endPosition));
 }
예제 #37
0
        private void _move(bool up)
        {
            int  lineStart = _textArea.Selection.StartPosition.Line;
            int  lineEnd   = _textArea.Selection.EndPosition.Line;
            bool reselect  = true;

            TextViewPosition posStart = new TextViewPosition(_textArea.Selection.StartPosition.Location);
            TextViewPosition posEnd   = new TextViewPosition(_textArea.Selection.EndPosition.Location);

            if (_textArea.Document.GetOffset(posStart.Location) > _textArea.Document.GetOffset(posEnd.Location))
            {
                TextViewPosition t = posEnd;
                posEnd   = posStart;
                posStart = t;
                int t2 = lineEnd;
                lineEnd   = lineStart;
                lineStart = t2;
            }

            if (up)
            {
                posStart.Line--;
                posEnd.Line--;
            }
            else
            {
                posStart.Line++;
                posEnd.Line++;
            }

            if (_textArea.Selection.GetText() == "")
            {
                lineStart = _textArea.Caret.Line;
                lineEnd   = _textArea.Caret.Line;
                reselect  = false;
            }

            List <DocumentLine> lines = new List <DocumentLine>();

            if (up && lineStart == 1)
            {
                return;
            }
            if (!up && lineEnd == _textArea.Document.LineCount)
            {
                return;
            }

            if (up)
            {
                for (int i = lineStart - 1; i <= lineEnd; i++)
                {
                    lines.Add(_textArea.Document.GetLineByNumber(i));
                }
            }
            else
            {
                for (int i = lineStart; i <= lineEnd + 1; i++)
                {
                    lines.Add(_textArea.Document.GetLineByNumber(i));
                }
            }

            if (lines.Count > 0)
            {
                int caretLine = _textArea.Caret.Line + (up ? -1 : 1);
                int caretPos  = _textArea.Caret.Column;

                _textArea.Selection = Selection.Create(_textArea, lines[0].Offset, lines.Last().Offset + lines.Last().TotalLength);

                if (up)
                {
                    using (_textArea.Document.RunUpdate()) {
                        var start = Selection.Create(_textArea, lines[1].Offset, lines.Last().Offset + lines.Last().TotalLength).GetText();
                        var end   = Selection.Create(_textArea, lines[0].Offset, lines[0].Offset + lines[0].TotalLength).GetText();

                        if (!start.EndsWith("\r\n"))
                        {
                            start += "\r\n";
                            end    = end.Substring(0, end.Length - 2);
                        }

                        var newText = start + end;
                        _textArea.Selection.ReplaceSelectionWithText(newText);
                    }
                }
                else
                {
                    using (_textArea.Document.RunUpdate()) {
                        var start = Selection.Create(_textArea, lines[lines.Count - 1].Offset, lines[lines.Count - 1].Offset + lines[lines.Count - 1].TotalLength).GetText();
                        var end   = Selection.Create(_textArea, lines[0].Offset, lines[lines.Count - 2].Offset + lines[lines.Count - 2].TotalLength).GetText();

                        if (!start.EndsWith("\r\n"))
                        {
                            start += "\r\n";
                            end    = end.Substring(0, end.Length - 2);
                        }

                        var newText = start + end;
                        _textArea.Selection.ReplaceSelectionWithText(newText);
                    }
                }

                _textArea.Caret.Line   = caretLine;
                _textArea.Caret.Column = caretPos;

                if (reselect)
                {
                    _textArea.Selection = Selection.Create(_textArea, _textArea.Document.GetOffset(posStart.Location), _textArea.Document.GetOffset(posEnd.Location));
                }

                _textArea.Caret.BringCaretToView();
                //_textEditor.ScrollToLine(caretLine);
            }
        }
예제 #38
0
        public override Selection StartSelectionOrSetEndpoint(TextViewPosition startPosition, TextViewPosition endPosition)
        {
            var document = textArea.Document;

            if (document == null)
            {
                throw ThrowUtil.NoDocumentAssigned();
            }
            return(Create(textArea, start, endPosition));
        }
예제 #39
0
 bool IsInVirtualSpace(TextViewPosition pos)
 {
     return(pos.VisualColumn > textArea.TextView.GetOrConstructVisualLine(textArea.Document.GetLineByNumber(pos.Line)).VisualLength);
 }
예제 #40
0
        private static IEnumerable <Rect> GetRectsForSegmentImpl(TextView textView, ISegment segment, bool extendToFullWidthAtLineEnd)
        {
            int segmentStart = segment.Offset;
            int segmentEnd   = segment.Offset + segment.Length;

            segmentStart = segmentStart.CoerceValue(0, textView.Document.TextLength);
            segmentEnd   = segmentEnd.CoerceValue(0, textView.Document.TextLength);

            TextViewPosition start;
            TextViewPosition end;

            if (segment is SelectionSegment)
            {
                SelectionSegment sel = (SelectionSegment)segment;
                start = new TextViewPosition(textView.Document.GetLocation(sel.StartOffset), sel.StartVisualColumn);
                end   = new TextViewPosition(textView.Document.GetLocation(sel.EndOffset), sel.EndVisualColumn);
            }
            else
            {
                start = new TextViewPosition(textView.Document.GetLocation(segmentStart));
                end   = new TextViewPosition(textView.Document.GetLocation(segmentEnd));
            }

            foreach (VisualLine vl in textView.VisualLines)
            {
                int vlStartOffset = vl.FirstDocumentLine.Offset;
                if (vlStartOffset > segmentEnd)
                {
                    break;
                }
                int vlEndOffset = vl.LastDocumentLine.Offset + vl.LastDocumentLine.Length;
                if (vlEndOffset < segmentStart)
                {
                    continue;
                }

                int segmentStartVC;
                if (segmentStart < vlStartOffset)
                {
                    segmentStartVC = 0;
                }
                else
                {
                    segmentStartVC = vl.ValidateVisualColumn(start, extendToFullWidthAtLineEnd);
                }

                int segmentEndVC;
                if (segmentEnd > vlEndOffset)
                {
                    segmentEndVC = extendToFullWidthAtLineEnd ? int.MaxValue : vl.VisualLengthWithEndOfLineMarker;
                }
                else
                {
                    segmentEndVC = vl.ValidateVisualColumn(end, extendToFullWidthAtLineEnd);
                }

                foreach (var rect in ProcessTextLines(textView, vl, segmentStartVC, segmentEndVC))
                {
                    yield return(rect);
                }
            }
        }
예제 #41
0
 /// <summary>
 /// If this selection is empty, starts a new selection from <paramref name="startPosition"/> to
 /// <paramref name="endPosition"/>, otherwise, changes the endpoint of this selection.
 /// </summary>
 public abstract Selection StartSelectionOrSetEndpoint(TextViewPosition startPosition, TextViewPosition endPosition);
예제 #42
0
 public RestoreCaretAndSelectionUndoAction(TextArea textArea)
 {
     this.textAreaReference = new WeakReference(textArea);
     this.caretPosition     = textArea.Caret.Position;
     this.selection         = textArea.Selection;
 }
 /// <inheritdoc/>
 public override Selection SetEndpoint(TextViewPosition endPosition)
 {
     return new RectangleSelection(textArea, startLine, startXPos, endPosition);
 }
예제 #44
0
 /// <summary>
 /// If this selection is empty, starts a new selection from <paramref name="startPosition"/> to
 /// <paramref name="endPosition"/>, otherwise, changes the endpoint of this selection.
 /// </summary>
 public abstract Selection StartSelectionOrSetEndpoint(TextViewPosition startPosition, TextViewPosition endPosition);
예제 #45
0
        /// <summary>
        ///     Gets the word under mouse.
        /// </summary>
        /// <param name="document">The document.</param>
        /// <param name="position">The position.</param>
        /// <returns></returns>
        public static string GetWordUnderMouse(this TextDocument document, TextViewPosition position)
        {
            string wordHovered = string.Empty;

            var line   = position.Line;
            var column = position.Column;

            var offset = document.GetOffset(line, column);

            if (offset >= document.TextLength)
            {
                offset--;
            }

            if (offset < 0)
            {
                return(string.Empty);
            }

            var textAtOffset = document.GetText(offset, 1);

            /////
            // Get text backward of the mouse position, until the first space
            /////
            while (!textAtOffset.IsWordBreakCharacter( ))
            {
                wordHovered = textAtOffset + wordHovered;

                offset--;

                if (offset < 0)
                {
                    break;
                }

                textAtOffset = document.GetText(offset, 1);
            }

            /////
            // Get text forward the mouse position, until the first space
            /////
            offset = document.GetOffset(line, column);
            if (offset < document.TextLength - 1)
            {
                offset++;

                textAtOffset = document.GetText(offset, 1);

                while (!textAtOffset.IsWordBreakCharacter( ))
                {
                    wordHovered = wordHovered + textAtOffset;

                    offset++;

                    if (offset >= document.TextLength)
                    {
                        break;
                    }

                    textAtOffset = document.GetText(offset, 1);
                }
            }

            return(wordHovered);
        }
예제 #46
0
 bool IsInVirtualSpace(TextViewPosition pos)
 {
     return pos.VisualColumn > textArea.TextView.GetOrConstructVisualLine(textArea.Document.GetLineByNumber(pos.Line)).VisualLength;
 }
예제 #47
0
		static TextViewPosition GetPrevCaretPosition(TextView textView, TextViewPosition caretPosition, VisualLine visualLine, CaretPositioningMode mode, bool enableVirtualSpace) {
			int pos = visualLine.GetNextCaretPosition(caretPosition.VisualColumn, LogicalDirection.Backward, mode, enableVirtualSpace);
			if (pos >= 0)
				return visualLine.GetTextViewPosition(pos);
			var previousDocumentLine = visualLine.FirstDocumentLine.PreviousLine;
			if (previousDocumentLine != null) {
				var previousLine = textView.GetOrConstructVisualLine(previousDocumentLine);
				pos = previousLine.GetNextCaretPosition(previousLine.VisualLength + 1, LogicalDirection.Backward, mode, enableVirtualSpace);
				return previousLine.GetTextViewPosition(pos);
			}
			return new TextViewPosition(0, 0);
		}
예제 #48
0
 /// <summary>
 /// Initializes a new instance of the <see cref=""/> class.
 /// </summary>
 /// <param name="a">The a<see cref="TextViewPosition"/>.</param>
 /// <param name="b">The b<see cref="TextViewPosition"/>.</param>
 /// <param name="text">The text<see cref="string"/>.</param>
 public TextViewSelection(TextViewPosition a, TextViewPosition b, string text)
 {
     StartPosition = TextViewPosition.Min(a, b);
     EndPosition   = TextViewPosition.Max(a, b);
     Text          = text;
 }
예제 #49
0
        private void ShowFloatingToolBar()
        {
            // Find the screen position of the start of the selection
            var selectionStartLocation = Editor.Document.GetLocation(Editor.SelectionStart);
            var selectionStartPosition = new TextViewPosition(selectionStartLocation);
            var selectionStartPoint = Editor.TextArea.TextView.GetVisualPosition(selectionStartPosition, VisualYPosition.LineTop);

            var popupPoint = new Point(
                selectionStartPoint.X + 30,
                selectionStartPoint.Y - 35);

            floatingToolBar.Show(Editor, popupPoint);
        }
        private RectangleSelection(TextArea textArea, int startLine, double startXPos, TextViewPosition end)
            : base(textArea)
        {
            InitDocument();
            _startLine = startLine;
            _endLine   = end.Line;
            _startXPos = startXPos;
            _endXPos   = GetXPos(textArea, end);
            CalculateSegments();
            _topLeftOffset     = _segments.First().StartOffset;
            _bottomRightOffset = _segments.Last().EndOffset;

            StartPosition = GetStart();
            EndPosition   = end;
        }
예제 #51
0
 /// <summary>
 /// Validates the visual column and returns the correct one.
 /// </summary>
 public int ValidateVisualColumn(TextViewPosition position, bool allowVirtualSpace)
 {
     return ValidateVisualColumn(Document.GetOffset(position.Location), position.VisualColumn, allowVirtualSpace);
 }
        static TextViewPosition GetUpDownCaretPosition(TextView textView, TextViewPosition caretPosition, CaretMovementType direction, VisualLine visualLine, TextLine textLine, bool enableVirtualSpace, ref double xPos)
        {
            // moving up/down happens using the desired visual X position
            if (double.IsNaN(xPos))
            {
                xPos = visualLine.GetTextLineVisualXPosition(textLine, caretPosition.VisualColumn);
            }
            // now find the TextLine+VisualLine where the caret will end up in
            VisualLine targetVisualLine = visualLine;
            TextLine   targetLine;
            int        textLineIndex = visualLine.TextLines.IndexOf(textLine);

            switch (direction)
            {
            case CaretMovementType.LineUp:
            {
                // Move up: move to the previous TextLine in the same visual line
                // or move to the last TextLine of the previous visual line
                int prevLineNumber = visualLine.FirstDocumentLine.LineNumber - 1;
                if (textLineIndex > 0)
                {
                    targetLine = visualLine.TextLines[textLineIndex - 1];
                }
                else if (prevLineNumber >= 1)
                {
                    DocumentLine prevLine = textView.Document.GetLineByNumber(prevLineNumber);
                    targetVisualLine = textView.GetOrConstructVisualLine(prevLine);
                    targetLine       = targetVisualLine.TextLines[targetVisualLine.TextLines.Count - 1];
                }
                else
                {
                    targetLine = null;
                }
                break;
            }

            case CaretMovementType.LineDown:
            {
                // Move down: move to the next TextLine in the same visual line
                // or move to the first TextLine of the next visual line
                int nextLineNumber = visualLine.LastDocumentLine.LineNumber + 1;
                if (textLineIndex < visualLine.TextLines.Count - 1)
                {
                    targetLine = visualLine.TextLines[textLineIndex + 1];
                }
                else if (nextLineNumber <= textView.Document.LineCount)
                {
                    DocumentLine nextLine = textView.Document.GetLineByNumber(nextLineNumber);
                    targetVisualLine = textView.GetOrConstructVisualLine(nextLine);
                    targetLine       = targetVisualLine.TextLines[0];
                }
                else
                {
                    targetLine = null;
                }
                break;
            }

            case CaretMovementType.PageUp:
            case CaretMovementType.PageDown:
            {
                // Page up/down: find the target line using its visual position
                double yPos = visualLine.GetTextLineVisualYPosition(textLine, VisualYPosition.LineMiddle);
                if (direction == CaretMovementType.PageUp)
                {
                    yPos -= textView.RenderSize.Height;
                }
                else
                {
                    yPos += textView.RenderSize.Height;
                }
                DocumentLine newLine = textView.GetDocumentLineByVisualTop(yPos);
                targetVisualLine = textView.GetOrConstructVisualLine(newLine);
                targetLine       = targetVisualLine.GetTextLineByVisualYPosition(yPos);
                break;
            }

            default:
                throw new NotSupportedException(direction.ToString());
            }
            if (targetLine != null)
            {
                double yPos            = targetVisualLine.GetTextLineVisualYPosition(targetLine, VisualYPosition.LineMiddle);
                int    newVisualColumn = targetVisualLine.GetVisualColumn(new Point(xPos, yPos), enableVirtualSpace);

                // prevent wrapping to the next line; TODO: could 'IsAtEnd' help here?
                int targetLineStartCol = targetVisualLine.GetTextLineVisualStartColumn(targetLine);
                if (newVisualColumn >= targetLineStartCol + targetLine.Length)
                {
                    if (newVisualColumn <= targetVisualLine.VisualLength)
                    {
                        newVisualColumn = targetLineStartCol + targetLine.Length - 1;
                    }
                }
                return(targetVisualLine.GetTextViewPosition(newVisualColumn));
            }
            else
            {
                return(caretPosition);
            }
        }
예제 #53
0
 static Rect GetCharacterBounds(this TextView textView, TextViewPosition pos, HwndSource source)
 {
     VisualLine vl = textView.GetVisualLine(pos.Line);
     if (vl == null)
         return EMPTY_RECT;
     // this may happen during layout changes in AvalonDock, so we just return an empty rectangle
     // in those cases. It should be refreshed immediately.
     if (source.RootVisual == null || !source.RootVisual.IsAncestorOf(textView))
         return EMPTY_RECT;
     TextLine line = vl.GetTextLine(pos.VisualColumn);
     Rect displayRect;
     // calculate the display rect for the current character
     if (pos.VisualColumn < vl.VisualLengthWithEndOfLineMarker) {
         displayRect = line.GetTextBounds(pos.VisualColumn, 1).First().Rectangle;
         displayRect.Offset(0, vl.GetTextLineVisualYPosition(line, VisualYPosition.LineTop));
     } else {
         // if we are in virtual space, we just use one wide-space as character width
         displayRect = new Rect(vl.GetVisualPosition(pos.VisualColumn, VisualYPosition.TextTop),
                                new Size(textView.WideSpaceWidth, textView.DefaultLineHeight));
     }
     // adjust to current scrolling
     displayRect.Offset(-textView.ScrollOffset);
     return textView
         .TransformToAncestor(source.RootVisual).TransformBounds(displayRect) // rect on root visual
         .TransformToDevice(source.RootVisual); // rect on HWND
 }
예제 #54
0
        private RectangleSelection(TextArea textArea, int startLine, double startXPos, TextViewPosition end)
            : base(textArea)
        {
            InitDocument();
            this.startLine = startLine;
            this.endLine   = end.Line;
            this.startXPos = startXPos;
            this.endXPos   = GetXPos(textArea, end);
            CalculateSegments();
            this.topLeftOffset     = this.segments.First().StartOffset;
            this.bottomRightOffset = this.segments.Last().EndOffset;

            this.start = GetStart();
            this.end   = end;
        }
예제 #55
0
 /// <summary>
 /// Returns a new selection with the changed end point.
 /// </summary>
 /// <exception cref="NotSupportedException">Cannot set endpoint for empty selection</exception>
 public abstract Selection SetEndpoint(TextViewPosition endPosition);
예제 #56
0
        static IEnumerable <Rect> GetRectsForSegmentImpl(TextView textView, ISegment segment, bool extendToFullWidthAtLineEnd)
        {
            Vector scrollOffset = textView.ScrollOffset;
            int    segmentStart = segment.Offset;
            int    segmentEnd   = segment.Offset + segment.Length;

            segmentStart = segmentStart.CoerceValue(0, textView.Document.TextLength);
            segmentEnd   = segmentEnd.CoerceValue(0, textView.Document.TextLength);

            TextViewPosition start;
            TextViewPosition end;

            if (segment is SelectionSegment)
            {
                SelectionSegment sel = (SelectionSegment)segment;
                start = new TextViewPosition(textView.Document.GetLocation(sel.StartOffset), sel.StartVisualColumn);
                end   = new TextViewPosition(textView.Document.GetLocation(sel.EndOffset), sel.EndVisualColumn);
            }
            else
            {
                start = new TextViewPosition(textView.Document.GetLocation(segmentStart), -1);
                end   = new TextViewPosition(textView.Document.GetLocation(segmentEnd), -1);
            }

            foreach (VisualLine vl in textView.VisualLines)
            {
                int vlStartOffset = vl.FirstDocumentLine.Offset;
                if (vlStartOffset > segmentEnd)
                {
                    break;
                }
                int vlEndOffset = vl.LastDocumentLine.Offset + vl.LastDocumentLine.Length;
                if (vlEndOffset < segmentStart)
                {
                    continue;
                }

                int segmentStartVC;
                if (segmentStart < vlStartOffset)
                {
                    segmentStartVC = 0;
                }
                else
                {
                    segmentStartVC = vl.ValidateVisualColumn(start, extendToFullWidthAtLineEnd);
                }

                int segmentEndVC;
                if (segmentEnd > vlEndOffset)
                {
                    segmentEndVC = extendToFullWidthAtLineEnd ? int.MaxValue : vl.VisualLengthWithEndOfLineMarker;
                }
                else
                {
                    segmentEndVC = vl.ValidateVisualColumn(end, extendToFullWidthAtLineEnd);
                }

                TextLine lastTextLine = vl.TextLines.Last();

                for (int i = 0; i < vl.TextLines.Count; i++)
                {
                    TextLine line           = vl.TextLines[i];
                    double   y              = vl.GetTextLineVisualYPosition(line, VisualYPosition.LineTop);
                    int      visualStartCol = vl.GetTextLineVisualStartColumn(line);
                    int      visualEndCol   = visualStartCol + line.Length;
                    if (line != lastTextLine)
                    {
                        visualEndCol -= line.TrailingWhitespaceLength;
                    }

                    if (segmentEndVC < visualStartCol)
                    {
                        break;
                    }
                    if (lastTextLine != line && segmentStartVC > visualEndCol)
                    {
                        continue;
                    }
                    int segmentStartVCInLine = Math.Max(segmentStartVC, visualStartCol);
                    int segmentEndVCInLine   = Math.Min(segmentEndVC, visualEndCol);
                    y -= scrollOffset.Y;
                    if (segmentStartVCInLine == segmentEndVCInLine)
                    {
                        // GetTextBounds crashes for length=0, so we'll handle this case with GetDistanceFromCharacterHit
                        // We need to return a rectangle to ensure empty lines are still visible
                        double pos = vl.GetTextLineVisualXPosition(line, segmentStartVCInLine);
                        pos -= scrollOffset.X;
                        // The following special cases are necessary to get rid of empty rectangles at the end of a TextLine if "Show Spaces" is active.
                        // If not excluded once, the same rectangle is calculated (and added) twice (since the offset could be mapped to two visual positions; end/start of line), if there is no trailing whitespace.
                        // Skip this TextLine segment, if it is at the end of this line and this line is not the last line of the VisualLine and the selection continues and there is no trailing whitespace.
                        if (segmentEndVCInLine == visualEndCol && i < vl.TextLines.Count - 1 && segmentEndVC > segmentEndVCInLine && line.TrailingWhitespaceLength == 0)
                        {
                            continue;
                        }
                        if (segmentStartVCInLine == visualStartCol && i > 0 && segmentStartVC < segmentStartVCInLine && vl.TextLines[i - 1].TrailingWhitespaceLength == 0)
                        {
                            continue;
                        }
                        yield return(new Rect(pos, y, 1, line.Height));
                    }
                    else
                    {
                        Rect lastRect = Rect.Empty;
                        if (segmentStartVCInLine <= visualEndCol)
                        {
                            foreach (TextBounds b in line.GetTextBounds(segmentStartVCInLine, segmentEndVCInLine - segmentStartVCInLine))
                            {
                                double left  = b.Rectangle.Left - scrollOffset.X;
                                double right = b.Rectangle.Right - scrollOffset.X;
                                if (!lastRect.IsEmpty)
                                {
                                    yield return(lastRect);
                                }
                                // left>right is possible in RTL languages
                                lastRect = new Rect(Math.Min(left, right), y, Math.Abs(right - left), line.Height);
                            }
                        }
                        if (segmentEndVC >= vl.VisualLengthWithEndOfLineMarker)
                        {
                            double left            = (segmentStartVC > vl.VisualLengthWithEndOfLineMarker ? vl.GetTextLineVisualXPosition(lastTextLine, segmentStartVC) : line.Width) - scrollOffset.X;
                            double right           = ((segmentEndVC == int.MaxValue || line != lastTextLine) ? Math.Max(((IScrollInfo)textView).ExtentWidth, ((IScrollInfo)textView).ViewportWidth) : vl.GetTextLineVisualXPosition(lastTextLine, segmentEndVC)) - scrollOffset.X;
                            Rect   extendSelection = new Rect(Math.Min(left, right), y, Math.Abs(right - left), line.Height);
                            if (!lastRect.IsEmpty)
                            {
                                if (extendSelection.IntersectsWith(lastRect))
                                {
                                    lastRect.Union(extendSelection);
                                    yield return(lastRect);
                                }
                                else
                                {
                                    yield return(lastRect);

                                    yield return(extendSelection);
                                }
                            }
                            else
                            {
                                yield return(extendSelection);
                            }
                        }
                        else
                        {
                            yield return(lastRect);
                        }
                    }
                }
            }
        }
예제 #57
0
 bool InsertVirtualSpaces(string newText, TextViewPosition start, TextViewPosition end)
 {
     return (!string.IsNullOrEmpty(newText) || !(IsInVirtualSpace(start) && IsInVirtualSpace(end)))
         && newText != "\r\n"
         && newText != "\n"
         && newText != "\r";
 }
예제 #58
0
 /// <summary>
 /// The Min.
 /// </summary>
 /// <param name="a">The a<see cref="TextViewPosition"/>.</param>
 /// <param name="b">The b<see cref="TextViewPosition"/>.</param>
 /// <returns>The <see cref="TextViewPosition"/>.</returns>
 public static TextViewPosition Min(TextViewPosition a, TextViewPosition b)
 {
     return(a > b ? b : a);
 }
		static IEnumerable<Rect> GetRectsForSegmentImpl(TextView textView, ISegment segment, bool extendToFullWidthAtLineEnd)
		{
			int segmentStart = segment.Offset;
			int segmentEnd = segment.Offset + segment.Length;
			
			segmentStart = segmentStart.CoerceValue(0, textView.Document.TextLength);
			segmentEnd = segmentEnd.CoerceValue(0, textView.Document.TextLength);
			
			TextViewPosition start;
			TextViewPosition end;
			
			if (segment is SelectionSegment) {
				SelectionSegment sel = (SelectionSegment)segment;
				start = new TextViewPosition(textView.Document.GetLocation(sel.StartOffset), sel.StartVisualColumn);
				end = new TextViewPosition(textView.Document.GetLocation(sel.EndOffset), sel.EndVisualColumn);
			} else {
				start = new TextViewPosition(textView.Document.GetLocation(segmentStart), -1);
				end = new TextViewPosition(textView.Document.GetLocation(segmentEnd), -1);
			}
			
			foreach (VisualLine vl in textView.VisualLines) {
				int vlStartOffset = vl.FirstDocumentLine.Offset;
				if (vlStartOffset > segmentEnd)
					break;
				int vlEndOffset = vl.LastDocumentLine.Offset + vl.LastDocumentLine.Length;
				if (vlEndOffset < segmentStart)
					continue;
				
				int segmentStartVC;
				if (segmentStart < vlStartOffset)
					segmentStartVC = 0;
				else
					segmentStartVC = vl.ValidateVisualColumn(start, extendToFullWidthAtLineEnd);
				
				int segmentEndVC;
				if (segmentEnd > vlEndOffset)
					segmentEndVC = extendToFullWidthAtLineEnd ? int.MaxValue : vl.VisualLengthWithEndOfLineMarker;
				else
					segmentEndVC = vl.ValidateVisualColumn(end, extendToFullWidthAtLineEnd);
				
				foreach (var rect in ProcessTextLines(textView, vl, segmentStartVC, segmentEndVC))
					yield return rect;
			}
		}
예제 #60
0
 /// <summary>
 /// The Max.
 /// </summary>
 /// <param name="a">The a<see cref="TextViewPosition"/>.</param>
 /// <param name="b">The b<see cref="TextViewPosition"/>.</param>
 /// <returns>The <see cref="TextViewPosition"/>.</returns>
 public static TextViewPosition Max(TextViewPosition a, TextViewPosition b)
 {
     return(a > b ? a : b);
 }