Browse Source

XML schema namespaces now visible in completion window.

git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/trunk@4264 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61
shortcuts
Matt Ward 16 years ago
parent
commit
eb27110cbf
  1. 58
      src/AddIns/DisplayBindings/XmlEditor/Project/Src/XmlCodeCompletionBinding.cs
  2. 13
      src/AddIns/DisplayBindings/XmlEditor/Project/Src/XmlCompletionItem.cs
  3. 2
      src/AddIns/DisplayBindings/XmlEditor/Project/Src/XmlTreeView.cs
  4. 91
      src/AddIns/DisplayBindings/XmlEditor/Project/Src/XmlView.cs

58
src/AddIns/DisplayBindings/XmlEditor/Project/Src/XmlCodeCompletionBinding.cs

@ -1,29 +1,28 @@ @@ -1,29 +1,28 @@
// <file>
// <copyright see="prj:///doc/copyright.txt"/>
// <license see="prj:///doc/license.txt"/>
// <owner name="Siegfried Pammer" email="sie_pam@gmx.at"/>
// <owner name="Matthew Ward" email="mrward@users.sourceforge.net"/>
// <version>$Revision$</version>
// </file>
using System;
using System.IO;
using ICSharpCode.Core;
using ICSharpCode.SharpDevelop.Editor;
using ICSharpCode.SharpDevelop.Editor.CodeCompletion;
namespace ICSharpCode.XmlEditor
{
/// <summary>
/// Description of XmlCodeCompletionBinding.
/// </summary>
public class XmlCodeCompletionBinding : ICodeCompletionBinding
{
static XmlCodeCompletionBinding instance;
public static XmlCodeCompletionBinding Instance {
get {
if (instance == null)
if (instance == null) {
instance = new XmlCodeCompletionBinding();
}
return instance;
}
}
@ -35,43 +34,50 @@ namespace ICSharpCode.XmlEditor @@ -35,43 +34,50 @@ namespace ICSharpCode.XmlEditor
public CodeCompletionKeyPressResult HandleKeyPress(ITextEditor editor, char ch)
{
string text = editor.Document.GetText(0, editor.Caret.Offset);
string extension = Path.GetExtension(editor.FileName);
string defaultNamespacePrefix = XmlSchemaManager.GetNamespacePrefix(extension);
XmlSchemaCompletionData defaultSchemaCompletionData = XmlSchemaManager.GetSchemaCompletionData(extension);
XmlCompletionDataProvider provider = new XmlCompletionDataProvider(XmlSchemaManager.SchemaCompletionDataItems,
defaultSchemaCompletionData,
defaultNamespacePrefix);
editor.ShowCompletionWindow(provider.GenerateCompletionData(text, ch));
XmlCompletionDataProvider provider = GetProvider(editor.FileName);
ICompletionItemList completionItems = provider.GenerateCompletionData(text, ch);
ICompletionListWindow completionWindow = editor.ShowCompletionWindow(completionItems);
if (completionWindow != null) {
XmlCompletionItem item = completionItems.SuggestedItem as XmlCompletionItem;
if (item.DataType == XmlCompletionDataType.NamespaceUri) {
completionWindow.Width = double.NaN;
}
}
if (ch == '<' || ch == ' ' || ch == '=')
if ((ch == '<') || (ch == ' ') || (ch == '=')) {
return CodeCompletionKeyPressResult.Completed;
else
}
return CodeCompletionKeyPressResult.CompletedIncludeKeyInCompletion;
}
public bool CtrlSpace(ITextEditor editor)
{
// Attribute value completion.
string text = editor.Document.Text;
int offset = editor.Caret.Offset;
string extension = Path.GetExtension(editor.FileName);
string defaultNamespacePrefix = XmlSchemaManager.GetNamespacePrefix(extension);
XmlSchemaCompletionData defaultSchemaCompletionData = XmlSchemaManager.GetSchemaCompletionData(extension);
XmlCompletionDataProvider provider = new XmlCompletionDataProvider(XmlSchemaManager.SchemaCompletionDataItems,
defaultSchemaCompletionData,
defaultNamespacePrefix);
// Attribute value completion.
if (XmlParser.IsInsideAttributeValue(text, offset)) {
XmlElementPath path = XmlParser.GetActiveElementStartPath(text, offset);
if (path.Elements.Count > 0) {
XmlCompletionDataProvider provider = GetProvider(editor.FileName);
editor.ShowCompletionWindow(provider.GetAttributeValueCompletionData(path, XmlParser.GetAttributeNameAtIndex(text, offset)));
return true;
}
}
return false;
}
public static XmlCompletionDataProvider GetProvider(string fileName)
{
string extension = Path.GetExtension(fileName);
if (PropertyService.DataDirectory != null) {
XmlSchemaCompletionDataCollection schemas = XmlSchemaManager.SchemaCompletionDataItems;
string defaultNamespacePrefix = XmlSchemaManager.GetNamespacePrefix(extension);
XmlSchemaCompletionData defaultSchemaCompletionData = XmlSchemaManager.GetSchemaCompletionData(extension);
return new XmlCompletionDataProvider(schemas, defaultSchemaCompletionData, defaultNamespacePrefix);
}
// for unit tests
return new XmlCompletionDataProvider(new XmlSchemaCompletionDataCollection(), null, String.Empty);
}
}
}

