Browse Source

Get rid of RichTextModelOutput

pull/1654/head
Siegfried Pammer 6 years ago
parent
commit
18e8f358be
  1. 1
      ILSpy/ILSpy.csproj
  2. 61
      ILSpy/Languages/CSharpHighlightingTokenWriter.cs
  3. 5
      ILSpy/Languages/CSharpLanguage.cs
  4. 119
      ILSpy/RichTextModelOutput.cs

1
ILSpy/ILSpy.csproj

@ -173,7 +173,6 @@
<AutoGen>True</AutoGen>
<DependentUpon>Resources.resx</DependentUpon>
</Compile>
<Compile Include="RichTextModelOutput.cs" />
<Compile Include="Search\AbstractEntitySearchStrategy.cs" />
<Compile Include="Search\NamespaceSearchStrategy.cs" />
<Compile Include="Search\AssemblySearchStrategy.cs" />

61
ILSpy/Languages/CSharpHighlightingTokenWriter.cs

@ -29,8 +29,6 @@ namespace ICSharpCode.ILSpy
{
class CSharpHighlightingTokenWriter : DecoratingTokenWriter
{
ISmartTextOutput textOutput;
HighlightingColor visibilityKeywordsColor;
HighlightingColor namespaceKeywordsColor;
HighlightingColor structureKeywordsColor;
@ -65,12 +63,15 @@ namespace ICSharpCode.ILSpy
HighlightingColor trueKeywordColor;
HighlightingColor typeKeywordsColor;
public CSharpHighlightingTokenWriter(TokenWriter decoratedWriter, ISmartTextOutput textOutput) : base(decoratedWriter)
public RichTextModel HighlightingModel { get; } = new RichTextModel();
public CSharpHighlightingTokenWriter(TokenWriter decoratedWriter, ISmartTextOutput textOutput = null, ILocatable locatable = null)
: base(decoratedWriter)
{
this.textOutput = textOutput;
var highlighting = HighlightingManager.Instance.GetDefinition("C#");
//this.defaultTextColor = ???;
this.locatable = locatable;
this.textOutput = textOutput;
this.visibilityKeywordsColor = highlighting.GetNamedColor("Visibility");
this.namespaceKeywordsColor = highlighting.GetNamedColor("NamespaceKeywords");
@ -264,11 +265,11 @@ namespace ICSharpCode.ILSpy
if (nodeStack.PeekOrDefault() is AttributeSection)
color = attributeKeywordsColor;
if (color != null) {
textOutput.BeginSpan(color);
BeginSpan(color);
}
base.WriteKeyword(role, keyword);
if (color != null) {
textOutput.EndSpan();
EndSpan();
}
}
@ -307,11 +308,11 @@ namespace ICSharpCode.ILSpy
break;
}
if (color != null) {
textOutput.BeginSpan(color);
BeginSpan(color);
}
base.WritePrimitiveType(type);
if (color != null) {
textOutput.EndSpan();
EndSpan();
}
}
@ -377,11 +378,11 @@ namespace ICSharpCode.ILSpy
break;
}
if (color != null) {
textOutput.BeginSpan(color);
BeginSpan(color);
}
base.WriteIdentifier(identifier);
if (color != null) {
textOutput.EndSpan();
EndSpan();
}
}
@ -395,11 +396,11 @@ namespace ICSharpCode.ILSpy
color = trueKeywordColor;
}
if (color != null) {
textOutput.BeginSpan(color);
BeginSpan(color);
}
base.WritePrimitiveValue(value, literalValue);
if (color != null) {
textOutput.EndSpan();
EndSpan();
}
}
@ -451,5 +452,39 @@ namespace ICSharpCode.ILSpy
base.EndNode(node);
nodeStack.Pop();
}
readonly Stack<HighlightingColor> colorStack = new Stack<HighlightingColor>();
HighlightingColor currentColor = new HighlightingColor();
int currentColorBegin = -1;
readonly ILocatable locatable;
readonly ISmartTextOutput textOutput;
private void BeginSpan(HighlightingColor highlightingColor)
{
if (textOutput != null) {
textOutput.BeginSpan(highlightingColor);
return;
}
if (currentColorBegin > -1)
HighlightingModel.SetHighlighting(currentColorBegin, locatable.Length - currentColorBegin, currentColor);
colorStack.Push(currentColor);
currentColor = currentColor.Clone();
currentColorBegin = locatable.Length;
currentColor.MergeWith(highlightingColor);
currentColor.Freeze();
}
private void EndSpan()
{
if (textOutput != null) {
textOutput.EndSpan();
return;
}
HighlightingModel.SetHighlighting(currentColorBegin, locatable.Length - currentColorBegin, currentColor);
currentColor = colorStack.Pop();
currentColorBegin = locatable.Length;
}
}
}

