Browse Source

ported XamlBinding to Mirador

git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/trunk@3928 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61
shortcuts
Siegfried Pammer 16 years ago
parent
commit
d61419f6bc
  1. 1
      src/AddIns/BackendBindings/XamlBinding/XamlBinding.Tests/Properties/AssemblyInfo.cs
  2. 75
      src/AddIns/BackendBindings/XamlBinding/XamlBinding.Tests/XamlBinding.Tests.csproj
  3. 48
      src/AddIns/BackendBindings/XamlBinding/XamlBinding.Tests/XamlExpressionFinderTests.cs
  4. 10
      src/AddIns/BackendBindings/XamlBinding/XamlBinding/Properties/AssemblyInfo.cs
  5. 44
      src/AddIns/BackendBindings/XamlBinding/XamlBinding/XamlBinding.addin
  6. 97
      src/AddIns/BackendBindings/XamlBinding/XamlBinding/XamlBinding.csproj
  7. 45
      src/AddIns/BackendBindings/XamlBinding/XamlBinding/XamlClassReturnType.cs
  8. 56
      src/AddIns/BackendBindings/XamlBinding/XamlBinding/XamlCodeCompletionBinding.cs
  9. 128
      src/AddIns/BackendBindings/XamlBinding/XamlBinding/XamlCompilationUnit.cs
  10. 54
      src/AddIns/BackendBindings/XamlBinding/XamlBinding/XamlCompletionItemProvider.cs
  11. 63
      src/AddIns/BackendBindings/XamlBinding/XamlBinding/XamlExpressionContext.cs
  12. 75
      src/AddIns/BackendBindings/XamlBinding/XamlBinding/XamlExpressionFinder.cs
  13. 151
      src/AddIns/BackendBindings/XamlBinding/XamlBinding/XamlParser.cs
  14. 350
      src/AddIns/BackendBindings/XamlBinding/XamlBinding/XamlResolver.cs

1
src/AddIns/BackendBindings/XamlBinding/XamlBinding.Tests/Properties/AssemblyInfo.cs

@ -0,0 +1 @@ @@ -0,0 +1 @@


75
src/AddIns/BackendBindings/XamlBinding/XamlBinding.Tests/XamlBinding.Tests.csproj

@ -0,0 +1,75 @@ @@ -0,0 +1,75 @@
<Project ToolsVersion="3.5" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<ProjectGuid>{F390DA70-1FE1-4715-81A0-389AB010C130}</ProjectGuid>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<OutputType>Library</OutputType>
<RootNamespace>ICSharpCode.XamlBinding.Tests</RootNamespace>
<AssemblyName>ICSharpCode.XamlBinding.Tests</AssemblyName>
<TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
<AppDesignerFolder>Properties</AppDesignerFolder>
<SourceAnalysisOverrideSettingsFile>C:\Users\Daniel\AppData\Roaming\ICSharpCode/SharpDevelop3.0\Settings.SourceAnalysis</SourceAnalysisOverrideSettingsFile>
<OutputPath>..\..\..\..\..\bin\UnitTests\</OutputPath>
<AllowUnsafeBlocks>False</AllowUnsafeBlocks>
<NoStdLib>False</NoStdLib>
<WarningLevel>4</WarningLevel>
<TreatWarningsAsErrors>false</TreatWarningsAsErrors>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>Full</DebugType>
<Optimize>False</Optimize>
<CheckForOverflowUnderflow>True</CheckForOverflowUnderflow>
<DefineConstants>DEBUG;TRACE</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
<DebugSymbols>False</DebugSymbols>
<DebugType>None</DebugType>
<Optimize>True</Optimize>
<CheckForOverflowUnderflow>False</CheckForOverflowUnderflow>
<DefineConstants>TRACE</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition=" '$(Platform)' == 'AnyCPU' ">
<RegisterForComInterop>False</RegisterForComInterop>
<GenerateSerializationAssemblies>Auto</GenerateSerializationAssemblies>
<BaseAddress>4194304</BaseAddress>
<PlatformTarget>AnyCPU</PlatformTarget>
<FileAlignment>4096</FileAlignment>
</PropertyGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.Targets" />
<ItemGroup>
<Reference Include="nunit.framework">
<HintPath>..\..\..\..\Tools\NUnit\nunit.framework.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core">
<RequiredTargetFramework>3.5</RequiredTargetFramework>
</Reference>
<Reference Include="System.Xml" />
<Reference Include="System.Xml.Linq">
<RequiredTargetFramework>3.5</RequiredTargetFramework>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="..\..\..\..\Main\GlobalAssemblyInfo.cs">
<Link>Properties\GlobalAssemblyInfo.cs</Link>
</Compile>
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="XamlExpressionFinderTests.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\..\Main\ICSharpCode.SharpDevelop.Dom\Project\ICSharpCode.SharpDevelop.Dom.csproj">
<Project>{924EE450-603D-49C1-A8E5-4AFAA31CE6F3}</Project>
<Name>ICSharpCode.SharpDevelop.Dom</Name>
</ProjectReference>
<ProjectReference Include="..\..\..\DisplayBindings\XmlEditor\Project\XmlEditor.csproj">
<Project>{6B717BD1-CD5E-498C-A42E-9E6A4584DC48}</Project>
<Name>XmlEditor</Name>
</ProjectReference>
<ProjectReference Include="..\XamlBinding\XamlBinding.csproj">
<Project>{7C96B65D-28A5-4F28-A35B-8D83CE831EE8}</Project>
<Name>XamlBinding</Name>
</ProjectReference>
</ItemGroup>
</Project>

