Browse Source

XamlColorizer improvements

Completion improvements

git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/trunk@3965 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61
shortcuts
Siegfried Pammer 17 years ago
parent
commit
9c4f449feb
  1. 4
      src/AddIns/BackendBindings/XamlBinding/XamlBinding/CompletionDataHelper.cs
  2. 5
      src/AddIns/BackendBindings/XamlBinding/XamlBinding/XamlBinding.csproj
  3. 150
      src/AddIns/BackendBindings/XamlBinding/XamlBinding/XamlColorizer.cs
  4. 53
      src/AddIns/BackendBindings/XamlBinding/XamlBinding/XamlColorizerServer.cs
  5. 24
      src/AddIns/BackendBindings/XamlBinding/XamlBinding/XamlCompletionItemList.cs

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

@ -222,6 +222,10 @@ namespace ICSharpCode.XamlBinding @@ -222,6 +222,10 @@ namespace ICSharpCode.XamlBinding
case XamlContext.InAttributeValue:
if (entity is IProperty) {
IProperty prop = entity as IProperty;
QualifiedName item = path.Elements[path.Elements.Count - 1];
if (item.Name == "Setter" && item.Namespace == XamlNamespace) {
// TODO : add context of markup extension
}
IReturnType type = prop.ReturnType;
if (type != null) {
TypeCompletion(type.GetUnderlyingClass(), list);

5
src/AddIns/BackendBindings/XamlBinding/XamlBinding/XamlBinding.csproj

@ -115,10 +115,5 @@ @@ -115,10 +115,5 @@
<Project>{6C55B776-26D4-4DB3-A6AB-87E783B2F3D1}</Project>
<Name>ICSharpCode.AvalonEdit</Name>
</ProjectReference>
<ProjectReference Include="..\..\..\DisplayBindings\AvalonEdit.AddIn\AvalonEdit.AddIn.csproj">
<Project>{0162E499-42D0-409B-AA25-EED21F75336B}</Project>
<Name>AvalonEdit.AddIn</Name>
<Private>False</Private>
</ProjectReference>
</ItemGroup>
</Project>

150
src/AddIns/BackendBindings/XamlBinding/XamlBinding/XamlColorizer.cs

@ -5,70 +5,162 @@ @@ -5,70 +5,162 @@
// <version>$Revision$</version>
// </file>
using ICSharpCode.SharpDevelop.DefaultEditor.Gui.Editor;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.TextFormatting;
using ICSharpCode.AvalonEdit.AddIn;
using ICSharpCode.AvalonEdit.Document;
using ICSharpCode.AvalonEdit.Gui;
using ICSharpCode.SharpDevelop;
using ICSharpCode.SharpDevelop.Dom;
using ICSharpCode.SharpDevelop.Dom.Refactoring;
using ICSharpCode.SharpDevelop.Gui;
using ICSharpCode.XmlEditor;
namespace ICSharpCode.XamlBinding
{
public class XamlColorizer : ColorizingTransformer
public class XamlColorizer : DocumentColorizingTransformer
{
static readonly XamlColorizerSettings defaultSettings = new XamlColorizerSettings();
XamlColorizerSettings settings = defaultSettings;
static readonly char[] punctuationItems = {'"' }; // , '<', '>', '.', '=', '{', '}'
string fileContent;
public AvalonEditViewContent Content { get; set; }
public IViewContent Content { get; set; }
public XamlColorizer(AvalonEditViewContent content)
public XamlColorizer(IViewContent content)
{
this.Content = content;
}
protected override void Colorize(ITextRunConstructionContext context)
protected override void ColorizeLine(DocumentLine line)
{
DocumentLine line = context.VisualLine.FirstDocumentLine;
ParseInformation parseInfo = ParserService.GetParseInformation(Content.PrimaryFileName);
XamlResolver resolver = new XamlResolver();
while (line != null) {
if (!line.IsDeleted) {
int index = 0;
do {
index = line.Text.IndexOfAny(punctuationItems, index);
if (index > -1) {
int col = context.VisualLine.GetVisualColumn(index);
ChangeVisualElements(index, index + 1, ColorizePunctuation);
index++;
}
} while (index > -1);
}
IFileDocumentProvider document = this.Content as IFileDocumentProvider;
if (document == null)
return;
this.fileContent = document.GetDocumentForFile(this.Content.PrimaryFile).Text;
if (!line.IsDeleted) {
HighlightingInfo[] infos = GetInfoForLine(line);
line = line.NextLine;
foreach (HighlightingInfo info in infos) {
ResolveResult rr = resolver.Resolve(info.GetExpressionResult(), parseInfo, fileContent);
IReturnType type = (rr != null) ? rr.ResolvedType : null;
if (type != null) {
//if (type.IsGenericReturnType)
ChangeLinePart(line.Offset + info.StartOffset, line.Offset + info.EndOffset, HighlightProperty);
}
}
}
}
struct HighlightingInfo
{
string token;
int startOffset;
int endOffset;
int lineOffset;
XmlElementPath path;
public HighlightingInfo(string token, int startOffset, int endOffset, int lineOffset, XmlElementPath path)
{
this.token = token;
this.startOffset = startOffset;
this.endOffset = endOffset;
this.lineOffset = lineOffset;
this.path = path;
}
public string Token {
get { return token; }
}
public int StartOffset {
get { return startOffset; }
}
public int EndOffset {
get { return endOffset; }
}
public int LineOffset {
get { return lineOffset; }
}
public XmlElementPath Path {
get { return path; }
}
public ExpressionResult GetExpressionResult()
{
return new ExpressionResult(token, new XamlExpressionContext(path, token, false));
}
}
void ColorizePunctuation(VisualLineElement element)
HighlightingInfo[] GetInfoForLine(DocumentLine line)
{
int index = -1;
List<HighlightingInfo> infos = new List<HighlightingInfo>();
do {
index = line.Text.IndexOf('=', index + 1);
if (index > -1) {
string expr = XmlParser.GetAttributeNameAtIndex(this.fileContent, index + line.Offset);
XmlElementPath path = XmlParser.GetActiveElementStartPathAtIndex(this.fileContent, index + line.Offset);
if (!string.IsNullOrEmpty(expr) && path != null && path.Elements.Count > 0) {
int startIndex = line.Text.Substring(0, index).LastIndexOf(expr);
infos.Add(new HighlightingInfo(expr, startIndex, startIndex + expr.Length, line.Offset, path));
}
}
} while (index > -1);
return infos.ToArray();
}
void HighlightProperty(VisualLineElement element)
{
element.TextRunProperties.SetForegroundBrush(settings.PropertyForegroundBrush);
element.TextRunProperties.SetBackgroundBrush(settings.PropertyBackgroundBrush);
}
void HighlightEvent(VisualLineElement element)
{
element.TextRunProperties.SetForegroundBrush(settings.EventForegroundBrush);
element.TextRunProperties.SetBackgroundBrush(settings.EventBackgroundBrush);
}
}
public static class Extensions
{
public static string[] Split(this string s, StringSplitOptions options, params char[] delimiters)
{
element.TextRunProperties.SetForegroundBrush(settings.PunctuationForegroundBrush);
element.TextRunProperties.SetBackgroundBrush(settings.PunctuationBackgroundBrush);
return s.Split(delimiters, options);
}
}
public class XamlColorizerSettings
{
public Brush PunctuationForegroundBrush { get; set; }
public Brush PunctuationBackgroundBrush { get; set; }
public Brush PropertyForegroundBrush { get; set; }
public Brush PropertyBackgroundBrush { get; set; }
public Brush EventForegroundBrush { get; set; }
public Brush EventBackgroundBrush { get; set; }
public XamlColorizerSettings()
{
this.PunctuationBackgroundBrush = Brushes.Transparent;
this.PunctuationForegroundBrush = Brushes.Red;
this.PropertyBackgroundBrush = Brushes.Transparent;
this.PropertyForegroundBrush = Brushes.Red;
this.EventBackgroundBrush = Brushes.Transparent;
this.EventForegroundBrush = Brushes.Green;
}
}
}

53
src/AddIns/BackendBindings/XamlBinding/XamlBinding/XamlColorizerServer.cs

@ -5,12 +5,12 @@ @@ -5,12 +5,12 @@
// <version>$Revision$</version>
// </file>
using ICSharpCode.AvalonEdit.Gui;
using ICSharpCode.SharpDevelop;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using ICSharpCode.AvalonEdit.AddIn;
using ICSharpCode.AvalonEdit.Gui;
using ICSharpCode.Core;
using ICSharpCode.SharpDevelop.Gui;
@ -28,48 +28,15 @@ namespace ICSharpCode.XamlBinding @@ -28,48 +28,15 @@ namespace ICSharpCode.XamlBinding
static void WorkbenchSingleton_Workbench_ViewOpened(object sender, ViewContentEventArgs e)
{
if (e.Content is AvalonEditViewContent) {
AvalonEditViewContent content = e.Content as AvalonEditViewContent;
if (!Path.GetExtension(content.CodeEditor.FileName).Equals(".xaml", StringComparison.OrdinalIgnoreCase))
return;
content.CodeEditor.TextArea.TextView.LineTransformers.Insert(1, new XamlColorizer(content));
}
}
}
public class ColorizerDoozer : IDoozer
{
public bool HandleConditions {
get {
return false;
}
}
public object BuildItem(object caller, Codon codon, System.Collections.ArrayList subItems)
{
return new ColorizerDescriptor(codon);
}
}
public class ColorizerDescriptor
{
Codon codon;
ColorizingTransformer colorizer;
public ColorizingTransformer Colorizer {
get {
if (colorizer == null)
this.colorizer = (ColorizingTransformer)codon.AddIn.CreateObject(codon.Properties["class"]);
return colorizer;
if (!Path.GetExtension(e.Content.PrimaryFileName).Equals(".xaml", StringComparison.OrdinalIgnoreCase))
return;
ITextEditorProvider textEditor = e.Content as ITextEditorProvider;
if (textEditor != null) {
TextView textView = textEditor.TextEditor.GetService(typeof(TextView)) as TextView;
if (textView != null) {
textView.LineTransformers.Add(new XamlColorizer(e.Content));
}
}
}
public ColorizerDescriptor(Codon codon)
{
this.codon = codon;
}
}
}

24
src/AddIns/BackendBindings/XamlBinding/XamlBinding/XamlCompletionItemList.cs

@ -5,11 +5,12 @@ @@ -5,11 +5,12 @@
// <version>$Revision: 3731 $</version>
// </file>
using ICSharpCode.SharpDevelop.Gui;
using System;
using System.Linq;
using ICSharpCode.AvalonEdit.AddIn;
using ICSharpCode.NRefactory.Ast;
using ICSharpCode.SharpDevelop;
using ICSharpCode.SharpDevelop.DefaultEditor.Gui.Editor;
using ICSharpCode.SharpDevelop.Dom;
using ICSharpCode.SharpDevelop.Dom.Refactoring;
using ICSharpCode.XmlEditor;
@ -71,6 +72,15 @@ namespace ICSharpCode.XamlBinding @@ -71,6 +72,15 @@ namespace ICSharpCode.XamlBinding
insertionString += "Property=\"\"";
context.Editor.Document.Insert(context.EndOffset, insertionString);
context.Editor.Caret.Offset = context.EndOffset + insertionString.Length - 1;
XmlElementPath path = XmlParser.GetActiveElementStartPathAtIndex(context.Editor.Document.Text, context.Editor.Caret.Offset);
if (path != null && path.Elements.Count > 0) {
path.Elements.RemoveLast();
path.Elements.Add(new QualifiedName("Setter", CompletionDataHelper.XamlNamespace));
IEntity newEntity = new DefaultProperty(c, "Property");
ICompletionItemList list = CompletionDataHelper.CreateListForContext(context.Editor, XamlContext.InAttributeValue, path, newEntity);
context.Editor.ShowCompletionWindow(list);
}
}
}
} else {
@ -117,16 +127,14 @@ namespace ICSharpCode.XamlBinding @@ -117,16 +127,14 @@ namespace ICSharpCode.XamlBinding
node.Modifier = Modifiers.None;
AvalonEditViewContent viewContent = FileService.OpenFile(part.CompilationUnit.FileName) as AvalonEditViewContent;
// TODO : shouldn't we be able to use viewContent.CodeEditor.textEditorAdapter here? (Property missing?)
ITextEditor wrapper = new CodeEditorAdapter(viewContent.CodeEditor);
IViewContent viewContent = FileService.OpenFile(part.CompilationUnit.FileName);
IFileDocumentProvider document = viewContent as IFileDocumentProvider;
if (viewContent != null) {
if (viewContent != null || document != null) {
if (lastMember != null)
unit.ProjectContent.Language.CodeGenerator.InsertCodeAfter(lastMember, wrapper.Document, node);
unit.ProjectContent.Language.CodeGenerator.InsertCodeAfter(lastMember, document.GetDocumentForFile(viewContent.PrimaryFile), node);
else
unit.ProjectContent.Language.CodeGenerator.InsertCodeAtEnd(part.Region, wrapper.Document, node);
unit.ProjectContent.Language.CodeGenerator.InsertCodeAtEnd(part.Region, document.GetDocumentForFile(viewContent.PrimaryFile), node);
}
return;
}

Loading…
Cancel
Save