13
src/AddIns/DisplayBindings/XmlEditor/Project/Src/XmlCompletionItem.cs

@ -28,10 +28,10 @@ namespace ICSharpCode.XmlEditor @@ -28,10 +28,10 @@ namespace ICSharpCode.XmlEditor
public class XmlCompletionItem : DefaultCompletionItem
{
XmlCompletionDataType dataType = XmlCompletionDataType.XmlElement;
string description = string.Empty;
string description = String.Empty;
public XmlCompletionItem(string text)
: this(text, string.Empty, XmlCompletionDataType.XmlElement)
: this(text, String.Empty, XmlCompletionDataType.XmlElement)
{
}
@ -41,7 +41,7 @@ namespace ICSharpCode.XmlEditor @@ -41,7 +41,7 @@ namespace ICSharpCode.XmlEditor
}
public XmlCompletionItem(string text, XmlCompletionDataType dataType)
: this(text, string.Empty, dataType)
: this(text, String.Empty, dataType)
{
}
@ -57,9 +57,11 @@ namespace ICSharpCode.XmlEditor @@ -57,9 +57,11 @@ namespace ICSharpCode.XmlEditor
/// the xs:annotation/xs:documentation element.
/// </summary>
public override string Description {
get {
return description;
get { return description; }
}
public XmlCompletionDataType DataType {
get { return dataType; }
}
public override void Complete(CompletionContext context)
@ -83,6 +85,5 @@ namespace ICSharpCode.XmlEditor @@ -83,6 +85,5 @@ namespace ICSharpCode.XmlEditor
{
return "[" + this.Text + "]";
}
}
}

2
src/AddIns/DisplayBindings/XmlEditor/Project/Src/XmlTreeView.cs