48
src/AddIns/BackendBindings/XamlBinding/XamlBinding.Tests/XamlExpressionFinderTests.cs

@ -0,0 +1,48 @@ @@ -0,0 +1,48 @@
// <file>
// <copyright see="prj:///doc/copyright.txt"/>
// <license see="prj:///doc/license.txt"/>
// <author name="Daniel Grunwald"/>
// <version>$Revision: 3494 $</version>
// </file>
using ICSharpCode.SharpDevelop.Dom;
using System;
using NUnit.Framework;
using NUnit.Framework.SyntaxHelpers;
namespace ICSharpCode.XamlBinding.Tests
{
[TestFixture]
public class XamlExpressionFinderTests
{
XamlExpressionContext GetXamlContext(string text)
{
return (XamlExpressionContext)GetContext(text);
}
ExpressionContext GetContext(string text)
{
return XamlExpressionFinder.Instance.FindExpression(text, text.Length).Context;
}
[Test]
public void FindContextAfterElementName()
{
XamlExpressionContext c = GetXamlContext("<Window><Grid");
Assert.AreEqual(2, c.ElementPath.Elements.Count);
Assert.AreEqual("Window > Grid", c.ElementPath.ToString());
Assert.IsNull(c.AttributeName);
Assert.IsFalse(c.InAttributeValue);
}
[Test]
public void FindContextAtElementStart()
{
XamlExpressionContext c = GetXamlContext("<Window><");
Assert.AreEqual(0, c.ElementPath.Elements.Count);
Assert.IsNull(c.AttributeName);
Assert.IsFalse(c.InAttributeValue);
}
}
}

10
src/AddIns/BackendBindings/XamlBinding/XamlBinding/Properties/AssemblyInfo.cs

@ -0,0 +1,10 @@ @@ -0,0 +1,10 @@
// <file>
// <copyright see="prj:///doc/copyright.txt"/>
// <license see="prj:///doc/license.txt"/>
// <owner name="Matthew Ward" email="mrward@users.sourceforge.net"/>
// <version>$Revision: 2564 $</version>
// </file>
using System.Reflection;
[assembly: AssemblyDescription("Provides XAML integration in code-completion")]

44
src/AddIns/BackendBindings/XamlBinding/XamlBinding/XamlBinding.addin

@ -0,0 +1,44 @@ @@ -0,0 +1,44 @@
<AddIn name = "XAML binding"
author = "Daniel Grunwald"
copyright = "prj:///doc/copyright.txt"
url = "http://icsharpcode.net"
description = "Enables code-completion for xaml files."
addInManagerHidden = "preinstalled">
<!--
This file is unrelated to the WPF Designer, it is a language binding for .xaml files.
It allows using Go to definition, find references, code completion in .xaml files.
-->
<Manifest>
<Identity name = "ICSharpCode.XamlBinding"/>
<Dependency addin = "ICSharpCode.XmlEditor" requirePreload = "true"/>
</Manifest>
<Runtime>
<Import assembly = "ICSharpCode.XamlBinding.dll"/>
</Runtime>
<Path name = "/Workspace/Icons">
<Icon id = "XamlFileIcon"
extensions = ".xaml"
resource = "FileIcons.XmlIcon" />
</Path>
<Path name = "/SharpDevelop/Workbench/FileFilter">
<FileFilter id = "Xaml"
insertbefore="AllFiles"
name = "Xaml files (*.xaml)"
extensions = "*.xaml"/>
</Path>
<Path name = "/Workspace/Parser">
<Parser id = "XAML"
supportedextensions = ".xaml"
class = "ICSharpCode.XamlBinding.XamlParser"/>
</Path>
<Path name = "/AddIns/DefaultTextEditor/CodeCompletion">
<CodeCompletionBinding id = "Xaml" extensions = ".xaml" class = "ICSharpCode.XamlBinding.XamlCodeCompletionBinding"/>
</Path>
</AddIn>

97
src/AddIns/BackendBindings/XamlBinding/XamlBinding/XamlBinding.csproj

