Browse Source

Highlight current line added in AvalonEdit

pull/289/head
Patryk Mikos 12 years ago
parent
commit
9206e93451
  1. 1
      src/AddIns/DisplayBindings/AvalonEdit.AddIn/AvalonEdit.AddIn.csproj
  2. 19
      src/AddIns/DisplayBindings/AvalonEdit.AddIn/Src/CodeEditorView.cs
  3. 101
      src/AddIns/DisplayBindings/AvalonEdit.AddIn/Src/CurrentLineHighlightRenderer.cs
  4. 23
      src/AddIns/DisplayBindings/AvalonEdit.AddIn/Src/Options/HighlightingOptions.xaml.cs

1
src/AddIns/DisplayBindings/AvalonEdit.AddIn/AvalonEdit.AddIn.csproj

@ -109,6 +109,7 @@ @@ -109,6 +109,7 @@
<Compile Include="Src\ContextActions\FindBaseClasses.cs" />
<Compile Include="Src\ContextActions\FindDerivedClassesOrOverrides.cs" />
<Compile Include="Src\ContextActions\GoToEntityAction.cs" />
<Compile Include="Src\CurrentLineHighlightRenderer.cs" />
<Compile Include="Src\EnhancedScrollBar.cs" />
<Compile Include="Src\HiddenDefinitionRenderer.cs" />
<Compile Include="Src\SyntaxHighlighting\CustomizingHighlighter.cs" />

19
src/AddIns/DisplayBindings/AvalonEdit.AddIn/Src/CodeEditorView.cs

@ -48,6 +48,7 @@ namespace ICSharpCode.AvalonEdit.AddIn @@ -48,6 +48,7 @@ namespace ICSharpCode.AvalonEdit.AddIn
public ITextEditor Adapter { get; set; }
BracketHighlightRenderer bracketRenderer;
CurrentLineHighlightRenderer currentLineRenderer;
//CaretReferencesRenderer caretReferencesRenderer;
ContextActionsRenderer contextActionsRenderer;
HiddenDefinition.HiddenDefinitionRenderer hiddenDefinitionRenderer;
@ -57,6 +58,7 @@ namespace ICSharpCode.AvalonEdit.AddIn @@ -57,6 +58,7 @@ namespace ICSharpCode.AvalonEdit.AddIn
this.CommandBindings.Add(new CommandBinding(ApplicationCommands.Help, OnHelpExecuted));
this.bracketRenderer = new BracketHighlightRenderer(this.TextArea.TextView);
this.currentLineRenderer = new CurrentLineHighlightRenderer(this.TextArea.TextView);
//this.caretReferencesRenderer = new CaretReferencesRenderer(this);
this.contextActionsRenderer = new ContextActionsRenderer(this);
this.hiddenDefinitionRenderer = new HiddenDefinition.HiddenDefinitionRenderer(this);
@ -71,6 +73,7 @@ namespace ICSharpCode.AvalonEdit.AddIn @@ -71,6 +73,7 @@ namespace ICSharpCode.AvalonEdit.AddIn
this.TextArea.TextView.MouseDown += TextViewMouseDown;
this.TextArea.TextView.MouseUp += TextViewMouseUp;
this.TextArea.Caret.PositionChanged += HighlightBrackets;
this.TextArea.Caret.PositionChanged += HighlightCurrentLine;
this.TextArea.TextView.VisualLinesChanged += CodeEditorView_VisualLinesChanged;
SetupTabSnippetHandler();
@ -104,6 +107,9 @@ namespace ICSharpCode.AvalonEdit.AddIn @@ -104,6 +107,9 @@ namespace ICSharpCode.AvalonEdit.AddIn
case "HighlightBrackets":
HighlightBrackets(null, e);
break;
case "HighlightCurrentLine":
HighlightCurrentLine(null, e);
break;
case "EnableFolding":
UpdateParseInformationForFolding();
break;
@ -114,7 +120,8 @@ namespace ICSharpCode.AvalonEdit.AddIn @@ -114,7 +120,8 @@ namespace ICSharpCode.AvalonEdit.AddIn
}
}
#region CaretPositionChanged - Bracket Highlighting
#region CaretPositionChanged - Bracket Highlighting and current line highlighting
/// <summary>
/// Highlights matching brackets.
/// </summary>
@ -145,6 +152,15 @@ namespace ICSharpCode.AvalonEdit.AddIn @@ -145,6 +152,15 @@ namespace ICSharpCode.AvalonEdit.AddIn
}
}
}
/// <summary>
/// Highlights current line
/// </summary>
void HighlightCurrentLine(object sender, EventArgs e)
{
this.currentLineRenderer.SetHighlight(this.TextArea.Caret.Line);
}
#endregion
#region Custom Tab command (code snippet expansion)
@ -512,6 +528,7 @@ namespace ICSharpCode.AvalonEdit.AddIn @@ -512,6 +528,7 @@ namespace ICSharpCode.AvalonEdit.AddIn
var customizations = CustomizedHighlightingColor.FetchCustomizations(language);
CustomizingHighlighter.ApplyCustomizationsToDefaultElements(this, customizations);
BracketHighlightRenderer.ApplyCustomizationsToRendering(this.bracketRenderer, customizations);
CurrentLineHighlightRenderer.ApplyCustomizationsToRendering(this.currentLineRenderer, customizations);
HighlightingOptions.ApplyToRendering(this, customizations);
this.TextArea.TextView.Redraw(); // manually redraw if default elements didn't change but customized highlightings did
}

