public static XmlSyntaxErrorVM GetErrorPositionFromAvalonException(HighlightingDefinitionInvalidException ex, TextDocument codeDocument) { if (String.IsNullOrEmpty(codeDocument.Text)) { return(null); } if (ex.InnerException != null && ex.InnerException is System.Xml.XmlException) { var exXml = ex.InnerException as System.Xml.XmlException; var errorLine = codeDocument.GetLineByNumber(exXml.LineNumber); return(new XmlSyntaxErrorVM { Message = exXml.Message, Highlight = new WordHighlight(errorLine.Offset, errorLine.Length) }); } if (ex.InnerException != null && ex.InnerException is System.Xml.Schema.XmlSchemaValidationException) { var exXml = ex.InnerException as System.Xml.Schema.XmlSchemaValidationException; var errorLine = codeDocument.GetLineByNumber(exXml.LineNumber); return(new XmlSyntaxErrorVM { Message = exXml.Message, Highlight = new WordHighlight(errorLine.Offset, errorLine.Length) }); } var regexMatch = TryTheseRegexUntilMatches(ex.Message, new[] { // https://regex101.com/r/ieg7bu/4 @"Error at line (\d+):", // https://regex101.com/r/9ZbB2G/2 @"Error at position \(line (\d+)" }); if (regexMatch?.Success ?? false) { var lineNum = regexMatch.Groups[1].Value.ParseIntOrDefault(); var errorLine = codeDocument.GetLineByNumber(lineNum); return(new XmlSyntaxErrorVM { Message = ex.Message, Highlight = new WordHighlight(errorLine.Offset, errorLine.Length), }); } return(new XmlSyntaxErrorVM { Message = ex.Message, Highlight = null, }); }
protected virtual void OnReloadHighlighting(object sender, EventArgs e) { if (this.Document.HighlightingStrategy != null) { try { this.Document.HighlightingStrategy = HighlightingStrategyFactory.CreateHighlightingStrategy(this.Document.HighlightingStrategy.Name); } catch (HighlightingDefinitionInvalidException highlightingDefinitionInvalidException1) { HighlightingDefinitionInvalidException highlightingDefinitionInvalidException = highlightingDefinitionInvalidException1; MessageBox.Show(highlightingDefinitionInvalidException.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand); } this.OptionsChanged(); } }
public void LoadFile(string fileName, Stream stream, bool autoLoadHighlighting, bool autodetectEncoding) { if (stream == null) { throw new ArgumentNullException("stream"); } this.BeginUpdate(); this.document.TextContent = string.Empty; this.document.UndoStack.ClearAll(); this.document.BookmarkManager.Clear(); if (autoLoadHighlighting) { try { this.document.HighlightingStrategy = HighlightingStrategyFactory.CreateHighlightingStrategyForFile(fileName); } catch (HighlightingDefinitionInvalidException highlightingDefinitionInvalidException1) { HighlightingDefinitionInvalidException highlightingDefinitionInvalidException = highlightingDefinitionInvalidException1; MessageBox.Show(highlightingDefinitionInvalidException.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand); } } if (!autodetectEncoding) { using (StreamReader streamReader = new StreamReader(fileName, this.Encoding)) { this.Document.TextContent = streamReader.ReadToEnd(); } } else { System.Text.Encoding encoding = this.Encoding; this.Document.TextContent = FileReader.ReadFileContent(stream, ref encoding); this.Encoding = encoding; } this.FileName = fileName; this.Document.UpdateQueue.Clear(); this.EndUpdate(); this.OptionsChanged(); this.Refresh(); }