@ -0,0 +1,97 @@ @@ -0,0 +1,97 @@
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="3.5">
<PropertyGroup>
<ProjectGuid>{7C96B65D-28A5-4F28-A35B-8D83CE831EE8}</ProjectGuid>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<OutputType>Library</OutputType>
<RootNamespace>ICSharpCode.XamlBinding</RootNamespace>
<AssemblyName>ICSharpCode.XamlBinding</AssemblyName>
<OutputPath>..\..\..\..\..\AddIns\AddIns\BackendBindings\XamlBinding</OutputPath>
<AllowUnsafeBlocks>False</AllowUnsafeBlocks>
<NoStdLib>False</NoStdLib>
<WarningLevel>4</WarningLevel>
<TreatWarningsAsErrors>false</TreatWarningsAsErrors>
<SourceAnalysisOverrideSettingsFile>C:\Users\Daniel\AppData\Roaming\ICSharpCode/SharpDevelop3.0\Settings.SourceAnalysis</SourceAnalysisOverrideSettingsFile>
<TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>Full</DebugType>
<CheckForOverflowUnderflow>True</CheckForOverflowUnderflow>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<Optimize>False</Optimize>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
<DebugSymbols>False</DebugSymbols>
<DebugType>None</DebugType>
<CheckForOverflowUnderflow>False</CheckForOverflowUnderflow>
<DefineConstants>TRACE</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition=" '$(Platform)' == 'AnyCPU' ">
<RegisterForComInterop>False</RegisterForComInterop>
<GenerateSerializationAssemblies>Auto</GenerateSerializationAssemblies>
<BaseAddress>4194304</BaseAddress>
<PlatformTarget>AnyCPU</PlatformTarget>
<FileAlignment>4096</FileAlignment>
</PropertyGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.Targets" />
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core">
<RequiredTargetFramework>3.5</RequiredTargetFramework>
</Reference>
<Reference Include="System.Data" />
<Reference Include="System.Windows.Forms" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<None Include="XamlBinding.addin">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<Compile Include="..\..\..\..\Main\GlobalAssemblyInfo.cs">
<Link>Properties\GlobalAssemblyInfo.cs</Link>
</Compile>
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="XamlClassReturnType.cs" />
<Compile Include="XamlCodeCompletionBinding.cs" />
<Compile Include="XamlCompilationUnit.cs" />
<Compile Include="XamlCompletionItemProvider.cs" />
<Compile Include="XamlExpressionContext.cs" />
<Compile Include="XamlExpressionFinder.cs" />
<Compile Include="XamlParser.cs" />
<Compile Include="XamlResolver.cs" />
<ProjectReference Include="..\..\..\..\Libraries\ICSharpCode.TextEditor\Project\ICSharpCode.TextEditor.csproj">
<Project>{2D18BE89-D210-49EB-A9DD-2246FBB3DF6D}</Project>
<Name>ICSharpCode.TextEditor</Name>
<Private>False</Private>
</ProjectReference>
<ProjectReference Include="..\..\..\..\Libraries\NRefactory\Project\NRefactory.csproj">
<Project>{3A9AE6AA-BC07-4A2F-972C-581E3AE2F195}</Project>
<Name>NRefactory</Name>
<Private>False</Private>
</ProjectReference>
<ProjectReference Include="..\..\..\..\Main\Base\Project\ICSharpCode.SharpDevelop.csproj">
<Project>{2748AD25-9C63-4E12-877B-4DCE96FBED54}</Project>
<Name>ICSharpCode.SharpDevelop</Name>
<Private>False</Private>
</ProjectReference>
<ProjectReference Include="..\..\..\..\Main\Core\Project\ICSharpCode.Core.csproj">
<Project>{35CEF10F-2D4C-45F2-9DD1-161E0FEC583C}</Project>
<Name>ICSharpCode.Core</Name>
<Private>False</Private>
</ProjectReference>
<ProjectReference Include="..\..\..\..\Main\ICSharpCode.SharpDevelop.Dom\Project\ICSharpCode.SharpDevelop.Dom.csproj">
<Project>{924EE450-603D-49C1-A8E5-4AFAA31CE6F3}</Project>
<Name>ICSharpCode.SharpDevelop.Dom</Name>
<Private>False</Private>
</ProjectReference>
<ProjectReference Include="..\..\..\DisplayBindings\XmlEditor\Project\XmlEditor.csproj">
<Project>{6B717BD1-CD5E-498C-A42E-9E6A4584DC48}</Project>
<Name>XmlEditor</Name>
<Private>False</Private>
</ProjectReference>
</ItemGroup>
<!--<Target Name="AfterBuild">
<Copy SourceFiles="@(CopyFile)" DestinationFolder="$(OutputPath)" />
</Target>-->
</Project>

45
src/AddIns/BackendBindings/XamlBinding/XamlBinding/XamlClassReturnType.cs

@ -0,0 +1,45 @@ @@ -0,0 +1,45 @@
// <file>
// <copyright see="prj:///doc/copyright.txt"/>
// <license see="prj:///doc/license.txt"/>
// <author name="Daniel Grunwald"/>
// <version>$Revision: 2569 $</version>
// </file>
using System;
using ICSharpCode.SharpDevelop.Dom;
namespace ICSharpCode.XamlBinding
{
/// <summary>
/// Description of XamlClassReturnType.
/// </summary>
public class XamlClassReturnType : ProxyReturnType
{
XamlCompilationUnit compilationUnit;
string xmlNamespace;
string className;
public XamlClassReturnType(XamlCompilationUnit compilationUnit, string xmlNamespace, string className)
{
if (compilationUnit == null)
throw new ArgumentNullException("compilationUnit");
this.compilationUnit = compilationUnit;
this.xmlNamespace = xmlNamespace;
this.className = className ?? "";
}
public override IReturnType BaseType
{
get
{
return compilationUnit.FindType(xmlNamespace, className);
}
}
public override string Name
{
get { return className; }
}
}
}

56
src/AddIns/BackendBindings/XamlBinding/XamlBinding/XamlCodeCompletionBinding.cs

