本文整理汇总了C#中MonoDevelop.Projects.CodeGeneration.RefactorerContext类的典型用法代码示例。如果您正苦于以下问题:C# RefactorerContext类的具体用法?C# RefactorerContext怎么用?C# RefactorerContext使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
RefactorerContext类属于MonoDevelop.Projects.CodeGeneration命名空间,在下文中一共展示了RefactorerContext类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: MemberReference
public MemberReference (RefactorerContext rctx, FilePath fileName, int position, int line, int column, string name, string textLine)
{
this.position = position;
this.line = line;
this.column = column;
this.fileName = fileName;
this.name = name;
this.rctx = rctx;
this.textLine = textLine;
if (textLine == null || textLine.Length == 0)
textLine = name;
}
示例2: RenameClass
public override IClass RenameClass (RefactorerContext ctx, IClass cls, string newName)
{
IEditableTextFile file = ctx.GetFile (cls.Region.FileName);
if (file == null)
return null;
int pos1 = file.GetPositionFromLineColumn (cls.Region.BeginLine, cls.Region.BeginColumn);
int pos2 = file.GetPositionFromLineColumn (cls.Region.EndLine, cls.Region.EndColumn);
string txt = file.GetText (pos1, pos2);
Regex targetExp = new Regex(@"\sclass\s*(" + cls.Name + @")\s", RegexOptions.Multiline);
Match match = targetExp.Match (" " + txt + " ");
if (!match.Success)
return null;
int pos = pos1 + match.Groups [1].Index - 1;
file.DeleteText (pos, cls.Name.Length);
file.InsertText (pos, newName);
return GetGeneratedClass (ctx, file, cls);
}
示例3: AddAttribute
public virtual void AddAttribute (RefactorerContext ctx, IType cls, CodeAttributeDeclaration attr)
{
IEditableTextFile buffer = ctx.GetFile (cls.CompilationUnit.FileName);
CodeTypeDeclaration type = new CodeTypeDeclaration ("temp");
type.CustomAttributes.Add (attr);
CodeDomProvider provider = GetCodeDomProvider ();
StringWriter sw = new StringWriter ();
provider.GenerateCodeFromType (type, sw, GetOptions (false));
string code = sw.ToString ();
int start = code.IndexOf ('[');
int end = code.LastIndexOf (']');
code = code.Substring (start, end-start+1) + Environment.NewLine;
int line = cls.Location.Line;
int col = cls.Location.Column;
int pos = buffer.GetPositionFromLineColumn (line, col);
code = Indent (code, GetLineIndent (buffer, line), false);
buffer.InsertText (pos, code);
}
示例4: CreateClass
public IType CreateClass (RefactorerContext ctx, string directory, string namspace, CodeTypeDeclaration type)
{
CodeCompileUnit unit = new CodeCompileUnit ();
CodeNamespace ns = new CodeNamespace (namspace);
ns.Types.Add (type);
unit.Namespaces.Add (ns);
string file = Path.Combine (directory, type.Name + ".cs");
StreamWriter sw = new StreamWriter (file);
CodeDomProvider provider = GetCodeDomProvider ();
provider.GenerateCodeFromCompileUnit (unit, sw, GetOptions (false));
sw.Close ();
ICompilationUnit pi = ProjectDomService.Parse (ctx.ParserContext.Project, file).CompilationUnit;
IList<IType> clss = pi.Types;
if (clss.Count > 0)
return clss [0];
else
throw new Exception ("Class creation failed. The parser did not find the created class.");
}
示例5: AddFoldingRegion
public override int AddFoldingRegion (RefactorerContext ctx, IType cls, string regionName)
{
IEditableTextFile buffer = ctx.GetFile (cls.CompilationUnit.FileName);
int pos = GetNewMethodPosition (buffer, cls);
string eolMarker = Environment.NewLine;
if (cls.SourceProject != null) {
TextStylePolicy policy = cls.SourceProject.Policies.Get<TextStylePolicy> ();
if (policy != null)
eolMarker = policy.GetEolMarker ();
}
int line, col;
buffer.GetLineColumnFromPosition (pos, out line, out col);
string indent = buffer.GetText (buffer.GetPositionFromLineColumn (line, 1), pos);
string pre = "#region " + regionName + eolMarker;
string post = indent + "#endregion" + eolMarker;
buffer.InsertText (pos, pre + post);
return pos + pre.Length;
}
示例6: FindVariableReferences
public override IEnumerable<MemberReference> FindVariableReferences (RefactorerContext ctx, string fileName, LocalVariable var)
{
//System.Console.WriteLine("Find variable references !!!");
// ParsedDocument parsedDocument = ProjectDomService.ParseFile (fileName);
NRefactoryResolver resolver = new NRefactoryResolver (ctx.ParserContext, var.CompilationUnit, ICSharpCode.NRefactory.SupportedLanguage.CSharp, null, fileName);
resolver.CallingMember = var.DeclaringMember;
FindMemberAstVisitor visitor = new FindMemberAstVisitor (resolver, ctx.GetFile (fileName), var);
visitor.RunVisitor ();
SetContext (visitor.FoundReferences, ctx);
return visitor.FoundReferences;
}
示例7: FindClassReferences
public override IEnumerable<MemberReference> FindClassReferences (RefactorerContext ctx, string fileName, IType cls, bool includeXmlComment)
{
IEditableTextFile file = ctx.GetFile (fileName);
NRefactoryResolver resolver = new NRefactoryResolver (ctx.ParserContext, cls.CompilationUnit, ICSharpCode.NRefactory.SupportedLanguage.CSharp, null, fileName);
FindMemberAstVisitor visitor = new FindMemberAstVisitor (resolver, file, cls);
visitor.IncludeXmlDocumentation = includeXmlComment;
visitor.RunVisitor ();
SetContext (visitor.FoundReferences, ctx);
return visitor.FoundReferences;
}
示例8: ImplementMember
public override IMember ImplementMember (RefactorerContext ctx, IType cls, IMember member, IReturnType privateImplementationType)
{
if (privateImplementationType != null) {
// Workaround for bug in the code generator. Generic private implementation types are not generated correctly when they are generic.
Ambience amb = new CSharpAmbience ();
string tn = amb.GetString (privateImplementationType, OutputFlags.IncludeGenerics | OutputFlags.UseFullName | OutputFlags.UseIntrinsicTypeNames);
privateImplementationType = new DomReturnType (tn);
}
return base.ImplementMember (ctx, cls, member, privateImplementationType);
}
示例9: AddLocalNamespaceImport
public override void AddLocalNamespaceImport (RefactorerContext ctx, string fileName, string nsName, DomLocation caretLocation)
{
IEditableTextFile file = ctx.GetFile (fileName);
int pos = 0;
ParsedDocument parsedDocument = parser.Parse (ctx.ParserContext, fileName, file.Text);
StringBuilder text = new StringBuilder ();
string indent = "";
if (parsedDocument.CompilationUnit != null) {
IUsing containingUsing = null;
foreach (IUsing u in parsedDocument.CompilationUnit.Usings) {
if (u.IsFromNamespace && u.Region.Contains (caretLocation)) {
containingUsing = u;
}
}
if (containingUsing != null) {
indent = GetLineIndent (file, containingUsing.Region.Start.Line);
IUsing lastUsing = null;
foreach (IUsing u in parsedDocument.CompilationUnit.Usings) {
if (u == containingUsing)
continue;
if (containingUsing.Region.Contains (u.Region)) {
if (u.IsFromNamespace)
break;
lastUsing = u;
}
}
if (lastUsing != null) {
pos = file.GetPositionFromLineColumn (lastUsing.Region.End.Line, lastUsing.Region.End.Column);
} else {
pos = file.GetPositionFromLineColumn (containingUsing.ValidRegion.Start.Line, containingUsing.ValidRegion.Start.Column);
// search line end
while (pos < file.Length) {
char ch = file.GetCharAt (pos);
if (ch == '\n') {
if (file.GetCharAt (pos + 1) == '\r')
pos++;
break;
} else if (ch == '\r') {
break;
}
pos++;
}
}
} else {
AddGlobalNamespaceImport (ctx, fileName, nsName);
return;
}
}
if (pos != 0)
text.AppendLine ();
text.Append (indent);
text.Append ("\t");
text.Append ("using ");
text.Append (nsName);
text.Append (";");
if (pos == 0)
text.AppendLine ();
if (file is Mono.TextEditor.ITextEditorDataProvider) {
Mono.TextEditor.TextEditorData data = ((Mono.TextEditor.ITextEditorDataProvider)file).GetTextEditorData ();
int caretOffset = data.Caret.Offset;
int insertedChars = data.Insert (pos, text.ToString ());
if (pos < caretOffset) {
data.Caret.Offset = caretOffset + insertedChars;
}
} else {
file.InsertText (pos, text.ToString ());
}
}
示例10: CompleteStatement
public override DomLocation CompleteStatement (RefactorerContext ctx, string fileName, DomLocation caretLocation)
{
IEditableTextFile file = ctx.GetFile (fileName);
int pos = file.GetPositionFromLineColumn (caretLocation.Line + 1, 1);
StringBuilder line = new StringBuilder ();
int lineNr = caretLocation.Line + 1, column = 1, maxColumn = 1, lastPos = pos;
while (lineNr == caretLocation.Line + 1) {
maxColumn = column;
lastPos = pos;
line.Append (file.GetCharAt (pos));
pos++;
file.GetLineColumnFromPosition (pos, out lineNr, out column);
}
string trimmedline = line.ToString ().Trim ();
string indent = line.ToString ().Substring (0, line.Length - line.ToString ().TrimStart (' ', '\t').Length);
if (trimmedline.EndsWith (";") || trimmedline.EndsWith ("{"))
return caretLocation;
if (trimmedline.StartsWith ("if") ||
trimmedline.StartsWith ("while") ||
trimmedline.StartsWith ("switch") ||
trimmedline.StartsWith ("for") ||
trimmedline.StartsWith ("foreach")) {
if (!trimmedline.EndsWith (")")) {
file.InsertText (lastPos, " () {" + Environment.NewLine + indent + TextEditorProperties.IndentString + Environment.NewLine + indent + "}");
caretLocation.Column = maxColumn + 1;
} else {
file.InsertText (lastPos, " {" + Environment.NewLine + indent + TextEditorProperties.IndentString + Environment.NewLine + indent + "}");
caretLocation.Column = indent.Length + 1;
caretLocation.Line++;
}
} else if (trimmedline.StartsWith ("do")) {
file.InsertText (lastPos, " {" + Environment.NewLine + indent + TextEditorProperties.IndentString + Environment.NewLine + indent + "} while ();");
caretLocation.Column = indent.Length + 1;
caretLocation.Line++;
} else {
file.InsertText (lastPos, ";" + Environment.NewLine + indent);
caretLocation.Column = indent.Length;
caretLocation.Line++;
}
return caretLocation;
}
示例11: SetContext
public void SetContext (RefactorerContext rctx)
{
this.rctx = rctx;
}
示例12: Refactor
public void Refactor (IProgressMonitor monitor, RefactorerContext rctx, IRefactorer r, string fileName)
{
try {
IEnumerable<MemberReference> refs = r.FindParameterReferences (rctx, fileName, param, includeXmlComment);
if (refs != null)
references.AddRange (refs);
} catch (Exception ex) {
monitor.ReportError (GettextCatalog.GetString ("Could not look for references in file '{0}': {1}", fileName, ex.Message), ex);
}
}
示例13: GetUpdatedClass
// public IType ImplementInterface (ICompilationUnit pinfo, IType klass, IType iface, bool explicitly, IType declaringClass, IReturnType hintReturnType)
// {
// if (klass == null)
// throw new ArgumentNullException ("klass");
// if (iface == null)
// throw new ArgumentNullException ("iface");
// RefactorerContext gctx = GetGeneratorContext (klass);
// klass = GetUpdatedClass (gctx, klass);
//
// bool alreadyImplemented;
// IReturnType prefix = null;
//
// List<KeyValuePair<IMember,IReturnType>> toImplement = new List<KeyValuePair<IMember,IReturnType>> ();
//
// prefix = new DomReturnType (iface);
//
// // Stub out non-implemented events defined by @iface
// foreach (IEvent ev in iface.Events) {
// if (ev.IsSpecialName)
// continue;
// bool needsExplicitly = explicitly;
//
// alreadyImplemented = gctx.ParserContext.GetInheritanceTree (klass).Any (x => x.ClassType != ClassType.Interface && x.Events.Any (y => y.Name == ev.Name));
//
// if (!alreadyImplemented)
// toImplement.Add (new KeyValuePair<IMember,IReturnType> (ev, needsExplicitly ? prefix : null));
// }
//
// // Stub out non-implemented methods defined by @iface
// foreach (IMethod method in iface.Methods) {
// if (method.IsSpecialName)
// continue;
// bool needsExplicitly = explicitly;
// alreadyImplemented = false;
// foreach (IType t in gctx.ParserContext.GetInheritanceTree (klass)) {
// if (t.ClassType == ClassType.Interface)
// continue;
// foreach (IMethod cmet in t.Methods) {
// if (cmet.Name == method.Name && Equals (cmet.Parameters, method.Parameters)) {
// if (!needsExplicitly && !cmet.ReturnType.Equals (method.ReturnType))
// needsExplicitly = true;
// else
// alreadyImplemented |= !needsExplicitly || (iface.FullName == GetExplicitPrefix (cmet.ExplicitInterfaces));
// }
// }
// }
//
// if (!alreadyImplemented)
// toImplement.Add (new KeyValuePair<IMember,IReturnType> (method, needsExplicitly ? prefix : null));
// }
//
// // Stub out non-implemented properties defined by @iface
// foreach (IProperty prop in iface.Properties) {
// if (prop.IsSpecialName)
// continue;
// bool needsExplicitly = explicitly;
// alreadyImplemented = false;
// foreach (IType t in gctx.ParserContext.GetInheritanceTree (klass)) {
// if (t.ClassType == ClassType.Interface)
// continue;
// foreach (IProperty cprop in t.Properties) {
// if (cprop.Name == prop.Name) {
// if (!needsExplicitly && !cprop.ReturnType.Equals (prop.ReturnType))
// needsExplicitly = true;
// else
// alreadyImplemented |= !needsExplicitly || (iface.FullName == GetExplicitPrefix (cprop.ExplicitInterfaces));
// }
// }
// }
// if (!alreadyImplemented)
// toImplement.Add (new KeyValuePair<IMember,IReturnType> (prop, needsExplicitly ? prefix : null)); }
//
// Ambience ambience = AmbienceService.GetAmbienceForFile (klass.CompilationUnit.FileName);
// //implement members
// ImplementMembers (klass, toImplement, ambience.GetString (iface, OutputFlags.ClassBrowserEntries | OutputFlags.IncludeGenerics | OutputFlags.IncludeParameters) + " implementation");
// gctx.Save ();
//
// klass = GetUpdatedClass (gctx, klass);
// foreach (IType baseClass in iface.SourceProjectDom.GetInheritanceTree (iface)) {
// if (baseClass.Equals (iface) || baseClass.FullName == "System.Object")
// continue;
// klass = ImplementInterface (pinfo, klass, baseClass, explicitly, declaringClass, hintReturnType);
// }
//
//
// return klass;
// }
IType GetUpdatedClass (RefactorerContext gctx, IType klass)
{
IEditableTextFile file = gctx.GetFile (klass.CompilationUnit.FileName);
ParsedDocument doc = ProjectDomService.Parse (gctx.ParserContext.Project, file.Name, delegate () { return file.Text; });
IType result = gctx.ParserContext.GetType (klass.FullName, klass.TypeParameters.Count, true);
if (result is CompoundType) {
IType hintType = doc.CompilationUnit.GetType (klass.FullName, klass.TypeParameters.Count);
if (hintType != null)
((CompoundType)result).SetMainPart (file.Name, hintType.Location);
}
return result;
}
示例14: CreateClass
public IType CreateClass (Project project, string language, string directory, string namspace, CodeTypeDeclaration type)
{
ProjectDom ctx = ProjectDomService.GetProjectDom (project);
RefactorerContext gctx = new RefactorerContext (ctx, fileProvider, null);
IRefactorer gen = LanguageBindingService.GetRefactorerForLanguage (language);
IType c = gen.CreateClass (gctx, directory, namspace, type);
gctx.Save ();
return c;
}
示例15: FindMemberReferences
public override MemberReferenceCollection FindMemberReferences (RefactorerContext ctx, string fileName, IClass cls, IMember member)
{
return null;
}