private string GetText(ArchAngel.Interfaces.Template.File scriptFile, int startPos) { string body; for (int i = 0; i < SearchHelper.FoundLocations.Count; i++) { if (SearchHelper.FoundLocations[i].ScriptFile == scriptFile) { body = SearchHelper.FoundLocations[i].Body; if (body.Length == 0) { continue; } int lineStart = startPos; // Find the start of the line while (lineStart > 0 && lineStart < body.Length) { if (body[lineStart] == '\n' || body[lineStart] == '\r') { lineStart += 1; break; } lineStart -= 1; } int lineEnd = lineStart; // startPos; // Find the end of the line while (lineEnd >= 0 && lineEnd < body.Length) { if (body[lineEnd] == '\n' || body[lineEnd] == '\r') { //lineEnd -= 1; break; } lineEnd += 1; } // Get the text of the line in question int textLength = lineEnd - lineStart; string returnVal = ""; if (textLength > 0 && lineStart >= 0 && lineStart < body.Length && (lineStart + textLength) < body.Length) { returnVal = body.Substring(lineStart, textLength); if (returnVal.Length > 0) { // Remove any tabs from the line text returnVal = returnVal.Replace("\t", " "); } } return(returnVal); } } return(""); }
public static void RunFind() //string textToFind, Scope scope, SearchFunctions searchFunctions, FindReplaceOptions options) { m_foundLocations.Clear(); switch (searchFunctions) { case SearchFunctions.AllFunctions: // Process open functions first ArchAngel.Interfaces.Template.File currentScriptFile = ContentItems.Templates.CurrentFile; SyntaxEditor editor = ContentItems.Templates.Instance.SyntaxEditor; if (editor != null) { FindInText(editor.Text, Options.FindText, scope, currentScriptFile, true, Options); } // Process remaining functions foreach (var scriptFile in ArchAngel.Interfaces.SharedData.CurrentProject.TemplateProject.AllScriptFiles) { // Don't add functions that have already been added as open functions if (scriptFile != currentScriptFile) { FindInText(scriptFile.Script.Body, Options.FindText, scope, scriptFile, true, Options); } } break; case SearchFunctions.CurrentFunction: if (ContentItems.Templates.CurrentFile != null) { FindInText(ContentItems.Templates.Instance.SyntaxEditor.Text, Options.FindText, scope, ContentItems.Templates.CurrentFile, true, Options, ContentItems.Templates.Instance.SyntaxEditor.SelectedView.Selection.StartOffset, false); } break; default: throw new NotImplementedException("Not coded yet."); } }
public static void FindNext() //string textToFind, Scope scope, SearchFunctions searchFunctions, FindReplaceOptions options) { switch (searchFunctions) { case SearchFunctions.CurrentFunction: if (ContentItems.Templates.CurrentFile != null) { ArchAngel.Interfaces.Template.File function = ContentItems.Templates.CurrentFile; SyntaxEditor editor = ContentItems.Templates.Instance.SyntaxEditor; FindInText(editor.Text, Options.FindText, scope, function, true, Options, editor.SelectedView.Selection.StartOffset, true); } break; case SearchFunctions.OpenFunctions: break; case SearchFunctions.AllFunctions: break; default: throw new NotImplementedException("Not coded yet."); } }
/// <summary> /// Fills FoundLocations with positions in the text. /// </summary> /// <param name="text"></param> /// <param name="textToFind"></param> /// <param name="scope"></param> /// <param name="scriptFile"></param> /// <param name="isTemplateFunction"></param> /// <param name="options"></param> /// <param name="userOffset"></param> /// <param name="findOneOnly"></param> /// <returns></returns> private static bool FindInText(string text, string textToFind, Scope scope, ArchAngel.Interfaces.Template.File scriptFile, bool isTemplateFunction, FindReplaceOptions options, int userOffset, bool findOneOnly) { char delimiter = '%'; if (ArchAngel.Interfaces.SharedData.CurrentProject != null && ArchAngel.Interfaces.SharedData.CurrentProject.TemplateProject != null && ArchAngel.Interfaces.SharedData.CurrentProject.TemplateProject.Delimiter == Interfaces.Template.TemplateProject.DelimiterTypes.T4) { delimiter = '#'; } text = text.Replace("\r\n", "\n"); // TODO: this function is in dire need of refactoring. The searching code is repeated in multiple places. bool found = false; if (string.IsNullOrEmpty(textToFind)) { return(false); } if (!isTemplateFunction) { // Only template functions should have 'script' and 'output' scope = Scope.Both; } if (!options.MatchCase) { text = text.ToLower(); textToFind = textToFind.ToLower(); } int scriptStartPos = 0; int scriptEndPos = 0; int nextPos; switch (scope) { case Scope.ScriptOnly: for (int i = 0; i < text.Length; i++) { if (text[i] == '<' && text.Length > (i + 1) && text[i + 1] == delimiter) { scriptStartPos = i + 2; } else if (text[i] == delimiter && text.Length > (i + 1) && text[i + 1] == '>') { scriptEndPos = i; // Replace text string script = text.Substring(scriptStartPos, scriptEndPos - scriptStartPos); nextPos = script.IndexOf(textToFind); while (nextPos >= 0) { if (!options.MatchWholeWord || (options.MatchWholeWord && IsWholeWord(ref script, ref textToFind, ref nextPos))) { m_foundLocations.Add(new FoundLocation(scriptFile, scriptStartPos + nextPos, textToFind.Length)); if (scriptStartPos + nextPos > userOffset) { found = true; if (findOneOnly) { m_foundLocations.Clear(); m_foundLocations.Add(new FoundLocation(scriptFile, scriptStartPos + nextPos, textToFind.Length)); return(found); } } } nextPos = script.IndexOf(textToFind, nextPos + 1); } i = scriptEndPos + 2; } } break; case Scope.OutputOnly: scriptStartPos = 0; // This works for template functions only for (int i = 0; i < text.Length; i++) { if (text[i] == delimiter && text.Length > (i + 1) && text[i + 1] == '>') { scriptStartPos = i + 2; } else if (text[i] == '<' && text.Length > (i + 1) && text[i + 1] == delimiter) { scriptEndPos = i; // Replace text string script = text.Substring(scriptStartPos, scriptEndPos - scriptStartPos); nextPos = script.IndexOf(textToFind); while (nextPos >= 0) { if (!options.MatchWholeWord || (options.MatchWholeWord && IsWholeWord(ref text, ref textToFind, ref nextPos))) { m_foundLocations.Add(new FoundLocation(scriptFile, scriptStartPos + nextPos, textToFind.Length)); if (scriptStartPos + nextPos > userOffset) { found = true; if (findOneOnly) { m_foundLocations.Clear(); m_foundLocations.Add(new FoundLocation(scriptFile, scriptStartPos + nextPos, textToFind.Length)); return(found); } } } nextPos = script.IndexOf(textToFind, nextPos + 1); } i = scriptEndPos + 2; } } // Search the remaining text if (scriptEndPos < text.Length) { string script = text.Substring(scriptStartPos); nextPos = script.IndexOf(textToFind); while (nextPos >= 0) { if (!options.MatchWholeWord || (options.MatchWholeWord && IsWholeWord(ref text, ref textToFind, ref nextPos))) { m_foundLocations.Add(new FoundLocation(scriptFile, scriptStartPos + nextPos, textToFind.Length)); if (scriptStartPos + nextPos > userOffset) { found = true; if (findOneOnly) { m_foundLocations.Clear(); m_foundLocations.Add(new FoundLocation(scriptFile, scriptStartPos + nextPos, textToFind.Length)); return(found); } } } nextPos = script.IndexOf(textToFind, nextPos + 1); } } break; case Scope.Both: nextPos = text.IndexOf(textToFind); while (nextPos >= 0) { if (!options.MatchWholeWord || (options.MatchWholeWord && IsWholeWord(ref text, ref textToFind, ref nextPos))) { m_foundLocations.Add(new FoundLocation(scriptFile, nextPos, textToFind.Length)); if (scriptStartPos + nextPos > userOffset) { found = true; if (findOneOnly) { m_foundLocations.Clear(); m_foundLocations.Add(new FoundLocation(scriptFile, nextPos, textToFind.Length)); return(found); } } } nextPos = text.IndexOf(textToFind, nextPos + 1); } break; default: throw new NotImplementedException("Not coded yet"); } return(found); }
private static void FindInText(string text, string textToFind, Scope scope, ArchAngel.Interfaces.Template.File scriptFile, bool isTemplateFunction, FindReplaceOptions options) { FindInText(text, textToFind, scope, scriptFile, isTemplateFunction, options, -1, false); }
public FoundLocation(ArchAngel.Interfaces.Template.File scriptFile, int startPos, int length) { ScriptFile = scriptFile; StartPos = startPos; Length = length; }
internal void DisplayScriptFile(ArchAngel.Interfaces.Template.File file) { PopulateIterators(true); _CurrentFile = file; if (file.Iterator == ArchAngel.Interfaces.Template.IteratorTypes.None) { comboBoxIterator.Text = "one file"; crumbBarTestObjects.Items.Clear(); crumbBarTestObjects.Enabled = false; } else { comboBoxIterator.Text = "one file per " + file.Iterator.ToString(); PopulateTestCrumbBar(file.Iterator); } syntaxEditorFilename.Text = file.Name; comboBoxSyntax.Text = Slyce.Common.SyntaxEditorHelper.LanguageNameFromEnum(file.Script.Syntax); switch (file.Encoding.EncodingName) { case "US-ASCII": comboBoxEncoding.Text = "ASCII"; break; case "Unicode": comboBoxEncoding.Text = "Unicode"; break; case "Unicode (UTF-8)": comboBoxEncoding.Text = "UTF8"; break; default: throw new NotImplementedException("Encoding name not handled yet in AfterNodeSelect(): " + file.Encoding.EncodingName); } syntaxEditor1.Text = file.Script.Body; SetSyntax(file.Script.Syntax, syntaxEditor1, syntaxEditorTest); labelFilename.Visible = true; labelFilename.Text = "Name:"; comboBoxIterator.Visible = true; syntaxEditorFilename.Visible = true; comboBoxSyntax.Visible = true; syntaxEditor1.Visible = true; comboBoxStaticFiles.Visible = false; labelHeader.Text = " Create "; superTabItemScript.Text = " Script file "; labelHeader.Image = imageList1.Images[IMG_FILE]; labelSkipStaticFile.Visible = false; syntaxEditorSkipStaticFile.Visible = false; panel3.Height = syntaxEditorFilename.Bottom + 10; timer1.Stop(); timer1.Interval = 10; timer1.Start(); }
private void treeFiles_AfterNodeSelect(object sender, AdvTreeNodeEventArgs e) { _CurrentFile = null; syntaxEditorTest.Visible = false; labelResults.Visible = false; superTabControl1.SelectedTab = superTabItemScript; if (treeFiles.SelectedIndex <= 0) { comboBoxIterator.Visible = false; syntaxEditorFilename.Visible = false; comboBoxSyntax.Visible = false; labelFilename.Visible = false; syntaxEditor1.Visible = false; labelHeader.Text = ""; labelHeader.Image = imageList1.Images[IMG_CLOSED_FOLDER]; labelSkipStaticFile.Visible = false; syntaxEditorSkipStaticFile.Visible = false; } else if (e.Node.Tag is ArchAngel.Interfaces.Template.Folder) { PopulateIterators(false); ArchAngel.Interfaces.Template.Folder folder = (ArchAngel.Interfaces.Template.Folder)e.Node.Tag; if (folder.Iterator == ArchAngel.Interfaces.Template.IteratorTypes.None) { comboBoxIterator.Text = "one folder"; crumbBarTestObjects.Items.Clear(); crumbBarTestObjects.Enabled = false; } else { comboBoxIterator.Text = "one folder per " + folder.Iterator.ToString(); PopulateTestCrumbBar(folder.Iterator); } syntaxEditorFilename.Text = folder.Name; labelFilename.Visible = true; labelFilename.Text = "Name:"; comboBoxIterator.Visible = true; syntaxEditorFilename.Visible = true; comboBoxSyntax.Visible = false; syntaxEditor1.Visible = false; comboBoxStaticFiles.Visible = false; labelHeader.Text = " Create"; superTabItemScript.Text = " Folder script "; labelHeader.Image = imageList1.Images[IMG_CLOSED_FOLDER]; labelSkipStaticFile.Visible = false; syntaxEditorSkipStaticFile.Visible = false; panel3.Height = syntaxEditorFilename.Bottom + 10; } else if (e.Node.Tag is ArchAngel.Interfaces.Template.File) { DisplayScriptFile((ArchAngel.Interfaces.Template.File)e.Node.Tag); } else if (e.Node.Tag is ArchAngel.Interfaces.Template.StaticFile) { PopulateIterators(true); ArchAngel.Interfaces.Template.StaticFile staticFile = (ArchAngel.Interfaces.Template.StaticFile)e.Node.Tag; if (staticFile.Iterator == ArchAngel.Interfaces.Template.IteratorTypes.None) { comboBoxIterator.Text = "one file"; crumbBarTestObjects.Items.Clear(); crumbBarTestObjects.Enabled = false; } else { comboBoxIterator.Text = "one file per " + staticFile.Iterator.ToString(); crumbBarTestObjects.Items.Clear(); crumbBarTestObjects.Enabled = true; } labelFilename.Visible = true; labelFilename.Text = "Name:"; comboBoxIterator.Visible = true; syntaxEditorFilename.Visible = true; comboBoxSyntax.Visible = false; syntaxEditor1.Visible = false; labelHeader.Text = " Create"; superTabItemScript.Text = " Static file "; labelHeader.Image = imageList1.Images[IMG_CLOSED_FOLDER]; string filename = staticFile.Name; syntaxEditorFilename.Text = staticFile.Name; comboBoxStaticFiles.Items.Clear(); foreach (string sf in ArchAngel.Interfaces.SharedData.CurrentProject.TemplateProject.ResourceFiles) comboBoxStaticFiles.Items.Add(sf); if (!string.IsNullOrEmpty(staticFile.ResourceName)) { comboBoxStaticFiles.SelectedItem = staticFile.ResourceName; syntaxEditorFilename.Text = filename; } //else if (comboBoxStaticFiles.Items.Count > 0) //{ // comboBoxStaticFiles.SelectedIndex = 0; // syntaxEditorFilename.Text = comboBoxStaticFiles.Text; //} comboBoxStaticFiles.Visible = true; labelSkipStaticFile.Visible = true; syntaxEditorSkipStaticFile.Visible = true; syntaxEditorSkipStaticFile.Text = staticFile.SkipThisFileScript; labelSkipStaticFile.Left = labelFilename.Left; labelSkipStaticFile.Top = labelFilename.Bottom + 20; syntaxEditorSkipStaticFile.Left = syntaxEditorFilename.Left; syntaxEditorSkipStaticFile.Top = labelSkipStaticFile.Bottom + 5; syntaxEditorSkipStaticFile.Width = syntaxEditorFilename.Width; syntaxEditorSkipStaticFile.Height = 200; panel3.Height = syntaxEditorSkipStaticFile.Bottom + 10; labelSkipStaticFile.BringToFront(); } }
public void AddNewFile() { if (ArchAngel.Interfaces.SharedData.CurrentProject.TemplateProject.IsOfficial) { if (MessageBox.Show(this, "Changes can't be made to built-in templates. Do you want to save-as a custom template?", "Built-in template", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation) == DialogResult.Yes) { SaveAs(); } return; } try { if (treeFiles.SelectedNodes.Count == 0) { MessageBox.Show(this, "Select a folder to add this file to first.", "No Folder Selected", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return; } else if (treeFiles.SelectedNodes.Count > 1) { throw new Exception("Only one node should be selected."); } Cursor = Cursors.WaitCursor; Refresh(); Node selectedNode = treeFiles.SelectedNodes[0]; ArchAngel.Interfaces.Template.Folder parentFolder = (ArchAngel.Interfaces.Template.Folder)selectedNode.Tag; ArchAngel.Interfaces.Template.File newFile = new ArchAngel.Interfaces.Template.File() { Id = ArchAngel.Interfaces.SharedData.CurrentProject.TemplateProject.GetNextAvailableFileId(), Name = "NewFile", Iterator = ArchAngel.Interfaces.Template.IteratorTypes.None, ParentFolder = parentFolder }; parentFolder.Files.Add(newFile); CreateNewFileAndAddToTree(selectedNode, newFile); syntaxEditorFilename.Focus(); } finally { Controller.Instance.MainForm.Activate(); Cursor = Cursors.Default; } }