@ -0,0 +1,56 @@ @@ -0,0 +1,56 @@
// <file>
// <copyright see="prj:///doc/copyright.txt"/>
// <license see="prj:///doc/license.txt"/>
// <author name="Daniel Grunwald"/>
// <version>$Revision: 3494 $</version>
// </file>
using ICSharpCode.SharpDevelop;
using System;
using ICSharpCode.SharpDevelop.DefaultEditor.Gui.Editor;
using ICSharpCode.SharpDevelop.Dom;
using ICSharpCode.SharpDevelop.Gui;
using ICSharpCode.TextEditor.Gui.CompletionWindow;
using ICSharpCode.XmlEditor;
namespace ICSharpCode.XamlBinding
{
public class XamlCodeCompletionBinding : ICodeCompletionBinding
{
public CodeCompletionKeyPressResult HandleKeyPress(ICSharpCode.SharpDevelop.ITextEditor editor, char ch)
{
XamlCompletionItemProvider provider;
switch (ch) {
case '<':
provider = new XamlCompletionItemProvider(XamlExpressionContext.Empty);
provider.ShowCompletion(editor);
return CodeCompletionKeyPressResult.Completed;
default:
if (char.IsLetter(ch)) {
int offset = editor.Caret.Offset;
if (offset > 0) {
char c = editor.Document.GetCharAt(offset - 1);
if (c == ' ' || c == '\t') {
XmlElementPath path = XmlParser.GetActiveElementStartPathAtIndex(editor.Document.Text, offset);
if (path != null && path.Elements.Count > 0) {
provider = new XamlCompletionItemProvider(new XamlExpressionContext(path, "", false));
provider.ShowCompletion(editor);
return CodeCompletionKeyPressResult.CompletedIncludeKeyInCompletion;
}
}
}
}
break;
}
return CodeCompletionKeyPressResult.None;
}
public bool CtrlSpace(ICSharpCode.SharpDevelop.ITextEditor editor)
{
XamlCompletionItemProvider provider = new XamlCompletionItemProvider(XamlExpressionContext.Empty);
provider.ShowCompletion(editor);
return true;
}
}
}

128
src/AddIns/BackendBindings/XamlBinding/XamlBinding/XamlCompilationUnit.cs

@ -0,0 +1,128 @@ @@ -0,0 +1,128 @@
// <file>
// <copyright see="prj:///doc/copyright.txt"/>
// <license see="prj:///doc/license.txt"/>
// <author name="Daniel Grunwald"/>
// <version>$Revision: 3494 $</version>
// </file>
using System;
using ICSharpCode.SharpDevelop.Dom;
using System.Collections;
using System.Collections.Generic;
namespace ICSharpCode.XamlBinding
{
/// <summary>
/// Description of XamlCompilationUnit.
/// </summary>
public class XamlCompilationUnit : DefaultCompilationUnit
{
public XamlCompilationUnit(IProjectContent projectContent)
: base(projectContent)
{
}
public IReturnType CreateType(string xmlNamespace, string className)
{
if (xmlNamespace.StartsWith("clr-namespace:")) {
return CreateClrNamespaceType(this.ProjectContent, xmlNamespace, className);
}
else {
return new XamlClassReturnType(this, xmlNamespace, className);
}
}
static IReturnType CreateClrNamespaceType(IProjectContent pc, string xmlNamespace, string className)
{
string namespaceName = GetNamespaceNameFromClrNamespace(xmlNamespace);
return new GetClassReturnType(pc, namespaceName + "." + className, 0);
}
static string GetNamespaceNameFromClrNamespace(string xmlNamespace)
{
string namespaceName = xmlNamespace.Substring("clr-namespace:".Length);
int pos = namespaceName.IndexOf(';');
if (pos >= 0) {
// we expect that the target type is also a reference of the project, so we
// can ignore the assembly part after the ;
namespaceName = namespaceName.Substring(0, pos);
}
return namespaceName;
}
public IReturnType FindType(string xmlNamespace, string className)
{
return FindType(this.ProjectContent, xmlNamespace, className);
}
public static IReturnType FindType(IProjectContent pc, string xmlNamespace, string className)
{
if (pc == null)
throw new ArgumentNullException("pc");
if (xmlNamespace == null || className == null)
return null;
if (xmlNamespace.StartsWith("clr-namespace:")) {
return CreateClrNamespaceType(pc, xmlNamespace, className);
}
else {
IReturnType type = FindTypeInAssembly(pc, xmlNamespace, className);
if (type != null)
return type;
foreach (IProjectContent p in pc.ReferencedContents) {
type = FindTypeInAssembly(p, xmlNamespace, className);
if (type != null)
return type;
}
return null;
}
}
static IReturnType FindTypeInAssembly(IProjectContent projectContent, string xmlNamespace, string className)
{
foreach (IAttribute att in projectContent.GetAssemblyAttributes()) {
if (att.PositionalArguments.Count == 2
&& att.AttributeType.FullyQualifiedName == "System.Windows.Markup.XmlnsDefinitionAttribute") {
string namespaceName = att.PositionalArguments[1] as string;
if (xmlNamespace.Equals(att.PositionalArguments[0]) && namespaceName != null) {
IClass c = projectContent.GetClass(namespaceName + "." + className, 0);
if (c != null)
return c.DefaultReturnType;
}
}
}
return null;
}
public static ArrayList GetNamespaceMembers(IProjectContent pc, string xmlNamespace)
{
if (pc == null)
throw new ArgumentNullException("pc");
if (xmlNamespace == null)
return null;
if (xmlNamespace.StartsWith("clr-namespace:")) {
return pc.GetNamespaceContents(GetNamespaceNameFromClrNamespace(xmlNamespace));
}
else {
ArrayList list = new ArrayList();
AddNamespaceMembersInAssembly(pc, xmlNamespace, list);
foreach (IProjectContent p in pc.ReferencedContents) {
AddNamespaceMembersInAssembly(p, xmlNamespace, list);
}
return list;
}
}
static void AddNamespaceMembersInAssembly(IProjectContent projectContent, string xmlNamespace, ArrayList list)
{
foreach (IAttribute att in projectContent.GetAssemblyAttributes()) {
if (att.PositionalArguments.Count == 2
&& att.AttributeType.FullyQualifiedName == "System.Windows.Markup.XmlnsDefinitionAttribute") {
string namespaceName = att.PositionalArguments[1] as string;
if (xmlNamespace.Equals(att.PositionalArguments[0]) && namespaceName != null) {
projectContent.AddNamespaceContents(list, namespaceName, projectContent.Language, false);
}
}
}
}
}
}

