예제 #1
0
        void AddTask(Task task)
        {
            if (!isEnabled)
            {
                return;
            }
            if (!CheckTask(task))
            {
                return;
            }

            if (task.Line >= 1 && task.Line <= textEditor.Document.TotalNumberOfLines)
            {
                LoggingService.Debug(task.ToString());
                int offset    = textEditor.Document.PositionToOffset(task.Line, task.Column);
                int endOffset = TextUtilities.GetNextCaretPosition(DocumentUtilitites.GetTextSource(textEditor.Document), offset, System.Windows.Documents.LogicalDirection.Forward, CaretPositioningMode.WordBorderOrSymbol);
                if (endOffset < 0)
                {
                    endOffset = textEditor.Document.TextLength;
                }
                int length = endOffset - offset;

                if (length < 2)
                {
                    // marker should be at least 2 characters long, but take care that we don't make
                    // it longer than the document
                    length = Math.Min(2, textEditor.Document.TextLength - offset);
                }

                ITextMarker marker = this.markerService.Create(offset, length);

                Color markerColor = Colors.Transparent;

                switch (task.TaskType)
                {
                case TaskType.Error:
                    markerColor = Colors.Red;
                    break;

                case TaskType.Message:
                    markerColor = Colors.Blue;
                    break;

                case TaskType.Warning:
                    markerColor = Colors.Orange;
                    break;
                }

                marker.MarkerColor = markerColor;
                marker.MarkerType  = TextMarkerType.SquigglyUnderline;

                marker.ToolTip = task.Description;

                marker.Tag = task;
            }
        }
예제 #2
0
            SearchedFile SearchFile(FileName fileName)
            {
                ITextBuffer buffer = fileFinder.Create(fileName);

                if (buffer == null)
                {
                    return(null);
                }

                ThrowIfCancellationRequested();

                var                 source      = DocumentUtilitites.GetTextSource(buffer);
                TextDocument        document    = null;
                DocumentHighlighter highlighter = null;
                int                 offset      = 0;
                int                 length      = source.TextLength;

                if (Target == SearchTarget.CurrentSelection && Selection != null)
                {
                    offset = Selection.Offset;
                    length = Selection.Length;
                }
                List <SearchResultMatch> results = new List <SearchResultMatch>();

                foreach (var result in strategy.FindAll(source, offset, length))
                {
                    ThrowIfCancellationRequested();
                    if (document == null)
                    {
                        document = new TextDocument(source);
                        var highlighting = HighlightingManager.Instance.GetDefinitionByExtension(Path.GetExtension(fileName));
                        if (highlighting != null)
                        {
                            highlighter = new DocumentHighlighter(document, highlighting.MainRuleSet);
                        }
                        else
                        {
                            highlighter = null;
                        }
                    }
                    var start   = document.GetLocation(result.Offset).ToLocation();
                    var end     = document.GetLocation(result.Offset + result.Length).ToLocation();
                    var builder = SearchResultsPad.CreateInlineBuilder(start, end, document, highlighter);
                    results.Add(new AvalonEditSearchResultMatch(fileName, start, end, result.Offset, result.Length, builder, result));
                }
                if (results.Count > 0)
                {
                    return(new SearchedFile(fileName, results));
                }
                else
                {
                    return(null);
                }
            }
예제 #3
0
            SearchResultMatch Find(FileName file, IDocument document, int searchOffset, int length)
            {
                var result = strategy.FindNext(DocumentUtilitites.GetTextSource(document), searchOffset, length);

                if (result != null)
                {
                    var start = document.OffsetToPosition(result.Offset);
                    var end   = document.OffsetToPosition(result.EndOffset);
                    return(new AvalonEditSearchResultMatch(file, start, end, result.Offset, result.Length, null, null, result));
                }
                return(null);
            }
        public static void ShowAsSearchResults(string title, List <Reference> list)
        {
            if (list == null)
            {
                return;
            }
            List <SearchResultMatch> results  = new List <SearchResultMatch>(list.Count);
            TextDocument             document = null;
            FileName     fileName             = null;
            IHighlighter highlighter          = null;

            foreach (Reference r in list)
            {
                var f = new FileName(r.FileName);
                if (document == null || !f.Equals(fileName))
                {
                    document = new TextDocument(DocumentUtilitites.GetTextSource(ParserService.GetParseableFileContent(r.FileName)));
                    fileName = new FileName(r.FileName);
                    var def = HighlightingManager.Instance.GetDefinitionByExtension(Path.GetExtension(r.FileName));
                    if (def != null)
                    {
                        highlighter = new DocumentHighlighter(document, def.MainRuleSet);
                    }
                    else
                    {
                        highlighter = null;
                    }
                }
                var start             = document.GetLocation(r.Offset).ToLocation();
                var end               = document.GetLocation(r.Offset + r.Length).ToLocation();
                var builder           = SearchResultsPad.CreateInlineBuilder(start, end, document, highlighter);
                SearchResultMatch res = new SearchResultMatch(fileName, start, end, r.Offset, r.Length, builder);
                results.Add(res);
            }
            SearchResultsPad.Instance.ShowSearchResults(title, results);
            SearchResultsPad.Instance.BringToFront();
        }
