internal static string CreateWrapperClassForMember(IMember member, string fileName, TextEditor editor) { if (member == null) { return(""); } StringBuilder result = new StringBuilder(); int startLine = member.Location.Line; int endLine = member.Location.Line; if (!member.BodyRegion.IsEmpty) { endLine = member.BodyRegion.End.Line + 1; } string text; result.Append("class " + member.DeclaringType.Name + " {"); if (editor != null) { int col, maxLine; editor.GetLineColumnFromPosition(editor.TextLength - 1, out col, out maxLine); endLine = System.Math.Max(endLine, maxLine); int endPos = editor.GetPositionFromLineColumn(endLine, editor.GetLineLength(endLine)); if (endPos < 0) { endPos = editor.TextLength; } int startPos = Math.Max(0, editor.GetPositionFromLineColumn(startLine, 0)); text = editor.GetText(startPos, endPos); } else { Mono.TextEditor.Document doc = new Mono.TextEditor.Document(); doc.Text = File.ReadAllText(fileName) ?? ""; startLine = Math.Min(doc.LineCount, Math.Max(1, startLine)); endLine = Math.Min(doc.LineCount, Math.Max(1, endLine)); int startOffset = doc.LocationToOffset(startLine - 1, 0); text = doc.GetTextAt(startOffset, doc.LocationToOffset(endLine - 1, doc.GetLine(endLine - 1).EditableLength) - startOffset); } if (!string.IsNullOrEmpty(text)) { result.Append(text); } result.Append("}"); return(result.ToString()); }
MemberReference CreateReference(int line, int col, string name) { int pos = text.LocationToOffset(line - 1, col - 1); int spos = text.LocationToOffset(line - 1, 0); int epos = text.LocationToOffset(line, 0); if (epos == -1) { epos = text.Length - 1; } string txt; // FIXME: do we always need to do this? or just in my test cases so far? :) // use the base name and not the FullName name = GetNameWithoutPrefix(name); // FIXME: is there a better way to do this? // update @pos to point to the actual identifier and not the // public/private/whatever modifier. int i; txt = text.GetTextBetween(pos, text.Length - 1); if (txt != null && (i = txt.IndexOf(name)) > 0) { pos += i; } if (spos != -1) { txt = text.GetTextBetween(spos, epos - 1); } else { txt = null; } return(new MemberReference(null, fileName, pos, line, col, name, txt)); }
public void GoToDeclaration () { if (item is CompoundType) { CompoundType compoundType = (CompoundType)item; monitor = IdeApp.Workbench.ProgressMonitors.GetSearchProgressMonitor (true, true); using (monitor) { foreach (IType part in compoundType.Parts) { FileProvider provider = new FileProvider (part.CompilationUnit.FileName); Mono.TextEditor.Document doc = new Mono.TextEditor.Document (); System.IO.TextReader textReader = provider.Open (); doc.Text = textReader.ReadToEnd (); textReader.Close (); int position = doc.LocationToOffset (part.Location.Line, part.Location.Column); while (position + part.Name.Length < doc.Length) { if (doc.GetTextAt (position, part.Name.Length) == part.Name) break; position++; } monitor.ReportResult (new MonoDevelop.Ide.FindInFiles.SearchResult (provider, position, part.Name.Length)); } } return; } IdeApp.ProjectOperations.JumpToDeclaration (item); }
public override List <Change> PerformChanges(RefactoringOptions options, object properties) { List <Change> result = new List <Change> (); IType type = options.SelectedItem as IType; if (type == null) { return(result); } string newName = GetCorrectFileName(type); if (type.CompilationUnit.Types.Count == 1) { result.Add(new RenameFileChange(type.CompilationUnit.FileName, newName)); } else { StringBuilder content = new StringBuilder(); if (options.Dom.Project is DotNetProject) { content.Append(StandardHeaderService.GetHeader(options.Dom.Project, newName, true) + Environment.NewLine); } INRefactoryASTProvider provider = options.GetASTProvider(); Mono.TextEditor.TextEditorData data = options.GetTextEditorData(); ICSharpCode.NRefactory.Ast.CompilationUnit unit = provider.ParseFile(options.Document.Editor.Text); TypeFilterTransformer typeFilterTransformer = new TypeFilterTransformer((type is InstantiatedType) ? ((InstantiatedType)type).UninstantiatedType.DecoratedFullName : type.DecoratedFullName); unit.AcceptVisitor(typeFilterTransformer, null); if (typeFilterTransformer.TypeDeclaration == null) { return(result); } Mono.TextEditor.Document generatedDocument = new Mono.TextEditor.Document(); generatedDocument.Text = provider.OutputNode(options.Dom, unit); int startLine = 0; int minLine = typeFilterTransformer.TypeDeclaration.StartLocation.Line; foreach (var attr in typeFilterTransformer.TypeDeclaration.Attributes) { minLine = Math.Min(minLine, attr.StartLocation.Line); } for (int i = minLine - 1; i >= 1; i--) { string lineText = data.Document.GetTextAt(data.Document.GetLine(i)).Trim(); if (string.IsNullOrEmpty(lineText)) { continue; } if (lineText.StartsWith("///")) { startLine = i; } else { break; } } int start; if (startLine >= 1) { start = data.Document.GetLine(startLine).Offset; } else { var startLocation = typeFilterTransformer.TypeDeclaration.StartLocation; startLocation.Column = 1; foreach (var attr in typeFilterTransformer.TypeDeclaration.Attributes) { if (attr.StartLocation < startLocation) { startLocation = attr.StartLocation; } } start = data.Document.LocationToOffset(startLocation.Line, 1); } int length = data.Document.LocationToOffset(typeFilterTransformer.TypeDeclaration.EndLocation.Line, typeFilterTransformer.TypeDeclaration.EndLocation.Column) - start; ICSharpCode.NRefactory.Ast.CompilationUnit generatedCompilationUnit = provider.ParseFile(generatedDocument.Text); TypeSearchVisitor typeSearchVisitor = new TypeSearchVisitor(); generatedCompilationUnit.AcceptVisitor(typeSearchVisitor, null); int genStart = generatedDocument.LocationToOffset(typeSearchVisitor.Types[0].StartLocation.Line, 0); foreach (var attr in typeSearchVisitor.Types[0].Attributes) { genStart = Math.Min(genStart, generatedDocument.LocationToOffset(attr.StartLocation.Line, 0)); } int genEnd = generatedDocument.LocationToOffset(typeSearchVisitor.Types[0].EndLocation.Line, typeSearchVisitor.Types[0].EndLocation.Column); ((Mono.TextEditor.IBuffer)generatedDocument).Replace(genStart, genEnd - genStart, data.Document.GetTextAt(start, length)); content.Append(generatedDocument.Text); result.Add(new CreateFileChange(newName, content.ToString())); TextReplaceChange removeDeclaration = new TextReplaceChange(); removeDeclaration.Description = "Remove type declaration"; removeDeclaration.FileName = type.CompilationUnit.FileName; removeDeclaration.Offset = start; removeDeclaration.RemovedChars = length; result.Add(removeDeclaration); } result.Add(new SaveProjectChange(options.Document.Project)); return(result); }
public override List<Change> PerformChanges (RefactoringOptions options, object properties) { List<Change> result = new List<Change> (); IType type = options.SelectedItem as IType; if (type == null) return result; string newName = GetCorrectFileName (type); if (type.CompilationUnit.Types.Count == 1) { result.Add (new RenameFileChange (type.CompilationUnit.FileName, newName)); } else { StringBuilder content = new StringBuilder (); if (options.Dom.Project is DotNetProject) content.Append (StandardHeaderService.GetHeader (options.Dom.Project, type.CompilationUnit.FileName, true) + Environment.NewLine); INRefactoryASTProvider provider = options.GetASTProvider (); Mono.TextEditor.TextEditorData data = options.GetTextEditorData (); ICSharpCode.NRefactory.Ast.CompilationUnit unit = provider.ParseFile (options.Document.TextEditor.Text); TypeFilterTransformer typeFilterTransformer = new TypeFilterTransformer ((type is InstantiatedType) ? ((InstantiatedType)type).UninstantiatedType.DecoratedFullName : type.DecoratedFullName); unit.AcceptVisitor (typeFilterTransformer, null); if (typeFilterTransformer.TypeDeclaration == null) return result; Mono.TextEditor.Document generatedDocument = new Mono.TextEditor.Document (); generatedDocument.Text = provider.OutputNode (options.Dom, unit); int startLine = -1; int minLine = typeFilterTransformer.TypeDeclaration.StartLocation.Line; foreach (var attr in typeFilterTransformer.TypeDeclaration.Attributes) { minLine = Math.Min (minLine, attr.StartLocation.Line); } for (int i = minLine - 2; i >= 0; i--) { string lineText = data.Document.GetTextAt (data.Document.GetLine (i)).Trim (); if (string.IsNullOrEmpty (lineText)) continue; if (lineText.StartsWith ("///")) { startLine = i; } else { break; } } int start; if (startLine >= 0) { start = data.Document.GetLine (startLine).Offset; } else { var startLocation = typeFilterTransformer.TypeDeclaration.StartLocation; startLocation.Column = 0; foreach (var attr in typeFilterTransformer.TypeDeclaration.Attributes) { if (attr.StartLocation < startLocation) startLocation = attr.StartLocation; } start = data.Document.LocationToOffset (startLocation.Line - 1, 0); } int length = data.Document.LocationToOffset (typeFilterTransformer.TypeDeclaration.EndLocation.Line - 1, typeFilterTransformer.TypeDeclaration.EndLocation.Column) - start; ICSharpCode.NRefactory.Ast.CompilationUnit generatedCompilationUnit = provider.ParseFile (generatedDocument.Text); TypeSearchVisitor typeSearchVisitor = new TypeSearchVisitor (); generatedCompilationUnit.AcceptVisitor (typeSearchVisitor, null); int genStart = generatedDocument.LocationToOffset (typeSearchVisitor.Types[0].StartLocation.Line - 1, 0); foreach (var attr in typeSearchVisitor.Types[0].Attributes) { genStart = Math.Min (genStart, generatedDocument.LocationToOffset (attr.StartLocation.Line - 1, 0)); } int genEnd = generatedDocument.LocationToOffset (typeSearchVisitor.Types[0].EndLocation.Line - 1, typeSearchVisitor.Types[0].EndLocation.Column - 1); ((Mono.TextEditor.IBuffer)generatedDocument).Replace (genStart, genEnd - genStart, data.Document.GetTextAt (start, length)); content.Append (generatedDocument.Text); result.Add (new CreateFileChange (newName, content.ToString ())); TextReplaceChange removeDeclaration = new TextReplaceChange (); removeDeclaration.Description = "Remove type declaration"; removeDeclaration.FileName = type.CompilationUnit.FileName; removeDeclaration.Offset = start; removeDeclaration.RemovedChars = length; result.Add (removeDeclaration); } result.Add (new SaveProjectChange (options.Document.Project)); return result; }
internal static string CreateWrapperClassForMember(IMember member, string fileName, TextEditor editor) { if (member == null) return ""; StringBuilder result = new StringBuilder (); int startLine = member.Location.Line; int endLine = member.Location.Line; if (!member.BodyRegion.IsEmpty) endLine = member.BodyRegion.End.Line + 1; string text; result.Append ("class " + member.DeclaringType.Name + " {"); if (editor != null) { int col, maxLine; editor.GetLineColumnFromPosition (editor.TextLength - 1, out col, out maxLine); endLine = System.Math.Max (endLine, maxLine); int endPos = editor.GetPositionFromLineColumn (endLine, editor.GetLineLength (endLine)); if (endPos < 0) endPos = editor.TextLength; text = editor.GetText (editor.GetPositionFromLineColumn (startLine, 0), endPos); } else { Mono.TextEditor.Document doc = new Mono.TextEditor.Document (); doc.Text = File.ReadAllText (fileName) ?? ""; startLine = Math.Min (doc.LineCount, Math.Max (1, startLine)); endLine = Math.Min (doc.LineCount, Math.Max (1, endLine)); int startOffset = doc.LocationToOffset (startLine - 1, 0); text = doc.GetTextAt (startOffset, doc.LocationToOffset (endLine - 1, doc.GetLine (endLine - 1).EditableLength) - startOffset); } if (!string.IsNullOrEmpty (text)) result.Append (text); result.Append ("}"); return result.ToString (); }
public int GetPositionFromLineColumn(int line, int column) { return(document.LocationToOffset(line, column)); }