Browse Source

Use exception for invalid highlighting definitions instead of displaying a MessageBox.

git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/branches/2.1@2523 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61
shortcuts
Daniel Grunwald 18 years ago
parent
commit
86edbfe8e0
  1. 3
      src/AddIns/BackendBindings/CSharpBinding/Project/Src/Project/VBToCSharpConverter.cs
  2. 3
      src/AddIns/BackendBindings/VBNetBinding/Project/Src/Project/CSharpToVBConverter.cs
  3. 1
      src/Libraries/ICSharpCode.TextEditor/Project/ICSharpCode.TextEditor.csproj
  4. 3
      src/Libraries/ICSharpCode.TextEditor/Project/Resources/Patch-Mode.xshd
  5. 6
      src/Libraries/ICSharpCode.TextEditor/Project/Src/Document/HighlightingStrategy/DefaultHighlightingStrategy.cs
  6. 37
      src/Libraries/ICSharpCode.TextEditor/Project/Src/Document/HighlightingStrategy/HighlightingDefinitionInvalidException.cs
  7. 44
      src/Libraries/ICSharpCode.TextEditor/Project/Src/Document/HighlightingStrategy/HighlightingDefinitionParser.cs
  8. 26
      src/Libraries/ICSharpCode.TextEditor/Project/Src/Document/HighlightingStrategy/HighlightingManager.cs
  9. 6
      src/Libraries/ICSharpCode.TextEditor/Project/Src/Document/HighlightingStrategy/SyntaxModes/FileSyntaxModeProvider.cs
  10. 6
      src/Libraries/ICSharpCode.TextEditor/Project/Src/Document/HighlightingStrategy/SyntaxModes/SyntaxMode.cs
  11. 6
      src/Libraries/ICSharpCode.TextEditor/Project/Src/Gui/TextEditorControlBase.cs
  12. 18
      src/Main/Base/Project/Src/TextEditor/Commands/TextAreaContextmenuCommands.cs
  13. 18
      src/Main/Base/Project/Src/TextEditor/Gui/Editor/TextEditorDisplayBinding.cs

3
src/AddIns/BackendBindings/CSharpBinding/Project/Src/Project/VBToCSharpConverter.cs

@ -42,7 +42,8 @@ namespace CSharpBinding @@ -42,7 +42,8 @@ namespace CSharpBinding
protected override void ConvertAst(CompilationUnit compilationUnit, List<ISpecial> specials)
{
PreprocessingDirective.VBToCSharp(specials);
compilationUnit.AcceptVisitor(new VBNetToCSharpConvertVisitor(), null);
compilationUnit.AcceptVisitor(new VBNetConstructsConvertVisitor(), null);
compilationUnit.AcceptVisitor(new ToCSharpConvertVisitor(), null);
}
}
}

3
src/AddIns/BackendBindings/VBNetBinding/Project/Src/Project/CSharpToVBConverter.cs

@ -35,7 +35,8 @@ namespace VBNetBinding @@ -35,7 +35,8 @@ namespace VBNetBinding
protected override void ConvertAst(CompilationUnit compilationUnit, List<ISpecial> specials)
{
PreprocessingDirective.CSharpToVB(specials);
compilationUnit.AcceptVisitor(new CSharpToVBNetConvertVisitor(), null);
compilationUnit.AcceptVisitor(new CSharpConstructsVisitor(), null);
compilationUnit.AcceptVisitor(new ToVBNetConvertVisitor(), null);
}
protected override void CopyProperties(IProject sourceProject, IProject targetProject)

1
src/Libraries/ICSharpCode.TextEditor/Project/ICSharpCode.TextEditor.csproj

@ -55,6 +55,7 @@ @@ -55,6 +55,7 @@
<Compile Include="Src\Document\DefaultDocument.cs" />
<Compile Include="Src\Document\DocumentEventArgs.cs" />
<Compile Include="Src\Document\DocumentFactory.cs" />
<Compile Include="Src\Document\HighlightingStrategy\HighlightingDefinitionInvalidException.cs" />
<Compile Include="Src\Document\IDocument.cs" />
<Compile Include="Src\Document\ISegment.cs" />
<Compile Include="Src\Document\TextUtilities.cs" />

