public void Draw(TextView textView, DrawingContext drawingContext) { textView.EnsureVisualLines(); #region Highlight Error Tokens var errorsToVisualize = _solution.ErrorService.GetErrorsFromDocument(_codeitem); foreach(var err in errorsToVisualize) { if(err.Range != null) { foreach(Rect r in BackgroundGeometryBuilder.GetRectsForSegment(textView, err.Range)) { //drawingContext.DrawRectangle(null, _errorPen, r); drawingContext.DrawLine(_errorPen, r.BottomLeft, r.BottomRight); } } else { var line = _editor.Document.GetLineByNumber(err.StartLine); if(line != null) { var segment = new TextSegment { StartOffset = line.Offset, EndOffset = line.EndOffset }; foreach(Rect r in BackgroundGeometryBuilder.GetRectsForSegment(textView, segment)) { drawingContext.DrawRectangle(_errorBrush, _errorPen, r); } } } } #endregion }
protected override ISegment CreateTrackedSegment(int offset, int length) { var segment = new TextSegment(); segment.StartOffset = offset; segment.Length = length; textSegmentCollection.Add(segment); return segment; }
public void FullDocumentTest() { var segment = new TextSegment { StartOffset = 0, Length = document.TextLength }; string html = HtmlClipboard.CreateHtmlFragment(document, highlighter, segment, new HtmlOptions()); Assert.AreEqual("<span style=\"color: #008000; font-weight: bold; \">using</span> System.Text;<br>" + Environment.NewLine + " <span style=\"color: #ff0000; \">string</span> " + "text = <span style=\"color: #191970; font-weight: bold; \">SomeMethod</span>();", html); }
private static void DrawRectangle(TextView textView, DrawingContext drawingContext, SourcePosition sourceSegment, Brush brush, Pen pen) { var segment = new ICSharpCode.AvalonEdit.Document.TextSegment { StartOffset = sourceSegment.IndexStart, EndOffset = sourceSegment.IndexEnd }; foreach (var rectangle in BackgroundGeometryBuilder.GetRectsForSegment(textView, segment)) { drawingContext.DrawRectangle(brush, pen, rectangle); } }
// value equality for TextSegment public static bool EqualsByValue(this TextSegment segment, TextSegment otherSegment) { if (otherSegment == null) { return false; } return segment.StartOffset == otherSegment.StartOffset && segment.EndOffset == otherSegment.EndOffset && segment.Length == otherSegment.Length; }
protected ISegment GetCurrentQuerySegment(ITextSource text, int offset) { var start = -1; var end = -1; var depth = 0; var result = new TextSegment() { StartOffset = 0, Length = text.TextLength }; XmlUtils.ProcessFragment(text, (r, o, st) => { switch (r.NodeType) { case XmlNodeType.Element: if (depth == 0) { start = o; } if (r.IsEmptyElement) { end = text.IndexOf("/>", o, text.TextLength - o, StringComparison.Ordinal) + 2; if (depth == 0 && offset >= start && offset < end) { result = new TextSegment() { StartOffset = start, EndOffset = end }; return false; } } else { depth++; } break; case XmlNodeType.EndElement: depth--; if (depth == 0) { end = text.IndexOf('>', o, text.TextLength - o) + 1; if (offset >= start && offset < end) { result = new TextSegment() { StartOffset = start, EndOffset = end }; return false; } } break; } return true; }); return result; }
public void Draw(TextView textView, DrawingContext drawingContext) { textView.EnsureVisualLines(); if (_line.IsDeleted) return; var segment = new TextSegment { StartOffset = _line.Offset, EndOffset = _line.EndOffset }; // ReSharper disable once RedundantArgumentDefaultValue foreach (var current in BackgroundGeometryBuilder.GetRectsForSegment(textView, segment, false)) { drawingContext.DrawRoundedRectangle(new SolidColorBrush(EditorOptions.Instance.HighlightedLineColor), new Pen(Brushes.Red, 0.0), new Rect(current.Location, new Size(textView.ActualWidth, current.Height)), 3.0, 3.0); } }
private static void DrawLine(TextView textView, DrawingContext drawingContext, int?line, Brush brush) { if (line == null || line == 0) { return; } textView.EnsureVisualLines(); var startOffset = textView.Document.GetLineByNumber(line.Value).Offset; var textSegment = new ICSharpCode.AvalonEdit.Document.TextSegment { StartOffset = startOffset }; foreach (var rectangle in BackgroundGeometryBuilder.GetRectsForSegment(textView, textSegment)) { drawingContext.DrawRectangle(brush, EdgePen, new Rect(rectangle.Location, new Size(textView.ActualWidth, rectangle.Height))); } }
public void Draw(TextView textView, DrawingContext drawingContext) { textView.EnsureVisualLines(); #region Highlight Current Line if(lastLine != _editor.TextArea.Caret.Line) { var line = _editor.Document.GetLineByOffset(_editor.CaretOffset); var segment = new TextSegment { StartOffset = line.Offset, EndOffset = line.EndOffset }; foreach(Rect r in BackgroundGeometryBuilder.GetRectsForSegment(textView, segment)) { drawingContext.DrawRoundedRectangle(_lineSelection, _borderPen, new Rect(r.Location, new Size(textView.ActualWidth, r.Height)), 3, 3); } } #endregion #region Highlight Brackets if(_editor.TextArea.Caret.Offset > 1 && TokenHelper.BRAKETS.ContainsKey(_editor.TextArea.Document.GetCharAt(_editor.TextArea.Caret.Offset - 1))) { var offset = _editor.TextArea.Caret.Offset; var seg = _projectitem.SegmentService.QueryCodeSegmentAt(offset); if(seg != null && TokenHelper.BRAKETS.ContainsValue(seg.Token)) { var other = seg.FindOtherBracked(true); var col = other != null ? _braketMatch : _braketMatchFail; var braketsegment = new TextSegment { StartOffset = seg.Range.Offset, EndOffset = seg.Range.EndOffset }; foreach(Rect r in BackgroundGeometryBuilder.GetRectsForSegment(textView, braketsegment)) { drawingContext.DrawRectangle(col, _borderPen, r); } if(other != null) { braketsegment = new TextSegment { StartOffset = other.Range.Offset, EndOffset = other.Range.EndOffset }; foreach(Rect r in BackgroundGeometryBuilder.GetRectsForSegment(textView, braketsegment)) { drawingContext.DrawRectangle(_braketMatch, _borderPen, r); } } } } #endregion }
private void DoSpellCheck() { if (view == null) return; if (!view.TextView.VisualLinesValid) return; spellCheckRenderer.ErrorSegments.Clear(); IEnumerable<VisualLine> visualLines = view.TextView.VisualLines.AsParallel(); foreach (VisualLine currentLine in visualLines) { int startIndex = 0; string originalText = view.Document.GetText(currentLine.FirstDocumentLine.Offset, currentLine.LastDocumentLine.EndOffset - currentLine.FirstDocumentLine.Offset); originalText = Regex.Replace(originalText, "[\\u2018\\u2019\\u201A\\u201B\\u2032\\u2035]", "'"); var textWithoutUrls = uriFinderRegex.Replace(originalText, ""); var query = wordSeparatorRegex.Split(textWithoutUrls) .Where(s => !string.IsNullOrEmpty(s)); foreach (var word in query) { string trimmedWord = word.Trim('\'', '_', '-'); int num = currentLine.FirstDocumentLine.Offset + originalText.IndexOf(trimmedWord, startIndex, StringComparison.InvariantCultureIgnoreCase); if (!spellingService.Spell(trimmedWord)) { var textSegment = new TextSegment { StartOffset = num, Length = word.Length }; spellCheckRenderer.ErrorSegments.Add(textSegment); } startIndex = originalText.IndexOf(word, startIndex, StringComparison.InvariantCultureIgnoreCase) + word.Length; } } }
public void Draw(TextView textView, DrawingContext drawingContext) { textView.EnsureVisualLines(); bool odd = true; if (!G.AppSettings.isFixedBackgroundLines) if (textView.VisualLines[0].FirstDocumentLine.LineNumber % 2 == 0) odd = false; for (int i = 0; i < textView.VisualLines.Count; i++) { //textView.LineTransformers.Add(IVisualLineTransformer) //textView.Options var segment = new TextSegment { StartOffset = textView.VisualLines[i].FirstDocumentLine.Offset, EndOffset = textView.VisualLines[i].LastDocumentLine.EndOffset }; foreach (Rect r in BackgroundGeometryBuilder.GetRectsForSegment(textView, segment)) { if (!odd) //if (textView.VisualLines[i].FirstDocumentLine.LineNumber % 2 == 0) drawingContext.DrawRectangle(scbEven, border, new Rect(r.Location, new Size(textView.ActualWidth, r.Height)) ); else drawingContext.DrawRectangle(scbOdd, border, new Rect(r.Location, new Size(textView.ActualWidth, r.Height)) ); if (odd) odd = false; else odd = true; } } //foreach }
public void DoSearch(SearchType searchType, bool selectSearch) { if (view == null) return; if (!view.TextView.VisualLinesValid) return; ClearSearchHits(); if (searchSettings == null) return; if (string.IsNullOrEmpty(searchSettings.SearchTerm)) return; var searchTerm = searchSettings.SearchTerm; if (!searchSettings.Regex) { searchTerm = Regex.Escape(searchTerm); } if (searchSettings.WholeWord) { searchTerm = @"\b" + searchTerm + @"\b"; } if (!searchSettings.CaseSensitive) { searchTerm = @"(?i)" + searchTerm; } foreach (DocumentLine currentDocLine in view.Document.Lines) { VisualLine currentLine = view.TextView.GetOrConstructVisualLine(currentDocLine); string originalText = view.Document.GetText(currentLine.FirstDocumentLine.Offset, currentLine.LastDocumentLine.EndOffset - currentLine.FirstDocumentLine.Offset); try { foreach (Match match in Regex.Matches(originalText, searchTerm)) { var textSegment = new TextSegment { StartOffset = currentLine.FirstDocumentLine.Offset + match.Index, Length = match.Length }; searchRenderer.SearchHitsSegments.Add(textSegment); } } catch (ArgumentException) {} // catch malformed regex } TextSegment newFoundHit = null; var startLookingFrom = view.Editor.CaretOffset; // consider the already selected text when searching, skip it on SearchType.Next if (!view.Editor.TextArea.Selection.IsEmpty && searchType != SearchType.Next && searchType != SearchType.Replace) { startLookingFrom = view.Editor.SelectionStart; } switch (searchType) { case SearchType.Normal: case SearchType.Next: newFoundHit = (from hit in SearchHits let hitDistance = hit.StartOffset - startLookingFrom where hitDistance >= 0 orderby hitDistance select hit) .FirstOrDefault() ?? SearchHits.FirstOrDefault(); break; case SearchType.Prev: newFoundHit = (from hit in SearchHits let hitDistance = hit.StartOffset - startLookingFrom where hitDistance < 0 orderby hitDistance descending select hit) .FirstOrDefault() ?? SearchHits.Reverse().FirstOrDefault(); break; } // logic for explicit searches if (searchType != SearchType.NoSelect) { newFoundHit.ExecuteSafely(hit => { // special case: don't select text when CTRL+F pressed with an old, existing search, just highlight if (selectSearch) { view.Editor.Select(hit.StartOffset, hit.Length); view.Editor.ScrollToLine(view.Editor.Document.GetLineByOffset(view.Editor.SelectionStart).LineNumber); } lastCaretPosition = view.Editor.CaretOffset; CurrentHitIndex = SearchHits.Select((v, i) => new { hit = v, index = i }).First(arg => arg.hit.Equals(newFoundHit)).index + 1; }); } NumberOfHits = searchRenderer.SearchHitsSegments.Count; // don't show index of a match if we're searching without a search bar, if there are no matches, or if we're in a NoSelect search and there isn't an already selected old match in the editor var selectedText = new TextSegment { StartOffset = view.Editor.SelectionStart, Length = view.Editor.SelectionLength }; if (!searchSettings.SearchingWithBar || !searchRenderer.SearchHitsSegments.Any() || (!selectSearch && newFoundHit != null && !newFoundHit.EqualsByValue(selectedText))) { CurrentHitIndex = 0; } var replaceTerm = searchSettings.ReplaceTerm; if (searchType == SearchType.Replace && !view.Editor.TextArea.Selection.IsEmpty) { view.Editor.TextArea.Selection.ReplaceSelectionWithText(replaceTerm.Trim()); newFoundHit = (from hit in SearchHits let hitDistance = hit.StartOffset - startLookingFrom where hitDistance >= 0 orderby hitDistance select hit) .FirstOrDefault() ?? SearchHits.FirstOrDefault(); newFoundHit.ExecuteSafely(hit => { // special case: don't select text when CTRL+F pressed with an old, existing search, just highlight if (selectSearch) { view.Editor.Select(hit.StartOffset, hit.Length); view.Editor.ScrollToLine(view.Editor.Document.GetLineByOffset(view.Editor.SelectionStart).LineNumber); } lastCaretPosition = view.Editor.CaretOffset; CurrentHitIndex = SearchHits.Select((v, i) => new { hit = v, index = i }).First(arg => arg.hit.Equals(newFoundHit)).index + 1; }); } // don't highlight matches when searching without the search bar if (!searchSettings.SearchingWithBar) { ClearSearchHits(); } }
public override void Complete(TextArea textArea, ISegment completionSegment, EventArgs insertionRequestEventArgs) { var text = textArea.Document.Text; var caretOffset = textArea.Caret.Offset; int startOffset = 0; string word = ""; for (int i = caretOffset - 1; i >= 0; i--) { var ch = text[i]; if (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r' || ch == '(') { startOffset = i + 1; break; } word = text[i] + word; } var segment = new TextSegment(); segment.StartOffset = startOffset; segment.EndOffset = caretOffset; textArea.Document.Replace(segment, ""); _snippet.CreateAvalonEditSnippet(null).Insert(textArea); }
private void BlockUncomment(Editor.FullEditor editor) { if (editor.Helper == null || editor.ReadOnly) return; if (string.IsNullOrWhiteSpace(editor.Helper.BlockCommentStart) && !string.IsNullOrWhiteSpace(editor.Helper.LineComment)) { LineUncomment(editor); return; } if (string.IsNullOrWhiteSpace(editor.Helper.BlockCommentStart)) return; var segment = SelectionSegment(editor); // If the selection is on the inside of the comment if (editor.Document.GetText(segment.Offset, editor.Helper.BlockCommentStart.Length) != editor.Helper.BlockCommentStart && segment.Offset > editor.Helper.BlockCommentStart.Length && editor.Document.GetText(segment.Offset - editor.Helper.BlockCommentStart.Length - 1, editor.Helper.BlockCommentStart.Length + 1) == (editor.Helper.BlockCommentStart + " ")) { segment = new TextSegment() { StartOffset = segment.Offset - (editor.Helper.BlockCommentStart.Length + 1), EndOffset = segment.EndOffset + editor.Helper.BlockCommentEnd.Length + 1 }; } var text = editor.Document.GetText(segment); var output = new System.Text.StringBuilder(); var newEnd = NewBlockCommentEnd(editor.Helper.BlockCommentEnd); var endWithSpace = " " + editor.Helper.BlockCommentEnd; bool inComment = false; for (var i = 0; i < text.Length; i++) { if (!inComment && IsNext(text, editor.Helper.BlockCommentStart, i)) { inComment = true; i += editor.Helper.BlockCommentStart.Length - 1; if (IsNext(text, " ", i + 1)) i++; } else if (inComment && IsNext(text, newEnd, i)) { output.Append(editor.Helper.BlockCommentEnd); i += newEnd.Length - 1; } else if (inComment && IsNext(text, endWithSpace, i)) { inComment = false; i += endWithSpace.Length - 1; } else if (inComment && IsNext(text, editor.Helper.BlockCommentEnd, i)) { inComment = false; i += editor.Helper.BlockCommentEnd.Length - 1; } else { output.Append(text[i]); } } editor.Document.Replace(segment, output.ToString()); }
static bool GetColor(TextSegment node) { return(node != null ? node.color : BLACK); }
public IEnumerable<ISegment> GetDeletableSegments(ISegment segment) { var result = new List<ISegment>(); var notDeletable = new List<TextSegment>(); foreach (var block in Blocks.Where(b => b.Offset < segment.EndOffset && b.EndOffset > segment.Offset)) { if (block is TextBlockParticleCaption) notDeletable.Add(new TextSegment() {StartOffset = block.Offset, EndOffset = block.EndOffset}); if (block is TextBlockParticle) notDeletable.Add(new TextSegment() {StartOffset = block.Offset, EndOffset = block.Offset + 1}); } var beg = segment.Offset; while (beg < segment.EndOffset) { beg = FirstDeletable(beg, segment.EndOffset, notDeletable); var seg = new TextSegment() {StartOffset = beg}; var notDel = FirstNotDeletable(++beg, segment.EndOffset, notDeletable); seg.EndOffset = notDel; result.Add(seg); beg = ++notDel; } return result; }
void ISegmentTree.Add(TextSegment s) { AddSegment(s); }
void ISegmentTree.Remove(TextSegment s) { RemoveSegment(s); }
void ISegmentTree.UpdateAugmentedData(TextSegment node) { UpdateAugmentedData(node); }
private static void DrawLine(TextView textView, DrawingContext drawingContext, int? line, Brush brush) { if (line == null || line == 0) { return; } textView.EnsureVisualLines(); var startOffset = textView.Document.GetLineByNumber(line.Value).Offset; var textSegment = new ICSharpCode.AvalonEdit.Document.TextSegment { StartOffset = startOffset }; foreach (var rectangle in BackgroundGeometryBuilder.GetRectsForSegment(textView, textSegment)) { drawingContext.DrawRectangle(brush, EdgePen, new Rect(rectangle.Location, new Size(textView.ActualWidth, rectangle.Height))); } }
public List<Rect> GetRectByCharIndex(int headIndex, int tailIndex) { var seg = new TextSegment() { StartOffset = headIndex, EndOffset = tailIndex + 1, }; _view.Editor.TextArea.TextView.EnsureVisualLines(); var list = new List<Rect>(); foreach (Rect r in BackgroundGeometryBuilder.GetRectsForSegment(_view.Editor.TextArea.TextView, seg)) { list.Add(r); } return list; }
void FixTreeOnInsert(TextSegment node) { Debug.Assert(node != null); Debug.Assert(node.color == RED); Debug.Assert(node.left == null || node.left.color == BLACK); Debug.Assert(node.right == null || node.right.color == BLACK); TextSegment parentNode = node.parent; if (parentNode == null) { // we inserted in the root -> the node must be black // since this is a root node, making the node black increments the number of black nodes // on all paths by one, so it is still the same for all paths. node.color = BLACK; return; } if (parentNode.color == BLACK) { // if the parent node where we inserted was black, our red node is placed correctly. // since we inserted a red node, the number of black nodes on each path is unchanged // -> the tree is still balanced return; } // parentNode is red, so there is a conflict here! // because the root is black, parentNode is not the root -> there is a grandparent node TextSegment grandparentNode = parentNode.parent; TextSegment uncleNode = Sibling(parentNode); if (uncleNode != null && uncleNode.color == RED) { parentNode.color = BLACK; uncleNode.color = BLACK; grandparentNode.color = RED; FixTreeOnInsert(grandparentNode); return; } // now we know: parent is red but uncle is black // First rotation: if (node == parentNode.right && parentNode == grandparentNode.left) { RotateLeft(parentNode); node = node.left; } else if (node == parentNode.left && parentNode == grandparentNode.right) { RotateRight(parentNode); node = node.right; } // because node might have changed, reassign variables: parentNode = node.parent; grandparentNode = parentNode.parent; // Now recolor a bit: parentNode.color = BLACK; grandparentNode.color = RED; // Second rotation: if (node == parentNode.left && parentNode == grandparentNode.left) { RotateRight(grandparentNode); } else { // because of the first rotation, this is guaranteed: Debug.Assert(node == parentNode.right && parentNode == grandparentNode.right); RotateLeft(grandparentNode); } }
void FixTreeOnDelete(TextSegment node, TextSegment parentNode) { Debug.Assert(node == null || node.parent == parentNode); if (parentNode == null) { return; } // warning: node may be null TextSegment sibling = Sibling(node, parentNode); if (sibling.color == RED) { parentNode.color = RED; sibling.color = BLACK; if (node == parentNode.left) { RotateLeft(parentNode); } else { RotateRight(parentNode); } sibling = Sibling(node, parentNode); // update value of sibling after rotation } if (parentNode.color == BLACK && sibling.color == BLACK && GetColor(sibling.left) == BLACK && GetColor(sibling.right) == BLACK) { sibling.color = RED; FixTreeOnDelete(parentNode, parentNode.parent); return; } if (parentNode.color == RED && sibling.color == BLACK && GetColor(sibling.left) == BLACK && GetColor(sibling.right) == BLACK) { sibling.color = RED; parentNode.color = BLACK; return; } if (node == parentNode.left && sibling.color == BLACK && GetColor(sibling.left) == RED && GetColor(sibling.right) == BLACK) { sibling.color = RED; sibling.left.color = BLACK; RotateRight(sibling); } else if (node == parentNode.right && sibling.color == BLACK && GetColor(sibling.right) == RED && GetColor(sibling.left) == BLACK) { sibling.color = RED; sibling.right.color = BLACK; RotateLeft(sibling); } sibling = Sibling(node, parentNode); // update value of sibling after rotation sibling.color = parentNode.color; parentNode.color = BLACK; if (node == parentNode.left) { if (sibling.right != null) { Debug.Assert(sibling.right.color == RED); sibling.right.color = BLACK; } RotateLeft(parentNode); } else { if (sibling.left != null) { Debug.Assert(sibling.left.color == RED); sibling.left.color = BLACK; } RotateRight(parentNode); } }
private void DoSpellCheck() { if (_editor == null) return; if (!_editor.EditBox.TextArea.TextView.VisualLinesValid) return; var userSettings = App.UserSettings; _spellCheckRenderer.ErrorSegments.Clear(); IEnumerable<VisualLine> visualLines = _editor.EditBox.TextArea.TextView.VisualLines.AsParallel(); foreach (var currentLine in visualLines) { var startIndex = 0; var originalText = _editor.EditBox.Document.GetText(currentLine.FirstDocumentLine.Offset, currentLine.LastDocumentLine.EndOffset - currentLine.FirstDocumentLine.Offset); originalText = Regex.Replace(originalText, "[\\u2018\\u2019\\u201A\\u201B\\u2032\\u2035]", "'"); var textWithout = userSettings.SpellCheckIgnoreCodeBlocks ? _codeBlock.Replace(originalText, "") : originalText; if (userSettings.SpellCheckIgnoreMarkupTags) textWithout = _markupTag.Replace(textWithout, ""); textWithout = _uriFinderRegex.Replace(textWithout, ""); textWithout = _mardownUri.Replace(textWithout, ""); if (userSettings.SpellCheckIgnoreCodeBlocks) textWithout = _inlineCode.Replace(textWithout, ""); var words = _wordSeparatorRegex.Split(textWithout).Where(s => !string.IsNullOrEmpty(s)); if (userSettings.SpellCheckIgnoreAllCaps) words = words.Where(w => w != w.ToUpper()).ToArray(); if (userSettings.SpellCheckIgnoreWordsWithDigits) words = words.Where(w => !Regex.Match(w, "\\d").Success).ToArray(); foreach (var word in words) { var trimmedWord = word.Trim('\'', '_', '-'); var num = currentLine.FirstDocumentLine.Offset + originalText.IndexOf(trimmedWord, startIndex, StringComparison.InvariantCultureIgnoreCase); if (!_spellingService.Spell(trimmedWord)) { var textSegment = new TextSegment {StartOffset = num, Length = word.Length}; _spellCheckRenderer.ErrorSegments.Add(textSegment); } startIndex = originalText.IndexOf(word, startIndex, StringComparison.InvariantCultureIgnoreCase) + word.Length; } } }
public virtual void Complete(TextArea textArea, ISegment completionSegment, EventArgs insertionRequestEventArgs) { var text = textArea.Document.Text; var caretOffset = textArea.Caret.Offset; int startOffset = 0; string word = ""; for (int i = caretOffset - 1; i >= 0; i--) { var ch = text[i]; if (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r' || ch == '(' || ch == ')' || ch == ':' || ch == '.' || ch == '!' || ch == '[' || ch == ']') { startOffset = i + 1; break; } word = text[i] + word; } var segment = new TextSegment(); segment.StartOffset = startOffset; segment.EndOffset = caretOffset; // Check if the completed character is a colon (:) and if it is // remove the $ from the variable name. var idx = startOffset; if (idx > 0) idx--; var chCompleted = text[idx]; if (chCompleted == ':' && (this is VariableCompletionData)) textArea.Document.Replace(segment, Name.Substring(1)); else textArea.Document.Replace(segment, Name); }
private void CorrectOffset(TextSegment foundHit, int correctionOffset) { foundHit.StartOffset += correctionOffset; foundHit.EndOffset += correctionOffset; }
public void ExtendSelection() { INode selectedNode = null; IList<ISpecial> commentsBlankLines; var parsedCU = ParseDocument(editor, out commentsBlankLines); if (parsedCU == null) return; ISegment oldSelection = new TextSegment { StartOffset = editor.SelectionStart, Length = editor.SelectionLength }; Selection extendedSelection = ExtendSelection(editor, parsedCU, commentsBlankLines, out selectedNode, new Type[] { typeof(INode) }); // any node type SelectText(extendedSelection, editor); if (previousSelections.Count == 0 || !(previousSelections.Peek().Offset == oldSelection.Offset && previousSelections.Peek().EndOffset == oldSelection.EndOffset)) { previousSelections.Push(oldSelection); LoggingService.Debug("pushed: " + oldSelection); } else { LoggingService.Debug("not accepted: " + oldSelection); } }
public void Draw(TextView textView, DrawingContext drawingContext) { try { textView.EnsureVisualLines(); /////////////////////////////////////////////////////////////////////////////////////////////////////////////////// bool odd = true; if (!G.AppSettings.isFixedBackgroundLines) if (textView.VisualLines.Count>0) if (textView.VisualLines[0].FirstDocumentLine.LineNumber % 2 == 0) odd = false; for (int i = 0; i < textView.VisualLines.Count; i++) { var segment2 = new TextSegment { StartOffset = textView.VisualLines[i].FirstDocumentLine.Offset, EndOffset = textView.VisualLines[i].LastDocumentLine.EndOffset }; foreach (Rect r in BackgroundGeometryBuilder.GetRectsForSegment(textView, segment2)) { if (!odd) drawingContext.DrawRectangle(scbEven, border, new Rect(r.Location, new Size(textView.ActualWidth, r.Height)));//draw even line else drawingContext.DrawRectangle(scbOdd, border, new Rect(r.Location, new Size(textView.ActualWidth, r.Height)));//draw odd line if (G.AppSettings.isShowUnderline) { string str = editor.Document.GetText(textView.VisualLines[i].FirstDocumentLine.Offset, textView.VisualLines[i].FirstDocumentLine.Length); //if (str.Contains("<frame_end>") || str.Contains("<bmp_end>")) if (Contains(str, G.AppSettings.underlineThisWords)) { double halfPenWidth = lpen.Thickness / 2; GuidelineSet guidelines = new GuidelineSet(); guidelines.GuidelinesX.Add(r.Left + halfPenWidth); guidelines.GuidelinesX.Add(r.Right + halfPenWidth); guidelines.GuidelinesY.Add(r.Top + halfPenWidth); guidelines.GuidelinesY.Add(r.Bottom + halfPenWidth); drawingContext.PushGuidelineSet(guidelines); drawingContext.DrawLine(lpen, new Point(r.Left, r.Bottom), new Point(textView.ActualWidth, r.Bottom)); //draw underline } } if (odd) odd = false; else odd = true; } //foreach } //for //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// var line = editor.Document.GetLineByOffset(editor.CaretOffset); var segment = new TextSegment { StartOffset = line.Offset, EndOffset = line.EndOffset }; foreach (Rect r in BackgroundGeometryBuilder.GetRectsForSegment(textView, segment)) { drawingContext.DrawRectangle(lgbLine, border, new Rect(r.Location, new Size(textView.ActualWidth, r.Height))); //Draw current cursor line if (G.AppSettings.isShowUnderline) { string str2 = editor.Document.GetText(segment); if (Contains(str2, G.AppSettings.underlineThisWords)) { double halfPenWidth = lpen.Thickness / 2; GuidelineSet guidelines = new GuidelineSet(); guidelines.GuidelinesX.Add(r.Left + halfPenWidth); guidelines.GuidelinesX.Add(r.Right + halfPenWidth); guidelines.GuidelinesY.Add(r.Top + halfPenWidth); guidelines.GuidelinesY.Add(r.Bottom + halfPenWidth); drawingContext.PushGuidelineSet(guidelines); drawingContext.DrawLine(lpen, new Point(r.Left, r.Bottom), new Point(textView.ActualWidth, r.Bottom)); //correct underline } } } } catch (Exception ex) { new wException(ex).ShowDialog(); } }//Draw end
public void PartOfHighlightedWordTest() { var segment = new TextSegment { StartOffset = 1, Length = 3 }; string html = HtmlClipboard.CreateHtmlFragment(document, highlighter, segment, new HtmlOptions()); Assert.AreEqual("<span style=\"color: #008000; font-weight: bold; \">sin</span>", html); }
void SimpleCoco() { parserName = "DummyParser"; if (la.kind == 6) { Get(); copySection = new TextSegment() { StartOffset = t.pos }; while (StartOf(1)) { Get(); } Expect(7); copySection.EndOffset = t.pos + t.val.Length; } if (StartOf(2)) { Get(); usingSection = new TextSegment() { StartOffset = t.pos }; while (StartOf(3)) { Get(); } usingSection.EndOffset = t.pos + 1; } parserSection = new TextSegment() { StartOffset = la.pos }; if (la.kind == 8) { Get(); } else if (la.kind == 9) { Get(); while (la.kind == 10) { Get(); Expect(11); } } else SynErr(50); Expect(1); parserName = t.val; if (StartOf(4)) { Get(); while (StartOf(4)) { Get(); } } if (la.kind == 12) { Get(); } if (la.kind == 13) { ListInfo chars = new ListInfo() { name = "CHARACTERS", segment = new TextSegment() { StartOffset = la.pos } }; Get(); while (la.kind == 1) { SetDecl(); } chars.segment.EndOffset = t.pos + (t.val != null ? t.val.Length : 1); lists.Add(chars); } if (la.kind == 14) { ListInfo toks = new ListInfo() { name = "TOKENS", segment = new TextSegment() { StartOffset = la.pos } }; Get(); while (la.kind == 1 || la.kind == 3 || la.kind == 5) { TokenDecl(); } toks.segment.EndOffset = t.pos + (t.val != null ? t.val.Length : 1); lists.Add(toks); } if (la.kind == 15) { ListInfo prags = new ListInfo() { name = "PRAGMAS", segment = new TextSegment() { StartOffset = la.pos } }; Get(); while (la.kind == 1 || la.kind == 3 || la.kind == 5) { TokenDecl(); } prags.segment.EndOffset = t.pos + (t.val != null ? t.val.Length : 1); lists.Add(prags); } while (la.kind == 16) { Get(); Expect(17); TokenExpr(); Expect(18); TokenExpr(); if (la.kind == 19) { Get(); } } while (la.kind == 20) { Get(); Set(); } while (!(la.kind == 0 || la.kind == 21)) {SynErr(51); Get();} Expect(21); while (la.kind == 1) { ListInfo prod = new ListInfo() { name = la.val, segment = new TextSegment() { StartOffset = la.pos } }; Get(); if (la.kind == 29 || la.kind == 31) { AttrDecl(); } if (la.kind == 47) { SemText(); } ExpectWeak(22, 5); Expression(); ExpectWeak(23, 6); prod.segment.EndOffset = t.pos + (t.val != null ? t.val.Length : 1); productions.Add(prod); } Expect(24); Expect(1); Expect(23); parserSection.EndOffset = t.pos + 1; }