public SparqlUpdateAutoCompleter(ITextEditorAdaptor <T> editor) : base(editor, null) { foreach (String keyword in SparqlSpecsHelper.SparqlUpdate11Keywords) { this._keywords.Add(new KeywordData(keyword)); this._keywords.Add(new KeywordData(keyword.ToLower())); } }
/// <summary> /// Finds the next occurrence of the currently configured search in the given text editor /// </summary> /// <param name="editor">Text Editor</param> /// <returns>True if a match if found, false otherwise</returns> public bool FindNext(ITextEditorAdaptor <T> editor) { if (this.Scope == FindAndReplaceScope.CurrentDocument || editor.SelectionLength == 0) { return(this.FindNext(editor, -1, editor.Text.Length)); } else { return(this.FindNext(editor, Math.Max(0, editor.SelectionStart - 1), editor.SelectionStart + editor.SelectionLength)); } }
public GoToLine(ITextEditorAdaptor <TextEditor> editor) { InitializeComponent(); this._line = editor.Control.Document.GetLineByOffset(editor.CaretOffset).LineNumber; this._maxLine = editor.Control.Document.LineCount; this.txtLineNumber.Text = this._line.ToString(); this.lblLineNumber.Content = String.Format((String)this.lblLineNumber.Content, this._maxLine); this.txtLineNumber.SelectAll(); this.txtLineNumber.Focus(); }
public GoToLine(ITextEditorAdaptor<TextEditor> editor) { InitializeComponent(); this._line = editor.Control.Document.GetLineByOffset(editor.CaretOffset).LineNumber; this._maxLine = editor.Control.Document.LineCount; this.txtLineNumber.Text = this._line.ToString(); this.lblLineNumber.Content = String.Format((String)this.lblLineNumber.Content, this._maxLine); this.txtLineNumber.SelectAll(); this.txtLineNumber.Focus(); }
/// <summary> /// Creates a document /// </summary> /// <param name="editor">Text Editor</param> /// <param name="filename">Filename</param> /// <param name="title">Title</param> internal Document(ITextEditorAdaptor <T> editor, String filename, String title) { if (editor == null) { throw new ArgumentNullException("editor"); } this._editor = editor; this._filename = filename; this._title = title; //Subscribe to relevant events on the Editor this._editor.TextChanged += new TextEditorEventHandler <T>(this.HandleTextChanged); this._editor.DoubleClick += new TextEditorEventHandler <T>(this.HandleDoubleClick); }
/// <summary> /// Finds the first occurrence of the currently configured search in the given text editor /// </summary> /// <param name="editor">Text Editor</param> public void Find(ITextEditorAdaptor <T> editor) { bool fromStart = (editor.CaretOffset == 0); if (!this.FindNext(editor) && !fromStart) { if (this.ShouldRestartSearchFromStart()) { editor.CaretOffset = 0; editor.SelectionStart = 0; editor.SelectionLength = 0; this.FindNext(editor); } } }
/// <summary> /// Replace the next occurrence of the currently configured search in the given text editor /// </summary> /// <param name="editor">Text Editor</param> public void Replace(ITextEditorAdaptor <T> editor) { if (String.IsNullOrEmpty(this.ReplaceText)) { this.ShowMessage("No Replace Text specified"); } //Check whether the relevant Text is already selected if (!String.IsNullOrEmpty(this.FindText)) { if (this.FindText.Equals(editor.GetText(editor.SelectionStart, editor.SelectionLength))) { //If it is remove the selection so the FindNext() call will simply find the currently highlighted text editor.SelectionStart = 0; editor.SelectionLength = 0; } } bool fromStart = (editor.CaretOffset == 0); if (this.FindNext(editor)) { editor.Replace(editor.SelectionStart, editor.SelectionLength, this.ReplaceText); editor.SelectionLength = this.ReplaceText.Length; editor.CaretOffset = editor.SelectionStart; } else if (!fromStart) { if (this.ShouldRestartSearchFromStart()) { editor.CaretOffset = 0; editor.SelectionStart = 0; editor.SelectionLength = 0; if (this.FindNext(editor)) { editor.Replace(editor.SelectionStart, editor.SelectionLength, this.ReplaceText); editor.SelectionLength = this.ReplaceText.Length; } } } }
/// <summary> /// Creates a new auto-complete for the specific SPARQL syntax /// </summary> /// <param name="editor">Text Editor</param> /// <param name="syntax">SPARQL Syntax</param> public SparqlAutoCompleter(ITextEditorAdaptor <T> editor, SparqlQuerySyntax?syntax) : base(editor) { //Alter the Regex patterns this.PrefixRegexPattern = this.PrefixRegexPattern.Substring(1, this.PrefixRegexPattern.Length - 6); this.BlankNodePattern = @"_:(\p{L}|\d)(\p{L}|\p{N}|-|_)*"; //Add Prefix Definitions to Keywords this._keywords.Add(new SparqlStyleBaseDeclarationData()); this._keywords.Add(new SparqlStyleDefaultPrefixDeclarationData()); foreach (VocabularyDefinition vocab in AutoCompleteManager.Vocabularies) { this._keywords.Add(new SparqlStylePrefixDeclarationData(vocab.Prefix, vocab.NamespaceUri)); } //If not Query Syntax don't add any Query Keywords if (syntax == null) { return; } //Add Keywords relevant to the Syntax this._syntax = (SparqlQuerySyntax)syntax; foreach (String keyword in SparqlSpecsHelper.SparqlQuery10Keywords) { this._keywords.Add(new KeywordData(keyword)); this._keywords.Add(new KeywordData(keyword.ToLower())); } if (syntax != SparqlQuerySyntax.Sparql_1_0) { foreach (String keyword in SparqlSpecsHelper.SparqlQuery11Keywords) { this._keywords.Add(new KeywordData(keyword)); this._keywords.Add(new KeywordData(keyword.ToLower())); } } //Sort the Keywords this._keywords.Sort(); }
/// <summary> /// Gets the auto-completer for a given text editor /// </summary> /// <typeparam name="T">Control Type</typeparam> /// <param name="name">Syntax Name</param> /// <param name="editor">Text Editor</param> /// <returns>Auto-Completer if available, null otherwise</returns> public static IAutoCompleter <T> GetAutoCompleter <T>(String name, ITextEditorAdaptor <T> editor) { foreach (AutoCompleteDefinition def in _builtinCompleters) { if (def.Name.Equals(name)) { try { Type ctype = def.Type; Type target = ctype.MakeGenericType(new Type[] { typeof(T) }); IAutoCompleter <T> completer = (IAutoCompleter <T>)Activator.CreateInstance(target, new Object[] { editor }); return(completer); } catch { //Ignore errors as we'll try further definitions (if applicable) //or return null at the end } } } return(null); }
/// <summary> /// Creates a new auto-completer /// </summary> /// <param name="editor">Text Editor</param> public NTriplesAutoCompleter(ITextEditorAdaptor <T> editor) : base(editor) { }
/// <summary> /// Creates a new auto-completer /// </summary> /// <param name="editor">Text Editor</param> public TurtleAutoCompleter(ITextEditorAdaptor <T> editor) : base(editor) { }
public BaseAutoCompleter(ITextEditorAdaptor <T> editor) { this._editor = editor; }
/// <summary> /// Creates a new auto-completer /// </summary> /// <param name="editor">Text Editor</param> public Notation3AutoCompleter(ITextEditorAdaptor <T> editor) : base(editor) { }
/// <summary> /// Replace all occurrences of the currently configured search in the given text editor /// </summary> /// <param name="editor">Text Editor</param> public void ReplaceAll(ITextEditorAdaptor <T> editor) { if (String.IsNullOrEmpty(this.ReplaceText)) { this.ShowMessage("No Replace Text specified"); } int origPos = editor.CaretOffset; if (this.Scope != FindAndReplaceScope.Selection) { //Check whether the relevant Text is already selected if (!String.IsNullOrEmpty(this.FindText)) { if (this.FindText.Equals(editor.GetText(editor.SelectionStart, editor.SelectionLength))) { //If it is remove the selection so the FindNext() call will simply find the currently highlighted text editor.SelectionStart = 0; editor.SelectionLength = 0; } } } //Replace All works over the entire document unless there was already a selection present int minPos, maxPos; bool restoreSelection = false; if (editor.SelectionLength > 0 && this.Scope == FindAndReplaceScope.Selection) { restoreSelection = true; minPos = editor.SelectionStart - 1; maxPos = editor.SelectionStart + editor.SelectionLength; editor.CaretOffset = Math.Max(minPos, 0); } else { minPos = -1; maxPos = editor.Text.Length; editor.CaretOffset = 0; } editor.SelectionStart = 0; editor.SelectionLength = 0; try { editor.BeginUpdate(); String replace = this.ReplaceText; while (this.FindNext(editor, minPos, maxPos)) { int diff = replace.Length - editor.SelectionLength; editor.Replace(editor.SelectionStart, editor.SelectionLength, replace); editor.SelectionLength = replace.Length; editor.CaretOffset = editor.SelectionStart; maxPos += diff; } } finally { editor.EndUpdate(); } editor.CaretOffset = origPos; editor.ScrollToLine(editor.GetLineByOffset(origPos)); if (restoreSelection) { editor.Select(minPos + 1, maxPos - minPos - 1); } }
/// <summary> /// Replace all occurrences of the currently configured search in the given text editor /// </summary> /// <param name="editor">Text Editor</param> public void ReplaceAll(ITextEditorAdaptor <T> editor) { if (String.IsNullOrEmpty(this.ReplaceText)) { this.ShowMessage("No Replace Text specified"); } int origPos = editor.CaretOffset; if (this.Scope != FindAndReplaceScope.Selection) { // Always clear any existing selection if replacing over the entire document editor.SelectionStart = 0; editor.SelectionLength = 0; } //Replace All works over the entire document unless there was already a selection present int minPos, maxPos; bool restoreSelection = false; if (editor.SelectionLength > 0 && this.Scope == FindAndReplaceScope.Selection) { restoreSelection = true; minPos = editor.SelectionStart - 1; maxPos = editor.SelectionStart + editor.SelectionLength; editor.CaretOffset = Math.Max(minPos, 0); } else { minPos = -1; maxPos = editor.Text.Length; editor.CaretOffset = 0; } editor.SelectionStart = 0; editor.SelectionLength = 0; try { editor.BeginUpdate(); String replace = this.ReplaceText; while (this.FindNext(editor, minPos, maxPos)) { int diff; // Do replacement if (this.UseRegex && this.HasCaptureGroups) { // Using capture groups StringBuilder builder = new StringBuilder(); int start = 0; foreach (Match m in this._hasCaptureGroups.Matches(replace)) { // Append preceding verbatim text // +1 is necessary because builder.Append(replace.Substring(start, m.Index + 1)); start = m.Index + m.Length; int captureGrop = Int32.Parse(m.Groups[1].Value); builder.Append(this._currentMatch.Groups[captureGrop].Value); } // Append remaining verbatim text builder.Append(replace.Substring(start)); diff = builder.Length - editor.SelectionLength; editor.Replace(editor.SelectionStart, editor.SelectionLength, builder.ToString()); } else { // Simple text replacement diff = replace.Length - editor.SelectionLength; editor.Replace(editor.SelectionStart, editor.SelectionLength, replace); } editor.SelectionLength = replace.Length; editor.CaretOffset = editor.SelectionStart; minPos = editor.SelectionStart + editor.SelectionLength + 1; maxPos += diff; } } finally { editor.EndUpdate(); } editor.CaretOffset = origPos; editor.ScrollToLine(editor.GetLineByOffset(origPos)); if (restoreSelection) { editor.Select(minPos + 1, maxPos - minPos - 1); } }
/// <summary> /// Creates a new auto-completer /// </summary> /// <param name="editor">Text Editor</param> public Sparql10AutoCompleter(ITextEditorAdaptor <T> editor) : base(editor, SparqlQuerySyntax.Sparql_1_0) { }
/// <summary> /// Creates new event arguments /// </summary> /// <param name="editor">Text Editor</param> public TextEditorEventArgs(ITextEditorAdaptor <T> editor) { this.TextEditor = editor; }
/// <summary> /// Creates a document /// </summary> /// <param name="editor">Text Editor</param> internal Document(ITextEditorAdaptor <T> editor) : this(editor, null, null) { }
/// <summary> /// Creates a document /// </summary> /// <param name="editor">Text Editor</param> /// <param name="filename">Filename</param> internal Document(ITextEditorAdaptor <T> editor, String filename) : this(editor, filename, Path.GetFileName(filename)) { }
/// <summary> /// Finds the next occurrence of the currently configured search in the given text editor within the specified bounds /// </summary> /// <param name="editor">Text Editor</param> /// <param name="minPos">Minimum Position</param> /// <param name="maxPos">Maximum Position</param> /// <returns></returns> public bool FindNext(ITextEditorAdaptor <T> editor, int minPos, int maxPos) { if (String.IsNullOrEmpty(this.FindText)) { this.ShowMessage("No Find Text specified"); return(false); } String find = this.FindText; //Validate Regex Regex regex = null; RegexOptions regexOps = this.MatchCase ? RegexOptions.None : RegexOptions.IgnoreCase; regexOps |= RegexOptions.Multiline; regexOps |= RegexOptions.CultureInvariant; if (this.UseRegex) { try { regex = new Regex(find, regexOps); } catch (Exception ex) { this.ShowMessage("Regular Expression is malformed - " + ex.Message); return(false); } } else { this._currentMatch = null; } //Add Search Text to Combo Box for later reuse if (!this.RecentFindTexts.Contains(find)) { this.RecentFindTexts.Add(find); } int start = editor.CaretOffset; int pos; int length = find.Length; StringComparison compareMode = this.MatchCase ? StringComparison.Ordinal : StringComparison.OrdinalIgnoreCase; if (this.SearchUp) { //Search portion of Document prior to current position if (this.UseRegex) { MatchCollection ms = regex.Matches(editor.Text.Substring(0, start)); if (ms.Count == 0) { this._currentMatch = null; pos = -1; } else { this._currentMatch = ms[ms.Count - 1]; pos = this._currentMatch.Index; length = this._currentMatch.Length; } } else { pos = editor.Text.Substring(0, start).LastIndexOf(find, compareMode); } } else { //Search position of Document subsequent to current position (incl. any selected text) start += editor.SelectionLength; if (this.UseRegex) { this._currentMatch = regex.Match(editor.Text.Substring(start)); if (!this._currentMatch.Success) { pos = -1; } else { pos = start + this._currentMatch.Index; length = this._currentMatch.Length; } } else { pos = editor.Text.IndexOf(find, start, compareMode); } } //If we've found the position of the next highlight it and return true otherwise return false if (pos <= -1) { return(false); } //Check we meet any document range restrictions if (pos < minPos || pos > maxPos) { return(false); } //If Matching on whole word ensure that their are boundaries before and after the match if (this.MatchWholeWord) { //Check boundary before if (pos > 0) { char c = editor.Text[pos - 1]; if (Char.IsLetterOrDigit(c)) { //Not a boundary so adjust start position and recurse editor.CaretOffset = pos + length; if (!this.SearchUp) { editor.CaretOffset -= editor.SelectionLength; } return(this.FindNext(editor)); } } //Check boundary after if (pos + length < editor.Text.Length - 1) { char c = editor.Text[pos + length]; if (Char.IsLetterOrDigit(c)) { //Not a boundary so adjust start position and recurse editor.CaretOffset = pos + length - 1; if (!this.SearchUp) { editor.CaretOffset -= editor.SelectionLength; } return(this.FindNext(editor)); } } } editor.Select(pos, length); editor.CaretOffset = pos; editor.ScrollToLine(editor.GetLineByOffset(pos)); return(true); }
/// <summary> /// Replace the next occurrence of the currently configured search in the given text editor /// </summary> /// <param name="editor">Text Editor</param> public void Replace(ITextEditorAdaptor <T> editor) { if (String.IsNullOrEmpty(this.ReplaceText)) { this.ShowMessage("No Replace Text specified"); } //Check whether the relevant Text is already selected bool alreadySelected = false; if (!String.IsNullOrEmpty(this.FindText)) { if (this.UseRegex && this._currentMatch != null) { alreadySelected = true; } else if (this.FindText.Equals(editor.GetText(editor.SelectionStart, editor.SelectionLength))) { alreadySelected = true; } } bool fromStart = (editor.CaretOffset == 0); if (alreadySelected || this.FindNext(editor)) { if (this.UseRegex && this.HasCaptureGroups) { // Using capture groups String replace = this.ReplaceText; StringBuilder builder = new StringBuilder(); int start = 0; foreach (Match m in this._hasCaptureGroups.Matches(replace)) { // Append preceding verbatim text // +1 is necessary because builder.Append(replace.Substring(start, m.Index + 1)); start = m.Index + m.Length; int captureGrop = Int32.Parse(m.Groups[1].Value); builder.Append(this._currentMatch.Groups[captureGrop].Value); } // Append remaining verbatim text builder.Append(replace.Substring(start)); editor.Replace(editor.SelectionStart, editor.SelectionLength, builder.ToString()); editor.SelectionLength = builder.Length; editor.CaretOffset = editor.SelectionStart; } else { // Simple text replacement editor.Replace(editor.SelectionStart, editor.SelectionLength, this.ReplaceText); editor.SelectionLength = this.ReplaceText.Length; editor.CaretOffset = editor.SelectionStart; } } else if (!fromStart) { if (!this.ShouldRestartSearchFromStart()) { return; } editor.CaretOffset = 0; editor.SelectionStart = 0; editor.SelectionLength = 0; if (this.FindNext(editor)) { if (this.UseRegex && this.HasCaptureGroups) { // Using capture groups String replace = this.ReplaceText; StringBuilder builder = new StringBuilder(); int start = 0; foreach (Match m in this._hasCaptureGroups.Matches(replace)) { // Append preceding verbatim text // +1 is necessary because builder.Append(replace.Substring(start, m.Index + 1)); start = m.Index + m.Length; int captureGrop = Int32.Parse(m.Groups[1].Value); builder.Append(this._currentMatch.Groups[captureGrop].Value); } // Append remaining verbatim text builder.Append(replace.Substring(start)); editor.Replace(editor.SelectionStart, editor.SelectionLength, builder.ToString()); editor.SelectionLength = builder.Length; editor.CaretOffset = editor.SelectionStart; } else { // Simple text replacement editor.Replace(editor.SelectionStart, editor.SelectionLength, this.ReplaceText); editor.SelectionLength = this.ReplaceText.Length; editor.CaretOffset = editor.SelectionStart; } } } }
/// <summary> /// Creates a new auto-completer /// </summary> /// <param name="editor">Text Editor</param> public SparqlAutoCompleter(ITextEditorAdaptor <T> editor) : this(editor, SparqlQuerySyntax.Sparql_1_1) { }