Browse Source

implement customizable multi highlighter

newNRvisualizers
Siegfried Pammer 14 years ago
parent
commit
6247fcb190
  1. 4
      src/AddIns/BackendBindings/CSharpBinding/Project/CSharpBinding.addin
  2. 14
      src/AddIns/BackendBindings/CSharpBinding/Project/Src/CSharpLanguageBinding.cs
  3. 104
      src/AddIns/BackendBindings/CSharpBinding/Project/Src/CSharpSemanticHighlighter.cs
  4. 10
      src/AddIns/BackendBindings/CSharpBinding/Project/Src/FormattingStrategy/CSharpFormattingStrategy.cs
  5. 4
      src/AddIns/BackendBindings/CSharpBinding/Project/Src/Parser/CSharpSymbolSearch.cs
  6. 4
      src/AddIns/BackendBindings/CSharpBinding/Project/Src/Parser/Parser.cs
  7. 4
      src/AddIns/BackendBindings/XamlBinding/XamlBinding/XamlSymbolSearch.cs
  8. 1
      src/AddIns/DisplayBindings/AvalonEdit.AddIn/AvalonEdit.AddIn.addin
  9. 1
      src/AddIns/DisplayBindings/AvalonEdit.AddIn/AvalonEdit.AddIn.csproj
  10. 10
      src/AddIns/DisplayBindings/AvalonEdit.AddIn/Src/AvalonEditViewContent.cs
  11. 38
      src/AddIns/DisplayBindings/AvalonEdit.AddIn/Src/AvalonEditorControlService.cs
  12. 2
      src/AddIns/DisplayBindings/AvalonEdit.AddIn/Src/ChangeMarkerMargin.cs
  13. 32
      src/AddIns/DisplayBindings/AvalonEdit.AddIn/Src/CodeEditor.cs
  14. 41
      src/AddIns/DisplayBindings/AvalonEdit.AddIn/Src/CustomizableHighlightingColorizer.cs
  15. 2
      src/AddIns/DisplayBindings/AvalonEdit.AddIn/Src/DocumentPrinter.cs
  16. 110
      src/AddIns/DisplayBindings/AvalonEdit.AddIn/Src/DocumentSyntaxHighlighter.cs
  17. 8
      src/AddIns/Misc/SearchAndReplace/Project/Engine/SearchManager.cs
  18. 32
      src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Document/TextDocument.cs
  19. 48
      src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Highlighting/DocumentHighlighter.cs
  20. 21
      src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Highlighting/HighlightedLine.cs
  21. 7
      src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Highlighting/HighlightingColor.cs
  22. 36
      src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Highlighting/HighlightingColorizer.cs
  23. 124
      src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Highlighting/IHighlighter.cs
  24. 2
      src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/TextEditor.cs
  25. 11
      src/Libraries/NRefactory/ICSharpCode.NRefactory/Editor/IDocument.cs
  26. 17
      src/Libraries/NRefactory/ICSharpCode.NRefactory/Editor/ReadOnlyDocument.cs
  27. 10
      src/Libraries/NRefactory/ICSharpCode.NRefactory/Editor/StringBuilderDocument.cs
  28. 2
      src/Main/Base/Project/ICSharpCode.SharpDevelop.csproj
  29. 4
      src/Main/Base/Project/Src/Editor/IEditorControlService.cs
  30. 99
      src/Main/Base/Project/Src/Editor/ISyntaxHighlighter.cs
  31. 2
      src/Main/Base/Project/Src/Editor/Search/ProvidedDocumentInformation.cs
  32. 4
      src/Main/Base/Project/Src/Editor/Search/SearchResultsPad.cs
  33. 28
      src/Main/Base/Project/Src/Editor/SyntaxHighlighterKnownSpanNames.cs
  34. 3
      src/Main/Base/Project/Src/Services/Debugger/BreakpointBookmark.cs
  35. 3
      src/Main/Base/Project/Src/Services/Debugger/CurrentLineBookmark.cs
  36. 6
      src/Main/Base/Project/Src/Services/RefactoringService/FindReferencesAndRenameHelper.cs

4
src/AddIns/BackendBindings/CSharpBinding/Project/CSharpBinding.addin

@ -74,6 +74,10 @@ @@ -74,6 +74,10 @@
<SyntaxMode id="C#" name="C#" extensions=".cs" resource="CSharpBinding.Resources.CSharp-Semantic.xshd" />
</Path>
<Path name="/SharpDevelop/ViewContent/AvalonEdit/Highlighters">
<Highlighter id="CSharpSemantic" extensions=".cs" class="CSharpBinding.CSharpSemanticHighlighter" />
</Path>
<Path path = "/SharpDevelop/BackendBindings/ProjectOptions/C#">
<OptionPanel id = "Application"
label = "${res:Dialog.ProjectOptions.ApplicationSettings}"

14
src/AddIns/BackendBindings/CSharpBinding/Project/Src/CSharpLanguageBinding.cs