54
src/AddIns/BackendBindings/XamlBinding/XamlBinding/XamlCompletionItemProvider.cs

@ -0,0 +1,54 @@ @@ -0,0 +1,54 @@
// <file>
// <copyright see="prj:///doc/copyright.txt"/>
// <license see="prj:///doc/license.txt"/>
// <author name="Daniel Grunwald"/>
// <version>$Revision: 3494 $</version>
// </file>
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using ICSharpCode.SharpDevelop;
using ICSharpCode.SharpDevelop.DefaultEditor.Gui.Editor;
using ICSharpCode.SharpDevelop.Dom;
using ICSharpCode.SharpDevelop.Gui;
using ICSharpCode.SharpDevelop.Project;
using ICSharpCode.TextEditor.Gui.CompletionWindow;
namespace ICSharpCode.XamlBinding
{
sealed class XamlCompletionItemProvider : CtrlSpaceCompletionItemProvider
{
public XamlCompletionItemProvider()
{
}
public XamlCompletionItemProvider(XamlExpressionContext context)
: base(context)
{
}
public override ICompletionItemList GenerateCompletionListForCompletionData(ArrayList arr, ExpressionContext context)
{
DefaultCompletionItemList list = new DefaultCompletionItemList();
list.Items.AddRange(base.GenerateCompletionListForCompletionData(arr, context).Items);
if (context is XamlExpressionContext) {
XamlExpressionContext xContext = context as XamlExpressionContext;
if (string.IsNullOrEmpty(xContext.AttributeName) && !xContext.InAttributeValue) {
list.Items.Add(new DefaultCompletionItem("!--"));
list.Items.Add(new DefaultCompletionItem("![CDATA["));
list.Items.Add(new DefaultCompletionItem("?"));
}
}
list.SortItems();
return list;
}
}
}

63
src/AddIns/BackendBindings/XamlBinding/XamlBinding/XamlExpressionContext.cs

@ -0,0 +1,63 @@ @@ -0,0 +1,63 @@
// <file>
// <copyright see="prj:///doc/copyright.txt"/>
// <license see="prj:///doc/license.txt"/>
// <author name="Daniel Grunwald"/>
// <version>$Revision: 3494 $</version>
// </file>
using System;
using System.Text;
using ICSharpCode.SharpDevelop.Dom;
using ICSharpCode.XmlEditor;
namespace ICSharpCode.XamlBinding
{
/// <summary>
/// Represents the context of a location in a XAML document.
/// </summary>
public sealed class XamlExpressionContext : ExpressionContext
{
public static readonly XamlExpressionContext Empty = new XamlExpressionContext(new XmlElementPath(), null, false);
public readonly XmlElementPath ElementPath;
public readonly string AttributeName;
public readonly bool InAttributeValue;
public XamlExpressionContext(XmlElementPath elementPath, string attributeName, bool inAttributeValue)
{
if (elementPath == null)
throw new ArgumentNullException("elementPath");
this.ElementPath = elementPath;
this.AttributeName = attributeName;
this.InAttributeValue = inAttributeValue;
}
public override bool ShowEntry(object o)
{
return true;
}
public override string ToString()
{
StringBuilder b = new StringBuilder();
b.Append("[XamlExpressionContext ");
for (int i = 0; i < ElementPath.Elements.Count; i++) {
if (i > 0) b.Append(">");
if (!string.IsNullOrEmpty(ElementPath.Elements[i].Prefix)) {
b.Append(ElementPath.Elements[i].Prefix);
b.Append(':');
}
b.Append(ElementPath.Elements[i].Name);
}
if (AttributeName != null) {
b.Append(" AttributeName=");
b.Append(AttributeName);
if (InAttributeValue) {
b.Append(" InAttributeValue");
}
}
b.Append("]");
return b.ToString();
}
}
}

75
src/AddIns/BackendBindings/XamlBinding/XamlBinding/XamlExpressionFinder.cs

@ -0,0 +1,75 @@ @@ -0,0 +1,75 @@
// <file>
// <copyright see="prj:///doc/copyright.txt"/>
// <license see="prj:///doc/license.txt"/>
// <author name="Daniel Grunwald"/>
// <version>$Revision: 3494 $</version>
// </file>
using System;
using System.Text;
using System.Xml;
using ICSharpCode.Core;
using ICSharpCode.SharpDevelop.Dom;
using ICSharpCode.XmlEditor;
namespace ICSharpCode.XamlBinding
{
/// <summary>
/// Description of XamlExpressionFinder.
/// </summary>
public class XamlExpressionFinder : IExpressionFinder
{
public static readonly XamlExpressionFinder Instance = new XamlExpressionFinder();
bool IsValidChar(char c)
{
return char.IsLetterOrDigit(c) || c == '_' || c == ':' || c == '.';
}
public ExpressionResult FindExpression(string text, int offset)
{
int pos = offset - 1;
while (pos > 0 && IsValidChar(text[pos])) {
pos--;
}
pos++;
return new ExpressionResult(text.Substring(pos, offset - pos), GetContext(text, offset));
}
public ExpressionResult FindFullExpression(string text, int offset)
{
int start = offset - 1;
while (start > 0 && IsValidChar(text[start])) {
start--;
}
start++;
while (offset < text.Length && IsValidChar(text[offset])) {
offset++;
}
return new ExpressionResult(text.Substring(start, offset - start), GetContext(text, offset));
}
public string RemoveLastPart(string expression)
{
return "";
}
ExpressionContext GetContext(string text, int offset)
{
XmlElementPath path = XmlParser.GetActiveElementStartPathAtIndex(text, offset);
if (path == null || path.Elements.Count == 0) {
if (offset > 0 && text[offset - 1] == '<')
return XamlExpressionContext.Empty;
else
return ExpressionContext.Default;
}
string attributeName = XmlParser.GetAttributeNameAtIndex(text, offset);
if (!string.IsNullOrEmpty(attributeName)) {
return new XamlExpressionContext(path, attributeName, XmlParser.IsInsideAttributeValue(text, offset));
}
else {
return new XamlExpressionContext(path, null, false);
}
}
}
}