101
src/AddIns/DisplayBindings/AvalonEdit.AddIn/Src/CurrentLineHighlightRenderer.cs

@ -0,0 +1,101 @@ @@ -0,0 +1,101 @@
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Windows;
using System.Windows.Media;
using ICSharpCode.AvalonEdit.Document;
using ICSharpCode.AvalonEdit.Rendering;
using ICSharpCode.SharpDevelop.Editor;
namespace ICSharpCode.AvalonEdit.AddIn
{
public class CurrentLineHighlightRenderer : IBackgroundRenderer
{
int line;
Pen borderPen;
Brush backgroundBrush;
TextView textView;
public static readonly Color DefaultBackground = Color.FromArgb(22, 20, 220, 224);
public static readonly Color DefaultBorder = Color.FromArgb(52, 0, 255, 110);
public const string CurrentLineHighlight = "Current line highlight";
public CurrentLineHighlightRenderer(TextView textView)
{
if (textView == null)
throw new ArgumentNullException("textView");
this.textView = textView;
this.textView.BackgroundRenderers.Add(this);
this.line = -1;
}
public void SetHighlight(int caretLine)
{
this.line = caretLine;
this.textView.InvalidateLayer(this.Layer);
}
void UpdateColors(Color background, Color foreground)
{
this.borderPen = new Pen(new SolidColorBrush(foreground), 1);
this.borderPen.Freeze();
this.backgroundBrush = new SolidColorBrush(background);
this.backgroundBrush.Freeze();
}
public KnownLayer Layer
{
get
{
return KnownLayer.Selection;
}
}
public void Draw(TextView textView, DrawingContext drawingContext)
{
BackgroundGeometryBuilder builder = new BackgroundGeometryBuilder();
builder.CornerRadius = 1;
builder.AlignToMiddleOfPixels = true;
var visualLine = this.textView.GetVisualLine(line);
if(visualLine == null) return;
var textViewPos = visualLine.GetTextViewPosition(0);
if(textViewPos == null) return;
var position = this.textView.GetVisualPosition(textViewPos, VisualYPosition.LineTop);
if(position == null) return;
var lineWidth = this.textView.ActualWidth;
var lineHeigth = visualLine.Height;
var linePosX = position.X - this.textView.ScrollOffset.X;
var linePosY = position.Y - this.textView.ScrollOffset.Y;
builder.AddRectangle(textView, new Rect(linePosX, linePosY, lineWidth, lineHeigth));
Geometry geometry = builder.CreateGeometry();
if (geometry != null) {
drawingContext.DrawGeometry(backgroundBrush, borderPen, geometry);
}
}
public static void ApplyCustomizationsToRendering(CurrentLineHighlightRenderer renderer, IEnumerable<CustomizedHighlightingColor> customizations)
{
renderer.UpdateColors(DefaultBackground, DefaultBorder);
foreach (CustomizedHighlightingColor color in customizations) {
if (color.Name == CurrentLineHighlight) {
renderer.UpdateColors(color.Background ?? Colors.Pink, color.Foreground ?? Colors.Pink);
break;
}
}
}
}
}