3
src/Libraries/ICSharpCode.TextEditor/Project/Resources/Patch-Mode.xshd

@ -1,7 +1,6 @@ @@ -1,7 +1,6 @@
<?xml version="1.0"?>
<!-- syntaxdefinition for C/C++ 2001 by Andrea Paatz and Mike Krueger -->
<SyntaxDefinition name = "Patch files" extensions = ".patch;.diff">
<SyntaxDefinition name = "Patch" extensions = ".patch;.diff">
<!--<Digits name = "Digits" bold = "false" italic = "false" color = "DarkBlue"/>-->
<RuleSets>

6
src/Libraries/ICSharpCode.TextEditor/Project/Src/Document/HighlightingStrategy/DefaultHighlightingStrategy.cs

@ -156,8 +156,8 @@ namespace ICSharpCode.TextEditor.Document @@ -156,8 +156,8 @@ namespace ICSharpCode.TextEditor.Document
}
}
if (!found) {
MessageBox.Show("The RuleSet " + aSpan.Rule + " could not be found in mode definition " + this.Name, "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1);
aSpan.RuleSet = null;
throw new HighlightingDefinitionInvalidException("The RuleSet " + aSpan.Rule + " could not be found in mode definition " + this.Name);
}
} else {
aSpan.RuleSet = null;
@ -166,7 +166,7 @@ namespace ICSharpCode.TextEditor.Document @@ -166,7 +166,7 @@ namespace ICSharpCode.TextEditor.Document
}
if (defaultRuleSet == null) {
MessageBox.Show("No default RuleSet is defined for mode definition " + this.Name, "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1);
throw new HighlightingDefinitionInvalidException("No default RuleSet is defined for mode definition " + this.Name);
}
}
@ -179,8 +179,8 @@ namespace ICSharpCode.TextEditor.Document @@ -179,8 +179,8 @@ namespace ICSharpCode.TextEditor.Document
if (highlighter != null) {
ruleSet.Highlighter = highlighter;
} else {
MessageBox.Show("The mode defintion " + ruleSet.Reference + " which is refered from the " + this.Name + " mode definition could not be found", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1);
ruleSet.Highlighter = this;
throw new HighlightingDefinitionInvalidException("The mode defintion " + ruleSet.Reference + " which is refered from the " + this.Name + " mode definition could not be found");
}
} else {
ruleSet.Highlighter = this;

37
src/Libraries/ICSharpCode.TextEditor/Project/Src/Document/HighlightingStrategy/HighlightingDefinitionInvalidException.cs

@ -0,0 +1,37 @@ @@ -0,0 +1,37 @@
// <file>
// <copyright see="prj:///doc/copyright.txt"/>
// <license see="prj:///doc/license.txt"/>
// <owner name="Mike Krüger" email="mike@icsharpcode.net"/>
// <version>$Revision$</version>
// </file>
using System;
using System.Runtime.Serialization;
namespace ICSharpCode.TextEditor.Document
{
/// <summary>
/// Indicates that the highlighting definition that was tried to load was invalid.
/// You get this exception only once per highlighting definition, after that the definition
/// is replaced with the default highlighter.
/// </summary>
[Serializable()]
public class HighlightingDefinitionInvalidException : Exception
{
public HighlightingDefinitionInvalidException() : base()
{
}
public HighlightingDefinitionInvalidException(string message) : base(message)
{
}
public HighlightingDefinitionInvalidException(string message, Exception innerException) : base(message, innerException)
{
}
protected HighlightingDefinitionInvalidException(SerializationInfo info, StreamingContext context) : base(info, context)
{
}
}
}

44
src/Libraries/ICSharpCode.TextEditor/Project/Src/Document/HighlightingStrategy/HighlightingDefinitionParser.cs

@ -17,8 +17,6 @@ namespace ICSharpCode.TextEditor.Document @@ -17,8 +17,6 @@ namespace ICSharpCode.TextEditor.Document
{
public static class HighlightingDefinitionParser
{
static List<ValidationEventArgs> errors = null;
public static DefaultHighlightingStrategy Parse(SyntaxMode syntaxMode, XmlReader xmlReader)
{
return Parse(null, syntaxMode, xmlReader);
@ -31,10 +29,16 @@ namespace ICSharpCode.TextEditor.Document @@ -31,10 +29,16 @@ namespace ICSharpCode.TextEditor.Document
if (xmlReader == null)
throw new ArgumentNullException("xmlTextReader");
try {
List<ValidationEventArgs> errors = null;
XmlReaderSettings settings = new XmlReaderSettings();
Stream shemaStream = typeof(HighlightingDefinitionParser).Assembly.GetManifestResourceStream("ICSharpCode.TextEditor.Resources.Mode.xsd");
settings.Schemas.Add("", new XmlTextReader(shemaStream));
settings.Schemas.ValidationEventHandler += new ValidationEventHandler(ValidationHandler);
settings.Schemas.ValidationEventHandler += delegate(object sender, ValidationEventArgs args) {
if (errors == null) {
errors = new List<ValidationEventArgs>();
}
errors.Add(args);
};
settings.ValidationType = ValidationType.Schema;
XmlReader validatingReader = XmlReader.Create(xmlReader, settings);
@ -47,7 +51,7 @@ namespace ICSharpCode.TextEditor.Document @@ -47,7 +51,7 @@ namespace ICSharpCode.TextEditor.Document
if (doc.DocumentElement.HasAttribute("extends")) {
KeyValuePair<SyntaxMode, ISyntaxModeFileProvider> entry = HighlightingManager.Manager.FindHighlighterEntry(doc.DocumentElement.GetAttribute("extends"));
if (entry.Key == null) {
MessageBox.Show("Cannot find referenced highlighting source " + doc.DocumentElement.GetAttribute("extends"));
throw new HighlightingDefinitionInvalidException("Cannot find referenced highlighting source " + doc.DocumentElement.GetAttribute("extends"));
} else {
highlighter = Parse(highlighter, entry.Key, entry.Value.GetSyntaxModeFile(entry.Key));
if (highlighter == null) return null;
@ -89,36 +93,18 @@ namespace ICSharpCode.TextEditor.Document @@ -89,36 +93,18 @@ namespace ICSharpCode.TextEditor.Document
xmlReader.Close();
if(errors!=null) {
ReportErrors(syntaxMode.FileName);
errors = null;
return null;
if (errors != null) {
StringBuilder msg = new StringBuilder();
foreach (ValidationEventArgs args in errors) {
msg.AppendLine(args.Message);
}
throw new HighlightingDefinitionInvalidException(msg.ToString());
} else {
return highlighter;
}
} catch (Exception e) {
MessageBox.Show("Could not load mode definition file '" + syntaxMode.FileName + "'.\n" + e.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
return null;
}
}
private static void ValidationHandler(object sender, ValidationEventArgs args)
{
if (errors == null) {
errors=new List<ValidationEventArgs>();
}
errors.Add(args);
}
private static void ReportErrors(string fileName)
{
StringBuilder msg = new StringBuilder();
msg.Append("Could not load mode definition file. Reason:\n\n");
foreach(ValidationEventArgs args in errors) {
msg.Append(args.Message);
msg.Append(Console.Out.NewLine);
throw new HighlightingDefinitionInvalidException("Could not load mode definition file '" + syntaxMode.FileName + "'.\n", e);
}
MessageBox.Show(msg.ToString(), "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1);
}
}
}

26
src/Libraries/ICSharpCode.TextEditor/Project/Src/Document/HighlightingStrategy/HighlightingManager.cs

@ -9,6 +9,7 @@ using System; @@ -9,6 +9,7 @@ using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Diagnostics;
namespace ICSharpCode.TextEditor.Document
{
@ -17,7 +18,10 @@ namespace ICSharpCode.TextEditor.Document @@ -17,7 +18,10 @@ namespace ICSharpCode.TextEditor.Document
ArrayList syntaxModeFileProviders = new ArrayList();
static HighlightingManager highlightingManager;
// hash table from extension name to highlighting definition,
// OR from extension name to Pair SyntaxMode,ISyntaxModeFileProvider
Hashtable highlightingDefs = new Hashtable();
Hashtable extensionsToName = new Hashtable();
public Hashtable HighlightingDefinitions {
@ -28,7 +32,7 @@ namespace ICSharpCode.TextEditor.Document @@ -28,7 +32,7 @@ namespace ICSharpCode.TextEditor.Document
public static HighlightingManager Manager {
get {
return highlightingManager;
return highlightingManager;
}
}
@ -89,14 +93,20 @@ namespace ICSharpCode.TextEditor.Document @@ -89,14 +93,20 @@ namespace ICSharpCode.TextEditor.Document
{
SyntaxMode syntaxMode = (SyntaxMode)entry.Key;
ISyntaxModeFileProvider syntaxModeFileProvider = (ISyntaxModeFileProvider)entry.Value;
DefaultHighlightingStrategy highlightingStrategy = HighlightingDefinitionParser.Parse(syntaxMode, syntaxModeFileProvider.GetSyntaxModeFile(syntaxMode));
if (highlightingStrategy == null) {
highlightingStrategy = DefaultHighlighting;
DefaultHighlightingStrategy highlightingStrategy = null;
try {
highlightingStrategy = HighlightingDefinitionParser.Parse(syntaxMode, syntaxModeFileProvider.GetSyntaxModeFile(syntaxMode));
if (highlightingStrategy.Name != syntaxMode.Name) {
throw new HighlightingDefinitionInvalidException("The name specified in the .xshd '" + highlightingStrategy.Name + "' must be equal the syntax mode name '" + syntaxMode.Name + "'");
}
} finally {
if (highlightingStrategy == null) {
highlightingStrategy = DefaultHighlighting;
}
highlightingDefs[syntaxMode.Name] = highlightingStrategy;
highlightingStrategy.ResolveReferences();
}
highlightingDefs[syntaxMode.Name] = highlightingStrategy;
highlightingStrategy.ResolveReferences();
return highlightingStrategy;
}

6
src/Libraries/ICSharpCode.TextEditor/Project/Src/Document/HighlightingStrategy/SyntaxModes/FileSyntaxModeProvider.cs

@ -46,8 +46,7 @@ namespace ICSharpCode.TextEditor.Document @@ -46,8 +46,7 @@ namespace ICSharpCode.TextEditor.Document
{
string syntaxModeFile = Path.Combine(directory, syntaxMode.FileName);
if (!File.Exists(syntaxModeFile)) {
MessageBox.Show("Can't load highlighting definition " + syntaxModeFile + " (file not found)!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1);
return null;
throw new HighlightingDefinitionInvalidException("Can't load highlighting definition " + syntaxModeFile + " (file not found)!");
}
return new XmlTextReader(File.OpenRead(syntaxModeFile));
}
@ -70,8 +69,7 @@ namespace ICSharpCode.TextEditor.Document @@ -70,8 +69,7 @@ namespace ICSharpCode.TextEditor.Document
extensions));
goto bailout;
default:
MessageBox.Show("Unknown root node in syntax highlighting file :" + reader.Name, "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1);
goto bailout;
throw new HighlightingDefinitionInvalidException("Unknown root node in syntax highlighting file :" + reader.Name);
}
}
}

6
src/Libraries/ICSharpCode.TextEditor/Project/Src/Document/HighlightingStrategy/SyntaxModes/SyntaxMode.cs

@ -71,8 +71,7 @@ namespace ICSharpCode.TextEditor.Document @@ -71,8 +71,7 @@ namespace ICSharpCode.TextEditor.Document
case "SyntaxModes":
string version = reader.GetAttribute("version");
if (version != "1.0") {
MessageBox.Show("Unknown syntax mode file defininition with version " + version , "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1);
return syntaxModes;
throw new HighlightingDefinitionInvalidException("Unknown syntax mode file defininition with version " + version);
}
break;
case "Mode":
@ -81,8 +80,7 @@ namespace ICSharpCode.TextEditor.Document @@ -81,8 +80,7 @@ namespace ICSharpCode.TextEditor.Document
reader.GetAttribute("extensions")));
break;
default:
MessageBox.Show("Unknown node in syntax mode file :" + reader.Name, "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1);
return syntaxModes;
throw new HighlightingDefinitionInvalidException("Unknown node in syntax mode file :" + reader.Name);
}
break;
}

6
src/Libraries/ICSharpCode.TextEditor/Project/Src/Gui/TextEditorControlBase.cs

@ -501,7 +501,11 @@ namespace ICSharpCode.TextEditor @@ -501,7 +501,11 @@ namespace ICSharpCode.TextEditor
protected virtual void OnReloadHighlighting(object sender, EventArgs e)
{
if (Document.HighlightingStrategy != null) {
Document.HighlightingStrategy = HighlightingStrategyFactory.CreateHighlightingStrategy(Document.HighlightingStrategy.Name);
try {
Document.HighlightingStrategy = HighlightingStrategyFactory.CreateHighlightingStrategy(Document.HighlightingStrategy.Name);
} catch (HighlightingDefinitionInvalidException ex) {
MessageBox.Show(ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
OptionsChanged();
}
}

18
src/Main/Base/Project/Src/TextEditor/Commands/TextAreaContextmenuCommands.cs

@ -56,13 +56,17 @@ namespace ICSharpCode.SharpDevelop.DefaultEditor.Commands @@ -56,13 +56,17 @@ namespace ICSharpCode.SharpDevelop.DefaultEditor.Commands
i.Checked = false;
}
item.Checked = true;
IHighlightingStrategy strat = HighlightingStrategyFactory.CreateHighlightingStrategy(item.Text);
if (strat == null) {
throw new Exception("Strategy can't be null");
}
control.Document.HighlightingStrategy = strat;
if (control is SharpDevelopTextAreaControl) {
((SharpDevelopTextAreaControl)control).InitializeAdvancedHighlighter();
try {
IHighlightingStrategy strat = HighlightingStrategyFactory.CreateHighlightingStrategy(item.Text);
if (strat == null) {
throw new Exception("Strategy can't be null");
}
control.Document.HighlightingStrategy = strat;
if (control is SharpDevelopTextAreaControl) {
((SharpDevelopTextAreaControl)control).InitializeAdvancedHighlighter();
}
} catch (HighlightingDefinitionInvalidException ex) {
MessageBox.Show(ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
control.Refresh();
}

18
src/Main/Base/Project/Src/TextEditor/Gui/Editor/TextEditorDisplayBinding.cs

@ -51,8 +51,13 @@ namespace ICSharpCode.SharpDevelop.DefaultEditor.Gui.Editor @@ -51,8 +51,13 @@ namespace ICSharpCode.SharpDevelop.DefaultEditor.Gui.Editor
b2.textAreaControl.Dock = DockStyle.Fill;
b2.Load(fileName);
b2.textAreaControl.Document.HighlightingStrategy = HighlightingStrategyFactory.CreateHighlightingStrategyForFile(fileName);
b2.textAreaControl.InitializeAdvancedHighlighter();
try {
b2.textAreaControl.Document.HighlightingStrategy = HighlightingStrategyFactory.CreateHighlightingStrategyForFile(fileName);
b2.textAreaControl.InitializeAdvancedHighlighter();
} catch (HighlightingDefinitionInvalidException ex) {
b2.textAreaControl.Document.HighlightingStrategy = HighlightingStrategyFactory.CreateHighlightingStrategy();
MessageBox.Show(ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
b2.textAreaControl.InitializeFormatter();
b2.textAreaControl.ActivateQuickClassBrowserOnDemand();
@ -64,8 +69,13 @@ namespace ICSharpCode.SharpDevelop.DefaultEditor.Gui.Editor @@ -64,8 +69,13 @@ namespace ICSharpCode.SharpDevelop.DefaultEditor.Gui.Editor
TextEditorDisplayBindingWrapper b2 = new TextEditorDisplayBindingWrapper();
b2.textAreaControl.Document.TextContent = content;
b2.textAreaControl.Document.HighlightingStrategy = HighlightingStrategyFactory.CreateHighlightingStrategy(language);
b2.textAreaControl.InitializeAdvancedHighlighter();
try {
b2.textAreaControl.Document.HighlightingStrategy = HighlightingStrategyFactory.CreateHighlightingStrategy(language);
b2.textAreaControl.InitializeAdvancedHighlighter();
} catch (HighlightingDefinitionInvalidException ex) {
b2.textAreaControl.Document.HighlightingStrategy = HighlightingStrategyFactory.CreateHighlightingStrategy();
MessageBox.Show(ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
b2.textAreaControl.InitializeFormatter();
b2.textAreaControl.ActivateQuickClassBrowserOnDemand();
return b2;

Loading…
Cancel
Save