@ -134,7 +134,7 @@ namespace ICSharpCode.XmlEditor @@ -134,7 +134,7 @@ namespace ICSharpCode.XmlEditor
protected override void LoadFromPrimary()
{
IFileDocumentProvider provider = this.PrimaryViewContent as IFileDocumentProvider;
treeViewContainer.LoadXml(provider.GetDocumentForFile(this.PrimaryFile).Text, XmlView.GetProvider(Path.GetExtension(this.PrimaryFileName)));
treeViewContainer.LoadXml(provider.GetDocumentForFile(this.PrimaryFile).Text, XmlCodeCompletionBinding.GetProvider(Path.GetExtension(this.PrimaryFileName)));
XmlView view = XmlView.ForFile(this.PrimaryFile);
if (view != null) {
view.CheckIsWellFormed();

91
src/AddIns/DisplayBindings/XmlEditor/Project/Src/XmlView.cs

@ -1,7 +1,7 @@ @@ -1,7 +1,7 @@
// <file>
// <copyright see="prj:///doc/copyright.txt"/>
// <license see="prj:///doc/license.txt"/>
// <owner name="Siegfried Pammer" email="sie_pam@gmx.at"/>
// <owner name="Matthew Ward" email="mrward@users.sourceforge.net"/>
// <version>$Revision$</version>
// </file>
@ -59,44 +59,24 @@ namespace ICSharpCode.XmlEditor @@ -59,44 +59,24 @@ namespace ICSharpCode.XmlEditor
public static XmlView ForViewContent(IViewContent view)
{
if (view == null || view.PrimaryFile == null)
if ((view == null) || (view.PrimaryFile == null)) {
return null;
}
return ForFile(view.PrimaryFile);
}
static void FileClosedHandler(object sender, EventArgs e)
{
if (sender is OpenedFile)
if (sender is OpenedFile) {
mapping.Remove(sender as OpenedFile);
}
}
public string StylesheetFileName { get; set; }
public XmlCompletionDataProvider GetProvider()
{
return GetProvider(Path.GetExtension(File.FileName));
}
public static XmlCompletionDataProvider GetProvider(string extension)
{
string defaultNamespacePrefix;
XmlSchemaCompletionData defaultSchemaCompletionData;
XmlSchemaCompletionDataCollection schemas;
if (PropertyService.DataDirectory != null) {
schemas = XmlSchemaManager.SchemaCompletionDataItems;
defaultNamespacePrefix = XmlSchemaManager.GetNamespacePrefix(extension);
defaultSchemaCompletionData = XmlSchemaManager.GetSchemaCompletionData(extension);
} else {
// for NUnit tests
defaultNamespacePrefix = string.Empty;
schemas = new XmlSchemaCompletionDataCollection();
defaultSchemaCompletionData = null;
}
return new XmlCompletionDataProvider(schemas,
defaultSchemaCompletionData,
defaultNamespacePrefix);
return XmlCodeCompletionBinding.GetProvider(Path.GetExtension(File.FileName));
}
public ITextEditor TextEditor
@ -104,14 +84,13 @@ namespace ICSharpCode.XmlEditor @@ -104,14 +84,13 @@ namespace ICSharpCode.XmlEditor
get {
foreach (IViewContent view in File.RegisteredViewContents) {
ITextEditorProvider provider = view as ITextEditorProvider;
if (provider != null) {
IDocument document = provider.GetDocumentForFile(File);
if (document != null)
if (document != null) {
return provider.TextEditor;
}
}
}
return null;
}
}
@ -121,18 +100,16 @@ namespace ICSharpCode.XmlEditor @@ -121,18 +100,16 @@ namespace ICSharpCode.XmlEditor
get {
foreach (IViewContent view in File.RegisteredViewContents) {
XmlTreeView tree = view as XmlTreeView;
if (tree != null)
if (tree != null) {
return tree;
}
}
return null;
}
}
public static XmlView ActiveXmlView {
get {
return XmlView.ForViewContent(WorkbenchSingleton.Workbench.ActiveViewContent);
}
get { return XmlView.ForViewContent(WorkbenchSingleton.Workbench.ActiveViewContent); }
}
/// <summary>
@ -207,9 +184,7 @@ namespace ICSharpCode.XmlEditor @@ -207,9 +184,7 @@ namespace ICSharpCode.XmlEditor
/// Checks that the xml in this view is well-formed.
/// </summary>
public bool IsWellFormed {
get {
return CheckIsWellFormed();
}
get { return CheckIsWellFormed(); }
}
public bool CheckIsWellFormed()
@ -220,11 +195,9 @@ namespace ICSharpCode.XmlEditor @@ -220,11 +195,9 @@ namespace ICSharpCode.XmlEditor
XmlDocument Document = new XmlDocument();
Document.LoadXml(editor.Document.Text);
return true;
}
catch (XmlException ex) {
} catch (XmlException ex) {
AddTask(editor.FileName, ex.Message, ex.LinePosition - 1, ex.LineNumber - 1, TaskType.Error);
}
catch (WebException ex) {
} catch (WebException ex) {
AddTask(editor.FileName, ex.Message, 0, 0, TaskType.Error);
}
return false;
@ -435,8 +408,7 @@ namespace ICSharpCode.XmlEditor @@ -435,8 +408,7 @@ namespace ICSharpCode.XmlEditor
public string[] InferSchema()
{
ITextEditor editor = TextEditor;
if (editor == null)
return null;
if (editor == null) return null;
TaskService.ClearExceptCommentTasks();
if (IsWellFormed) {
@ -460,11 +432,9 @@ namespace ICSharpCode.XmlEditor @@ -460,11 +432,9 @@ namespace ICSharpCode.XmlEditor
public void FormatXml()
{
ITextEditor editor = TextEditor;
if (editor == null)
return;
if (editor == null) return;
TaskService.ClearExceptCommentTasks();
if (IsWellFormed) {
ReplaceAll(editor.Document.Text);
} else {
@ -479,8 +449,7 @@ namespace ICSharpCode.XmlEditor @@ -479,8 +449,7 @@ namespace ICSharpCode.XmlEditor
public void ReplaceAll(string xml)
{
ITextEditor editor = TextEditor;
if (editor == null)
return;
if (editor == null) return;
string formattedXml = SimpleFormat(IndentedFormat(xml));
editor.Document.Text = formattedXml;
@ -507,8 +476,7 @@ namespace ICSharpCode.XmlEditor @@ -507,8 +476,7 @@ namespace ICSharpCode.XmlEditor
string indentedText = string.Empty;
ITextEditor editor = TextEditor;
if (editor == null)
return indentedText;
if (editor == null) return indentedText;
try {
XmlTextReader reader = new XmlTextReader(new StringReader(xml));
@ -524,7 +492,6 @@ namespace ICSharpCode.XmlEditor @@ -524,7 +492,6 @@ namespace ICSharpCode.XmlEditor
} catch(Exception) {
indentedText = xml;
}
return indentedText;
}
@ -540,14 +507,14 @@ namespace ICSharpCode.XmlEditor @@ -540,14 +507,14 @@ namespace ICSharpCode.XmlEditor
OutputWindowWriteLine(StringParser.Parse("${res:MainWindow.XmlValidationMessages.ValidationStarted}"));
if (IsSchema) {
if (!ValidateSchema())
if (!ValidateSchema()) {
return;
} else {
if (!ValidateAgainstSchema())
}
} else if (!ValidateAgainstSchema()) {
return;
}
OutputWindowWriteLine(string.Empty);
OutputWindowWriteLine(String.Empty);
OutputWindowWriteLine(StringParser.Parse("${res:MainWindow.XmlValidationMessages.ValidationSuccess}"));
}
@ -555,7 +522,7 @@ namespace ICSharpCode.XmlEditor @@ -555,7 +522,7 @@ namespace ICSharpCode.XmlEditor
get {
string extension = Path.GetExtension(File.FileName);
if (extension != null) {
return string.Compare(".xsd", extension, StringComparison.OrdinalIgnoreCase) == 0;
return String.Compare(".xsd", extension, StringComparison.OrdinalIgnoreCase) == 0;
}
return false;
}
@ -568,8 +535,7 @@ namespace ICSharpCode.XmlEditor @@ -568,8 +535,7 @@ namespace ICSharpCode.XmlEditor
bool ValidateAgainstSchema()
{
ITextEditor editor = TextEditor;
if (editor == null)
return false;
if (editor == null) return false;
try {
StringReader stringReader = new StringReader(editor.Document.Text);
@ -617,8 +583,7 @@ namespace ICSharpCode.XmlEditor @@ -617,8 +583,7 @@ namespace ICSharpCode.XmlEditor
bool ValidateSchema()
{
ITextEditor editor = TextEditor;
if (editor == null)
return false;
if (editor == null) return false;
StringReader stringReader = new StringReader(editor.Document.Text);
XmlTextReader xmlReader = new XmlTextReader(stringReader);
@ -686,8 +651,7 @@ namespace ICSharpCode.XmlEditor @@ -686,8 +651,7 @@ namespace ICSharpCode.XmlEditor
TaskService.ClearExceptCommentTasks();
ITextEditor editor = TextEditor;
if (editor == null)
return;
if (editor == null) return;
if (IsWellFormed) {
if (IsValidXsl(xsl)) {
@ -819,9 +783,6 @@ namespace ICSharpCode.XmlEditor @@ -819,9 +783,6 @@ namespace ICSharpCode.XmlEditor
/// <summary>
/// Tries to get the filename from the inner exception otherwise returns the default filename.
/// </summary>
/// <param name="ex"></param>
/// <param name="defaultFileName"></param>
/// <returns></returns>
static string GetFileNameFromInnerException(Exception ex, string defaultFileName)
{
XmlException innerException = ex.InnerException as XmlException;

Loading…
Cancel
Save