/// <summary> /// Gets search results for a document /// </summary> private List <SearchMatch> GetResults(ScintillaControl sci) { if (this.searchBox.Text != String.Empty) { String pattern = this.searchBox.Text; FRSearch search = new FRSearch(pattern); search.IsEscaped = false; search.WholeWord = false; search.NoCase = true; search.IsRegex = true; search.Filter = SearchFilter.None; return(search.Matches(sci.Text)); } return(null); }
/// <summary> /// Checks if the member type is imported /// </summary> private Boolean MemberTypeImported(String type, String searchInText) { if (type == "*") { return(true); } SearchMatch result; String pattern = type; FRSearch search = new FRSearch(pattern); search.Filter = SearchFilter.OutsideCodeComments | SearchFilter.OutsideStringLiterals; search.NoCase = false; search.WholeWord = true; result = search.Match(searchInText); return(result != null); }
/// <summary> /// Gets search object for find and replace /// </summary> private FRSearch GetFRSearch() { String pattern = this.findComboBox.Text; FRSearch search = new FRSearch(pattern); search.IsRegex = this.regexCheckBox.Checked; search.IsEscaped = this.escapedCheckBox.Checked; search.WholeWord = this.wholeWordCheckBox.Checked; search.NoCase = !this.matchCaseCheckBox.Checked; search.Filter = SearchFilter.None; if (!this.commentsCheckBox.Checked) { search.Filter |= SearchFilter.OutsideCodeComments; } if (!this.stringsCheckBox.Checked) { search.Filter |= SearchFilter.OutsideStringLiterals; } return(search); }
/// <summary> /// Replaces all results specified by user input /// </summary> private void ReplaceAllButtonClick(Object sender, System.EventArgs e) { if (Globals.SciControl == null) { return; } ScintillaControl sci = Globals.SciControl; List <SearchMatch> matches = this.GetResults(sci); if (matches != null && this.lookComboBox.SelectedIndex == 1 && sci.SelText.Length > 0) { Int32 end = sci.MBSafeCharPosition(sci.SelectionEnd); Int32 start = sci.MBSafeCharPosition(sci.SelectionStart); matches = FRDialogGenerics.FilterMatches(matches, start, end); } if (matches != null) { sci.BeginUndoAction(); try { for (Int32 i = 0; i < matches.Count; i++) { FRDialogGenerics.SelectMatch(sci, matches[i]); String replaceWith = this.GetReplaceText(matches[i]); FRSearch.PadIndexes(matches, i, matches[i].Value, replaceWith); sci.EnsureVisible(sci.LineFromPosition(sci.MBSafePosition(matches[i].Index))); sci.ReplaceSel(replaceWith); } } finally { sci.EndUndoAction(); } FRDialogGenerics.UpdateComboBoxItems(this.findComboBox); FRDialogGenerics.UpdateComboBoxItems(this.replaceComboBox); String message = TextHelper.GetString("Info.ReplacedMatches"); String formatted = String.Format(message, matches.Count); this.ShowMessage(formatted, 0); this.lookupIsDirty = false; } }
/// <summary> /// Generates an FRSearch to find all instances of the given member name. /// Enables WholeWord and Match Case. No comment/string literal, escape characters, or regex searching. /// </summary> private static FRSearch GetFRSearch(string memberName, bool includeComments, bool includeStrings) { FRSearch search = new FRSearch(memberName); search.IsRegex = false; search.IsEscaped = false; search.WholeWord = true; search.NoCase = false; search.Filter = SearchFilter.None; if (!includeComments) { search.Filter |= SearchFilter.OutsideCodeComments; } if (!includeStrings) { search.Filter |= SearchFilter.OutsideStringLiterals; } return(search); }
/// <summary> /// Replaces only the matches in the current sci control /// </summary> public static void ReplaceMatches(IList <SearchMatch> matches, ScintillaControl sci, String replacement, String src) { if (sci == null || matches == null || matches.Count == 0) { return; } sci.BeginUndoAction(); try { for (Int32 i = 0; i < matches.Count; i++) { SelectMatch(sci, matches[i]); FRSearch.PadIndexes((List <SearchMatch>)matches, i, matches[i].Value, replacement); sci.EnsureVisible(sci.LineFromPosition(sci.MBSafePosition(matches[i].Index))); sci.ReplaceSel(replacement); } } finally { sci.EndUndoAction(); } }
/// <summary> /// Renames the given the set of matched references /// </summary> private void OnFindAllReferencesCompleted(object sender, RefactorCompleteEventArgs<IDictionary<string, List<SearchMatch>>> eventArgs) { UserInterfaceManager.ProgressDialog.Show(); UserInterfaceManager.ProgressDialog.SetTitle(TextHelper.GetString("Info.UpdatingReferences")); MessageBar.Locked = true; var isParameterVar = (Target.Member?.Flags & FlagType.ParameterVar) > 0; foreach (var entry in eventArgs.Results) { UserInterfaceManager.ProgressDialog.UpdateStatusMessage(TextHelper.GetString("Info.Updating") + " \"" + entry.Key + "\""); // re-open the document and replace all the text var doc = AssociatedDocumentHelper.LoadDocument(entry.Key); var sci = doc.SciControl; var targetMatches = entry.Value; if (isParameterVar) { var lineFrom = Target.Context.ContextFunction.LineFrom; var lineTo = Target.Context.ContextFunction.LineTo; var search = new FRSearch(NewName) {WholeWord = true, NoCase = false, SingleLine = true}; var matches = search.Matches(sci.Text, sci.PositionFromLine(lineFrom), lineFrom); matches.RemoveAll(it => it.Line < lineFrom || it.Line > lineTo); if (matches.Count != 0) { sci.BeginUndoAction(); try { for (var i = 0; i < matches.Count; i++) { var match = matches[i]; var expr = ASComplete.GetExpressionType(sci, sci.MBSafePosition(match.Index) + sci.MBSafeTextLength(match.Value)); if (expr.IsNull() || expr.Context.Value != NewName) continue; string replacement; var flags = expr.Member.Flags; if ((flags & FlagType.Static) > 0) replacement = ASContext.Context.CurrentClass.Name + "." + NewName; else if((flags & FlagType.LocalVar) == 0) replacement = "this." + NewName; else continue; RefactoringHelper.SelectMatch(sci, match); sci.EnsureVisible(sci.LineFromPosition(sci.MBSafePosition(match.Index))); sci.ReplaceSel(replacement); for (var j = 0; j < targetMatches.Count; j++) { var targetMatch = targetMatches[j]; if (targetMatch.Line <= match.Line) continue; FRSearch.PadIndexes(targetMatches, j, match.Value, replacement); if (targetMatch.Line == match.Line + 1) { targetMatch.LineText = sci.GetLine(match.Line); targetMatch.Column += replacement.Length - match.Value.Length; } break; } FRSearch.PadIndexes(matches, i + 1, match.Value, replacement); } } finally { sci.EndUndoAction(); } } } // replace matches in the current file with the new name RefactoringHelper.ReplaceMatches(targetMatches, sci, NewName); //Uncomment if we want to keep modified files //if (sci.IsModify) AssociatedDocumentHelper.MarkDocumentToKeep(entry.Key); doc.Save(); } if (newFileName != null) RenameFile(eventArgs.Results); Results = eventArgs.Results; AssociatedDocumentHelper.CloseTemporarilyOpenedDocuments(); if (OutputResults) ReportResults(); UserInterfaceManager.ProgressDialog.Hide(); MessageBar.Locked = false; FireOnRefactorComplete(); }
public static void Execute() { if ( Globals.SciControl == null ) return; ScintillaControl sci = Globals.SciControl; MainForm mainForm = (MainForm) Globals.MainForm; ITabbedDocument document = mainForm.CurrentDocument; String documentDirectory = Path.GetDirectoryName(document.FileName); ASContext context = (ASContext) ASContext.Context; string currentPackage = context.CurrentModel.Package; List<ClassModel> classes = context.CurrentModel.Classes; MemberList imports = context.CurrentModel.Imports; sci.BeginUndoAction(); String pattern = "}"; FRSearch search = new FRSearch(pattern); search.Filter = SearchFilter.None; List<SearchMatch> matches = search.Matches(sci.Text); if (matches == null || matches.Count == 0) return; if (classes.Count < 2) return; SearchMatch match = GetNextDocumentMatch(sci, matches, sci.PositionFromLine(classes[0].LineTo + 1)); Int32 packageEnd = sci.MBSafePosition(match.Index) + sci.MBSafeTextLength(match.Value); // wchar to byte text length String strImport = ""; if (imports.Count > 0) { for (int i=imports.Count-1; i>0; --i ) { MemberModel import = imports[i]; if(packageEnd < sci.PositionFromLine(import.LineFrom)) { sci.SelectionStart = sci.PositionFromLine(import.LineFrom); sci.SelectionEnd = sci.PositionFromLine(import.LineTo + 1); strImport = sci.SelText + strImport; sci.Clear(); } } } context.UpdateCurrentFile(true); Int32 prevClassEnd = packageEnd; foreach (ClassModel currentClass in classes) { // 最初のクラスは無視 if (currentClass == classes[0]) continue; sci.SelectionStart = prevClassEnd; sci.SelectionEnd = sci.PositionFromLine(currentClass.LineTo + 1); String content = "package " + currentPackage + "\n{\n" + Regex.Replace( strImport, @"^", "\t", RegexOptions.Multiline) + Regex.Replace( sci.SelText, @"^", "\t", RegexOptions.Multiline) + "\n}\n"; prevClassEnd = sci.PositionFromLine(currentClass.LineTo + 1); Encoding encoding = sci.Encoding; String file = documentDirectory + "\\" + currentClass.Name + ".as"; FileHelper.WriteFile(file, content, encoding); } sci.SelectionStart = packageEnd; sci.Clear(); sci.EndUndoAction(); }
private void UpdateReferencesNextTarget() { if (currentTargetIndex < targets.Count) { var currentTarget = targets[currentTargetIndex]; FileModel oldFileModel = currentTarget.OldFileModel; FRSearch search; string oldType; if (string.IsNullOrEmpty(oldFileModel.Package)) { search = new FRSearch("package"); search.WholeWord = true; oldType = Path.GetFileNameWithoutExtension(currentTarget.OldFilePath); } else { search = new FRSearch("package\\s+(" + oldFileModel.Package + ")"); oldType = oldFileModel.Package + "." + Path.GetFileNameWithoutExtension(currentTarget.OldFilePath); } search.IsRegex = true; search.Filter = SearchFilter.None; oldType = oldType.Trim('.'); MessageBar.Locked = true; string newFilePath = currentTarget.NewFilePath; var doc = AssociatedDocumentHelper.LoadDocument(currentTarget.TmpFilePath ?? newFilePath); ScintillaControl sci = doc.SciControl; search.SourceFile = sci.FileName; List <SearchMatch> matches = search.Matches(sci.Text); string packageReplacement = "package"; if (currentTarget.NewPackage != "") { packageReplacement += " " + currentTarget.NewPackage; } RefactoringHelper.ReplaceMatches(matches, sci, packageReplacement); int offset = "package ".Length; foreach (SearchMatch match in matches) { match.Column += offset; match.LineText = sci.GetLine(match.Line - 1); match.Value = currentTarget.NewPackage; } if (matches.Count > 0) { if (!Results.ContainsKey(newFilePath)) { Results[newFilePath] = new List <SearchMatch>(); } Results[newFilePath].AddRange(matches); } else if (sci.ConfigurationLanguage == "haxe") { // haxe modules don't need to specify a package if it's empty sci.InsertText(0, packageReplacement + ";\n\n"); } //Do we want to open modified files? //if (sci.IsModify) AssociatedDocumentHelper.MarkDocumentToKeep(file); doc.Save(); MessageBar.Locked = false; UserInterfaceManager.ProgressDialog.Show(); UserInterfaceManager.ProgressDialog.SetTitle(TextHelper.GetString("Info.FindingReferences")); UserInterfaceManager.ProgressDialog.UpdateStatusMessage(TextHelper.GetString("Info.SearchingFiles")); currentTargetResult = RefactoringHelper.GetRefactorTargetFromFile(oldFileModel.FileName, AssociatedDocumentHelper); if (currentTargetResult != null) { RefactoringHelper.FindTargetInFiles(currentTargetResult, UserInterfaceManager.ProgressDialog.UpdateProgress, FindFinished, true, true, true); } else { currentTargetIndex++; UserInterfaceManager.ProgressDialog.Hide(); UpdateReferencesNextTarget(); } } else { MoveRefactoredFiles(); ReopenInitialFiles(); FireOnRefactorComplete(); } }
// 文字列検索 private List <SearchMatch> searchString(ScintillaControl sci, String text) { FRSearch search = new FRSearch(text); return(search.Matches(sci.Text)); }
/// <summary> /// /// </summary> private void UpdateReferencesNextTarget() { if (targets.Count > 0) { currentTarget = targets[0]; targets.Remove(currentTarget); FileModel oldFileModel = currentTarget.OldFileModel; FRSearch search; string newType; if (string.IsNullOrEmpty(oldFileModel.Package)) { search = new FRSearch("(package)+\\s*"); newType = Path.GetFileNameWithoutExtension(currentTarget.OldFilePath); } else { search = new FRSearch("(package)+\\s+(" + oldFileModel.Package + ")\\s*"); newType = oldFileModel.Package + "." + Path.GetFileNameWithoutExtension(currentTarget.OldFilePath); } search.IsRegex = true; search.Filter = SearchFilter.None; newType = newType.Trim('.'); MessageBar.Locked = true; string newFilePath = currentTarget.NewFilePath; ScintillaControl sci = AssociatedDocumentHelper.LoadDocument(newFilePath); List <SearchMatch> matches = search.Matches(sci.Text); RefactoringHelper.ReplaceMatches(matches, sci, "package " + currentTarget.NewPackage + " ", null); int offset = "package ".Length; foreach (SearchMatch match in matches) { match.Column += offset; match.LineText = sci.GetLine(match.Line - 1); match.Value = currentTarget.NewPackage; } if (!Results.ContainsKey(newFilePath)) { Results[newFilePath] = new List <SearchMatch>(); } Results[newFilePath].AddRange(matches.ToArray()); PluginBase.MainForm.CurrentDocument.Save(); if (sci.IsModify) { AssociatedDocumentHelper.MarkDocumentToKeep(currentTarget.OldFilePath); } MessageBar.Locked = false; UserInterfaceManager.ProgressDialog.Show(); UserInterfaceManager.ProgressDialog.SetTitle(TextHelper.GetString("Info.FindingReferences")); UserInterfaceManager.ProgressDialog.UpdateStatusMessage(TextHelper.GetString("Info.SearchingFiles")); ASResult target = new ASResult() { Member = new MemberModel(newType, newType, FlagType.Import, 0) }; RefactoringHelper.FindTargetInFiles(target, UserInterfaceManager.ProgressDialog.UpdateProgress, FindFinished, true); } else { if (outputResults) { ReportResults(); } FireOnRefactorComplete(); } }
/// <summary> /// Replaces all results specified by user input /// </summary> private void ReplaceAllButtonClick(Object sender, System.EventArgs e) { if (Globals.SciControl == null) { return; } ScintillaControl sci = Globals.SciControl; List <SearchMatch> matches = this.GetResults(sci); Boolean selectionOnly = this.lookComboBox.SelectedIndex == 1 && sci.SelText.Length > 0; if (matches != null && selectionOnly) { Int32 end = sci.MBSafeCharPosition(sci.SelectionEnd); Int32 start = sci.MBSafeCharPosition(sci.SelectionStart); matches = FRDialogGenerics.FilterMatches(matches, start, end); } if (matches != null) { sci.BeginUndoAction(); try { var firstVisibleLine = sci.FirstVisibleLine; var pos = sci.CurrentPos; for (int i = 0, count = matches.Count; i < count; i++) { var match = matches[i]; var replacement = GetReplaceText(match); var replacementLenght = sci.MBSafeTextLength(replacement); if (sci.MBSafePosition(match.Index) < pos) { pos += replacementLenght - sci.MBSafeTextLength(match.Value); } if (selectionOnly) { FRDialogGenerics.SelectMatchInTarget(sci, match); } else { FRDialogGenerics.SelectMatch(sci, match); } FRSearch.PadIndexes(matches, i, match.Value, replacement); sci.EnsureVisible(sci.CurrentLine); if (selectionOnly) { sci.ReplaceTarget(replacementLenght, replacement); } else { sci.ReplaceSel(replacement); } } sci.FirstVisibleLine = firstVisibleLine; sci.SetSel(pos, pos); } finally { sci.EndUndoAction(); } FRDialogGenerics.UpdateComboBoxItems(this.findComboBox); FRDialogGenerics.UpdateComboBoxItems(this.replaceComboBox); String message = TextHelper.GetString("Info.ReplacedMatches"); String formatted = String.Format(message, matches.Count); this.ShowMessage(formatted, 0); this.lookupIsDirty = false; } }