本文整理汇总了C#中ICSharpCode.NRefactory.CSharp.Refactoring.TypeSystemAstBuilder类的典型用法代码示例。如果您正苦于以下问题:C# TypeSystemAstBuilder类的具体用法?C# TypeSystemAstBuilder怎么用?C# TypeSystemAstBuilder使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
TypeSystemAstBuilder类属于ICSharpCode.NRefactory.CSharp.Refactoring命名空间,在下文中一共展示了TypeSystemAstBuilder类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GeneratePartialClassContextStub
static string GeneratePartialClassContextStub(DebuggerCompletionContext context)
{
var compilation = SD.ParserService.GetCompilationForFile(context.FileName);
var file = SD.ParserService.GetExistingUnresolvedFile(context.FileName);
if (compilation == null || file == null)
return "";
var unresolvedMember = file.GetMember(context.Location);
if (unresolvedMember == null)
return "";
var member = unresolvedMember.Resolve(new SimpleTypeResolveContext(compilation.MainAssembly));
if (member == null)
return "";
var builder = new TypeSystemAstBuilder();
MethodDeclaration decl;
if (unresolvedMember is IMethod) {
// If it's a method, convert it directly (including parameters + type parameters)
decl = (MethodDeclaration)builder.ConvertEntity(member);
} else {
// Otherwise, create a method anyways, and copy the parameters
decl = new MethodDeclaration();
if (member is IParameterizedMember) {
foreach (var p in ((IParameterizedMember)member).Parameters) {
decl.Parameters.Add(builder.ConvertParameter(p));
}
}
}
decl.Name = "__DebuggerStub__";
decl.ReturnType = builder.ConvertType(member.ReturnType);
decl.Modifiers = unresolvedMember.IsStatic ? Modifiers.Static : Modifiers.None;
// Make the method look like an explicit interface implementation so that it doesn't appear in CC
decl.PrivateImplementationType = new SimpleType("__DummyType__");
decl.Body = GenerateBodyFromContext(builder, context);
return WrapInType(unresolvedMember.DeclaringTypeDefinition, decl).ToString();
}
示例2: VisitForeachStatement
public override void VisitForeachStatement(ForeachStatement foreachStatement)
{
base.VisitForeachStatement(foreachStatement);
var rr = ctx.Resolve(foreachStatement) as ForEachResolveResult;
if (rr == null)
return;
if (rr.ElementType.Kind == TypeKind.Unknown)
return;
if (ReflectionHelper.GetTypeCode(rr.ElementType) == TypeCode.Object)
return;
if (conversions == null) {
conversions = CSharpConversions.Get(ctx.Compilation);
}
Conversion c = conversions.ImplicitConversion(rr.ElementType, rr.ElementVariable.Type);
if (c.IsValid)
return;
var csResolver = ctx.GetResolverStateBefore(foreachStatement);
var builder = new TypeSystemAstBuilder(csResolver);
AstType elementType = builder.ConvertType(rr.ElementType);
AstType variableType = foreachStatement.VariableType;
string issueText = ctx.TranslateString("Collection element type '{0}' is not implicitly convertible to '{1}'");
string fixText = ctx.TranslateString("Use type '{0}'");
AddIssue(variableType, string.Format(issueText, elementType.GetText(), variableType.GetText()),
new CodeAction(string.Format(fixText, elementType.GetText()),
script => script.Replace(variableType, elementType)));
}
示例3: CreateShortType
public override AstType CreateShortType (IType fullType)
{
AstNode node = Unit.GetNodeAt(Location);
CSharpResolver csResolver = resolver.GetResolverStateBefore(node);
var builder = new TypeSystemAstBuilder (csResolver);
return builder.ConvertType (fullType);
}
示例4: EnumMemberCompletionData
public EnumMemberCompletionData(IType enumType, IEntity member, TypeSystemAstBuilder builder) : base(enumType.Name + "." + member.Name)
{
this.enumType = enumType;
this.member = member;
this.Image = ClassBrowserIconService.Const;
this.CompletionText = builder.ConvertType(enumType).ToString() + "." + member.Name;
}
示例5: GetShortType
protected string GetShortType (IType type)
{
if (builder == null) {
var ctx = ext.CSharpUnresolvedFile.GetTypeResolveContext (ext.UnresolvedFileCompilation, ext.Document.Editor.Caret.Location) as CSharpTypeResolveContext;
var state = new CSharpResolver (ctx);
builder = new TypeSystemAstBuilder (state);
var dt = state.CurrentTypeDefinition;
var declaring = ctx.CurrentTypeDefinition != null ? ctx.CurrentTypeDefinition.DeclaringTypeDefinition : null;
if (declaring != null) {
while (dt != null) {
if (dt.Equals (declaring)) {
builder.AlwaysUseShortTypeNames = true;
break;
}
dt = dt.DeclaringTypeDefinition;
}
}
}
try {
return GLib.Markup.EscapeText (builder.ConvertType(type).ToString (ext.FormattingPolicy.CreateOptions ()));
} catch (Exception e) {
LoggingService.LogError ("Exception while getting short type.", e);
return "";
}
}
示例6: GeneratePartialClassContextStub
static string GeneratePartialClassContextStub(ICodeContext context)
{
var member = context.CurrentMember;
if (member == null)
return "";
var builder = new TypeSystemAstBuilder();
MethodDeclaration decl;
if (member is IMethod) {
// If it's a method, convert it directly (including parameters + type parameters)
decl = (MethodDeclaration)builder.ConvertEntity(member);
} else {
// Otherwise, create a method anyways, and copy the parameters
decl = new MethodDeclaration();
if (member is IParameterizedMember) {
foreach (var p in ((IParameterizedMember)member).Parameters) {
decl.Parameters.Add(builder.ConvertParameter(p));
}
}
}
decl.Name = "__DebuggerStub__";
decl.ReturnType = builder.ConvertType(member.ReturnType);
decl.Modifiers = member.IsStatic ? Modifiers.Static : Modifiers.None;
// Make the method look like an explicit interface implementation so that it doesn't appear in CC
decl.PrivateImplementationType = new SimpleType("__DummyType__");
decl.Body = GenerateBodyFromContext(builder, context.LocalVariables.ToArray());
return WrapInType(context.CurrentTypeDefinition, decl).ToString();
}
示例7: Complete
public override void Complete(CompletionContext context)
{
if (declarationBegin > context.StartOffset) {
base.Complete(context);
return;
}
TypeSystemAstBuilder b = new TypeSystemAstBuilder(contextAtCaret);
b.ShowTypeParameterConstraints = false;
b.GenerateBody = true;
var entityDeclaration = b.ConvertEntity(this.Entity);
entityDeclaration.Modifiers &= ~(Modifiers.Virtual | Modifiers.Abstract);
entityDeclaration.Modifiers |= Modifiers.Override;
if (!this.Entity.IsAbstract) {
// modify body to call the base method
if (this.Entity.SymbolKind == SymbolKind.Method) {
var baseCall = new BaseReferenceExpression().Invoke(this.Entity.Name, ParametersToExpressions(this.Entity));
var body = entityDeclaration.GetChildByRole(Roles.Body);
body.Statements.Clear();
if (((IMethod)this.Entity).ReturnType.IsKnownType(KnownTypeCode.Void))
body.Statements.Add(new ExpressionStatement(baseCall));
else
body.Statements.Add(new ReturnStatement(baseCall));
} else if (this.Entity.SymbolKind == SymbolKind.Indexer || this.Entity.SymbolKind == SymbolKind.Property) {
Expression baseCall;
if (this.Entity.SymbolKind == SymbolKind.Indexer)
baseCall = new BaseReferenceExpression().Indexer(ParametersToExpressions(this.Entity));
else
baseCall = new BaseReferenceExpression().Member(this.Entity.Name);
var getterBody = entityDeclaration.GetChildByRole(PropertyDeclaration.GetterRole).Body;
if (!getterBody.IsNull) {
getterBody.Statements.Clear();
getterBody.Add(new ReturnStatement(baseCall.Clone()));
}
var setterBody = entityDeclaration.GetChildByRole(PropertyDeclaration.SetterRole).Body;
if (!setterBody.IsNull) {
setterBody.Statements.Clear();
setterBody.Add(new AssignmentExpression(baseCall.Clone(), new IdentifierExpression("value")));
}
}
}
var document = context.Editor.Document;
StringWriter w = new StringWriter();
var formattingOptions = FormattingOptionsFactory.CreateSharpDevelop();
var segmentDict = SegmentTrackingOutputFormatter.WriteNode(w, entityDeclaration, formattingOptions, context.Editor.Options);
using (document.OpenUndoGroup()) {
string newText = w.ToString().TrimEnd();
document.Replace(declarationBegin, context.EndOffset - declarationBegin, newText);
var throwStatement = entityDeclaration.Descendants.FirstOrDefault(n => n is ThrowStatement);
if (throwStatement != null) {
var segment = segmentDict[throwStatement];
context.Editor.Select(declarationBegin + segment.Offset, segment.Length);
}
CSharpFormatterHelper.Format(context.Editor, declarationBegin, newText.Length, formattingOptions);
}
}
示例8: Complete
public override void Complete(CompletionContext context)
{
if (declarationBegin > context.StartOffset) {
base.Complete(context);
return;
}
TypeSystemAstBuilder b = new TypeSystemAstBuilder(contextAtCaret);
b.GenerateBody = true;
var entityDeclaration = b.ConvertEntity(this.Entity);
entityDeclaration.Modifiers &= ~Modifiers.VisibilityMask; // remove visiblity
entityDeclaration.Modifiers |= Modifiers.Partial;
var document = context.Editor.Document;
StringWriter w = new StringWriter();
var formattingOptions = CSharpFormattingPolicies.Instance.GetProjectOptions(contextAtCaret.Compilation.GetProject());
var segmentDict = SegmentTrackingOutputFormatter.WriteNode(
w, entityDeclaration, formattingOptions.OptionsContainer.GetEffectiveOptions(), context.Editor.Options);
using (document.OpenUndoGroup()) {
string newText = w.ToString().TrimEnd();
document.Replace(declarationBegin, context.EndOffset - declarationBegin, newText);
var throwStatement = entityDeclaration.Descendants.FirstOrDefault(n => n is ThrowStatement);
if (throwStatement != null) {
var segment = segmentDict[throwStatement];
context.Editor.Select(declarationBegin + segment.Offset, segment.Length);
}
CSharpFormatterHelper.Format(context.Editor, declarationBegin, newText.Length, formattingOptions.OptionsContainer);
}
}
示例9: CSharpCompletionDataFactory
public CSharpCompletionDataFactory(CSharpCompletionContext completionContext, CSharpResolver contextAtCaret)
{
Debug.Assert(completionContext != null);
Debug.Assert(contextAtCaret != null);
this.completionContext = completionContext;
this.contextAtCaret = contextAtCaret;
this.builder = new TypeSystemAstBuilder(contextAtCaret);
}
示例10: CSharpResolver
public static string GetOverrideTargetName
(IMember m, CSharpTypeResolveContext resolveContext) {
var builder = new TypeSystemAstBuilder
(new CSharpResolver(resolveContext));
return builder.ConvertEntity(m).ToString()
// Builder automatically adds a trailing newline
.TrimEnd(Environment.NewLine.ToCharArray());
}
示例11: SignatureMarkupCreator
public SignatureMarkupCreator (CSharpResolver resolver, CSharpFormattingOptions formattingOptions)
{
this.colorStyle = SyntaxModeService.GetColorStyle (MonoDevelop.Ide.IdeApp.Preferences.ColorScheme);
this.resolver = resolver;
this.astBuilder = new TypeSystemAstBuilder (resolver) {
ConvertUnboundTypeArguments = true,
UseAliases = false
};
this.formattingOptions = formattingOptions;
}
示例12: ImportCompletionData
public ImportCompletionData(ITypeDefinition typeDef, CSharpResolver contextAtCaret, bool useFullName)
: base(typeDef)
{
this.Description = "using " + typeDef.Namespace + ";";
if (useFullName) {
var astBuilder = new TypeSystemAstBuilder(contextAtCaret);
insertionText = astBuilder.ConvertType(typeDef).ToString();
} else {
insertionText = typeDef.Name;
insertUsing = typeDef.Namespace;
}
}
示例13: MemberCompletionData
public MemberCompletionData(IType type, IEntity member, TypeSystemAstBuilder builder)
{
DeclarationCategory = DeclarationCategory.Enumeration_Member;
Type = type;
Member = member;
string typeName = builder.ConvertType(type).GetText();
SetDefaultText(typeName + "." + member.Name);
Documentation = member.Documentation;
}
示例14: InsertEventHandler
public override void InsertEventHandler(ITypeDefinition target, string name, IEvent eventDefinition, bool jumpTo)
{
IUnresolvedTypeDefinition match = null;
foreach (var part in target.Parts) {
if (match == null || EntityModelContextUtils.IsBetterPart(part, match, ".cs"))
match = part;
}
if (match == null) return;
var view = SD.FileService.OpenFile(new FileName(match.Region.FileName), jumpTo);
var editor = view.GetRequiredService<ITextEditor>();
var last = match.Members.LastOrDefault() ?? (IUnresolvedEntity)match;
editor.Caret.Location = last.BodyRegion.End;
var context = SDRefactoringContext.Create(editor, CancellationToken.None);
var node = context.RootNode.GetNodeAt<EntityDeclaration>(last.Region.Begin);
var resolver = context.GetResolverStateAfter(node);
var builder = new TypeSystemAstBuilder(resolver);
var delegateDecl = builder.ConvertEntity(eventDefinition.ReturnType.GetDefinition()) as DelegateDeclaration;
if (delegateDecl == null) return;
var throwStmt = new ThrowStatement(new ObjectCreateExpression(context.CreateShortType("System", "NotImplementedException")));
var decl = new MethodDeclaration() {
ReturnType = delegateDecl.ReturnType.Clone(),
Name = name,
Body = new BlockStatement() {
throwStmt
}
};
var param = delegateDecl.Parameters.Select(p => p.Clone()).OfType<ParameterDeclaration>().ToArray();
decl.Parameters.AddRange(param);
using (Script script = context.StartScript()) {
// FIXME : will not work properly if there are no members.
if (last == match) {
throw new NotImplementedException();
// TODO InsertWithCursor not implemented!
//script.InsertWithCursor("Insert event handler", Script.InsertPosition.End, decl).RunSynchronously();
} else {
// TODO does not jump correctly...
script.InsertAfter(node, decl);
editor.JumpTo(throwStmt.StartLocation.Line, throwStmt.StartLocation.Column);
}
}
}
示例15: TypeCompletionData
public TypeCompletionData(ICSharpCode.NRefactory.TypeSystem.IType type, bool fullName, bool isInAttributeContext, TypeSystemAstBuilder builder, bool addForTypeCreation)
{
Type = type;
FullName = fullName;
IsInAttributeContext = isInAttributeContext;
// Confirm that this doesn't also include other types, such as enums, structs, etc.
//DeclarationCategory = DeclarationCategory.Class;
DeclarationCategory = type.Kind.ResolveDeclarationCategoryFromTypeKind();
string typeName = fullName ? builder.ConvertType(type).GetText() : type.Name;
if (isInAttributeContext && typeName.EndsWith("Attribute") && typeName.Length > "Attribute".Length)
{
typeName = typeName.Substring(0, typeName.Length - "Attribute".Length);
}
SetDefaultText(typeName);
// for documentation, see type.GetDefinition().Documentation
Documentation = type.GetDefinition().Documentation;
}