151
src/AddIns/BackendBindings/XamlBinding/XamlBinding/XamlParser.cs

@ -0,0 +1,151 @@ @@ -0,0 +1,151 @@
// <file>
// <copyright see="prj:///doc/copyright.txt"/>
// <license see="prj:///doc/license.txt"/>
// <author name="Daniel Grunwald"/>
// <version>$Revision: 2568 $</version>
// </file>
using System;
using System.Diagnostics;
using System.IO;
using System.Xml;
using ICSharpCode.Core;
using ICSharpCode.SharpDevelop.Dom;
namespace ICSharpCode.XamlBinding
{
/// <summary>
/// Parses xaml files to partial classes for the Dom.
/// </summary>
public class XamlParser : IParser
{
string[] lexerTags;
public string[] LexerTags
{
get
{
return lexerTags;
}
set
{
lexerTags = value;
}
}
public LanguageProperties Language
{
get
{
return LanguageProperties.CSharp;
}
}
public bool CanParse(string fileName)
{
return Path.GetExtension(fileName).Equals(".xaml", StringComparison.OrdinalIgnoreCase);
}
public bool CanParse(ICSharpCode.SharpDevelop.Project.IProject project)
{
return false;
}
const string XamlNamespace = "http://schemas.microsoft.com/winfx/2006/xaml";
public ICompilationUnit Parse(IProjectContent projectContent, string fileName, string fileContent)
{
XamlCompilationUnit cu = new XamlCompilationUnit(projectContent);
cu.FileName = fileName;
try {
using (XmlTextReader r = new XmlTextReader(new StringReader(fileContent))) {
r.WhitespaceHandling = WhitespaceHandling.Significant;
r.Read();
r.MoveToContent();
DomRegion classStart = new DomRegion(r.LineNumber, r.LinePosition - 1);
string className = r.GetAttribute("Class", XamlNamespace);
if (string.IsNullOrEmpty(className)) {
LoggingService.Debug("XamlParser: returning empty cu because root element has no Class attribute");
}
else {
DefaultClass c = new DefaultClass(cu, className);
c.Modifiers = ModifierEnum.Partial;
c.Region = classStart;
c.BaseTypes.Add(TypeFromXmlNode(cu, r));
cu.Classes.Add(c);
DefaultMethod initializeComponent = new DefaultMethod(
"InitializeComponent",
projectContent.SystemTypes.Void,
ModifierEnum.Public | ModifierEnum.Synthetic,
classStart, DomRegion.Empty,
c);
c.Methods.Add(initializeComponent);
ParseXamlElement(cu, c, r);
if (r.NodeType == XmlNodeType.EndElement) {
c.Region = new DomRegion(classStart.BeginLine, classStart.BeginColumn, r.LineNumber, r.LinePosition + r.Name.Length);
}
}
}
}
catch (XmlException ex) {
LoggingService.Debug("XamlParser exception: " + ex.ToString());
cu.ErrorsDuringCompile = true;
}
return cu;
}
IReturnType TypeFromXmlNode(XamlCompilationUnit cu, XmlReader r)
{
return cu.CreateType(r.NamespaceURI, r.LocalName);
}
void ParseXamlElement(XamlCompilationUnit cu, DefaultClass c, XmlTextReader r)
{
Debug.Assert(r.NodeType == XmlNodeType.Element);
string name = r.GetAttribute("Name", XamlNamespace) ?? r.GetAttribute("Name");
bool isEmptyElement = r.IsEmptyElement;
if (!string.IsNullOrEmpty(name)) {
IReturnType type = TypeFromXmlNode(cu, r);
// Use position of Name attribute for field region
//if (!r.MoveToAttribute("Name", XamlNamespace)) {
// r.MoveToAttribute("Name");
//}
DomRegion position = new DomRegion(r.LineNumber, r.LinePosition, r.LineNumber, r.LinePosition + name.Length);
c.Fields.Add(new DefaultField(type, name, ModifierEnum.Internal, position, c));
}
if (isEmptyElement)
return;
while (r.Read()) {
if (r.NodeType == XmlNodeType.Element) {
ParseXamlElement(cu, c, r);
}
else if (r.NodeType == XmlNodeType.Comment) {
foreach (string tag in lexerTags) {
if (r.Value.Contains(tag)) {
cu.TagComments.Add(new TagComment(r.Value, new DomRegion(r.LineNumber, r.LinePosition, r.LineNumber, r.LinePosition + r.Value.Length)));
break;
}
}
}
else if (r.NodeType == XmlNodeType.EndElement) {
break;
}
}
}
public IExpressionFinder CreateExpressionFinder(string fileName)
{
return XamlExpressionFinder.Instance;
}
public IResolver CreateResolver()
{
return new XamlResolver();
}
}
}

350
src/AddIns/BackendBindings/XamlBinding/XamlBinding/XamlResolver.cs