23
src/AddIns/DisplayBindings/AvalonEdit.AddIn/Src/Options/HighlightingOptions.xaml.cs

@ -40,6 +40,7 @@ namespace ICSharpCode.AvalonEdit.AddIn.Options @@ -40,6 +40,7 @@ namespace ICSharpCode.AvalonEdit.AddIn.Options
CodeEditorOptions.Instance.BindToTextEditor(textEditor);
textEditor.Options = new TextEditorOptions(CodeEditorOptions.Instance);
bracketHighlighter = new BracketHighlightRenderer(textEditor.TextArea.TextView);
currentLineHighlighter = new CurrentLineHighlightRenderer(textEditor.TextArea.TextView);
foldingManager = FoldingManager.Install(textEditor.TextArea);
textMarkerService = new TextMarkerService(textEditor.Document);
textEditor.TextArea.TextView.BackgroundRenderers.Add(textMarkerService);
@ -48,6 +49,7 @@ namespace ICSharpCode.AvalonEdit.AddIn.Options @@ -48,6 +49,7 @@ namespace ICSharpCode.AvalonEdit.AddIn.Options
}
BracketHighlightRenderer bracketHighlighter;
CurrentLineHighlightRenderer currentLineHighlighter;
FoldingManager foldingManager;
TextMarkerService textMarkerService;
List<CustomizedHighlightingColor> customizationList;
@ -351,6 +353,26 @@ namespace ICSharpCode.AvalonEdit.AddIn.Options @@ -351,6 +353,26 @@ namespace ICSharpCode.AvalonEdit.AddIn.Options
bracketHighlight.PropertyChanged += item_PropertyChanged;
items.Add(bracketHighlight);
// Create entry for "Current Line highlight"
IHighlightingItem currentLineHighlight = new SimpleHighlightingItem(
CurrentLineHighlightRenderer.CurrentLineHighlight,
ta => {
ta.Document.Text = "example text line";
XshdSyntaxDefinition xshd = (XshdSyntaxDefinition)languageComboBox.SelectedItem;
if (xshd == null)
return;
var customizationsForCurrentLanguage = customizationList.Where(c => c.Language == null || c.Language == xshd.Name);
CurrentLineHighlightRenderer.ApplyCustomizationsToRendering(currentLineHighlighter, customizationsForCurrentLanguage);
currentLineHighlighter.SetHighlight(0);
})
{
Foreground = CurrentLineHighlightRenderer.DefaultBorder,
Background = CurrentLineHighlightRenderer.DefaultBackground
};
currentLineHighlight = new CustomizedHighlightingItem(customizationList, currentLineHighlight, language, canSetFont: false);
currentLineHighlight.PropertyChanged += item_PropertyChanged;
items.Add(currentLineHighlight);
// Create entry for "Folding controls"
IHighlightingItem foldingControls = new SimpleHighlightingItem(
FoldingControls,
@ -565,6 +587,7 @@ namespace ICSharpCode.AvalonEdit.AddIn.Options @@ -565,6 +587,7 @@ namespace ICSharpCode.AvalonEdit.AddIn.Options
}
textEditor.Select(0, 0);
bracketHighlighter.SetHighlight(null);
currentLineHighlighter.SetHighlight(0);
item.ShowExample(textEditor.TextArea);
ITextMarker m = textMarkerService.TextMarkers.SingleOrDefault();
if (m != null && m.Tag != null) {

Loading…
Cancel
Save