5
ILSpy/Languages/CSharpLanguage.cs

@ -632,10 +632,9 @@ namespace ICSharpCode.ILSpy
var flags = ConversionFlags.All & ~(ConversionFlags.ShowBody | ConversionFlags.PlaceReturnTypeAfterParameterList);
var output = new StringWriter();
var decoratedWriter = new TextWriterTokenWriter(output);
var richTextOutput = new RichTextModelOutput(decoratedWriter);
var writer = new CSharpHighlightingTokenWriter(TokenWriter.InsertRequiredSpaces(decoratedWriter), richTextOutput);
var writer = new CSharpHighlightingTokenWriter(TokenWriter.InsertRequiredSpaces(decoratedWriter), locatable: decoratedWriter);
new CSharpAmbience() { ConversionFlags = flags }.ConvertSymbol(entity, writer, new DecompilerSettings().CSharpFormattingOptions);
return new RichText(output.ToString(), richTextOutput.Model);
return new RichText(output.ToString(), writer.HighlightingModel);
}
public override CodeMappingInfo GetCodeMappingInfo(PEFile module, EntityHandle member)

119
ILSpy/RichTextModelOutput.cs

@ -1,119 +0,0 @@
// Copyright (c) 2011 AlphaSierraPapa for the SharpDevelop Team
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
using System;
using System.Collections.Generic;
using System.Reflection.Metadata;
using System.Text;
using System.Windows;
using ICSharpCode.AvalonEdit.Highlighting;
using ICSharpCode.Decompiler;
using ICSharpCode.Decompiler.CSharp.OutputVisitor;
using ICSharpCode.Decompiler.Disassembler;
using ICSharpCode.Decompiler.Metadata;
using ICSharpCode.Decompiler.TypeSystem;
namespace ICSharpCode.ILSpy
{
public class RichTextModelOutput : ISmartTextOutput
{
readonly Stack<HighlightingColor> colorStack = new Stack<HighlightingColor>();
HighlightingColor currentColor = new HighlightingColor();
int currentColorBegin = -1;
ILocatable textOutput;
public string IndentationString { get; set; } = "\t";
public RichTextModel Model { get; set; } = new RichTextModel();
public RichTextModelOutput(ILocatable textOutput)
{
this.textOutput = textOutput;
}
public void AddUIElement(Func<UIElement> element)
{
throw new NotSupportedException();
}
public void BeginSpan(HighlightingColor highlightingColor)
{
if (currentColorBegin > -1)
Model.SetHighlighting(currentColorBegin, textOutput.Length - currentColorBegin, currentColor);
colorStack.Push(currentColor);
currentColor = currentColor.Clone();
currentColorBegin = textOutput.Length;
currentColor.MergeWith(highlightingColor);
currentColor.Freeze();
}
public void EndSpan()
{
Model.SetHighlighting(currentColorBegin, textOutput.Length - currentColorBegin, currentColor);
currentColor = colorStack.Pop();
currentColorBegin = textOutput.Length;
}
public void Indent()
{
}
public void Unindent()
{
}
public void Write(char ch)
{
}
public void Write(string text)
{
}
public void WriteLine()
{
}
public void WriteReference(Decompiler.Disassembler.OpCodeInfo opCode, bool omitSuffix = false)
{
}
public void WriteReference(PEFile module, EntityHandle handle, string text, bool isDefinition = false)
{
}
public void WriteReference(IType type, string text, bool isDefinition = false)
{
}
public void WriteReference(IMember member, string text, bool isDefinition = false)
{
}
public void WriteLocalReference(string text, object reference, bool isDefinition = false)
{
}
public void MarkFoldStart(string collapsedText = "...", bool defaultCollapsed = false)
{
}
public void MarkFoldEnd()
{
}
}
}
Loading…
Cancel
Save