@ -0,0 +1,350 @@ @@ -0,0 +1,350 @@
// <file>
// <copyright see="prj:///doc/copyright.txt"/>
// <license see="prj:///doc/license.txt"/>
// <author name="Daniel Grunwald"/>
// <version>$Revision: 3539 $</version>
// </file>
using ICSharpCode.XmlEditor;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Xml;
using ICSharpCode.Core;
using ICSharpCode.SharpDevelop.Dom;
namespace ICSharpCode.XamlBinding
{
/// <summary>
/// Description of XamlResolver.
/// </summary>
public class XamlResolver : IResolver
{
IClass callingClass;
string resolveExpression;
XamlExpressionContext context;
ParseInformation parseInfo;
int caretLineNumber, caretColumn;
bool IsReaderAtTarget(XmlTextReader r)
{
if (r.LineNumber > caretLineNumber)
return true;
else if (r.LineNumber == caretLineNumber)
return r.LinePosition >= caretColumn;
else
return false;
}
public ResolveResult Resolve(ExpressionResult expressionResult, ParseInformation parseInfo, string fileContent)
{
this.resolveExpression = expressionResult.Expression;
this.parseInfo = parseInfo;
this.caretLineNumber = expressionResult.Region.BeginLine;
this.caretColumn = expressionResult.Region.BeginColumn;
this.callingClass = parseInfo.BestCompilationUnit.GetInnermostClass(caretLineNumber, caretColumn);
this.context = expressionResult.Context as XamlExpressionContext;
if (context == null)
return null;
try {
using (XmlTextReader r = new XmlTextReader(new StringReader(fileContent))) {
r.WhitespaceHandling = WhitespaceHandling.Significant;
// move reader to correct position
while (r.Read() && !IsReaderAtTarget(r)) { }
if (string.IsNullOrEmpty(context.AttributeName)) {
return ResolveElementName(r);
}
else if (context.InAttributeValue) {
MemberResolveResult mrr = ResolveAttribute(r, context.AttributeName);
if (mrr != null) {
return ResolveAttributeValue(mrr.ResolvedMember, resolveExpression);
}
}
else {
// in attribute name
return ResolveAttribute(r, resolveExpression);
}
}
return null;
}
catch (XmlException) {
return null;
}
}
ResolveResult ResolveElementName(XmlReader r)
{
string xmlNamespace;
string name;
if (resolveExpression.Contains(":")) {
string prefix = resolveExpression.Substring(0, resolveExpression.IndexOf(':'));
name = resolveExpression.Substring(resolveExpression.IndexOf(':') + 1);
xmlNamespace = r.LookupNamespace(prefix);
}
else {
xmlNamespace = r.LookupNamespace("");
name = resolveExpression;
}
if (name.Contains(".")) {
string propertyName = name.Substring(name.IndexOf('.') + 1);
name = name.Substring(0, name.IndexOf('.'));
return ResolveProperty(xmlNamespace, name, propertyName, true);
}
else {
IProjectContent pc = parseInfo.BestCompilationUnit.ProjectContent;
IReturnType resolvedType = XamlCompilationUnit.FindType(pc, xmlNamespace, name);
IClass resolvedClass = resolvedType != null ? resolvedType.GetUnderlyingClass() : null;
if (resolvedClass != null) {
return new TypeResolveResult(callingClass, null, resolvedClass);
}
else {
return null;
}
}
}
MemberResolveResult ResolveProperty(string xmlNamespace, string className, string propertyName, bool allowAttached)
{
IProjectContent pc = parseInfo.BestCompilationUnit.ProjectContent;
IReturnType resolvedType = XamlCompilationUnit.FindType(pc, xmlNamespace, className);
if (resolvedType != null && resolvedType.GetUnderlyingClass() != null) {
IMember member = resolvedType.GetProperties().Find(delegate(IProperty p) { return p.Name == propertyName; });
if (member == null) {
member = resolvedType.GetEvents().Find(delegate(IEvent p) { return p.Name == propertyName; });
}
if (member == null && allowAttached) {
member = resolvedType.GetMethods().Find(
delegate(IMethod p) {
return p.IsStatic && p.Parameters.Count == 1 && p.Name == "Get" + propertyName;
});
}
if (member != null)
return new MemberResolveResult(callingClass, null, member);
}
return null;
}
MemberResolveResult ResolveAttribute(XmlReader r, string attributeName)
{
if (context.ElementPath.Elements.Count == 0) {
return null;
}
string attributeXmlNamespace;
if (attributeName.Contains(":")) {
attributeXmlNamespace = r.LookupNamespace(attributeName.Substring(0, attributeName.IndexOf(':')));
attributeName = attributeName.Substring(attributeName.IndexOf(':') + 1);
}
else {
attributeXmlNamespace = r.LookupNamespace("");
}
if (attributeName.Contains(".")) {
string className = attributeName.Substring(0, attributeName.IndexOf('.'));
attributeName = attributeName.Substring(attributeName.IndexOf('.') + 1);
return ResolveProperty(attributeXmlNamespace, className, attributeName, true);
}
else {
ICSharpCode.XmlEditor.QualifiedName lastElement = context.ElementPath.Elements[context.ElementPath.Elements.Count - 1];
return ResolveProperty(lastElement.Namespace, lastElement.Name, attributeName, false);
}
}
ResolveResult ResolveAttributeValue(IMember propertyOrEvent, string expression)
{
if (propertyOrEvent == null)
return null;
if (propertyOrEvent is IEvent) {
return new MethodGroupResolveResult(callingClass, null, callingClass.DefaultReturnType, expression);
}
if (propertyOrEvent.Name == "Name" && callingClass != null) {
foreach (IField f in callingClass.Fields) {
if (f.Name == expression)
return new MemberResolveResult(callingClass, null, f);
}
}
IReturnType type = propertyOrEvent.ReturnType;
if (type == null) return null;
IClass c = type.GetUnderlyingClass();
if (c == null) return null;
if (c.ClassType == ClassType.Enum) {
foreach (IField f in c.Fields) {
if (f.Name == expression)
return new MemberResolveResult(callingClass, null, f);
}
}
return null;
}
public ArrayList CtrlSpace(int caretLineNumber, int caretColumn, ParseInformation parseInfo, string fileContent, ExpressionContext expressionContext)
{
this.parseInfo = parseInfo;
this.caretLineNumber = caretLineNumber;
this.caretColumn = caretColumn;
this.callingClass = parseInfo.BestCompilationUnit.GetInnermostClass(caretLineNumber, caretColumn);
this.context = expressionContext as XamlExpressionContext;
if (context == null) {
return null;
}
if (context.AttributeName == null) {
return CtrlSpaceForElement(fileContent);
}
else if (context.InAttributeValue) {
return CtrlSpaceForAttributeValue(fileContent, context);
}
else {
return CtrlSpaceForAttributeName(fileContent, context);
}
}
ArrayList CtrlSpaceForAttributeName(string fileContent, XamlExpressionContext context)
{
if (context.ElementPath.Elements.Count == 0)
return null;
QualifiedName lastElement = context.ElementPath.Elements[context.ElementPath.Elements.Count - 1];
XamlCompilationUnit cu = parseInfo.BestCompilationUnit as XamlCompilationUnit;
if (cu == null)
return null;
IReturnType rt = cu.CreateType(lastElement.Namespace, lastElement.Name);
if (rt == null)
return null;
ArrayList list = new ArrayList();
foreach (IProperty p in rt.GetProperties()) {
if (p.IsPublic && p.CanSet) {
list.Add(p);
}
}
return list;
}
ArrayList CtrlSpaceForAttributeValue(string fileContent, XamlExpressionContext context)
{
ArrayList attributes = CtrlSpaceForAttributeName(fileContent, context);
if (attributes != null) {
foreach (IProperty p in attributes.OfType<IProperty>()) {
if (p.Name == context.AttributeName && p.ReturnType != null) {
IClass c = p.ReturnType.GetUnderlyingClass();
if (c != null && c.ClassType == ClassType.Enum) {
return EnumCompletion(c);
}
}
}
}
return null;
}
ArrayList EnumCompletion(IClass enumClass)
{
ArrayList arr = new ArrayList();
foreach (IField f in enumClass.Fields) {
arr.Add(f);
}
return arr;
}
ArrayList CtrlSpaceForElement(string fileContent)
{
using (XmlTextReader r = new XmlTextReader(new StringReader(fileContent))) {
try {
r.WhitespaceHandling = WhitespaceHandling.Significant;
// move reader to correct position
while (r.Read() && !IsReaderAtTarget(r)) { }
}
catch (XmlException) {
}
ArrayList result = new ArrayList();
IProjectContent pc = parseInfo.BestCompilationUnit.ProjectContent;
resolveExpression = r.Name;
TypeResolveResult rr = ResolveElementName(r) as TypeResolveResult;
if (rr != null) {
AddPropertiesForType(result, r, rr);
}
foreach (var ns in r.GetNamespacesInScope(XmlNamespaceScope.ExcludeXml)) {
ArrayList list = XamlCompilationUnit.GetNamespaceMembers(pc, ns.Value);
if (list != null) {
foreach (IClass c in list.OfType<IClass>()) {
if (c.ClassType != ClassType.Class)
continue;
if (c.IsAbstract && c.IsStatic)
continue;
if (c.ClassInheritanceTree.Any(b => b.FullyQualifiedName == "System.Attribute"))
continue;
if (!c.Methods.Any(m => m.IsConstructor && m.IsPublic))
continue;
if (string.IsNullOrEmpty(ns.Key))
result.Add(c);
else
result.Add(new XamlCompletionClass(c, ns.Key));
}
}
}
return result;
}
}
void AddPropertiesForType(ArrayList result, XmlTextReader r, TypeResolveResult rr)
{
if (rr.ResolvedType != null) {
foreach (IProperty p in rr.ResolvedType.GetProperties()) {
if (!p.IsPublic)
continue;
if (!p.CanSet && !IsCollectionType(p.ReturnType))
continue;
string propPrefix = p.DeclaringType.Name;
if (!string.IsNullOrEmpty(r.Prefix))
propPrefix = r.Prefix + ":" + propPrefix;
result.Add(new XamlCompletionProperty(p, propPrefix));
}
}
}
bool IsCollectionType(IReturnType rt)
{
if (rt == null)
return false;
return rt.GetMethods().Any(m => m.Name == "Add" && m.IsPublic);
}
class XamlCompletionClass : DefaultClass, IEntity
{
string newName;
public XamlCompletionClass(IClass baseClass, string prefix)
: base(baseClass.CompilationUnit, baseClass.FullyQualifiedName)
{
this.Modifiers = baseClass.Modifiers;
newName = prefix + ":" + baseClass.Name;
}
string IEntity.Name
{
get { return newName; }
}
}
class XamlCompletionProperty : DefaultProperty, IEntity
{
string newName;
public XamlCompletionProperty(IProperty baseProperty, string prefix)
: base(baseProperty.DeclaringType, baseProperty.Name)
{
this.Modifiers = baseProperty.Modifiers;
newName = prefix + "." + baseProperty.Name;
}
string IEntity.Name
{
get { return newName; }
}
}
}
}
Loading…
Cancel
Save