예제 #5
0
        void DisplayTooltip(MouseEventArgs e)
        {
            int line = GetLineFromMousePosition(e);

            if (line == 0)
            {
                return;
            }

            int    startLine;
            bool   added;
            string oldText = changeWatcher.GetOldVersionFromLine(line, out startLine, out added);

            TextEditor editor = this.TextView.Services.GetService(typeof(TextEditor)) as TextEditor;

            markerService = this.TextView.Services.GetService(typeof(ITextMarkerService)) as ITextMarkerService;

            LineChangeInfo zeroLineInfo = changeWatcher.GetChange(0);

            int  offset, length;
            bool hasNewVersion = changeWatcher.GetNewVersionFromLine(line, out offset, out length);

            if (line == 1 && zeroLineInfo.Change == ChangeType.Deleted)
            {
                int zeroStartLine; bool zeroAdded;
                startLine = 1;
                string deletedText = changeWatcher.GetOldVersionFromLine(0, out zeroStartLine, out zeroAdded);
                var    docLine     = editor.Document.GetLineByNumber(line);
                string newLine     = DocumentUtilitites.GetLineTerminator(changeWatcher.CurrentDocument, 1);
                deletedText += newLine;
                deletedText += editor.Document.GetText(docLine.Offset, docLine.Length);
                if (oldText != null)
                {
                    oldText = deletedText + newLine + oldText;
                }
                else
                {
                    oldText = deletedText;
                }

                if (!hasNewVersion)
                {
                    offset        = 0;
                    length        = docLine.Length;
                    hasNewVersion = true;
                }
            }

            if (hasNewVersion)
            {
                if (marker != null)
                {
                    markerService.Remove(marker);
                }
                if (length <= 0)
                {
                    marker = null;
                    length = 0;
                }
                else
                {
                    marker = markerService.Create(offset, length);
                    marker.BackgroundColor = Colors.LightGreen;
                }
            }

            if (oldText != null)
            {
                LineChangeInfo currLineInfo = changeWatcher.GetChange(startLine);

                if (currLineInfo.Change == ChangeType.Deleted && !(line == 1 && zeroLineInfo.Change == ChangeType.Deleted))
                {
                    var docLine = editor.Document.GetLineByNumber(startLine);
                    if (docLine.DelimiterLength == 0)
                    {
                        oldText = DocumentUtilitites.GetLineTerminator(changeWatcher.CurrentDocument, startLine) + oldText;
                    }
                    oldText = editor.Document.GetText(docLine.Offset, docLine.TotalLength) + oldText;
                }

                DiffControl differ = new DiffControl();
                differ.editor.SyntaxHighlighting            = editor.SyntaxHighlighting;
                differ.editor.HorizontalScrollBarVisibility = ScrollBarVisibility.Hidden;
                differ.editor.VerticalScrollBarVisibility   = ScrollBarVisibility.Hidden;
                differ.editor.Document.Text = oldText;
                differ.Background           = Brushes.White;

                if (oldText == string.Empty)
                {
                    differ.editor.Visibility     = Visibility.Collapsed;
                    differ.copyButton.Visibility = Visibility.Collapsed;
                }
                else
                {
                    var baseDocument = new TextDocument(DocumentUtilitites.GetTextSource(changeWatcher.BaseDocument));
                    if (differ.editor.SyntaxHighlighting != null)
                    {
                        var mainHighlighter  = new DocumentHighlighter(baseDocument, differ.editor.SyntaxHighlighting.MainRuleSet);
                        var popupHighlighter = differ.editor.TextArea.GetService(typeof(IHighlighter)) as DocumentHighlighter;

                        popupHighlighter.InitialSpanStack = mainHighlighter.GetSpanStack(currLineInfo.OldStartLineNumber);
                    }
                }

                differ.revertButton.Click += delegate {
                    if (hasNewVersion)
                    {
                        Document.Replace(offset, length, oldText);
                        tooltip.IsOpen = false;
                    }
                };

                tooltip.Child = new Border()
                {
                    Child           = differ,
                    BorderBrush     = Brushes.Black,
                    BorderThickness = new Thickness(1)
                };

                if (tooltip.IsOpen)
                {
                    tooltip.IsOpen = false;
                }

                tooltip.IsOpen = true;

                tooltip.Closed += delegate {
                    if (marker != null)
                    {
                        markerService.Remove(marker);
                    }
                };
                tooltip.HorizontalOffset = -10;
                tooltip.VerticalOffset   =
                    TextView.GetVisualTopByDocumentLine(startLine) - TextView.ScrollOffset.Y;
                tooltip.Placement       = PlacementMode.Top;
                tooltip.PlacementTarget = this.TextView;
            }
        }