@ -4,8 +4,10 @@ @@ -4,8 +4,10 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.Design;
using CSharpBinding.FormattingStrategy;
using CSharpBinding.Refactoring;
using ICSharpCode.AvalonEdit.Highlighting;
using ICSharpCode.AvalonEdit.Rendering;
using ICSharpCode.Core;
using ICSharpCode.SharpDevelop;
@ -40,11 +42,6 @@ namespace CSharpBinding @@ -40,11 +42,6 @@ namespace CSharpBinding
{
base.Attach(editor);
this.editor = editor;
ISyntaxHighlighter highlighter = editor.GetService(typeof(ISyntaxHighlighter)) as ISyntaxHighlighter;
if (highlighter != null) {
semanticHighlighter = new CSharpSemanticHighlighter(editor, highlighter);
highlighter.AddAdditionalHighlighter(semanticHighlighter);
}
inspectionManager = new IssueManager(editor);
//codeManipulation = new CodeManipulation(editor);
@ -57,12 +54,7 @@ namespace CSharpBinding @@ -57,12 +54,7 @@ namespace CSharpBinding
public override void Detach()
{
//codeManipulation.Dispose();
ISyntaxHighlighter highlighter = editor.GetService(typeof(ISyntaxHighlighter)) as ISyntaxHighlighter;
if (highlighter != null) {
highlighter.RemoveAdditionalHighlighter(semanticHighlighter);
semanticHighlighter.Dispose();
semanticHighlighter = null;
}
IHighlighter highlighter = editor.GetService(typeof(IHighlighter)) as IHighlighter;
if (inspectionManager != null) {
inspectionManager.Dispose();
inspectionManager = null;

104
src/AddIns/BackendBindings/CSharpBinding/Project/Src/CSharpSemanticHighlighter.cs

@ -5,8 +5,11 @@ using System; @@ -5,8 +5,11 @@ using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Windows.Threading;
using CSharpBinding.Parser;
using ICSharpCode.AvalonEdit.Highlighting;
using ICSharpCode.AvalonEdit.Rendering;
using ICSharpCode.Core;
using ICSharpCode.NRefactory;
using ICSharpCode.NRefactory.CSharp;
using ICSharpCode.NRefactory.CSharp.Resolver;
@ -25,8 +28,8 @@ namespace CSharpBinding @@ -25,8 +28,8 @@ namespace CSharpBinding
/// </summary>
public class CSharpSemanticHighlighter : DepthFirstAstVisitor, IHighlighter, IDisposable
{
readonly ITextEditor textEditor;
readonly ISyntaxHighlighter syntaxHighlighter;
readonly IDocument document;
readonly HighlightingColor defaultTextColor;
readonly HighlightingColor referenceTypeColor;
readonly HighlightingColor valueTypeColor;
readonly HighlightingColor methodCallColor;
@ -47,34 +50,30 @@ namespace CSharpBinding @@ -47,34 +50,30 @@ namespace CSharpBinding
bool isInAccessor;
#region Constructor + Dispose
public CSharpSemanticHighlighter(ITextEditor textEditor, ISyntaxHighlighter syntaxHighlighter)
{
if (textEditor == null)
throw new ArgumentNullException("textEditor");
if (syntaxHighlighter == null)
throw new ArgumentNullException("syntaxHighlighter");
this.textEditor = textEditor;
this.syntaxHighlighter = syntaxHighlighter;
public CSharpSemanticHighlighter(IDocument document)
{
if (document == null)
throw new ArgumentNullException("document");
this.document = document;
IHighlightingDefinition highlightingDefinition = syntaxHighlighter.HighlightingDefinition;
this.referenceTypeColor = highlightingDefinition.GetNamedColor("ReferenceTypes");
this.valueTypeColor = highlightingDefinition.GetNamedColor("ValueTypes");
this.methodCallColor = highlightingDefinition.GetNamedColor("MethodCall");
this.fieldAccessColor = highlightingDefinition.GetNamedColor("FieldAccess");
this.valueKeywordColor = highlightingDefinition.GetNamedColor("NullOrValueKeywords");
this.parameterModifierColor = highlightingDefinition.GetNamedColor("ParameterModifiers");
this.inactiveCodeColor = highlightingDefinition.GetNamedColor("InactiveCode");
var highlighting = HighlightingManager.Instance.GetDefinition("C#");
this.defaultTextColor = highlighting.GetNamedColor("DefaultTextAndBackground");
this.referenceTypeColor = highlighting.GetNamedColor("ReferenceTypes");
this.valueTypeColor = highlighting.GetNamedColor("ValueTypes");
this.methodCallColor = highlighting.GetNamedColor("MethodCall");
this.fieldAccessColor = highlighting.GetNamedColor("FieldAccess");
this.valueKeywordColor = highlighting.GetNamedColor("NullOrValueKeywords");
this.parameterModifierColor = highlighting.GetNamedColor("ParameterModifiers");
this.inactiveCodeColor = highlighting.GetNamedColor("InactiveCode");
SD.ParserService.ParseInformationUpdated += ParserService_ParseInformationUpdated;
SD.ParserService.LoadSolutionProjectsThread.Finished += ParserService_LoadSolutionProjectsThreadEnded;
syntaxHighlighter.VisibleDocumentLinesChanged += syntaxHighlighter_VisibleDocumentLinesChanged;
}
public void Dispose()
{
SD.ParserService.ParseInformationUpdated -= ParserService_ParseInformationUpdated;
SD.ParserService.LoadSolutionProjectsThread.Finished -= ParserService_LoadSolutionProjectsThreadEnded;
syntaxHighlighter.VisibleDocumentLinesChanged -= syntaxHighlighter_VisibleDocumentLinesChanged;
}
#endregion
@ -140,9 +139,7 @@ namespace CSharpBinding @@ -140,9 +139,7 @@ namespace CSharpBinding
#region Event Handlers
void syntaxHighlighter_VisibleDocumentLinesChanged(object sender, EventArgs e)
{
// use this event to remove cached lines which are no longer visible
var visibleDocumentLines = new HashSet<IDocumentLine>(syntaxHighlighter.GetVisibleDocumentLines());
cachedLines.RemoveAll(c => !visibleDocumentLines.Contains(c.DocumentLine));
}
void ParserService_LoadSolutionProjectsThreadEnded(object sender, EventArgs e)
@ -150,16 +147,16 @@ namespace CSharpBinding @@ -150,16 +147,16 @@ namespace CSharpBinding
cachedLines.Clear();
invalidLines.Clear();
forceParseOnNextRefresh = true;
syntaxHighlighter.InvalidateAll();
OnHighlightingStateChanged(0, document.LineCount);
}
void ParserService_ParseInformationUpdated(object sender, ParseInformationEventArgs e)
{
if (e.FileName == textEditor.FileName && invalidLines.Count > 0) {
if (e.FileName == document.FileName && invalidLines.Count > 0) {
cachedLines.Clear();
foreach (IDocumentLine line in invalidLines) {
if (!line.IsDeleted) {
syntaxHighlighter.InvalidateLine(line);
OnHighlightingStateChanged(line.LineNumber, line.LineNumber);
}
}
invalidLines.Clear();
@ -168,8 +165,17 @@ namespace CSharpBinding @@ -168,8 +165,17 @@ namespace CSharpBinding
#endregion
#region IHighlighter implementation
public event HighlightingStateChangedEventHandler HighlightingStateChanged;
protected virtual void OnHighlightingStateChanged(int fromLineNumber, int toLineNumber)
{
if (HighlightingStateChanged != null) {
HighlightingStateChanged(this, fromLineNumber, toLineNumber);
}
}
IDocument IHighlighter.Document {
get { return textEditor.Document; }
get { return document; }
}
IEnumerable<HighlightingColor> IHighlighter.GetColorStack(int lineNumber)
@ -177,23 +183,18 @@ namespace CSharpBinding @@ -177,23 +183,18 @@ namespace CSharpBinding
return null;
}
event HighlightingStateChangedEventHandler IHighlighter.HighlightingStateChanged {
add { }
remove { }
}
void IHighlighter.UpdateHighlightingState(int lineNumber)
{
}
public HighlightedLine HighlightLine(int lineNumber)
{
IDocumentLine documentLine = textEditor.Document.GetLineByNumber(lineNumber);
IDocumentLine documentLine = document.GetLineByNumber(lineNumber);
if (hasCrashed) {
// don't highlight anymore after we've crashed
return new HighlightedLine(textEditor.Document, documentLine);
return new HighlightedLine(document, documentLine);
}
ITextSourceVersion newVersion = textEditor.Document.Version;
ITextSourceVersion newVersion = document.Version;
CachedLine cachedLine = null;
for (int i = 0; i < cachedLines.Count; i++) {
if (cachedLines[i].DocumentLine == documentLine) {
@ -215,9 +216,9 @@ namespace CSharpBinding @@ -215,9 +216,9 @@ namespace CSharpBinding
CSharpFullParseInformation parseInfo;
if (forceParseOnNextRefresh) {
forceParseOnNextRefresh = false;
parseInfo = SD.ParserService.Parse(textEditor.FileName, textEditor.Document) as CSharpFullParseInformation;
parseInfo = SD.ParserService.Parse(FileName.Create(document.FileName), document) as CSharpFullParseInformation;
} else {
parseInfo = SD.ParserService.GetCachedParseInformation(textEditor.FileName, textEditor.Document.Version) as CSharpFullParseInformation;
parseInfo = SD.ParserService.GetCachedParseInformation(FileName.Create(document.FileName), document.Version) as CSharpFullParseInformation;
}
if (parseInfo == null) {
if (!invalidLines.Contains(documentLine))
@ -237,7 +238,7 @@ namespace CSharpBinding @@ -237,7 +238,7 @@ namespace CSharpBinding
var compilation = SD.ParserService.GetCompilationForFile(parseInfo.FileName);
this.resolver = parseInfo.GetResolver(compilation);
HighlightedLine line = new HighlightedLine(textEditor.Document, documentLine);
HighlightedLine line = new HighlightedLine(document, documentLine);
this.line = line;
this.lineNumber = lineNumber;
if (Debugger.IsAttached) {
@ -253,11 +254,34 @@ namespace CSharpBinding @@ -253,11 +254,34 @@ namespace CSharpBinding
this.line = null;
this.resolver = null;
//Debug.WriteLine("Semantic highlighting for line {0} - added {1} sections", lineNumber, line.Sections.Count);
if (textEditor.Document.Version != null) {
cachedLines.Add(new CachedLine(line, textEditor.Document.Version));
if (document.Version != null) {
cachedLines.Add(new CachedLine(line, document.Version));
}
return line;
}
public HighlightingColor DefaultTextColor {
get {
return defaultTextColor;
}
}
public void BeginHighlighting()
{
}
public void EndHighlighting()
{
// use this event to remove cached lines which are no longer visible
// var visibleDocumentLines = new HashSet<IDocumentLine>(syntaxHighlighter.GetVisibleDocumentLines());
// cachedLines.RemoveAll(c => !visibleDocumentLines.Contains(c.DocumentLine));
}
public HighlightingColor GetNamedColor(string name)
{
return null;
}
#endregion
#region Colorize

10
src/AddIns/BackendBindings/CSharpBinding/Project/Src/FormattingStrategy/CSharpFormattingStrategy.cs

@ -5,6 +5,8 @@ using System; @@ -5,6 +5,8 @@ using System;
using System.Diagnostics;
using System.Linq;
using System.Text;
using ICSharpCode.AvalonEdit.Highlighting;
using ICSharpCode.AvalonEdit.Indentation.CSharp;
using ICSharpCode.Core;
using ICSharpCode.NRefactory.Editor;
@ -340,13 +342,13 @@ namespace CSharpBinding.FormattingStrategy @@ -340,13 +342,13 @@ namespace CSharpBinding.FormattingStrategy
return;
}
ISyntaxHighlighter highlighter = textArea.GetService(typeof(ISyntaxHighlighter)) as ISyntaxHighlighter;
IHighlighter highlighter = textArea.GetService(typeof(IHighlighter)) as IHighlighter;
bool isInMultilineComment = false;
bool isInMultilineString = false;
if (highlighter != null && lineAbove != null) {
var spanStack = highlighter.GetSpanColorNamesFromLineStart(lineNr);
isInMultilineComment = spanStack.Contains(SyntaxHighligherKnownSpanNames.Comment);
isInMultilineString = spanStack.Contains(SyntaxHighligherKnownSpanNames.String);
var spanStack = highlighter.GetColorStack(lineNr).Select(c => c.Name).ToArray();
isInMultilineComment = spanStack.Contains(SyntaxHighlighterKnownSpanNames.Comment);
isInMultilineString = spanStack.Contains(SyntaxHighlighterKnownSpanNames.String);
}
bool isInNormalCode = !(isInMultilineComment || isInMultilineString);

4
src/AddIns/BackendBindings/CSharpBinding/Project/Src/Parser/CSharpSymbolSearch.cs

@ -98,14 +98,14 @@ namespace CSharpBinding @@ -98,14 +98,14 @@ namespace CSharpBinding
if (parseInfo == null)
return;
ReadOnlyDocument document = null;
ISyntaxHighlighter highlighter = null;
IHighlighter highlighter = null;
List<Reference> results = new List<Reference>();
fr.FindReferencesInFile(
searchScope, parseInfo.UnresolvedFile, parseInfo.SyntaxTree, compilation,
delegate (AstNode node, ResolveResult result) {
if (document == null) {
document = new ReadOnlyDocument(textSource);
highlighter = SD.EditorControlService.CreateHighlighter(document, fileName);
highlighter = SD.EditorControlService.CreateHighlighter(document);
}
var region = new DomRegion(fileName, node.StartLocation, node.EndLocation);
int offset = document.GetOffset(node.StartLocation);

4
src/AddIns/BackendBindings/CSharpBinding/Project/Src/Parser/Parser.cs

@ -137,14 +137,14 @@ namespace CSharpBinding.Parser @@ -137,14 +137,14 @@ namespace CSharpBinding.Parser
throw new ArgumentException("Parse info does not have SyntaxTree");
ReadOnlyDocument document = null;
ISyntaxHighlighter highlighter = null;
IHighlighter highlighter = null;
new FindReferences().FindLocalReferences(
variable, csParseInfo.UnresolvedFile, csParseInfo.SyntaxTree, compilation,
delegate (AstNode node, ResolveResult result) {
if (document == null) {
document = new ReadOnlyDocument(fileContent);
highlighter = SD.EditorControlService.CreateHighlighter(document, csParseInfo.FileName);
highlighter = SD.EditorControlService.CreateHighlighter(document);
}
var region = new DomRegion(parseInfo.FileName, node.StartLocation, node.EndLocation);
int offset = document.GetOffset(node.StartLocation);

4
src/AddIns/BackendBindings/XamlBinding/XamlBinding/XamlSymbolSearch.cs

@ -87,13 +87,13 @@ namespace ICSharpCode.XamlBinding @@ -87,13 +87,13 @@ namespace ICSharpCode.XamlBinding
if (parseInfo == null)
return;
ReadOnlyDocument document = null;
ISyntaxHighlighter highlighter = null;
IHighlighter highlighter = null;
List<Reference> results = new List<Reference>();
XamlResolver resolver = new XamlResolver();
do {
if (document == null) {
document = new ReadOnlyDocument(textSource);
highlighter = SD.EditorControlService.CreateHighlighter(document, fileName);
highlighter = SD.EditorControlService.CreateHighlighter(document);
}
var result = resolver.Resolve(parseInfo, document.GetLocation(offset + entity.Name.Length / 2 + 1), compilation, cancellationToken);
int length = entity.Name.Length;

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

@ -13,6 +13,7 @@ @@ -13,6 +13,7 @@
<Import assembly = "ICSharpCode.AvalonEdit.AddIn.dll">
<Doozer name="SyntaxMode" class="ICSharpCode.AvalonEdit.AddIn.SyntaxModeDoozer"/>
<Doozer name="ContextActionOptionPanel" class="ICSharpCode.AvalonEdit.AddIn.ContextActions.ContextActionOptionPanelDoozer"/>
<Doozer name="Highlighter" class="ICSharpCode.AvalonEdit.AddIn.HighlighterDoozer"/>
</Import>
</Runtime>

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

@ -104,7 +104,6 @@ @@ -104,7 +104,6 @@
<Compile Include="Src\ContextActions\FindDerivedClassesOrOverrides.cs" />
<Compile Include="Src\ContextActions\GoToEntityAction.cs" />
<Compile Include="Src\DocumentSequence.cs" />
<Compile Include="Src\DocumentSyntaxHighlighter.cs" />
<Compile Include="Src\EnhancedScrollBar.cs" />
<Compile Include="Src\Snippets\CodeSnippetComparer.cs" />
<Compile Include="Src\Utils.cs" />

10
src/AddIns/DisplayBindings/AvalonEdit.AddIn/Src/AvalonEditViewContent.cs

@ -144,9 +144,6 @@ namespace ICSharpCode.AvalonEdit.AddIn @@ -144,9 +144,6 @@ namespace ICSharpCode.AvalonEdit.AddIn
return;
isLoading = true;
try {
// BookmarksDetach();
UpdateSyntaxHighlighting(file.FileName);
if (!file.IsUntitled) {
codeEditor.PrimaryTextEditor.IsReadOnly = (File.GetAttributes(file.FileName) & FileAttributes.ReadOnly) == FileAttributes.ReadOnly;
}
@ -165,12 +162,6 @@ namespace ICSharpCode.AvalonEdit.AddIn @@ -165,12 +162,6 @@ namespace ICSharpCode.AvalonEdit.AddIn
}
}
void UpdateSyntaxHighlighting(FileName fileName)
{
codeEditor.SyntaxHighlighting =
HighlightingManager.Instance.GetDefinitionByExtension(Path.GetExtension(fileName));
}
protected override void OnFileNameChanged(OpenedFile file)
{
base.OnFileNameChanged(file);
@ -188,7 +179,6 @@ namespace ICSharpCode.AvalonEdit.AddIn @@ -188,7 +179,6 @@ namespace ICSharpCode.AvalonEdit.AddIn
// processes the file name change
codeEditor.FileName = newFileName;
UpdateSyntaxHighlighting(newFileName);
SD.ParserService.ParseAsync(file.FileName, codeEditor.Document).FireAndForget();
}

38
src/AddIns/DisplayBindings/AvalonEdit.AddIn/Src/AvalonEditorControlService.cs

@ -3,9 +3,11 @@ @@ -3,9 +3,11 @@
using System;
using System.IO;
using System.Linq;
using ICSharpCode.AvalonEdit.AddIn.Options;
using ICSharpCode.AvalonEdit.Document;
using ICSharpCode.AvalonEdit.Highlighting;
using ICSharpCode.Core;
using ICSharpCode.NRefactory.Editor;
using ICSharpCode.SharpDevelop.Editor;
using ICSharpCode.SharpDevelop.Editor.AvalonEdit;
@ -29,18 +31,38 @@ namespace ICSharpCode.AvalonEdit.AddIn @@ -29,18 +31,38 @@ namespace ICSharpCode.AvalonEdit.AddIn
return new CodeCompletionEditorAdapter(editor);
}
public ISyntaxHighlighter CreateHighlighter(IDocument document, string fileName)
public IHighlighter CreateHighlighter(IDocument document)
{
var def = HighlightingManager.Instance.GetDefinitionByExtension(Path.GetExtension(fileName));
var doc = document.GetService(typeof(TextDocument)) as TextDocument;
var def = HighlightingManager.Instance.GetDefinitionByExtension(Path.GetExtension(document.FileName));
var doc = document as TextDocument;
if (def == null || doc == null)
return null;
var baseHighlighter = new DocumentHighlighter(doc, def.MainRuleSet);
var highlighter = new CustomizableHighlightingColorizer.CustomizingHighlighter(document,
CustomizedHighlightingColor.FetchCustomizations(def.Name), def, baseHighlighter);
return new DocumentSyntaxHighlighter(document, highlighter, def.Name);
var baseHighlighter = new DocumentHighlighter(doc, def);
var additionalHighlighters = AddInTree.BuildItems<IHighlighter>(HighlighterDoozer.AddInPath, doc, false);
var multiHighlighter = new MultiHighlighter(document, new[] { baseHighlighter }.Concat(additionalHighlighters).ToArray());
return new CustomizableHighlightingColorizer.CustomizingHighlighter(
document, CustomizedHighlightingColor.FetchCustomizations(def.Name),
def, multiHighlighter);
}
}
public class HighlighterDoozer : IDoozer
{
internal const string AddInPath = "/SharpDevelop/ViewContent/AvalonEdit/Highlighters";
public bool HandleConditions {
get { return false; }
}
public object BuildItem(BuildItemArgs args)
{
if (!(args.Caller is IDocument))
throw new ArgumentException("Caller must be IDocument!");
Codon codon = args.Codon;
if (!codon.Properties["extensions"].Split(';').Contains(Path.GetExtension(((IDocument)args.Caller).FileName)))
return null;
return Activator.CreateInstance(codon.AddIn.FindType(codon.Properties["class"]), args.Caller);
}
}
}

2
src/AddIns/DisplayBindings/AvalonEdit.AddIn/Src/ChangeMarkerMargin.cs

@ -232,7 +232,7 @@ namespace ICSharpCode.AvalonEdit.AddIn @@ -232,7 +232,7 @@ namespace ICSharpCode.AvalonEdit.AddIn
} else {
var baseDocument = new ReadOnlyDocument(changeWatcher.BaseDocument);
if (differ.editor.SyntaxHighlighting != null) {
var mainHighlighter = new DocumentHighlighter(baseDocument, differ.editor.SyntaxHighlighting.MainRuleSet);
var mainHighlighter = new DocumentHighlighter(baseDocument, differ.editor.SyntaxHighlighting);
var popupHighlighter = differ.editor.TextArea.GetService(typeof(IHighlighter)) as DocumentHighlighter;
popupHighlighter.InitialSpanStack = mainHighlighter.GetSpanStack(currLineInfo.OldStartLineNumber);

32
src/AddIns/DisplayBindings/AvalonEdit.AddIn/Src/CodeEditor.cs

@ -111,6 +111,7 @@ namespace ICSharpCode.AvalonEdit.AddIn @@ -111,6 +111,7 @@ namespace ICSharpCode.AvalonEdit.AddIn
set {
if (fileName != value) {
fileName = value;
this.document.FileName = fileName;
primaryTextEditorAdapter.FileNameChanged();
if (secondaryTextEditorAdapter != null)
@ -124,11 +125,30 @@ namespace ICSharpCode.AvalonEdit.AddIn @@ -124,11 +125,30 @@ namespace ICSharpCode.AvalonEdit.AddIn
if (changeWatcher != null) {
changeWatcher.Initialize(this.Document, value);
}
UpdateSyntaxHighlighting(value);
FetchParseInformation();
}
}
}
void UpdateSyntaxHighlighting(FileName fileName)
{
var highlighting = HighlightingManager.Instance.GetDefinitionByExtension(Path.GetExtension(fileName));
var highlighter = SD.EditorControlService.CreateHighlighter(document);
primaryTextEditor.SyntaxHighlighting = highlighting;
primaryTextEditor.TextArea.TextView.LineTransformers.RemoveWhere(t => t is HighlightingColorizer);
primaryTextEditor.TextArea.TextView.LineTransformers.Insert(0, new HighlightingColorizer(highlighter));
primaryTextEditor.UpdateCustomizedHighlighting();
if (secondaryTextEditor != null) {
secondaryTextEditor.SyntaxHighlighting = highlighting;
secondaryTextEditor.TextArea.TextView.LineTransformers.RemoveWhere(t => t is HighlightingColorizer);
secondaryTextEditor.TextArea.TextView.LineTransformers.Insert(0, new HighlightingColorizer(highlighter));
secondaryTextEditor.UpdateCustomizedHighlighting();
}
}
public void Redraw(ISegment segment, DispatcherPriority priority)
{
primaryTextEditor.TextArea.TextView.Redraw(segment, priority);
@ -547,18 +567,6 @@ namespace ICSharpCode.AvalonEdit.AddIn @@ -547,18 +567,6 @@ namespace ICSharpCode.AvalonEdit.AddIn
}
}
public IHighlightingDefinition SyntaxHighlighting {
get { return primaryTextEditor.SyntaxHighlighting; }
set {
primaryTextEditor.SyntaxHighlighting = value;
primaryTextEditor.UpdateCustomizedHighlighting();
if (secondaryTextEditor != null) {
secondaryTextEditor.SyntaxHighlighting = value;
secondaryTextEditor.UpdateCustomizedHighlighting();
}
}
}
void FetchParseInformation()
{
ParseInformation parseInfo = SD.ParserService.GetCachedParseInformation(this.FileName);

41
src/AddIns/DisplayBindings/AvalonEdit.AddIn/Src/CustomizableHighlightingColorizer.cs

@ -8,12 +8,12 @@ using System.Linq; @@ -8,12 +8,12 @@ using System.Linq;
using System.Windows;
using System.Windows.Media;
using System.Windows.Threading;
using ICSharpCode.AvalonEdit.Document;
using ICSharpCode.AvalonEdit.Editing;
using ICSharpCode.AvalonEdit.Highlighting;
using ICSharpCode.AvalonEdit.Rendering;
using ICSharpCode.NRefactory.Editor;
using ICSharpCode.SharpDevelop;
using ICSharpCode.SharpDevelop.Editor;
namespace ICSharpCode.AvalonEdit.AddIn
@ -127,7 +127,7 @@ namespace ICSharpCode.AvalonEdit.AddIn @@ -127,7 +127,7 @@ namespace ICSharpCode.AvalonEdit.AddIn
readonly IEnumerable<CustomizedHighlightingColor> customizations;
public CustomizableHighlightingColorizer(IHighlightingDefinition highlightingDefinition, IEnumerable<CustomizedHighlightingColor> customizations)
: base(highlightingDefinition.MainRuleSet)
: base(highlightingDefinition)
{
if (customizations == null)
throw new ArgumentNullException("customizations");
@ -137,14 +137,14 @@ namespace ICSharpCode.AvalonEdit.AddIn @@ -137,14 +137,14 @@ namespace ICSharpCode.AvalonEdit.AddIn
protected override void DeregisterServices(TextView textView)
{
textView.Services.RemoveService(typeof(ISyntaxHighlighter));
textView.Services.RemoveService(typeof(IHighlighter));
base.DeregisterServices(textView);
}
protected override void RegisterServices(TextView textView)
{
base.RegisterServices(textView);
textView.Services.AddService(typeof(ISyntaxHighlighter), (CustomizingHighlighter)textView.GetService(typeof(IHighlighter)));
// textView.Services.AddService(typeof(IHighlighter), (CustomizingHighlighter)textView.GetService(typeof(IHighlighter)));
}
protected override IHighlighter CreateHighlighter(TextView textView, TextDocument document)
@ -152,7 +152,7 @@ namespace ICSharpCode.AvalonEdit.AddIn @@ -152,7 +152,7 @@ namespace ICSharpCode.AvalonEdit.AddIn
return new CustomizingHighlighter(textView, customizations, highlightingDefinition, base.CreateHighlighter(textView, document));
}
internal sealed class CustomizingHighlighter : IHighlighter, ISyntaxHighlighter
internal sealed class CustomizingHighlighter : IHighlighter
{
readonly TextView textView;
readonly IEnumerable<CustomizedHighlightingColor> customizations;
@ -223,10 +223,10 @@ namespace ICSharpCode.AvalonEdit.AddIn @@ -223,10 +223,10 @@ namespace ICSharpCode.AvalonEdit.AddIn
highlighter.HighlightingStateChanged -= highlighter_HighlightingStateChanged;
}
void highlighter_HighlightingStateChanged(IHighlighter sender, int lineNumber)
void highlighter_HighlightingStateChanged(IHighlighter sender, int fromLineNumber, int toLineNumber)
{
if (HighlightingStateChanged != null)
HighlightingStateChanged(this, lineNumber);
HighlightingStateChanged(this, fromLineNumber, toLineNumber);
}
public IEnumerable<string> GetSpanColorNamesFromLineStart(int lineNumber)
@ -266,14 +266,14 @@ namespace ICSharpCode.AvalonEdit.AddIn @@ -266,14 +266,14 @@ namespace ICSharpCode.AvalonEdit.AddIn
public void InvalidateLine(IDocumentLine line)
{
if (textView == null)
throw new InvalidOperationException("ISyntaxHighlighter has no TextView assigned!");
throw new InvalidOperationException("IHighlighter has no TextView assigned!");
textView.Redraw(line, DispatcherPriority.Background);
}
public void InvalidateAll()
{
if (textView == null)
throw new InvalidOperationException("ISyntaxHighlighter has no TextView assigned!");
throw new InvalidOperationException("IHighlighter has no TextView assigned!");
textView.Redraw(DispatcherPriority.Background);
}
@ -285,7 +285,7 @@ namespace ICSharpCode.AvalonEdit.AddIn @@ -285,7 +285,7 @@ namespace ICSharpCode.AvalonEdit.AddIn
public IEnumerable<IDocumentLine> GetVisibleDocumentLines()
{
if (textView == null)
throw new InvalidOperationException("ISyntaxHighlighter has no TextView assigned!");
throw new InvalidOperationException("IHighlighter has no TextView assigned!");
List<IDocumentLine> result = new List<IDocumentLine>();
foreach (VisualLine line in textView.VisualLines) {
if (line.FirstDocumentLine == line.LastDocumentLine) {
@ -317,21 +317,14 @@ namespace ICSharpCode.AvalonEdit.AddIn @@ -317,21 +317,14 @@ namespace ICSharpCode.AvalonEdit.AddIn
}
}
public HighlightedInlineBuilder BuildInlines(int lineNumber)
public void BeginHighlighting()
{
HighlightedInlineBuilder builder = new HighlightedInlineBuilder(document.GetText(document.GetLineByNumber(lineNumber)));
HighlightedLine highlightedLine = HighlightLine(lineNumber);
int startOffset = highlightedLine.DocumentLine.Offset;
// copy only the foreground and background colors
foreach (HighlightedSection section in highlightedLine.Sections) {
if (section.Color.Foreground != null) {
builder.SetForeground(section.Offset - startOffset, section.Length, section.Color.Foreground.GetBrush(null));
}
if (section.Color.Background != null) {
builder.SetBackground(section.Offset - startOffset, section.Length, section.Color.Background.GetBrush(null));
}
}
return builder;
}
public void EndHighlighting()
{
}
}

2
src/AddIns/DisplayBindings/AvalonEdit.AddIn/Src/DocumentPrinter.cs

@ -26,7 +26,7 @@ namespace ICSharpCode.AvalonEdit.AddIn @@ -26,7 +26,7 @@ namespace ICSharpCode.AvalonEdit.AddIn
{
IHighlighter highlighter;
if (highlightingDefinition != null)
highlighter = new DocumentHighlighter(document, highlightingDefinition.MainRuleSet);
highlighter = new DocumentHighlighter(document, highlightingDefinition);
else
highlighter = null;
return ConvertTextDocumentToBlock(document, highlighter);

110
src/AddIns/DisplayBindings/AvalonEdit.AddIn/Src/DocumentSyntaxHighlighter.cs

@ -1,110 +0,0 @@ @@ -1,110 +0,0 @@
// 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.IO;
using System.Linq;
using ICSharpCode.AvalonEdit.AddIn.Options;
using ICSharpCode.AvalonEdit.Document;
using ICSharpCode.AvalonEdit.Highlighting;
using ICSharpCode.NRefactory.Editor;
using ICSharpCode.SharpDevelop.Editor;
using ICSharpCode.SharpDevelop.Editor.AvalonEdit;
namespace ICSharpCode.AvalonEdit.AddIn
{
public class DocumentSyntaxHighlighter : ISyntaxHighlighter
{
IDocument document;
IHighlighter highlighter;
string highlightingName;
public DocumentSyntaxHighlighter(IDocument document, IHighlighter highlighter, string highlightingName)
{
if (document == null)
throw new ArgumentNullException("document");
this.document = document;
this.highlighter = highlighter;
this.highlightingName = highlightingName;
}
public IEnumerable<string> GetSpanColorNamesFromLineStart(int lineNumber)
{
if (highlighter != null) {
// delayed evaluation doesn't cause a problem here: GetSpanStack is called immediately,
// only the where/select portian is evaluated later. But that won't be a problem because the
// HighlightingSpan instance shouldn't change once it's in use.
return from color in highlighter.GetColorStack(lineNumber - 1)
where color.Name != null
select color.Name;
} else {
return Enumerable.Empty<string>();
}
}
public HighlightingColor GetNamedColor(string name)
{
return CustomizableHighlightingColorizer.CustomizeColor(name, CustomizedHighlightingColor.FetchCustomizations(highlightingName));
}
public HighlightedInlineBuilder BuildInlines(int lineNumber)
{
HighlightedInlineBuilder builder = new HighlightedInlineBuilder(document.GetText(document.GetLineByNumber(lineNumber)));
if (highlighter != null) {
HighlightedLine highlightedLine = highlighter.HighlightLine(lineNumber);
int startOffset = highlightedLine.DocumentLine.Offset;
// copy only the foreground and background colors
foreach (HighlightedSection section in highlightedLine.Sections) {
if (section.Color.Foreground != null) {
builder.SetForeground(section.Offset - startOffset, section.Length, section.Color.Foreground.GetBrush(null));
}
if (section.Color.Background != null) {
builder.SetBackground(section.Offset - startOffset, section.Length, section.Color.Background.GetBrush(null));
}
}
}
return builder;
}
public HighlightingColor DefaultTextColor {
get {
return GetNamedColor(CustomizableHighlightingColorizer.DefaultTextAndBackground);
}
}
public event EventHandler VisibleDocumentLinesChanged;
public IHighlightingDefinition HighlightingDefinition {
get {
throw new NotImplementedException();
}
}
public void AddAdditionalHighlighter(IHighlighter highlighter)
{
throw new NotImplementedException();
}
public void RemoveAdditionalHighlighter(IHighlighter highlighter)
{
throw new NotImplementedException();
}
public void InvalidateLine(IDocumentLine line)
{
throw new NotImplementedException();
}
public void InvalidateAll()
{
throw new NotImplementedException();
}
public IEnumerable<IDocumentLine> GetVisibleDocumentLines()
{
return Enumerable.Empty<IDocumentLine>();
}
}
}

8
src/AddIns/Misc/SearchAndReplace/Project/Engine/SearchManager.cs

@ -194,10 +194,8 @@ namespace SearchAndReplace @@ -194,10 +194,8 @@ namespace SearchAndReplace
return null;
ThrowIfCancellationRequested();
TextDocument document = null;
ISyntaxHighlighter highlighter = null;
IHighlighter highlighter = null;
int offset = 0;
int length = source.TextLength;
if (Target == SearchTarget.CurrentSelection && Selection != null) {
@ -208,9 +206,9 @@ namespace SearchAndReplace @@ -208,9 +206,9 @@ namespace SearchAndReplace
foreach (var result in strategy.FindAll(source, offset, length)) {
ThrowIfCancellationRequested();
if (document == null) {
document = new TextDocument(source);
document = new TextDocument(source, fileName);
var highlighting = HighlightingManager.Instance.GetDefinitionByExtension(Path.GetExtension(fileName));
highlighter = SD.EditorControlService.CreateHighlighter(document, fileName);
highlighter = SD.EditorControlService.CreateHighlighter(document);
}
var start = document.GetLocation(result.Offset);
var end = document.GetLocation(result.Offset + result.Length);

32
src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Document/TextDocument.cs

@ -88,7 +88,7 @@ namespace ICSharpCode.AvalonEdit.Document @@ -88,7 +88,7 @@ namespace ICSharpCode.AvalonEdit.Document
/// <summary>
/// Create a new text document with the specified initial text.
/// </summary>
public TextDocument(IEnumerable<char> initialText)
public TextDocument(IEnumerable<char> initialText, string fileName = null)
{
if (initialText == null)
throw new ArgumentNullException("initialText");
@ -101,14 +101,15 @@ namespace ICSharpCode.AvalonEdit.Document @@ -101,14 +101,15 @@ namespace ICSharpCode.AvalonEdit.Document
anchorTree = new TextAnchorTree(this);
undoStack = new UndoStack();
this.fileName = fileName;
FireChangeEvents();
}
/// <summary>
/// Create a new text document with the specified initial text.
/// </summary>
public TextDocument(ITextSource initialText)
: this(GetTextFromTextSource(initialText))
public TextDocument(ITextSource initialText, string fileName = null)
: this(GetTextFromTextSource(initialText), fileName)
{
}
@ -1104,5 +1105,30 @@ namespace ICSharpCode.AvalonEdit.Document @@ -1104,5 +1105,30 @@ namespace ICSharpCode.AvalonEdit.Document
return this.ServiceProvider.GetService(serviceType);
}
#endregion
#region FileName
string fileName;
/// <inheritdoc/>
public event EventHandler FileNameChanged;
void OnFileNameChanged(EventArgs e)
{
EventHandler handler = this.FileNameChanged;
if (handler != null)
handler(this, e);
}
/// <inheritdoc/>
public string FileName {
get { return fileName; }
set {
if (fileName != value) {
fileName = value;
OnFileNameChanged(EventArgs.Empty);
}
}
}
#endregion
}
}

48
src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Highlighting/DocumentHighlighter.cs

@ -28,7 +28,7 @@ namespace ICSharpCode.AvalonEdit.Highlighting @@ -28,7 +28,7 @@ namespace ICSharpCode.AvalonEdit.Highlighting
readonly CompressingTreeList<SpanStack> storedSpanStacks = new CompressingTreeList<SpanStack>(object.ReferenceEquals);
readonly CompressingTreeList<bool> isValid = new CompressingTreeList<bool>((a, b) => a == b);
readonly IDocument document;
readonly HighlightingRuleSet baseRuleSet;
readonly IHighlightingDefinition definition;
bool isHighlighting;
/// <summary>
@ -41,14 +41,14 @@ namespace ICSharpCode.AvalonEdit.Highlighting @@ -41,14 +41,14 @@ namespace ICSharpCode.AvalonEdit.Highlighting
/// <summary>
/// Creates a new DocumentHighlighter instance.
/// </summary>
public DocumentHighlighter(TextDocument document, HighlightingRuleSet baseRuleSet)
public DocumentHighlighter(TextDocument document, IHighlightingDefinition definition)
{
if (document == null)
throw new ArgumentNullException("document");
if (baseRuleSet == null)
throw new ArgumentNullException("baseRuleSet");
if (definition == null)
throw new ArgumentNullException("definition");
this.document = document;
this.baseRuleSet = baseRuleSet;
this.definition = definition;
WeakLineTracker.Register(document, this);
InvalidateHighlighting();
}
@ -56,14 +56,14 @@ namespace ICSharpCode.AvalonEdit.Highlighting @@ -56,14 +56,14 @@ namespace ICSharpCode.AvalonEdit.Highlighting
/// <summary>
/// Creates a new DocumentHighlighter instance.
/// </summary>
public DocumentHighlighter(ReadOnlyDocument document, HighlightingRuleSet baseRuleSet)
public DocumentHighlighter(ReadOnlyDocument document, IHighlightingDefinition definition)
{
if (document == null)
throw new ArgumentNullException("document");
if (baseRuleSet == null)
throw new ArgumentNullException("baseRuleSet");
if (definition == null)
throw new ArgumentNullException("definition");
this.document = document;
this.baseRuleSet = baseRuleSet;
this.definition = definition;
InvalidateHighlighting();
}
@ -227,7 +227,7 @@ namespace ICSharpCode.AvalonEdit.Highlighting @@ -227,7 +227,7 @@ namespace ICSharpCode.AvalonEdit.Highlighting
} else {
firstInvalidLine = int.MaxValue;
}
OnHighlightStateChanged(lineNumber);
OnHighlightStateChanged(lineNumber, lineNumber);
} else if (firstInvalidLine == lineNumber) {
isValid[lineNumber] = true;
firstInvalidLine = isValid.IndexOf(false);
@ -264,10 +264,10 @@ namespace ICSharpCode.AvalonEdit.Highlighting @@ -264,10 +264,10 @@ namespace ICSharpCode.AvalonEdit.Highlighting
/// <remarks>This callback must not call HighlightLine or InvalidateHighlighting.
/// It may call GetSpanStack, but only for the changed line and lines above.
/// This method must not modify the document.</remarks>
protected virtual void OnHighlightStateChanged(int lineNumber)
protected virtual void OnHighlightStateChanged(int fromLineNumber, int toLineNumber)
{
if (HighlightingStateChanged != null)
HighlightingStateChanged(this, lineNumber);
HighlightingStateChanged(this, fromLineNumber, toLineNumber);
}
#region Highlighting Engine
@ -399,7 +399,7 @@ namespace ICSharpCode.AvalonEdit.Highlighting @@ -399,7 +399,7 @@ namespace ICSharpCode.AvalonEdit.Highlighting
HighlightingRuleSet CurrentRuleSet {
get {
if (spanStack.IsEmpty)
return baseRuleSet;
return definition.MainRuleSet;
else
return spanStack.Peek().RuleSet ?? emptyRuleSet;
}
@ -494,5 +494,27 @@ namespace ICSharpCode.AvalonEdit.Highlighting @@ -494,5 +494,27 @@ namespace ICSharpCode.AvalonEdit.Highlighting
return new Match[count];
}
#endregion
public HighlightingColor DefaultTextColor {
get {
return new HighlightingColor() {
};
}
}
public void BeginHighlighting()
{
}
public void EndHighlighting()
{
}
public HighlightingColor GetNamedColor(string name)
{
throw new NotSupportedException();
}
}
}

21
src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Highlighting/HighlightedLine.cs

@ -50,11 +50,6 @@ namespace ICSharpCode.AvalonEdit.Highlighting @@ -50,11 +50,6 @@ namespace ICSharpCode.AvalonEdit.Highlighting
/// </summary>
public IList<HighlightedSection> Sections { get; private set; }
/// <summary>
/// Gets the default color of all text outside a <see cref="HighlightedSection"/>.
/// </summary>
public HighlightingColor DefaultTextColor { get; set; }
[Conditional("DEBUG")]
void ValidateInvariants()
{
@ -263,5 +258,21 @@ namespace ICSharpCode.AvalonEdit.Highlighting @@ -263,5 +258,21 @@ namespace ICSharpCode.AvalonEdit.Highlighting
return "[" + GetType().Name + " " + ToHtml(new HtmlOptions()) + "]";
}
#endregion
public HighlightedInlineBuilder ToInlineBuilder()
{
HighlightedInlineBuilder builder = new HighlightedInlineBuilder(Document.GetText(DocumentLine));
int startOffset = DocumentLine.Offset;
// copy only the foreground and background colors
foreach (HighlightedSection section in Sections) {
if (section.Color.Foreground != null) {
builder.SetForeground(section.Offset - startOffset, section.Length, section.Color.Foreground.GetBrush(null));
}
if (section.Color.Background != null) {
builder.SetBackground(section.Offset - startOffset, section.Length, section.Color.Background.GetBrush(null));
}
}
return builder;
}
}
}

7
src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Highlighting/HighlightingColor.cs

@ -17,6 +17,13 @@ namespace ICSharpCode.AvalonEdit.Highlighting @@ -17,6 +17,13 @@ namespace ICSharpCode.AvalonEdit.Highlighting
[Serializable]
public class HighlightingColor : ISerializable
{
[NonSerialized]
public static readonly HighlightingColor DefaultColor = new HighlightingColor {
Background = new SimpleHighlightingBrush(Brushes.White),
Foreground = new SimpleHighlightingBrush(Brushes.Black),
Name = "<default>"
};
/// <summary>
/// Gets/Sets the name of the color.
/// </summary>

36
src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Highlighting/HighlightingColorizer.cs

@ -17,7 +17,7 @@ namespace ICSharpCode.AvalonEdit.Highlighting @@ -17,7 +17,7 @@ namespace ICSharpCode.AvalonEdit.Highlighting
/// </summary>
public class HighlightingColorizer : DocumentColorizingTransformer
{
readonly HighlightingRuleSet ruleSet;
readonly IHighlightingDefinition definition;
TextView textView;
IHighlighter highlighter;
@ -25,11 +25,23 @@ namespace ICSharpCode.AvalonEdit.Highlighting @@ -25,11 +25,23 @@ namespace ICSharpCode.AvalonEdit.Highlighting
/// Creates a new HighlightingColorizer instance.
/// </summary>
/// <param name="ruleSet">The root highlighting rule set.</param>
public HighlightingColorizer(HighlightingRuleSet ruleSet)
public HighlightingColorizer(IHighlightingDefinition definition)
{
if (ruleSet == null)
throw new ArgumentNullException("ruleSet");
this.ruleSet = ruleSet;
if (definition == null)
throw new ArgumentNullException("definition");
this.definition = definition;
this.highlighter = highlighter;
}
/// <summary>
/// Creates a new HighlightingColorizer instance.
/// </summary>
/// <param name="highlighter">The highlighter to be used.</param>
public HighlightingColorizer(IHighlighter highlighter)
{
if (highlighter == null)
throw new ArgumentNullException("highlighter");
this.highlighter = highlighter;
}
void textView_DocumentChanged(object sender, EventArgs e)
@ -77,7 +89,7 @@ namespace ICSharpCode.AvalonEdit.Highlighting @@ -77,7 +89,7 @@ namespace ICSharpCode.AvalonEdit.Highlighting
/// </summary>
protected virtual IHighlighter CreateHighlighter(TextView textView, TextDocument document)
{
return new DocumentHighlighter(document, ruleSet);
return highlighter ?? new DocumentHighlighter(document, definition);
}
/// <inheritdoc/>
@ -202,9 +214,9 @@ namespace ICSharpCode.AvalonEdit.Highlighting @@ -202,9 +214,9 @@ namespace ICSharpCode.AvalonEdit.Highlighting
/// Hey, the user typed "/*". Don't just recreate that line, but also the next one
/// because my highlighting state (at end of line) changed!
/// </remarks>
void OnHighlightStateChanged(IHighlighter sender, int lineNumber)
void OnHighlightStateChanged(IHighlighter sender, int fromlineNumber, int toLineNumber)
{
if (lineNumberBeingColorized != lineNumber) {
if (lineNumberBeingColorized < fromlineNumber || lineNumberBeingColorized > toLineNumber) {
// Ignore notifications for any line except the one we're interested in.
// This improves the performance as Redraw() can take quite some time when called repeatedly
// while scanning the document (above the visible area) for highlighting changes.
@ -216,7 +228,7 @@ namespace ICSharpCode.AvalonEdit.Highlighting @@ -216,7 +228,7 @@ namespace ICSharpCode.AvalonEdit.Highlighting
// If the highlighting state change applies to the lines below, too, the construction of each line
// will invalidate the next line, and the construction pass will regenerate all lines.
Debug.WriteLine("OnHighlightStateChanged forces redraw of line " + (lineNumber + 1));
Debug.WriteLine("OnHighlightStateChanged forces redraw of lines {0} to {1}", fromlineNumber + 1, toLineNumber + 1);
// If the VisualLine construction is in progress, we have to avoid sending redraw commands for
// anything above the line currently being constructed.
@ -257,8 +269,10 @@ namespace ICSharpCode.AvalonEdit.Highlighting @@ -257,8 +269,10 @@ namespace ICSharpCode.AvalonEdit.Highlighting
// so it will always invalidate the next visual line when a folded line is constructed
// and the highlighting stack has changed.
if (lineNumber + 1 <= textView.Document.LineCount)
textView.Redraw(textView.Document.GetLineByNumber(lineNumber + 1), DispatcherPriority.Normal);
for (int lineNumber = fromlineNumber; lineNumber <= toLineNumber; lineNumber++) {
if (lineNumber + 1 <= textView.Document.LineCount)
textView.Redraw(textView.Document.GetLineByNumber(lineNumber + 1), DispatcherPriority.Normal);
}
/*
* Meta-comment: "why does this have to be so complicated?"

124
src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Highlighting/IHighlighter.cs

@ -61,10 +61,132 @@ namespace ICSharpCode.AvalonEdit.Highlighting @@ -61,10 +61,132 @@ namespace ICSharpCode.AvalonEdit.Highlighting
/// for details about the requirements for a correct custom IHighlighter.
/// </remarks>
event HighlightingStateChangedEventHandler HighlightingStateChanged;
/// <summary>
/// Opens a group of HighlightLine calls.
/// </summary>
void BeginHighlighting();
/// <summary>
/// Closes the currently opened group of HighlightLine calls.
/// </summary>
void EndHighlighting();
/// <summary>
/// Retrieves the HighlightingColor with the specified name. Returns null if no color matching the name is found.
/// </summary>
HighlightingColor GetNamedColor(string name);
/// <summary>
/// Gets the default text color.
/// </summary>
HighlightingColor DefaultTextColor { get; }
}
public class MultiHighlighter : IHighlighter
{
readonly IHighlighter[] nestedHighlighters;
readonly IDocument document;
public MultiHighlighter(IDocument document, params IHighlighter[] nestedHighlighters)
{
if (document == null)
throw new ArgumentNullException("document");
if (nestedHighlighters == null)
throw new ArgumentNullException("additionalHighlighters");
foreach (var highlighter in nestedHighlighters) {
if (highlighter == null)
throw new ArgumentException("nulls not allowed!");
if (document != highlighter.Document)
throw new ArgumentException("all highlighters must be assigned to the same document!");
}
this.nestedHighlighters = nestedHighlighters;
this.document = document;
}
public event HighlightingStateChangedEventHandler HighlightingStateChanged {
add {
foreach (var highlighter in nestedHighlighters) {
highlighter.HighlightingStateChanged += value;
}
}
remove {
foreach (var highlighter in nestedHighlighters) {
highlighter.HighlightingStateChanged -= value;
}
}
}
public IDocument Document {
get {
return document;
}
}
public HighlightingColor DefaultTextColor {
get {
if (nestedHighlighters.Length > 0)
return nestedHighlighters[0].DefaultTextColor;
return HighlightingColor.DefaultColor;
}
}
public IEnumerable<HighlightingColor> GetColorStack(int lineNumber)
{
List<HighlightingColor> list = new List<HighlightingColor>();
for (int i = nestedHighlighters.Length - 1; i >= 0; i--) {
var s = nestedHighlighters[i].GetColorStack(lineNumber);
if (s != null)
list.AddRange(s);
}
return list;
}
public HighlightedLine HighlightLine(int lineNumber)
{
HighlightedLine line = new HighlightedLine(document, document.GetLineByNumber(lineNumber));
foreach (IHighlighter h in nestedHighlighters) {
line.MergeWith(h.HighlightLine(lineNumber));
}
return line;
}
public void UpdateHighlightingState(int lineNumber)
{
foreach (var h in nestedHighlighters) {
h.UpdateHighlightingState(lineNumber);
}
}
public void BeginHighlighting()
{
foreach (var h in nestedHighlighters) {
h.BeginHighlighting();
}
}
public void EndHighlighting()
{
foreach (var h in nestedHighlighters) {
h.EndHighlighting();
}
}
public HighlightingColor GetNamedColor(string name)
{
foreach (var h in nestedHighlighters) {
var color = h.GetNamedColor(name);
if (color != null)
return color;
}
return null;
}
}
/// <summary>
/// Event handler for <see cref="IHighlighter.HighlightingStateChanged"/>
/// </summary>
public delegate void HighlightingStateChangedEventHandler(IHighlighter sender, int lineNumber);
public delegate void HighlightingStateChangedEventHandler(IHighlighter sender, int fromLineNumber, int toLineNumber);
}

2
src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/TextEditor.cs

@ -344,7 +344,7 @@ namespace ICSharpCode.AvalonEdit @@ -344,7 +344,7 @@ namespace ICSharpCode.AvalonEdit
{
if (highlightingDefinition == null)
throw new ArgumentNullException("highlightingDefinition");
return new HighlightingColorizer(highlightingDefinition.MainRuleSet);
return new HighlightingColorizer(highlightingDefinition);
}
#endregion

11
src/Libraries/NRefactory/ICSharpCode.NRefactory/Editor/IDocument.cs

@ -190,5 +190,16 @@ namespace ICSharpCode.NRefactory.Editor @@ -190,5 +190,16 @@ namespace ICSharpCode.NRefactory.Editor
/// </summary>
/// <inheritdoc cref="ITextAnchor" select="remarks|example"/>
ITextAnchor CreateAnchor(int offset);
/// <summary>
/// Gets the name of the file the document is stored in.
/// Could also be a non-existent dummy file name or null if no name has been set.
/// </summary>
string FileName { get; }
/// <summary>
/// Fired when the file name of the document changes.
/// </summary>
event EventHandler FileNameChanged;
}
}

17
src/Libraries/NRefactory/ICSharpCode.NRefactory/Editor/ReadOnlyDocument.cs

@ -28,6 +28,7 @@ namespace ICSharpCode.NRefactory.Editor @@ -28,6 +28,7 @@ namespace ICSharpCode.NRefactory.Editor
public sealed class ReadOnlyDocument : IDocument
{
readonly ITextSource textSource;
readonly string fileName;
int[] lines;
static readonly char[] newline = { '\r', '\n' };
@ -35,12 +36,13 @@ namespace ICSharpCode.NRefactory.Editor @@ -35,12 +36,13 @@ namespace ICSharpCode.NRefactory.Editor
/// <summary>
/// Creates a new ReadOnlyDocument from the given text source.
/// </summary>
public ReadOnlyDocument(ITextSource textSource)
public ReadOnlyDocument(ITextSource textSource, string fileName = null)
{
if (textSource == null)
throw new ArgumentNullException("textSource");
// ensure that underlying buffer is immutable
this.textSource = textSource.CreateSnapshot();
this.fileName = fileName;
List<int> lines = new List<int>();
lines.Add(0);
int offset = 0;
@ -58,8 +60,8 @@ namespace ICSharpCode.NRefactory.Editor @@ -58,8 +60,8 @@ namespace ICSharpCode.NRefactory.Editor
/// <summary>
/// Creates a new ReadOnlyDocument from the given string.
/// </summary>
public ReadOnlyDocument(string text)
: this(new StringTextSource(text))
public ReadOnlyDocument(string text, string fileName = null)
: this(new StringTextSource(text), fileName)
{
}
@ -423,5 +425,14 @@ namespace ICSharpCode.NRefactory.Editor @@ -423,5 +425,14 @@ namespace ICSharpCode.NRefactory.Editor
{
return null;
}
/// <inheritdoc/>
/// <remarks>Will never be raised on <see cref="ReadOnlyDocument" />.</remarks>
public event EventHandler FileNameChanged;
/// <inheritdoc/>
public string FileName {
get { return fileName; }
}
}
}

10
src/Libraries/NRefactory/ICSharpCode.NRefactory/Editor/StringBuilderDocument.cs

@ -312,13 +312,13 @@ namespace ICSharpCode.NRefactory.Editor @@ -312,13 +312,13 @@ namespace ICSharpCode.NRefactory.Editor
/// <inheritdoc/>
public string Text {
get {
get {
if (cachedText == null)
cachedText = b.ToString();
return cachedText;
}
set {
Replace(0, b.Length, value);
Replace(0, b.Length, value);
}
}
@ -481,5 +481,11 @@ namespace ICSharpCode.NRefactory.Editor @@ -481,5 +481,11 @@ namespace ICSharpCode.NRefactory.Editor
{
return null;
}
public event EventHandler FileNameChanged;
public string FileName {
get { return string.Empty; }
}
}
}

2
src/Main/Base/Project/ICSharpCode.SharpDevelop.csproj

@ -150,7 +150,7 @@ @@ -150,7 +150,7 @@
<Compile Include="Src\Editor\IFileDocumentProvider.cs" />
<Compile Include="Src\Editor\IFormattingStrategy.cs" />
<Compile Include="Src\Editor\PermanentAnchor.cs" />
<Compile Include="Src\Editor\ISyntaxHighlighter.cs" />
<Compile Include="Src\Editor\SyntaxHighlighterKnownSpanNames.cs" />
<Compile Include="Src\Editor\ITextAreaToolTipProvider.cs">
<DependentUpon>ToolTipService.cs</DependentUpon>
</Compile>

4
src/Main/Base/Project/Src/Editor/IEditorControlService.cs

@ -18,7 +18,7 @@ namespace ICSharpCode.SharpDevelop.Editor @@ -18,7 +18,7 @@ namespace ICSharpCode.SharpDevelop.Editor
{
ITextEditor CreateEditor(out object control);
ITextEditorOptions GlobalOptions { get; }
ISyntaxHighlighter CreateHighlighter(IDocument document, string fileName);
IHighlighter CreateHighlighter(IDocument document);
}
// Fallback if AvalonEdit.AddIn is not available (e.g. some unit tests)
@ -67,7 +67,7 @@ namespace ICSharpCode.SharpDevelop.Editor @@ -67,7 +67,7 @@ namespace ICSharpCode.SharpDevelop.Editor
}
}
public ISyntaxHighlighter CreateHighlighter(IDocument document, string fileName)
public IHighlighter CreateHighlighter(IDocument document)
{
return null;
}

99
src/Main/Base/Project/Src/Editor/ISyntaxHighlighter.cs

@ -1,99 +0,0 @@ @@ -1,99 +0,0 @@
// 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.Linq;
using ICSharpCode.AvalonEdit.Highlighting;
using ICSharpCode.NRefactory.Editor;
namespace ICSharpCode.SharpDevelop.Editor
{
/// <summary>
/// Represents the syntax highlighter inside the text editor.
/// </summary>
[TextEditorService]
public interface ISyntaxHighlighter
{
/// <summary>
/// Retrieves the names of the spans that are active at the start of the specified line.
/// Nested spans are returned in inside-out order (first element of result enumerable is the innermost span).
/// </summary>
IEnumerable<string> GetSpanColorNamesFromLineStart(int lineNumber);
/// <summary>
/// Retrieves the HighlightingColor with the specified name. Returns null if no color matching the name is found.
/// </summary>
HighlightingColor GetNamedColor(string name);
/// <summary>
/// Gets the highlighting definition that is being used.
/// </summary>
IHighlightingDefinition HighlightingDefinition { get; }
/// <summary>
/// Adds an additional highlighting engine that runs in addition to the XSHD-based highlighting.
/// </summary>
void AddAdditionalHighlighter(IHighlighter highlighter);
/// <summary>
/// Removes an additional highlighting engine.
/// </summary>
void RemoveAdditionalHighlighter(IHighlighter highlighter);
/// <summary>
/// Invalidates a line, causing it to be re-highlighted.
/// </summary>
/// <remarks>
/// This method is intended to be called by additional highlighters that process external information
/// (e.g. semantic highlighting).
/// </remarks>
void InvalidateLine(IDocumentLine line);
/// <summary>
/// Invalidates all lines, causing-them to be re-highlighted.
/// </summary>
/// <remarks>
/// This method is intended to be called by additional highlighters that process external information
/// (e.g. semantic highlighting).
/// </remarks>
void InvalidateAll();
/// <summary>
/// Gets the document lines that are currently visible in the editor.
/// </summary>
IEnumerable<IDocumentLine> GetVisibleDocumentLines();
/// <summary>
/// Raised when the set of visible document lines has changed.
/// </summary>
event EventHandler VisibleDocumentLinesChanged;
/// <summary>
/// Creates a <see cref="HighlightedInlineBuilder"/> for a specified line.
/// </summary>
HighlightedInlineBuilder BuildInlines(int lineNumber);
/// <summary>
/// Gets the default text color.
/// </summary>
HighlightingColor DefaultTextColor { get; }
}
public static class SyntaxHighligherKnownSpanNames
{
public const string Comment = "Comment";
public const string String = "String";
public const string Char = "Char";
public static bool IsLineStartInsideComment(this ISyntaxHighlighter highligher, int lineNumber)
{
return highligher.GetSpanColorNamesFromLineStart(lineNumber).Contains(Comment);
}
public static bool IsLineStartInsideString(this ISyntaxHighlighter highligher, int lineNumber)
{
return highligher.GetSpanColorNamesFromLineStart(lineNumber).Contains(String);
}
}
}

2
src/Main/Base/Project/Src/Editor/Search/ProvidedDocumentInformation.cs

@ -27,7 +27,7 @@ namespace ICSharpCode.SharpDevelop.Editor.Search @@ -27,7 +27,7 @@ namespace ICSharpCode.SharpDevelop.Editor.Search
public IDocument Document {
get {
if (document == null) {
this.document = new TextDocument(textBuffer);
this.document = new TextDocument(textBuffer, fileName);
this.textBuffer = null;
}
return document;

4
src/Main/Base/Project/Src/Editor/Search/SearchResultsPad.cs

@ -147,12 +147,12 @@ namespace ICSharpCode.SharpDevelop.Editor.Search @@ -147,12 +147,12 @@ namespace ICSharpCode.SharpDevelop.Editor.Search
return new DummySearchResult { Text = title };
}
public static HighlightedInlineBuilder CreateInlineBuilder(TextLocation startPosition, TextLocation endPosition, IDocument document, ISyntaxHighlighter highlighter)
public static HighlightedInlineBuilder CreateInlineBuilder(TextLocation startPosition, TextLocation endPosition, IDocument document, IHighlighter highlighter)
{
if (startPosition.Line >= 1 && startPosition.Line <= document.LineCount) {
HighlightedInlineBuilder inlineBuilder;
if (highlighter != null) {
inlineBuilder = highlighter.BuildInlines(startPosition.Line);
inlineBuilder = highlighter.HighlightLine(startPosition.Line).ToInlineBuilder();
} else {
inlineBuilder = new HighlightedInlineBuilder(document.GetText(document.GetLineByNumber(startPosition.Line)));
}

28
src/Main/Base/Project/Src/Editor/SyntaxHighlighterKnownSpanNames.cs

@ -0,0 +1,28 @@ @@ -0,0 +1,28 @@
// 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.Linq;
using ICSharpCode.AvalonEdit.Highlighting;
using ICSharpCode.NRefactory.Editor;
namespace ICSharpCode.SharpDevelop.Editor
{
public static class SyntaxHighlighterKnownSpanNames
{
public const string Comment = "Comment";
public const string String = "String";
public const string Char = "Char";
public static bool IsLineStartInsideComment(this IHighlighter highligher, int lineNumber)
{
return highligher.GetColorStack(lineNumber).Any(c => c.Name == Comment);
}
public static bool IsLineStartInsideString(this IHighlighter highligher, int lineNumber)
{
return highligher.GetColorStack(lineNumber).Any(c => c.Name == String);
}
}
}

3
src/Main/Base/Project/Src/Services/Debugger/BreakpointBookmark.cs

@ -4,6 +4,7 @@ @@ -4,6 +4,7 @@
using System;
using System.Linq;
using System.Windows.Media;
using ICSharpCode.AvalonEdit.Highlighting;
using ICSharpCode.Core;
using ICSharpCode.NRefactory;
using ICSharpCode.NRefactory.Editor;
@ -129,7 +130,7 @@ namespace ICSharpCode.SharpDevelop.Debugging @@ -129,7 +130,7 @@ namespace ICSharpCode.SharpDevelop.Debugging
{
IDocumentLine line = this.Document.GetLineByNumber(this.LineNumber);
ITextMarker marker = markerService.Create(line.Offset, line.Length);
ISyntaxHighlighter highlighter = this.Document.GetService(typeof(ISyntaxHighlighter)) as ISyntaxHighlighter;
IHighlighter highlighter = this.Document.GetService(typeof(IHighlighter)) as IHighlighter;
marker.BackgroundColor = DefaultBackground;
marker.ForegroundColor = DefaultForeground;
marker.MarkerColor = DefaultBackground;

3
src/Main/Base/Project/Src/Services/Debugger/CurrentLineBookmark.cs

@ -3,6 +3,7 @@ @@ -3,6 +3,7 @@
using System;
using System.Windows.Media;
using ICSharpCode.AvalonEdit.Highlighting;
using ICSharpCode.Core;
using ICSharpCode.NRefactory;
using ICSharpCode.NRefactory.Editor;
@ -96,7 +97,7 @@ namespace ICSharpCode.SharpDevelop.Debugging @@ -96,7 +97,7 @@ namespace ICSharpCode.SharpDevelop.Debugging
{
IDocumentLine line = this.Document.GetLineByNumber(startLine);
ITextMarker marker = markerService.Create(line.Offset + startColumn - 1, Math.Max(endColumn - startColumn, 1));
ISyntaxHighlighter highlighter = this.Document.GetService(typeof(ISyntaxHighlighter)) as ISyntaxHighlighter;
IHighlighter highlighter = this.Document.GetService(typeof(IHighlighter)) as IHighlighter;
marker.BackgroundColor = DefaultBackground;
marker.ForegroundColor = DefaultForeground;

6
src/Main/Base/Project/Src/Services/RefactoringService/FindReferencesAndRenameHelper.cs

@ -361,13 +361,13 @@ namespace ICSharpCode.SharpDevelop.Refactoring @@ -361,13 +361,13 @@ namespace ICSharpCode.SharpDevelop.Refactoring
TextDocument document = null;
ITextSource buffer = null;
FileName fileName = null;
ISyntaxHighlighter highlighter = null;
IHighlighter highlighter = null;
foreach (Reference r in list) {
if (document == null || fileName != r.FileName) {
buffer = SD.FileService.GetFileContent(r.FileName);
document = new TextDocument(DocumentUtilitites.GetTextSource(buffer));
document = new TextDocument(DocumentUtilitites.GetTextSource(buffer), r.FileName);
fileName = new FileName(r.FileName);
highlighter = SD.EditorControlService.CreateHighlighter(document, fileName);
highlighter = SD.EditorControlService.CreateHighlighter(document);
}
var start = r.StartLocation;
var end = r.EndLocation;

Loading…
Cancel
Save