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 17 years ago
parent
commit
eb27110cbf
  1. 64
      src/AddIns/DisplayBindings/XmlEditor/Project/Src/XmlCodeCompletionBinding.cs
  2. 15
      src/AddIns/DisplayBindings/XmlEditor/Project/Src/XmlCompletionItem.cs
  3. 2
      src/AddIns/DisplayBindings/XmlEditor/Project/Src/XmlTreeView.cs
  4. 95
      src/AddIns/DisplayBindings/XmlEditor/Project/Src/XmlView.cs

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

@ -1,29 +1,28 @@
// <file> // <file>
// <copyright see="prj:///doc/copyright.txt"/> // <copyright see="prj:///doc/copyright.txt"/>
// <license see="prj:///doc/license.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> // <version>$Revision$</version>
// </file> // </file>
using System; using System;
using System.IO; using System.IO;
using ICSharpCode.Core;
using ICSharpCode.SharpDevelop.Editor; using ICSharpCode.SharpDevelop.Editor;
using ICSharpCode.SharpDevelop.Editor.CodeCompletion; using ICSharpCode.SharpDevelop.Editor.CodeCompletion;
namespace ICSharpCode.XmlEditor namespace ICSharpCode.XmlEditor
{ {
/// <summary>
/// Description of XmlCodeCompletionBinding.
/// </summary>
public class XmlCodeCompletionBinding : ICodeCompletionBinding public class XmlCodeCompletionBinding : ICodeCompletionBinding
{ {
static XmlCodeCompletionBinding instance; static XmlCodeCompletionBinding instance;
public static XmlCodeCompletionBinding Instance { public static XmlCodeCompletionBinding Instance {
get { get {
if (instance == null) if (instance == null) {
instance = new XmlCodeCompletionBinding(); instance = new XmlCodeCompletionBinding();
}
return instance; return instance;
} }
} }
@ -33,45 +32,52 @@ namespace ICSharpCode.XmlEditor
} }
public CodeCompletionKeyPressResult HandleKeyPress(ITextEditor editor, char ch) public CodeCompletionKeyPressResult HandleKeyPress(ITextEditor editor, char ch)
{ {
string text = editor.Document.GetText(0, editor.Caret.Offset); string text = editor.Document.GetText(0, editor.Caret.Offset);
string extension = Path.GetExtension(editor.FileName); XmlCompletionDataProvider provider = GetProvider(editor.FileName);
string defaultNamespacePrefix = XmlSchemaManager.GetNamespacePrefix(extension); ICompletionItemList completionItems = provider.GenerateCompletionData(text, ch);
XmlSchemaCompletionData defaultSchemaCompletionData = XmlSchemaManager.GetSchemaCompletionData(extension); ICompletionListWindow completionWindow = editor.ShowCompletionWindow(completionItems);
XmlCompletionDataProvider provider = new XmlCompletionDataProvider(XmlSchemaManager.SchemaCompletionDataItems, if (completionWindow != null) {
defaultSchemaCompletionData, XmlCompletionItem item = completionItems.SuggestedItem as XmlCompletionItem;
defaultNamespacePrefix); if (item.DataType == XmlCompletionDataType.NamespaceUri) {
completionWindow.Width = double.NaN;
editor.ShowCompletionWindow(provider.GenerateCompletionData(text, ch)); }
}
if (ch == '<' || ch == ' ' || ch == '=') if ((ch == '<') || (ch == ' ') || (ch == '=')) {
return CodeCompletionKeyPressResult.Completed; return CodeCompletionKeyPressResult.Completed;
else }
return CodeCompletionKeyPressResult.CompletedIncludeKeyInCompletion; return CodeCompletionKeyPressResult.CompletedIncludeKeyInCompletion;
} }
public bool CtrlSpace(ITextEditor editor) public bool CtrlSpace(ITextEditor editor)
{ {
// Attribute value completion.
string text = editor.Document.Text; string text = editor.Document.Text;
int offset = editor.Caret.Offset; 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)) { if (XmlParser.IsInsideAttributeValue(text, offset)) {
XmlElementPath path = XmlParser.GetActiveElementStartPath(text, offset); XmlElementPath path = XmlParser.GetActiveElementStartPath(text, offset);
if (path.Elements.Count > 0) { if (path.Elements.Count > 0) {
XmlCompletionDataProvider provider = GetProvider(editor.FileName);
editor.ShowCompletionWindow(provider.GetAttributeValueCompletionData(path, XmlParser.GetAttributeNameAtIndex(text, offset))); editor.ShowCompletionWindow(provider.GetAttributeValueCompletionData(path, XmlParser.GetAttributeNameAtIndex(text, offset)));
return true; return true;
} }
} }
return false; 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);
}
} }
} }

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

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

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

@ -134,7 +134,7 @@ namespace ICSharpCode.XmlEditor
protected override void LoadFromPrimary() protected override void LoadFromPrimary()
{ {
IFileDocumentProvider provider = this.PrimaryViewContent as IFileDocumentProvider; 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); XmlView view = XmlView.ForFile(this.PrimaryFile);
if (view != null) { if (view != null) {
view.CheckIsWellFormed(); view.CheckIsWellFormed();

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

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

Loading…
Cancel
Save